AoC/2021/day13.swift

932 lines
21 KiB
Swift
Raw Permalink Normal View History

2021-12-13 20:46:05 +01:00
@main
struct Day13: Puzzle {
var maxX: Int = 0
var maxY: Int = 0
mutating func run() {
maxX = dots.map(\.x).max()!
maxY = dots.map(\.y).max()!
for (pos, fold) in folds.enumerated() {
self.fold(fold)
if (pos == 0) {
print("Part 1:", Set(dots).count)
}
}
print("\n\nPart 2:\n")
for y in 0..<maxY {
let dots = dots.filter { $0.y == y }.map(\.x).sorted()
var x = 0
for dotX in dots where dotX >= x {
print(String(repeating: ".", count: dotX - x) + "#", terminator: "")
x = dotX + 1
}
print(String(repeating: ".", count: maxX - x))
}
}
mutating func fold(_ fold: Fold) {
switch fold {
case .x(let axis):
self.fold(at: axis, keyPath: \.x, max: maxX)
maxX = axis
case .y(let axis):
self.fold(at: axis, keyPath: \.y, max: maxY)
maxY = axis
}
}
mutating func fold(at limit: Int, keyPath: WritableKeyPath<Dot, Int>, max: Int) {
let mid = dots.partition { dot in dot[keyPath: keyPath] > limit }
for index in mid..<dots.endIndex {
dots[index][keyPath: keyPath] = 2 * limit - dots[index][keyPath: keyPath]
}
}
struct Dot: Hashable {
var x: Int
var y: Int
init(_ x: Int, _ y: Int) {
self.x = x
self.y = y
}
}
var dots: [Dot] = [
Dot(609, 754),
Dot(1101, 140),
Dot(1129, 317),
Dot(622, 327),
Dot(967, 218),
Dot(661, 789),
Dot(661, 127),
Dot(1183, 596),
Dot(649, 280),
Dot(932, 693),
Dot(1288, 157),
Dot(1111, 372),
Dot(781, 219),
Dot(994, 236),
Dot(1145, 770),
Dot(1153, 395),
Dot(755, 289),
Dot(502, 406),
Dot(850, 126),
Dot(902, 58),
Dot(907, 5),
Dot(268, 700),
Dot(646, 436),
Dot(1108, 523),
Dot(443, 343),
Dot(932, 201),
Dot(1115, 105),
Dot(1191, 278),
Dot(1241, 203),
Dot(1000, 252),
Dot(1290, 403),
Dot(144, 597),
Dot(927, 448),
Dot(782, 630),
Dot(131, 705),
Dot(333, 367),
Dot(38, 613),
Dot(1285, 294),
Dot(872, 93),
Dot(492, 759),
Dot(986, 203),
Dot(70, 740),
Dot(378, 693),
Dot(1004, 319),
Dot(731, 235),
Dot(479, 875),
Dot(1210, 389),
Dot(1215, 215),
Dot(214, 122),
Dot(599, 660),
Dot(500, 254),
Dot(8, 478),
Dot(120, 29),
Dot(70, 809),
Dot(373, 248),
Dot(1235, 441),
Dot(1293, 345),
Dot(454, 686),
Dot(985, 469),
Dot(1101, 478),
Dot(765, 287),
Dot(1029, 309),
Dot(667, 203),
Dot(1220, 800),
Dot(508, 285),
Dot(65, 675),
Dot(1078, 663),
Dot(592, 677),
Dot(236, 122),
Dot(462, 564),
Dot(1042, 418),
Dot(748, 852),
Dot(810, 254),
Dot(718, 229),
Dot(386, 22),
Dot(1191, 728),
Dot(432, 872),
Dot(392, 28),
Dot(972, 259),
Dot(336, 416),
Dot(1096, 480),
Dot(415, 871),
Dot(1241, 427),
Dot(165, 441),
Dot(510, 673),
Dot(750, 313),
Dot(1255, 779),
Dot(194, 854),
Dot(639, 107),
Dot(281, 585),
Dot(679, 519),
Dot(611, 644),
Dot(497, 171),
Dot(326, 730),
Dot(882, 456),
Dot(403, 453),
Dot(454, 630),
Dot(726, 227),
Dot(383, 502),
Dot(574, 528),
Dot(1120, 593),
Dot(214, 256),
Dot(165, 422),
Dot(425, 115),
Dot(795, 323),
Dot(673, 602),
Dot(5, 827),
Dot(1009, 619),
Dot(364, 194),
Dot(75, 390),
Dot(870, 122),
Dot(654, 449),
Dot(62, 665),
Dot(999, 228),
Dot(611, 508),
Dot(87, 660),
Dot(677, 880),
Dot(656, 449),
Dot(681, 336),
Dot(499, 70),
Dot(870, 638),
Dot(119, 728),
Dot(1185, 498),
Dot(28, 406),
Dot(1, 796),
Dot(334, 210),
Dot(803, 445),
Dot(17, 345),
Dot(58, 194),
Dot(1310, 162),
Dot(661, 827),
Dot(276, 649),
Dot(679, 883),
Dot(164, 506),
Dot(990, 236),
Dot(82, 327),
Dot(999, 485),
Dot(498, 208),
Dot(1228, 119),
Dot(529, 732),
Dot(252, 544),
Dot(579, 403),
Dot(808, 592),
Dot(251, 665),
Dot(912, 602),
Dot(502, 722),
Dot(85, 75),
Dot(440, 122),
Dot(755, 12),
Dot(898, 766),
Dot(878, 22),
Dot(924, 22),
Dot(679, 277),
Dot(1191, 616),
Dot(1278, 775),
Dot(1148, 871),
Dot(229, 37),
Dot(957, 854),
Dot(987, 448),
Dot(1285, 518),
Dot(1252, 476),
Dot(1295, 857),
Dot(822, 285),
Dot(731, 627),
Dot(199, 74),
Dot(82, 775),
Dot(1186, 299),
Dot(954, 488),
Dot(753, 508),
Dot(1232, 593),
Dot(1216, 663),
Dot(529, 284),
Dot(795, 571),
Dot(1118, 226),
Dot(467, 617),
Dot(691, 36),
Dot(181, 317),
Dot(994, 658),
Dot(651, 894),
Dot(1101, 754),
Dot(763, 603),
Dot(1088, 872),
Dot(45, 528),
Dot(937, 224),
Dot(1042, 470),
Dot(1066, 648),
Dot(1131, 95),
Dot(738, 434),
Dot(283, 60),
Dot(994, 302),
Dot(1268, 865),
Dot(795, 287),
Dot(75, 179),
Dot(1011, 425),
Dot(741, 75),
Dot(773, 659),
Dot(373, 700),
Dot(201, 479),
Dot(649, 614),
Dot(328, 893),
Dot(1103, 728),
Dot(1144, 285),
Dot(468, 45),
Dot(857, 591),
Dot(69, 203),
Dot(401, 339),
Dot(579, 659),
Dot(502, 172),
Dot(584, 891),
Dot(802, 609),
Dot(644, 359),
Dot(679, 11),
Dot(1260, 201),
Dot(194, 505),
Dot(507, 485),
Dot(738, 350),
Dot(170, 882),
Dot(84, 511),
Dot(1203, 123),
Dot(1240, 533),
Dot(811, 70),
Dot(736, 687),
Dot(856, 686),
Dot(698, 417),
Dot(726, 592),
Dot(572, 544),
Dot(1144, 609),
Dot(629, 376),
Dot(1039, 840),
Dot(455, 148),
Dot(580, 224),
Dot(848, 778),
Dot(684, 45),
Dot(1265, 304),
Dot(681, 406),
Dot(15, 228),
Dot(172, 814),
Dot(637, 549),
Dot(617, 610),
Dot(1185, 610),
Dot(1250, 275),
Dot(107, 291),
Dot(186, 261),
Dot(1247, 299),
Dot(467, 123),
Dot(350, 582),
Dot(782, 432),
Dot(38, 505),
Dot(843, 617),
Dot(1184, 630),
Dot(304, 712),
Dot(373, 221),
Dot(534, 865),
Dot(743, 402),
Dot(857, 620),
Dot(671, 857),
Dot(70, 361),
Dot(692, 252),
Dot(338, 658),
Dot(1121, 144),
Dot(1258, 266),
Dot(1058, 843),
Dot(773, 211),
Dot(1129, 516),
Dot(1088, 22),
Dot(15, 86),
Dot(343, 676),
Dot(70, 1),
Dot(55, 794),
Dot(691, 91),
Dot(982, 893),
Dot(1300, 628),
Dot(656, 162),
Dot(262, 362),
Dot(850, 854),
Dot(617, 284),
Dot(977, 280),
Dot(373, 640),
Dot(654, 221),
Dot(484, 882),
Dot(251, 235),
Dot(103, 639),
Dot(1293, 292),
Dot(1150, 525),
Dot(758, 432),
Dot(480, 124),
Dot(1235, 504),
Dot(1293, 549),
Dot(60, 275),
Dot(1036, 317),
Dot(512, 231),
Dot(599, 794),
Dot(783, 148),
Dot(350, 518),
Dot(45, 252),
Dot(1265, 140),
Dot(979, 448),
Dot(706, 878),
Dot(1203, 686),
Dot(288, 837),
Dot(180, 138),
Dot(572, 431),
Dot(199, 148),
Dot(530, 408),
Dot(711, 794),
Dot(1302, 682),
Dot(497, 295),
Dot(36, 549),
Dot(201, 415),
Dot(1009, 432),
Dot(94, 663),
Dot(1150, 77),
Dot(1096, 638),
Dot(803, 87),
Dot(211, 775),
Dot(320, 12),
Dot(166, 609),
Dot(629, 558),
Dot(572, 366),
Dot(1226, 766),
Dot(295, 789),
Dot(195, 105),
Dot(99, 712),
Dot(507, 87),
Dot(242, 383),
Dot(55, 354),
Dot(681, 558),
Dot(1135, 313),
Dot(192, 226),
Dot(895, 871),
Dot(508, 609),
Dot(1146, 394),
Dot(281, 588),
Dot(714, 312),
Dot(567, 234),
Dot(283, 834),
Dot(455, 893),
Dot(45, 140),
Dot(691, 673),
Dot(763, 123),
Dot(75, 715),
Dot(820, 506),
Dot(1285, 40),
Dot(242, 712),
Dot(475, 1),
Dot(453, 722),
Dot(965, 793),
Dot(475, 212),
Dot(201, 610),
Dot(333, 280),
Dot(934, 103),
Dot(570, 488),
Dot(1126, 10),
Dot(380, 404),
Dot(256, 332),
Dot(818, 311),
Dot(1179, 257),
Dot(725, 19),
Dot(131, 49),
Dot(947, 283),
Dot(84, 128),
Dot(402, 798),
Dot(544, 68),
Dot(835, 1),
Dot(637, 345),
Dot(846, 77),
Dot(117, 445),
Dot(599, 107),
Dot(755, 371),
Dot(596, 582),
Dot(216, 93),
Dot(32, 775),
Dot(999, 409),
Dot(50, 693),
Dot(184, 231),
Dot(343, 436),
Dot(768, 17),
Dot(507, 409),
Dot(880, 379),
Dot(1000, 700),
Dot(1266, 824),
Dot(529, 675),
Dot(119, 278),
Dot(284, 477),
Dot(1305, 280),
Dot(226, 582),
Dot(353, 854),
Dot(1238, 360),
Dot(1048, 476),
Dot(1079, 887),
Dot(960, 518),
Dot(619, 445),
Dot(530, 75),
Dot(863, 390),
Dot(972, 770),
Dot(759, 191),
Dot(1235, 390),
Dot(813, 218),
Dot(775, 728),
Dot(199, 1),
Dot(1283, 649),
Dot(800, 221),
Dot(1183, 416),
Dot(10, 87),
Dot(199, 809),
Dot(1272, 505),
Dot(1099, 775),
Dot(730, 574),
Dot(803, 436),
Dot(10, 135),
Dot(1118, 330),
Dot(1186, 150),
Dot(1200, 600),
Dot(179, 95),
Dot(1228, 648),
Dot(1218, 673),
Dot(731, 403),
Dot(780, 744),
Dot(763, 238),
Dot(1258, 765),
Dot(209, 140),
Dot(572, 618),
Dot(631, 211),
Dot(1081, 115),
Dot(584, 592),
Dot(432, 868),
Dot(787, 413),
Dot(700, 518),
Dot(555, 12),
Dot(1265, 670),
Dot(350, 312),
Dot(599, 659),
Dot(90, 823),
Dot(678, 312),
Dot(99, 523),
Dot(455, 1),
Dot(851, 49),
Dot(743, 660),
Dot(714, 694),
Dot(102, 14),
Dot(1183, 484),
Dot(870, 548),
Dot(242, 800),
Dot(759, 255),
Dot(200, 712),
Dot(110, 404),
Dot(1190, 865),
Dot(441, 553),
Dot(427, 289),
Dot(577, 819),
Dot(1198, 521),
Dot(1022, 57),
Dot(946, 252),
Dot(852, 117),
Dot(535, 166),
Dot(1146, 871),
Dot(937, 130),
Dot(1009, 294),
Dot(855, 746),
Dot(301, 619),
Dot(908, 798),
Dot(1255, 100),
Dot(1047, 236),
Dot(1004, 127),
Dot(683, 649),
Dot(596, 312),
Dot(189, 592),
Dot(584, 85),
Dot(107, 603),
Dot(734, 705),
Dot(1255, 794),
Dot(671, 37),
Dot(316, 816),
Dot(1183, 148),
Dot(599, 802),
Dot(999, 555),
Dot(131, 845),
Dot(842, 630),
Dot(1094, 435),
Dot(684, 493),
Dot(1038, 257),
Dot(17, 292),
Dot(1208, 14),
Dot(58, 582),
Dot(503, 691),
Dot(338, 684),
Dot(1225, 75),
Dot(818, 759),
Dot(227, 289),
Dot(649, 815),
Dot(835, 95),
Dot(447, 838),
Dot(468, 630),
Dot(196, 119),
Dot(1228, 366),
Dot(155, 70),
Dot(927, 18),
Dot(145, 129),
Dot(488, 285),
Dot(1255, 660),
Dot(338, 236),
Dot(684, 401),
Dot(1123, 501),
Dot(244, 210),
Dot(44, 70),
Dot(972, 322),
Dot(964, 333),
Dot(190, 301),
Dot(1260, 878),
Dot(555, 289),
Dot(738, 17),
Dot(574, 687),
Dot(821, 322),
Dot(294, 119),
Dot(1124, 261),
Dot(8, 864),
Dot(97, 633),
Dot(741, 560),
Dot(401, 107),
Dot(560, 313),
Dot(1252, 200),
Dot(1273, 508),
Dot(803, 485),
Dot(25, 294),
Dot(1193, 449),
Dot(266, 520),
Dot(927, 100),
Dot(848, 668),
Dot(1300, 135),
Dot(430, 379),
Dot(398, 809),
Dot(1285, 376),
Dot(70, 533),
Dot(800, 673),
Dot(869, 553),
Dot(552, 432),
Dot(736, 207),
Dot(227, 658),
Dot(202, 523),
Dot(1096, 414),
Dot(1300, 807),
Dot(611, 386),
Dot(1124, 40),
Dot(194, 768),
Dot(786, 789),
Dot(428, 134),
Dot(125, 610),
Dot(301, 723),
Dot(637, 154),
Dot(1114, 119),
Dot(1166, 597),
Dot(1253, 276),
Dot(373, 757),
Dot(842, 854),
Dot(447, 56),
Dot(338, 592),
Dot(1186, 819),
Dot(412, 766),
Dot(1042, 700),
Dot(529, 50),
Dot(932, 130),
Dot(914, 469),
Dot(766, 765),
Dot(1274, 549),
Dot(253, 555),
Dot(55, 787),
Dot(142, 630),
Dot(567, 423),
Dot(1054, 292),
Dot(584, 816),
Dot(870, 775),
Dot(530, 744),
Dot(560, 761),
Dot(376, 791),
Dot(127, 746),
Dot(142, 679),
Dot(383, 354),
Dot(1213, 185),
Dot(147, 458),
Dot(619, 449),
Dot(17, 549),
Dot(1292, 113),
Dot(830, 115),
Dot(1290, 711),
Dot(579, 627),
Dot(594, 744),
Dot(459, 49),
Dot(17, 101),
Dot(960, 312),
Dot(1114, 103),
Dot(962, 331),
Dot(510, 221),
Dot(960, 648),
Dot(808, 40),
Dot(1158, 854),
Dot(453, 31),
Dot(445, 757),
Dot(972, 722),
Dot(82, 648),
Dot(84, 766),
Dot(252, 350),
Dot(656, 673),
Dot(994, 572),
Dot(242, 576),
Dot(438, 93),
Dot(972, 684),
Dot(930, 765),
Dot(810, 366),
Dot(1245, 219),
Dot(796, 630),
Dot(912, 85),
Dot(851, 553),
Dot(590, 362),
Dot(522, 768),
Dot(1223, 471),
Dot(912, 187),
Dot(1000, 194),
Dot(455, 61),
Dot(1140, 882),
Dot(142, 862),
Dot(440, 548),
Dot(310, 194),
Dot(579, 267),
Dot(335, 527),
Dot(52, 129),
Dot(338, 572),
Dot(52, 317),
Dot(249, 334),
Dot(428, 456),
Dot(1240, 592),
Dot(649, 127),
Dot(1235, 715),
Dot(720, 532),
Dot(976, 236),
Dot(855, 820),
Dot(58, 694),
Dot(1019, 803),
Dot(1016, 119),
Dot(338, 302),
Dot(360, 441),
Dot(631, 277),
Dot(360, 119),
Dot(388, 374),
Dot(1054, 332),
Dot(398, 292),
Dot(1240, 361),
Dot(781, 675),
Dot(199, 746),
Dot(338, 266),
Dot(1068, 94),
Dot(1144, 733),
Dot(1191, 838),
Dot(1293, 101),
Dot(190, 593),
Dot(572, 17),
Dot(187, 393),
Dot(35, 673),
Dot(604, 878),
Dot(960, 376),
Dot(1285, 742),
Dot(1153, 767),
Dot(907, 229),
Dot(1236, 171),
Dot(74, 798),
Dot(529, 844),
Dot(304, 742),
Dot(1111, 509),
Dot(1186, 75),
Dot(330, 789),
Dot(902, 836),
Dot(507, 807),
Dot(594, 150),
Dot(1150, 824),
Dot(350, 648),
Dot(460, 374),
Dot(637, 602),
Dot(443, 775),
Dot(1029, 585),
Dot(1006, 152),
Dot(855, 74),
Dot(989, 367),
Dot(945, 308),
Dot(654, 673),
Dot(599, 115),
Dot(1148, 388),
Dot(1185, 442),
Dot(972, 266),
Dot(1247, 485),
Dot(57, 618),
Dot(1068, 383),
Dot(160, 525),
Dot(1006, 182),
Dot(82, 528),
Dot(542, 877),
Dot(189, 144),
Dot(515, 607),
Dot(950, 329),
Dot(335, 367),
Dot(927, 502),
Dot(808, 722),
Dot(790, 513),
Dot(147, 893),
Dot(1211, 630),
Dot(117, 683),
Dot(1247, 261),
Dot(380, 577),
Dot(338, 628),
Dot(1240, 85),
Dot(966, 194),
Dot(85, 819),
Dot(229, 331),
Dot(1115, 789),
Dot(100, 57),
Dot(534, 641),
Dot(857, 722),
Dot(867, 551),
Dot(15, 779),
Dot(1226, 847),
Dot(1101, 11),
Dot(701, 590),
Dot(1111, 522),
Dot(410, 246),
Dot(679, 375),
Dot(1275, 673),
Dot(161, 453),
Dot(1208, 822),
Dot(1163, 458),
Dot(244, 684),
Dot(169, 767),
Dot(490, 506),
Dot(25, 40),
Dot(1111, 677),
Dot(20, 403),
Dot(271, 115),
Dot(1094, 660),
Dot(527, 333),
Dot(92, 673),
Dot(838, 745),
Dot(1039, 100),
Dot(950, 791),
Dot(1279, 161),
Dot(401, 787),
Dot(291, 49),
Dot(883, 68),
Dot(644, 583),
Dot(1138, 528),
Dot(858, 696),
Dot(323, 448),
Dot(840, 231),
Dot(1150, 593),
Dot(165, 472),
Dot(885, 709),
Dot(1171, 123),
Dot(1275, 91),
Dot(410, 210),
Dot(664, 10),
Dot(200, 182),
Dot(363, 283),
Dot(529, 610),
Dot(1216, 399),
Dot(992, 478),
Dot(1266, 70),
Dot(194, 40),
Dot(681, 376),
Dot(753, 277),
Dot(857, 303),
Dot(1006, 742),
Dot(609, 670),
Dot(842, 712),
Dot(1131, 767),
Dot(579, 665),
Dot(1109, 610),
Dot(80, 239),
Dot(1240, 816),
Dot(69, 427),
Dot(557, 386),
Dot(781, 284),
Dot(494, 824),
Dot(160, 77),
Dot(36, 252),
Dot(838, 149),
Dot(325, 469),
Dot(611, 610),
Dot(599, 0),
Dot(612, 635),
Dot(979, 446),
Dot(887, 887),
Dot(974, 478),
Dot(609, 130),
Dot(551, 479),
Dot(345, 101),
Dot(343, 781),
Dot(880, 462),
Dot(726, 533),
Dot(164, 871),
Dot(1300, 124),
Dot(780, 408),
Dot(627, 649),
Dot(1246, 362),
Dot(1200, 404),
Dot(398, 1),
Dot(172, 528),
Dot(994, 322),
Dot(572, 528),
Dot(360, 775),
Dot(380, 490),
Dot(783, 522),
Dot(27, 649),
Dot(1278, 329),
Dot(1255, 110),
Dot(27, 245),
Dot(679, 617),
Dot(64, 362),
Dot(58, 418),
Dot(1230, 574),
Dot(281, 254),
Dot(408, 126),
Dot(701, 140),
Dot(659, 448),
Dot(971, 721),
Dot(766, 129),
Dot(671, 115),
Dot(1252, 582),
Dot(271, 840),
Dot(957, 40),
Dot(570, 406),
Dot(110, 490),
Dot(634, 543),
Dot(304, 152),
Dot(699, 610),
Dot(1131, 212),
Dot(1226, 511),
Dot(296, 598),
Dot(773, 30),
Dot(1223, 660),
Dot(693, 586),
Dot(84, 847),
Dot(1208, 72),
Dot(338, 722),
Dot(164, 785),
Dot(924, 418),
Dot(1240, 740),
Dot(75, 504),
Dot(330, 105),
Dot(455, 746),
Dot(842, 264),
Dot(818, 358),
]
enum Fold {
case x(Int)
case y(Int)
}
let folds: [Fold] = [
.x(655),
.y(447),
.x(327),
.y(223),
.x(163),
.y(111),
.x(81),
.y(55),
.x(40),
.y(27),
.y(13),
.y(6),
]
}