From 4ef8ff62ccfa03da5d40b869bd0eb20e40551170 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 2 Dec 2021 08:27:40 +0100 Subject: [PATCH 01/32] Day 1 and 2 --- day1.hs | 32 ++ day2.hs | 1029 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1061 insertions(+) create mode 100644 day1.hs create mode 100644 day2.hs diff --git a/day1.hs b/day1.hs new file mode 100644 index 0000000..60f04cd --- /dev/null +++ b/day1.hs @@ -0,0 +1,32 @@ + +list :: [Integer] +list = [118,121,123, 125, 134, 132, 137, 135, 136, 137, 138, 139, 163, 170, 171, 159, 161, 162, 156, 155, 151, 168, 172, 166, 159, 180, 187, 172, 161, 162, 161, 183, 182, 185, 175, 173, 174, 177, 192, 191, 190, 219, 225, 226, 216, 226, 230, 234, 236, 244, 235, 245, 266, 299, 300, 281, 291, 309, 303, 333, 332, 309, 312, 313, 314, 315, 339, 351, 353, 352, 338, 343, 337, 346, 338, 335, 332, 329, 345, 347, 332, 339, 354, 373, 376, 397, 400, 401, 433, 434, 440, 434, 438, 443, 448, 452, 456, 450, 459, 463, 464, 488, 492, 488, 506, 507, 516, 523, 524, 522, 546, 551, 553, 554, 553, 562, 559, 567, 569, 556, 557, 577, 581, 582, 591, 593, 594, 595, 598, 602, 611, 623, 604, 613, 603, 607, 612, 616, 612, 613, 612, 598, 607, 622, 619, 636, 638, 639, 637, 659, 657, 677, 678, 679, 655, 651, 655, 654, 662, 666, 672, 673, 676, 683, 684, 693, 688, 691, 698, 702, 704, 706, 707, 710, 709, 725, 731, 729, 767, 779, 780, 807, 806, 808, 815, 812, 811, 819, 820, 821, 822, 826, 812, 810, 820, 817, 803, 808, 826, 831, 837, 842, 839, 840, 827, 838, 840, 841, 842, 848, 841, 838, 839, 838, 839, 841, 821, 812, 813, 796, 817, 820, 845, 854, 857, 858, 866, 869, 870, 881, 882, 888, 889, 891, 886, 891, 901, 930, 940, 944, 943, 958, 952, 916, 903, 905, 907, 910, 915, 907, 906, 907, 919, 921, 923, 925, 924, 929, 931, 936, 937, 935, 953, 962, 968, 980, 981, 1003, 1005, 1006, 1011, 1013, 1014, 1015, 1016, 1015, 995, 1002, 1018, 1020, 1031, 1043, 1038, 1039, 1040, 1041, 1043, 1037, 1041, 1050, 1052, 1048, 1049, 1050, 1053, 1057, 1064, 1067, 1068, 1078, 1086, 1098, 1103, 1119, 1138, 1114, 1115, 1129, 1130, 1143, 1138, 1141, 1150, 1153, 1146, 1155, 1157, 1145, 1146, 1147, 1145, 1144, 1158, 1163, 1167, 1171, 1174, 1176, 1188, 1207, 1205, 1208, 1212, 1216, 1222, 1231, 1233, 1249, 1253, 1269, 1231, 1235, 1242, 1241, 1246, 1247, 1263, 1266, 1259, 1260, 1279, 1280, 1281, 1285, 1284, 1285, 1296, 1300, 1302, 1319, 1312, 1313, 1325, 1342, 1344, 1361, 1349, 1363, 1367, 1366, 1346, 1347, 1369, 1385, 1386, 1397, 1401, 1402, 1396, 1397, 1400, 1395, 1400, 1411, 1419, 1427, 1446, 1445, 1459, 1460, 1464, 1472, 1482, 1474, 1475, 1476, 1481, 1484, 1487, 1494, 1497, 1494, 1495, 1498, 1501, 1511, 1521, 1528, 1533, 1535, 1544, 1518, 1521, 1509, 1533, 1537, 1540, 1545, 1546, 1544, 1564, 1553, 1576, 1577, 1563, 1560, 1564, 1569, 1570, 1571, 1573, 1576, 1573, 1596, 1597, 1593, 1597, 1596, 1592, 1597, 1578, 1555, 1576, 1584, 1585, 1584, 1594, 1598, 1608, 1613, 1614, 1606, 1608, 1621, 1648, 1614, 1615, 1606, 1609, 1610, 1625, 1638, 1640, 1617, 1655, 1661, 1644, 1648, 1649, 1653, 1675, 1674, 1681, 1679, 1682, 1683, 1695, 1696, 1699, 1700, 1701, 1704, 1709, 1729, 1732, 1739, 1742, 1746, 1748, 1746, 1753, 1744, 1752, 1749, 1750, 1758, 1762, 1767, 1771, 1772, 1761, 1763, 1784, 1804, 1809, 1792, 1787, 1806, 1809, 1815, 1817, 1828, 1821, 1824, 1823, 1824, 1841, 1840, 1833, 1840, 1842, 1863, 1862, 1865, 1866, 1868, 1878, 1887, 1897, 1905, 1906, 1913, 1908, 1915, 1930, 1928, 1922, 1925, 1927, 1928, 1939, 1938, 1946, 1947, 1954, 1955, 1959, 1961, 1951, 1945, 1949, 1954, 1959, 1961, 1962, 1964, 1960, 1956, 1958, 1975, 1990, 1994, 2002, 2004, 2011, 2010, 2020, 2042, 2043, 2046, 2047, 2044, 2048, 2065, 2074, 2075, 2086, 2088, 2094, 2098, 2092, 2083, 2082, 2084, 2094, 2097, 2099, 2098, 2101, 2104, 2112, 2118, 2105, 2108, 2109, 2104, 2105, 2103, 2114, 2144, 2138, 2139, 2142, 2114, 2120, 2139, 2147, 2153, 2190, 2191, 2197, 2193, 2206, 2216, 2225, 2230, 2232, 2258, 2264, 2236, 2264, 2289, 2306, 2307, 2315, 2319, 2322, 2277, 2279, 2271, 2279, 2285, 2290, 2297, 2316, 2317, 2320, 2329, 2330, 2323, 2326, 2327, 2331, 2336, 2341, 2348, 2352, 2386, 2396, 2401, 2400, 2401, 2418, 2419, 2429, 2432, 2430, 2428, 2406, 2401, 2434, 2435, 2443, 2463, 2478, 2479, 2481, 2482, 2486, 2490, 2491, 2492, 2496, 2497, 2499, 2507, 2525, 2522, 2523, 2522, 2521, 2526, 2531, 2530, 2525, 2526, 2537, 2533, 2534, 2542, 2544, 2531, 2539, 2546, 2550, 2551, 2553, 2554, 2552, 2562, 2566, 2551, 2583, 2587, 2601, 2605, 2604, 2610, 2628, 2639, 2641, 2654, 2657, 2676, 2672, 2686, 2694, 2677, 2693, 2694, 2707, 2696, 2710, 2753, 2752, 2722, 2723, 2724, 2741, 2746, 2753, 2763, 2764, 2805, 2806, 2810, 2811, 2823, 2854, 2855, 2852, 2872, 2873, 2867, 2876, 2881, 2885, 2899, 2900, 2901, 2903, 2918, 2912, 2924, 2915, 2920, 2932, 2942, 2943, 2934, 2953, 2960, 2956, 2957, 2959, 2982, 2991, 3016, 3018, 3047, 3032, 3048, 3051, 3056, 3063, 3064, 3063, 3065, 3066, 3070, 3075, 3069, 3075, 3073, 3074, 3083, 3086, 3097, 3093, 3082, 3077, 3098, 3105, 3128, 3144, 3148, 3174, 3173, 3172, 3171, 3169, 3176, 3180, 3166, 3165, 3182, 3183, 3206, 3207, 3208, 3211, 3215, 3214, 3220, 3233, 3234, 3248, 3249, 3252, 3253, 3254, 3256, 3258, 3266, 3265, 3266, 3274, 3291, 3275, 3291, 3294, 3299, 3292, 3293, 3306, 3292, 3291, 3290, 3291, 3295, 3296, 3288, 3294, 3305, 3308, 3315, 3319, 3327, 3332, 3334, 3332, 3342, 3346, 3347, 3348, 3360, 3370, 3371, 3373, 3379, 3387, 3386, 3389, 3393, 3398, 3405, 3406, 3413, 3417, 3431, 3433, 3434, 3438, 3442, 3443, 3445, 3446, 3447, 3448, 3454, 3439, 3450, 3451, 3453, 3452, 3455, 3462, 3466, 3475, 3479, 3476, 3479, 3480, 3484, 3486, 3489, 3491, 3505, 3506, 3510, 3513, 3530, 3532, 3537, 3540, 3542, 3553, 3558, 3570, 3569, 3570, 3569, 3570, 3565, 3559, 3561, 3556, 3557, 3575, 3585, 3590, 3592, 3593, 3602, 3619, 3623, 3624, 3669, 3671, 3672, 3683, 3686, 3699, 3708, 3709, 3714, 3713, 3718, 3719, 3742, 3729, 3730, 3731, 3729, 3727, 3741, 3742, 3741, 3742, 3734, 3740, 3744, 3734, 3737, 3738, 3739, 3759, 3760, 3761, 3750, 3751, 3753, 3745, 3744, 3731, 3733, 3725, 3750, 3757, 3767, 3768, 3774, 3777, 3791, 3815, 3816, 3817, 3819, 3820, 3825, 3835, 3836, 3858, 3861, 3860, 3844, 3846, 3847, 3877, 3906, 3910, 3912, 3940, 3941, 3975, 3980, 3983, 3987, 4016, 4028, 4047, 4066, 4061, 4058, 4061, 4066, 4088, 4089, 4092, 4098, 4105, 4110, 4111, 4103, 4108, 4082, 4087, 4086, 4087, 4082, 4083, 4084, 4104, 4110, 4111, 4119, 4128, 4133, 4113, 4129, 4128, 4129, 4121, 4123, 4126, 4132, 4133, 4143, 4149, 4148, 4147, 4145, 4142, 4143, 4146, 4138, 4139, 4141, 4147, 4148, 4160, 4156, 4162, 4173, 4179, 4181, 4190, 4191, 4190, 4191, 4192, 4193, 4194, 4208, 4211, 4215, 4226, 4223, 4227, 4230, 4250, 4248, 4267, 4260, 4274, 4275, 4278, 4291, 4298, 4304, 4314, 4315, 4317, 4319, 4325, 4326, 4327, 4349, 4345, 4356, 4360, 4365, 4366, 4370, 4385, 4384, 4386, 4399, 4423, 4397, 4410, 4409, 4388, 4393, 4397, 4402, 4403, 4418, 4413, 4418, 4417, 4418, 4422, 4411, 4374, 4403, 4412, 4427, 4445, 4449, 4453, 4458, 4459, 4465, 4447, 4448, 4450, 4460, 4464, 4465, 4467, 4472, 4486, 4484, 4482, 4507, 4512, 4525, 4524, 4525, 4527, 4522, 4521, 4524, 4550, 4551, 4552, 4550, 4554, 4555, 4586, 4578, 4582, 4594, 4588, 4592, 4595, 4598, 4599, 4614, 4615, 4623, 4632, 4633, 4655, 4661, 4662, 4690, 4689, 4690, 4694, 4704, 4705, 4694, 4697, 4702, 4703, 4704, 4703, 4709, 4714, 4722, 4728, 4741, 4761, 4767, 4768, 4770, 4775, 4778, 4770, 4788, 4789, 4802, 4805, 4808, 4811, 4819, 4812, 4813, 4821, 4838, 4839, 4843, 4817, 4821, 4824, 4820, 4843, 4845, 4849, 4852, 4872, 4873, 4870, 4885, 4902, 4920, 4921, 4923, 4936, 4930, 4932, 4933, 4931, 4949, 4974, 4976, 4977, 4978, 4990, 4991, 5001, 5002, 5001, 5027, 5026, 5021, 5054, 5055, 5056, 5059, 5064, 5065, 5074, 5081, 5093, 5095, 5100, 5113, 5125, 5126, 5137, 5142, 5123, 5118, 5117, 5118, 5125, 5135, 5142, 5154, 5165, 5158, 5146, 5148, 5149, 5163, 5176, 5177, 5169, 5171, 5168, 5186, 5191, 5196, 5197, 5224, 5226, 5224, 5215, 5213, 5214, 5198, 5201, 5202, 5213, 5214, 5216, 5251, 5255, 5259, 5262, 5259, 5261, 5264, 5273, 5281, 5254, 5265, 5280, 5289, 5294, 5298, 5309, 5310, 5298, 5297, 5306, 5311, 5301, 5306, 5289, 5287, 5300, 5299, 5301, 5308, 5314, 5323, 5329, 5330, 5324, 5325, 5332, 5335, 5332, 5333, 5334, 5337, 5345, 5351, 5355, 5364, 5366, 5382, 5391, 5408, 5400, 5402, 5405, 5390, 5391, 5394, 5407, 5409, 5410, 5411, 5412, 5416, 5412, 5424, 5426, 5425, 5426, 5434, 5421, 5433, 5440, 5441, 5435, 5424, 5417, 5413, 5414, 5415, 5418, 5420, 5421, 5444, 5442, 5433, 5435, 5436, 5437, 5407, 5406, 5424, 5427, 5433, 5434, 5438, 5441, 5443, 5444, 5445, 5446, 5453, 5464, 5465, 5478, 5474, 5462, 5463, 5491, 5498, 5499, 5500, 5501, 5504, 5506, 5530, 5524, 5517, 5521, 5530, 5532, 5538, 5544, 5541, 5554, 5556, 5553, 5554, 5556, 5562, 5563, 5568, 5572, 5584, 5581, 5584, 5582, 5586, 5588, 5599, 5602, 5603, 5601, 5610, 5607, 5602, 5604, 5600, 5602, 5605, 5606, 5608, 5606, 5610, 5619, 5620, 5619, 5623, 5624, 5626, 5627, 5628, 5630, 5637, 5650, 5674, 5677, 5661, 5669, 5675, 5676, 5681, 5685, 5687, 5709, 5713, 5714, 5724, 5728, 5729, 5727, 5741, 5755, 5741, 5730, 5724, 5727, 5726, 5730, 5731, 5732, 5734, 5733, 5734, 5735, 5736, 5737, 5736, 5730, 5732, 5742, 5744, 5747, 5755, 5760, 5755, 5757, 5768, 5793, 5798, 5799, 5805, 5808, 5810, 5812, 5814, 5815, 5817, 5850, 5851, 5839, 5860, 5870, 5871, 5879, 5886, 5897, 5904, 5908, 5907, 5911, 5910, 5914, 5916, 5917, 5919, 5917, 5928, 5930, 5931, 5932, 5941, 5944, 5946, 5951, 5931, 5905, 5930, 5936, 5937, 5935, 5936, 5942, 5944, 5951, 5916, 5899, 5892, 5891, 5902, 5907, 5951, 5952, 5957, 5958, 5961, 5965, 5969, 5974, 5975, 5953, 5956, 5965, 5985, 5987, 6017, 6016, 6017, 5990, 5991, 5999, 5993, 5995, 6016, 6017, 6014, 6049, 6042, 6053, 6055, 6068, 6069, 6074, 6072, 6079, 6074, 6076, 6066, 6065, 6067, 6098, 6104, 6116, 6115, 6113, 6099, 6100, 6101, 6100, 6111, 6115, 6140, 6106, 6107, 6117, 6131, 6135, 6144, 6118, 6113, 6118, 6128, 6129, 6135, 6138, 6143, 6144, 6142, 6144, 6145, 6144, 6147, 6151, 6149, 6150, 6151, 6135, 6136, 6132, 6118, 6113, 6120, 6117, 6118, 6122, 6125, 6126, 6124, 6131, 6132, 6135, 6136, 6154, 6160, 6167, 6179, 6181, 6186, 6196, 6225, 6228, 6232, 6221, 6238, 6249, 6252, 6254, 6256, 6260, 6269, 6271, 6263, 6272, 6271, 6272, 6271, 6285, 6308, 6319, 6327, 6334, 6337, 6356, 6385, 6380, 6387, 6395, 6396, 6392, 6395, 6398, 6400, 6401, 6396, 6399, 6400, 6380, 6379, 6380, 6377, 6397, 6398, 6396, 6415, 6419, 6438, 6449, 6465, 6459, 6461, 6463, 6466, 6492, 6493, 6502, 6501, 6503, 6510, 6511, 6512, 6505, 6506, 6508, 6525, 6523, 6530, 6536, 6535, 6539, 6544, 6545, 6534, 6532, 6537, 6542, 6543, 6544, 6545, 6559, 6560, 6577, 6578, 6580, 6581, 6582, 6591, 6597, 6581, 6595, 6608, 6620, 6624, 6632, 6636, 6639, 6637, 6645, 6648, 6650, 6651, 6645, 6648, 6661, 6658, 6659, 6668, 6667, 6666, 6684, 6700, 6701, 6730, 6736, 6746, 6749, 6750, 6751, 6741, 6728, 6729, 6762, 6763, 6792, 6795, 6796, 6813, 6827, 6835, 6843, 6844, 6852, 6854, 6856, 6868, 6870, 6876, 6874, 6865, 6870, 6867, 6870, 6877, 6887, 6881, 6869, 6871, 6874, 6875, 6876, 6902, 6903, 6901, 6902, 6912, 6913, 6912, 6913, 6927, 6928, 6935, 6936, 6942, 6943, 6942, 6943, 6953, 6954, 6961, 6989, 6992, 6993, 6997, 7018, 7022, 7041, 7052, 7055, 7058, 7049, 7050, 7051, 7047, 7064, 7059, 7074, 7082, 7083, 7097, 7102, 7092, 7094, 7101, 7143, 7165, 7176, 7178, 7183, 7180, 7203, 7204, 7207, 7211, 7217, 7220, 7212, 7215, 7217, 7223, 7207, 7214, 7213, 7215, 7214, 7212, 7219, 7226, 7225, 7230, 7239, 7240, 7241, 7248, 7247, 7254, 7258, 7262, 7280, 7298, 7297, 7307, 7308, 7316, 7314, 7339, 7340, 7359, 7356, 7383, 7382, 7385, 7397, 7399, 7410, 7437, 7440, 7439, 7457, 7455, 7462, 7463, 7464, 7465, 7477, 7478, 7494, 7500, 7507, 7512, 7547, 7558, 7559, 7571, 7572, 7575, 7608, 7633, 7636, 7629, 7628, 7633, 7635, 7641, 7659, 7663, 7677, 7694, 7708, 7701, 7697, 7682, 7684, 7675, 7664, 7672, 7675, 7669, 7671, 7672, 7684, 7688, 7701, 7712, 7741, 7727, 7733, 7735, 7736, 7737, 7744, 7748, 7749, 7750, 7736, 7737, 7752, 7755, 7756, 7757, 7791, 7790, 7785, 7791, 7812, 7825, 7816, 7819, 7824, 7829, 7855, 7846, 7854, 7827, 7831, 7835, 7836, 7845, 7848, 7861, 7862, 7864, 7847, 7858, 7877, 7878, 7888, 7885, 7881, 7878, 7883, 7884, 7882, 7883, 7901, 7916, 7922, 7909, 7915, 7929, 7942, 7953, 7954, 7951, 7967, 7963] + +triples (x:y:z:rest) = sum : triples (y:z:rest) + where sum = x + y + z + +triples [] = [] +triples [_] = [] +triples [_ , _] = [] + +count [] = 0 +count [x] = 0 +count (x:y:rest) + | x < y = 1 + tailCount + | otherwise = tailCount + where tailCount = count (y: rest) + + + +{- + +>>> list +[118,121,123,125,134,132,137,135,136,137,138,139,163,170,171,159,161,162,156,155,151,168,172,166,159,180,187,172,161,162,161,183,182,185,175,173,174,177,192,191,190,219,225,226,216,226,230,234,236,244,235,245,266,299,300,281,291,309,303,333,332,309,312,313,314,315,339,351,353,352,338,343,337,346,338,335,332,329,345,347,332,339,354,373,376,397,400,401,433,434,440,434,438,443,448,452,456,450,459,463,464,488,492,488,506,507,516,523,524,522,546,551,553,554,553,562,559,567,569,556,557,577,581,582,591,593,594,595,598,602,611,623,604,613,603,607,612,616,612,613,612,598,607,622,619,636,638,639,637,659,657,677,678,679,655,651,655,654,662,666,672,673,676,683,684,693,688,691,698,702,704,706,707,710,709,725,731,729,767,779,780,807,806,808,815,812,811,819,820,821,822,826,812,810,820,817,803,808,826,831,837,842,839,840,827,838,840,841,842,848,841,838,839,838,839,841,821,812,813,796,817,820,845,854,857,858,866,869,870,881,882,888,889,891,886,891,901,930,940,944,943,958,952,916,903,905,907,910,915,907,906,907,919,921,923,925,924,929,931,936,937,935,953,962,968,980,981,1003,1005,1006,1011,1013,1014,1015,1016,1015,995,1002,1018,1020,1031,1043,1038,1039,1040,1041,1043,1037,1041,1050,1052,1048,1049,1050,1053,1057,1064,1067,1068,1078,1086,1098,1103,1119,1138,1114,1115,1129,1130,1143,1138,1141,1150,1153,1146,1155,1157,1145,1146,1147,1145,1144,1158,1163,1167,1171,1174,1176,1188,1207,1205,1208,1212,1216,1222,1231,1233,1249,1253,1269,1231,1235,1242,1241,1246,1247,1263,1266,1259,1260,1279,1280,1281,1285,1284,1285,1296,1300,1302,1319,1312,1313,1325,1342,1344,1361,1349,1363,1367,1366,1346,1347,1369,1385,1386,1397,1401,1402,1396,1397,1400,1395,1400,1411,1419,1427,1446,1445,1459,1460,1464,1472,1482,1474,1475,1476,1481,1484,1487,1494,1497,1494,1495,1498,1501,1511,1521,1528,1533,1535,1544,1518,1521,1509,1533,1537,1540,1545,1546,1544,1564,1553,1576,1577,1563,1560,1564,1569,1570,1571,1573,1576,1573,1596,1597,1593,1597,1596,1592,1597,1578,1555,1576,1584,1585,1584,1594,1598,1608,1613,1614,1606,1608,1621,1648,1614,1615,1606,1609,1610,1625,1638,1640,1617,1655,1661,1644,1648,1649,1653,1675,1674,1681,1679,1682,1683,1695,1696,1699,1700,1701,1704,1709,1729,1732,1739,1742,1746,1748,1746,1753,1744,1752,1749,1750,1758,1762,1767,1771,1772,1761,1763,1784,1804,1809,1792,1787,1806,1809,1815,1817,1828,1821,1824,1823,1824,1841,1840,1833,1840,1842,1863,1862,1865,1866,1868,1878,1887,1897,1905,1906,1913,1908,1915,1930,1928,1922,1925,1927,1928,1939,1938,1946,1947,1954,1955,1959,1961,1951,1945,1949,1954,1959,1961,1962,1964,1960,1956,1958,1975,1990,1994,2002,2004,2011,2010,2020,2042,2043,2046,2047,2044,2048,2065,2074,2075,2086,2088,2094,2098,2092,2083,2082,2084,2094,2097,2099,2098,2101,2104,2112,2118,2105,2108,2109,2104,2105,2103,2114,2144,2138,2139,2142,2114,2120,2139,2147,2153,2190,2191,2197,2193,2206,2216,2225,2230,2232,2258,2264,2236,2264,2289,2306,2307,2315,2319,2322,2277,2279,2271,2279,2285,2290,2297,2316,2317,2320,2329,2330,2323,2326,2327,2331,2336,2341,2348,2352,2386,2396,2401,2400,2401,2418,2419,2429,2432,2430,2428,2406,2401,2434,2435,2443,2463,2478,2479,2481,2482,2486,2490,2491,2492,2496,2497,2499,2507,2525,2522,2523,2522,2521,2526,2531,2530,2525,2526,2537,2533,2534,2542,2544,2531,2539,2546,2550,2551,2553,2554,2552,2562,2566,2551,2583,2587,2601,2605,2604,2610,2628,2639,2641,2654,2657,2676,2672,2686,2694,2677,2693,2694,2707,2696,2710,2753,2752,2722,2723,2724,2741,2746,2753,2763,2764,2805,2806,2810,2811,2823,2854,2855,2852,2872,2873,2867,2876,2881,2885,2899,2900,2901,2903,2918,2912,2924,2915,2920,2932,2942,2943,2934,2953,2960,2956,2957,2959,2982,2991,3016,3018,3047,3032,3048,3051,3056,3063,3064,3063,3065,3066,3070,3075,3069,3075,3073,3074,3083,3086,3097,3093,3082,3077,3098,3105,3128,3144,3148,3174,3173,3172,3171,3169,3176,3180,3166,3165,3182,3183,3206,3207,3208,3211,3215,3214,3220,3233,3234,3248,3249,3252,3253,3254,3256,3258,3266,3265,3266,3274,3291,3275,3291,3294,3299,3292,3293,3306,3292,3291,3290,3291,3295,3296,3288,3294,3305,3308,3315,3319,3327,3332,3334,3332,3342,3346,3347,3348,3360,3370,3371,3373,3379,3387,3386,3389,3393,3398,3405,3406,3413,3417,3431,3433,3434,3438,3442,3443,3445,3446,3447,3448,3454,3439,3450,3451,3453,3452,3455,3462,3466,3475,3479,3476,3479,3480,3484,3486,3489,3491,3505,3506,3510,3513,3530,3532,3537,3540,3542,3553,3558,3570,3569,3570,3569,3570,3565,3559,3561,3556,3557,3575,3585,3590,3592,3593,3602,3619,3623,3624,3669,3671,3672,3683,3686,3699,3708,3709,3714,3713,3718,3719,3742,3729,3730,3731,3729,3727,3741,3742,3741,3742,3734,3740,3744,3734,3737,3738,3739,3759,3760,3761,3750,3751,3753,3745,3744,3731,3733,3725,3750,3757,3767,3768,3774,3777,3791,3815,3816,3817,3819,3820,3825,3835,3836,3858,3861,3860,3844,3846,3847,3877,3906,3910,3912,3940,3941,3975,3980,3983,3987,4016,4028,4047,4066,4061,4058,4061,4066,4088,4089,4092,4098,4105,4110,4111,4103,4108,4082,4087,4086,4087,4082,4083,4084,4104,4110,4111,4119,4128,4133,4113,4129,4128,4129,4121,4123,4126,4132,4133,4143,4149,4148,4147,4145,4142,4143,4146,4138,4139,4141,4147,4148,4160,4156,4162,4173,4179,4181,4190,4191,4190,4191,4192,4193,4194,4208,4211,4215,4226,4223,4227,4230,4250,4248,4267,4260,4274,4275,4278,4291,4298,4304,4314,4315,4317,4319,4325,4326,4327,4349,4345,4356,4360,4365,4366,4370,4385,4384,4386,4399,4423,4397,4410,4409,4388,4393,4397,4402,4403,4418,4413,4418,4417,4418,4422,4411,4374,4403,4412,4427,4445,4449,4453,4458,4459,4465,4447,4448,4450,4460,4464,4465,4467,4472,4486,4484,4482,4507,4512,4525,4524,4525,4527,4522,4521,4524,4550,4551,4552,4550,4554,4555,4586,4578,4582,4594,4588,4592,4595,4598,4599,4614,4615,4623,4632,4633,4655,4661,4662,4690,4689,4690,4694,4704,4705,4694,4697,4702,4703,4704,4703,4709,4714,4722,4728,4741,4761,4767,4768,4770,4775,4778,4770,4788,4789,4802,4805,4808,4811,4819,4812,4813,4821,4838,4839,4843,4817,4821,4824,4820,4843,4845,4849,4852,4872,4873,4870,4885,4902,4920,4921,4923,4936,4930,4932,4933,4931,4949,4974,4976,4977,4978,4990,4991,5001,5002,5001,5027,5026,5021,5054,5055,5056,5059,5064,5065,5074,5081,5093,5095,5100,5113,5125,5126,5137,5142,5123,5118,5117,5118,5125,5135,5142,5154,5165,5158,5146,5148,5149,5163,5176,5177,5169,5171,5168,5186,5191,5196,5197,5224,5226,5224,5215,5213,5214,5198,5201,5202,5213,5214,5216,5251,5255,5259,5262,5259,5261,5264,5273,5281,5254,5265,5280,5289,5294,5298,5309,5310,5298,5297,5306,5311,5301,5306,5289,5287,5300,5299,5301,5308,5314,5323,5329,5330,5324,5325,5332,5335,5332,5333,5334,5337,5345,5351,5355,5364,5366,5382,5391,5408,5400,5402,5405,5390,5391,5394,5407,5409,5410,5411,5412,5416,5412,5424,5426,5425,5426,5434,5421,5433,5440,5441,5435,5424,5417,5413,5414,5415,5418,5420,5421,5444,5442,5433,5435,5436,5437,5407,5406,5424,5427,5433,5434,5438,5441,5443,5444,5445,5446,5453,5464,5465,5478,5474,5462,5463,5491,5498,5499,5500,5501,5504,5506,5530,5524,5517,5521,5530,5532,5538,5544,5541,5554,5556,5553,5554,5556,5562,5563,5568,5572,5584,5581,5584,5582,5586,5588,5599,5602,5603,5601,5610,5607,5602,5604,5600,5602,5605,5606,5608,5606,5610,5619,5620,5619,5623,5624,5626,5627,5628,5630,5637,5650,5674,5677,5661,5669,5675,5676,5681,5685,5687,5709,5713,5714,5724,5728,5729,5727,5741,5755,5741,5730,5724,5727,5726,5730,5731,5732,5734,5733,5734,5735,5736,5737,5736,5730,5732,5742,5744,5747,5755,5760,5755,5757,5768,5793,5798,5799,5805,5808,5810,5812,5814,5815,5817,5850,5851,5839,5860,5870,5871,5879,5886,5897,5904,5908,5907,5911,5910,5914,5916,5917,5919,5917,5928,5930,5931,5932,5941,5944,5946,5951,5931,5905,5930,5936,5937,5935,5936,5942,5944,5951,5916,5899,5892,5891,5902,5907,5951,5952,5957,5958,5961,5965,5969,5974,5975,5953,5956,5965,5985,5987,6017,6016,6017,5990,5991,5999,5993,5995,6016,6017,6014,6049,6042,6053,6055,6068,6069,6074,6072,6079,6074,6076,6066,6065,6067,6098,6104,6116,6115,6113,6099,6100,6101,6100,6111,6115,6140,6106,6107,6117,6131,6135,6144,6118,6113,6118,6128,6129,6135,6138,6143,6144,6142,6144,6145,6144,6147,6151,6149,6150,6151,6135,6136,6132,6118,6113,6120,6117,6118,6122,6125,6126,6124,6131,6132,6135,6136,6154,6160,6167,6179,6181,6186,6196,6225,6228,6232,6221,6238,6249,6252,6254,6256,6260,6269,6271,6263,6272,6271,6272,6271,6285,6308,6319,6327,6334,6337,6356,6385,6380,6387,6395,6396,6392,6395,6398,6400,6401,6396,6399,6400,6380,6379,6380,6377,6397,6398,6396,6415,6419,6438,6449,6465,6459,6461,6463,6466,6492,6493,6502,6501,6503,6510,6511,6512,6505,6506,6508,6525,6523,6530,6536,6535,6539,6544,6545,6534,6532,6537,6542,6543,6544,6545,6559,6560,6577,6578,6580,6581,6582,6591,6597,6581,6595,6608,6620,6624,6632,6636,6639,6637,6645,6648,6650,6651,6645,6648,6661,6658,6659,6668,6667,6666,6684,6700,6701,6730,6736,6746,6749,6750,6751,6741,6728,6729,6762,6763,6792,6795,6796,6813,6827,6835,6843,6844,6852,6854,6856,6868,6870,6876,6874,6865,6870,6867,6870,6877,6887,6881,6869,6871,6874,6875,6876,6902,6903,6901,6902,6912,6913,6912,6913,6927,6928,6935,6936,6942,6943,6942,6943,6953,6954,6961,6989,6992,6993,6997,7018,7022,7041,7052,7055,7058,7049,7050,7051,7047,7064,7059,7074,7082,7083,7097,7102,7092,7094,7101,7143,7165,7176,7178,7183,7180,7203,7204,7207,7211,7217,7220,7212,7215,7217,7223,7207,7214,7213,7215,7214,7212,7219,7226,7225,7230,7239,7240,7241,7248,7247,7254,7258,7262,7280,7298,7297,7307,7308,7316,7314,7339,7340,7359,7356,7383,7382,7385,7397,7399,7410,7437,7440,7439,7457,7455,7462,7463,7464,7465,7477,7478,7494,7500,7507,7512,7547,7558,7559,7571,7572,7575,7608,7633,7636,7629,7628,7633,7635,7641,7659,7663,7677,7694,7708,7701,7697,7682,7684,7675,7664,7672,7675,7669,7671,7672,7684,7688,7701,7712,7741,7727,7733,7735,7736,7737,7744,7748,7749,7750,7736,7737,7752,7755,7756,7757,7791,7790,7785,7791,7812,7825,7816,7819,7824,7829,7855,7846,7854,7827,7831,7835,7836,7845,7848,7861,7862,7864,7847,7858,7877,7878,7888,7885,7881,7878,7883,7884,7882,7883,7901,7916,7922,7909,7915,7929,7942,7953,7954,7951,7967,7963] + +>>> count $ triples list +1597 + +>>> count list +1553 + +-} diff --git a/day2.hs b/day2.hs new file mode 100644 index 0000000..5fcf1de --- /dev/null +++ b/day2.hs @@ -0,0 +1,1029 @@ +data Command = Forward Int | Down Int | Up Int deriving Show + + +command (position, depth) (Forward amount) = (position + amount, depth) +command (position, depth) (Down amount) = (position, depth + amount) +command (position, depth) (Up amount) = (position, depth - amount) + + +command2 (position, depth, aim) (Forward amount) = (position + amount, depth + amount * aim, aim) +command2 (position, depth, aim) (Down amount) = (position, depth, aim + amount) +command2 (position, depth, aim) (Up amount) = (position, depth, aim - amount) + +dropLast (a, b, c) = (a, b) + +list = [ + Forward 4, + Down 7, + Down 4, + Forward 2, + Down 4, + Down 9, + Down 1, + Forward 1, + Down 4, + Up 5, + Forward 5, + Down 3, + Forward 5, + Forward 6, + Down 5, + Down 3, + Forward 8, + Up 7, + Forward 1, + Forward 5, + Forward 7, + Up 2, + Up 7, + Down 1, + Down 6, + Forward 7, + Forward 6, + Down 7, + Up 9, + Down 8, + Down 3, + Down 1, + Down 4, + Down 3, + Forward 7, + Down 1, + Forward 6, + Down 3, + Forward 3, + Down 6, + Forward 5, + Down 7, + Up 5, + Forward 7, + Forward 9, + Up 1, + Forward 5, + Down 5, + Down 2, + Forward 1, + Up 7, + Down 8, + Down 2, + Forward 8, + Forward 1, + Up 1, + Up 5, + Forward 9, + Up 9, + Down 5, + Down 5, + Forward 6, + Forward 1, + Down 1, + Down 3, + Up 6, + Up 9, + Down 6, + Down 1, + Forward 1, + Down 8, + Down 3, + Down 8, + Up 6, + Forward 2, + Up 8, + Down 2, + Down 6, + Forward 1, + Down 2, + Down 1, + Down 5, + Down 8, + Forward 8, + Down 8, + Down 2, + Up 1, + Up 3, + Forward 1, + Down 6, + Up 5, + Forward 5, + Up 7, + Forward 3, + Forward 3, + Down 9, + Forward 1, + Forward 7, + Down 1, + Down 1, + Down 3, + Forward 4, + Down 4, + Forward 2, + Up 8, + Forward 2, + Down 4, + Down 2, + Up 1, + Forward 6, + Forward 6, + Down 9, + Forward 5, + Down 3, + Forward 5, + Down 6, + Down 9, + Forward 9, + Up 6, + Down 8, + Up 5, + Up 2, + Forward 5, + Down 4, + Forward 8, + Up 1, + Forward 2, + Down 9, + Down 4, + Up 7, + Down 1, + Down 6, + Down 7, + Down 7, + Down 2, + Down 2, + Up 7, + Down 1, + Up 7, + Up 4, + Forward 9, + Down 6, + Up 3, + Down 5, + Forward 8, + Up 4, + Forward 5, + Forward 8, + Forward 4, + Forward 7, + Forward 2, + Down 3, + Up 4, + Forward 7, + Forward 3, + Down 7, + Up 4, + Forward 8, + Forward 9, + Forward 9, + Up 6, + Up 5, + Forward 7, + Down 7, + Up 5, + Down 9, + Down 8, + Forward 5, + Forward 2, + Down 1, + Down 2, + Down 9, + Up 5, + Down 3, + Down 8, + Up 6, + Forward 9, + Down 9, + Up 3, + Up 9, + Forward 3, + Forward 9, + Down 5, + Up 3, + Down 6, + Forward 5, + Down 6, + Down 9, + Forward 3, + Down 7, + Down 5, + Down 9, + Forward 8, + Down 8, + Forward 8, + Forward 2, + Up 6, + Down 9, + Down 7, + Down 4, + Down 2, + Down 9, + Forward 7, + Up 1, + Forward 6, + Forward 7, + Down 3, + Down 5, + Down 3, + Forward 4, + Down 9, + Forward 2, + Down 1, + Forward 4, + Up 9, + Down 8, + Down 9, + Forward 5, + Up 2, + Down 7, + Down 2, + Down 7, + Forward 8, + Up 8, + Forward 9, + Up 8, + Down 3, + Down 8, + Forward 2, + Up 7, + Forward 4, + Up 8, + Forward 6, + Forward 6, + Forward 4, + Forward 3, + Down 4, + Up 7, + Down 8, + Forward 1, + Forward 9, + Forward 8, + Forward 8, + Down 6, + Down 6, + Down 4, + Down 8, + Forward 9, + Down 8, + Down 4, + Forward 2, + Forward 7, + Down 4, + Up 1, + Down 4, + Down 5, + Up 5, + Up 9, + Down 5, + Up 5, + Forward 2, + Down 8, + Forward 7, + Forward 2, + Up 4, + Forward 8, + Up 7, + Down 5, + Forward 5, + Down 5, + Down 6, + Down 7, + Forward 1, + Down 8, + Up 8, + Up 9, + Up 6, + Forward 2, + Forward 7, + Forward 4, + Down 6, + Up 4, + Up 3, + Down 3, + Forward 7, + Up 9, + Down 9, + Up 5, + Forward 1, + Up 7, + Down 7, + Down 7, + Up 8, + Down 3, + Forward 6, + Down 6, + Down 7, + Down 2, + Forward 5, + Up 8, + Up 5, + Up 1, + Down 5, + Forward 9, + Forward 9, + Forward 2, + Forward 6, + Up 8, + Forward 8, + Down 1, + Forward 6, + Up 7, + Up 3, + Forward 3, + Up 5, + Down 8, + Forward 2, + Down 5, + Down 5, + Forward 8, + Forward 8, + Forward 6, + Up 7, + Up 1, + Forward 1, + Down 7, + Down 1, + Up 9, + Forward 7, + Up 6, + Down 2, + Down 5, + Forward 1, + Down 1, + Up 1, + Forward 7, + Forward 7, + Forward 1, + Up 2, + Forward 2, + Down 9, + Down 8, + Forward 9, + Forward 8, + Up 9, + Down 1, + Down 3, + Up 1, + Down 3, + Up 9, + Down 7, + Up 6, + Down 1, + Down 2, + Down 2, + Down 4, + Forward 4, + Forward 3, + Down 7, + Forward 9, + Forward 1, + Down 2, + Down 2, + Down 4, + Down 7, + Up 7, + Forward 6, + Forward 2, + Down 6, + Down 3, + Down 3, + Down 8, + Up 8, + Forward 6, + Down 8, + Down 3, + Forward 7, + Down 4, + Down 4, + Up 1, + Up 8, + Forward 5, + Forward 6, + Down 8, + Down 9, + Up 7, + Forward 2, + Forward 4, + Down 4, + Forward 6, + Forward 5, + Down 4, + Up 5, + Down 5, + Forward 1, + Down 2, + Up 9, + Forward 9, + Forward 6, + Forward 8, + Down 2, + Down 9, + Up 1, + Forward 6, + Down 3, + Forward 6, + Forward 7, + Down 9, + Forward 1, + Forward 1, + Forward 5, + Up 5, + Up 6, + Forward 8, + Forward 6, + Forward 6, + Forward 8, + Down 6, + Down 8, + Forward 5, + Forward 7, + Forward 3, + Forward 3, + Up 5, + Forward 8, + Up 3, + Forward 6, + Forward 4, + Forward 3, + Up 8, + Down 1, + Down 3, + Down 7, + Up 8, + Forward 5, + Down 9, + Up 7, + Down 2, + Down 4, + Down 5, + Down 2, + Up 3, + Down 4, + Down 4, + Forward 4, + Forward 6, + Up 4, + Forward 5, + Forward 3, + Up 2, + Up 3, + Down 4, + Down 7, + Forward 7, + Forward 2, + Down 2, + Forward 5, + Up 4, + Forward 2, + Down 8, + Up 5, + Up 6, + Forward 2, + Forward 3, + Up 3, + Down 2, + Forward 7, + Down 6, + Forward 3, + Forward 6, + Up 9, + Forward 7, + Up 1, + Forward 1, + Forward 2, + Down 3, + Forward 9, + Down 1, + Up 5, + Up 7, + Up 8, + Down 5, + Up 4, + Down 9, + Forward 4, + Up 8, + Forward 3, + Forward 7, + Down 7, + Down 9, + Down 5, + Up 4, + Down 2, + Up 5, + Down 4, + Forward 3, + Down 7, + Up 5, + Forward 9, + Forward 9, + Forward 7, + Up 5, + Forward 7, + Up 3, + Forward 8, + Down 1, + Down 5, + Up 5, + Forward 4, + Forward 9, + Forward 9, + Up 5, + Down 9, + Up 7, + Up 3, + Forward 5, + Down 4, + Down 7, + Forward 9, + Down 9, + Up 3, + Forward 4, + Forward 7, + Down 9, + Forward 2, + Down 2, + Forward 1, + Down 2, + Up 7, + Down 6, + Forward 9, + Forward 1, + Down 5, + Forward 9, + Forward 7, + Up 4, + Forward 9, + Forward 7, + Up 4, + Down 1, + Down 3, + Down 3, + Down 1, + Down 9, + Forward 3, + Forward 5, + Forward 2, + Down 1, + Forward 9, + Down 6, + Up 6, + Down 7, + Down 1, + Down 1, + Up 1, + Up 8, + Down 6, + Down 3, + Down 9, + Forward 3, + Forward 9, + Down 4, + Forward 6, + Forward 7, + Down 3, + Forward 5, + Up 8, + Up 6, + Forward 3, + Forward 2, + Down 6, + Up 7, + Forward 3, + Forward 1, + Down 8, + Down 1, + Forward 1, + Forward 2, + Up 7, + Down 3, + Forward 9, + Forward 6, + Up 8, + Forward 3, + Down 3, + Down 9, + Down 3, + Down 6, + Down 6, + Down 2, + Forward 8, + Down 3, + Forward 2, + Up 8, + Forward 5, + Up 6, + Down 7, + Down 4, + Down 2, + Up 9, + Down 4, + Down 9, + Down 4, + Down 9, + Forward 2, + Down 5, + Down 5, + Forward 7, + Up 2, + Forward 5, + Forward 1, + Down 4, + Forward 6, + Forward 9, + Down 1, + Down 2, + Down 6, + Forward 5, + Up 9, + Up 1, + Up 9, + Up 4, + Down 1, + Up 3, + Down 5, + Forward 4, + Forward 3, + Forward 1, + Down 9, + Down 9, + Down 6, + Forward 4, + Forward 4, + Forward 1, + Down 9, + Down 8, + Down 7, + Forward 2, + Forward 7, + Forward 1, + Down 7, + Forward 8, + Forward 7, + Up 4, + Down 4, + Forward 8, + Forward 6, + Down 6, + Forward 9, + Down 2, + Forward 7, + Forward 1, + Down 5, + Down 5, + Down 3, + Down 7, + Forward 9, + Down 2, + Forward 3, + Forward 6, + Up 4, + Up 9, + Forward 6, + Up 8, + Up 2, + Forward 9, + Forward 3, + Forward 9, + Down 4, + Down 8, + Down 1, + Down 1, + Forward 4, + Up 3, + Forward 1, + Forward 5, + Down 5, + Down 6, + Down 7, + Forward 5, + Down 3, + Up 9, + Forward 5, + Down 4, + Up 9, + Forward 1, + Down 3, + Down 5, + Forward 2, + Down 7, + Up 7, + Down 2, + Forward 2, + Down 2, + Down 4, + Down 3, + Forward 6, + Down 7, + Down 2, + Down 1, + Forward 7, + Forward 9, + Forward 8, + Up 8, + Forward 5, + Down 8, + Forward 9, + Forward 9, + Up 5, + Forward 9, + Up 8, + Up 6, + Up 5, + Up 3, + Down 2, + Down 8, + Down 4, + Up 3, + Down 4, + Up 8, + Down 3, + Up 1, + Down 4, + Down 7, + Down 4, + Up 7, + Down 3, + Up 3, + Up 8, + Forward 3, + Down 2, + Forward 7, + Up 9, + Forward 2, + Down 1, + Down 1, + Forward 2, + Forward 9, + Up 6, + Forward 3, + Forward 2, + Down 1, + Down 3, + Down 6, + Forward 7, + Down 4, + Forward 2, + Down 6, + Up 3, + Up 5, + Forward 6, + Up 7, + Down 5, + Up 5, + Forward 5, + Down 6, + Forward 7, + Up 2, + Forward 3, + Forward 4, + Forward 9, + Down 4, + Down 5, + Up 7, + Up 1, + Down 1, + Down 5, + Forward 4, + Down 2, + Down 8, + Down 6, + Down 3, + Down 1, + Down 9, + Forward 5, + Forward 7, + Down 5, + Forward 7, + Down 6, + Up 8, + Up 9, + Down 4, + Forward 7, + Up 1, + Up 5, + Down 8, + Forward 5, + Up 8, + Forward 1, + Forward 7, + Forward 5, + Forward 8, + Forward 8, + Up 1, + Down 4, + Up 6, + Down 1, + Down 4, + Up 9, + Forward 4, + Forward 1, + Forward 5, + Up 7, + Forward 5, + Forward 7, + Up 3, + Up 6, + Up 3, + Forward 4, + Down 5, + Down 4, + Down 2, + Down 4, + Forward 7, + Down 5, + Forward 9, + Forward 2, + Down 3, + Up 7, + Forward 9, + Forward 3, + Down 9, + Up 8, + Forward 1, + Forward 3, + Up 8, + Down 3, + Up 9, + Down 4, + Forward 6, + Forward 8, + Down 4, + Forward 9, + Up 1, + Forward 3, + Forward 1, + Up 3, + Down 3, + Forward 1, + Forward 2, + Forward 8, + Up 3, + Down 9, + Forward 1, + Up 1, + Forward 2, + Down 4, + Forward 7, + Up 9, + Forward 2, + Down 6, + Down 8, + Up 1, + Forward 1, + Forward 7, + Down 8, + Forward 7, + Up 9, + Up 5, + Down 6, + Down 2, + Down 9, + Down 9, + Down 7, + Down 4, + Forward 4, + Up 2, + Up 8, + Forward 5, + Down 9, + Down 7, + Forward 3, + Forward 1, + Down 3, + Down 1, + Forward 5, + Up 2, + Up 9, + Up 2, + Forward 4, + Forward 3, + Forward 8, + Up 9, + Up 6, + Up 3, + Forward 7, + Forward 8, + Forward 8, + Forward 1, + Forward 1, + Forward 2, + Down 2, + Down 7, + Forward 4, + Up 2, + Down 4, + Forward 5, + Down 3, + Forward 1, + Down 3, + Up 2, + Forward 9, + Forward 7, + Forward 5, + Forward 1, + Forward 6, + Forward 9, + Up 3, + Down 8, + Down 8, + Forward 3, + Up 4, + Up 1, + Down 4, + Forward 8, + Up 9, + Down 4, + Down 5, + Forward 1, + Forward 7, + Up 2, + Down 2, + Down 2, + Down 1, + Up 6, + Forward 7, + Forward 7, + Down 7, + Down 8, + Forward 1, + Forward 8, + Down 3, + Up 5, + Up 9, + Down 2, + Down 9, + Forward 3, + Up 8, + Up 9, + Up 3, + Up 3, + Forward 9, + Up 6, + Up 7, + Down 6, + Down 8, + Forward 4, + Down 8, + Forward 4, + Forward 9, + Down 9, + Forward 4, + Up 6, + Up 5, + Down 2, + Forward 6, + Down 2, + Up 2, + Up 4, + Forward 4, + Forward 6, + Down 7, + Up 4, + Down 8, + Forward 1, + Down 5, + Forward 4, + Down 3, + Forward 3, + Down 5, + Forward 7, + Down 8, + Up 7, + Down 7, + Forward 1, + Forward 6, + Up 7, + Up 8, + Up 2, + Forward 1, + Forward 1, + Down 5, + Down 9, + Forward 7, + Forward 5, + Down 7, + Up 1, + Forward 6, + Forward 6, + Down 6, + Forward 1, + Down 1, + Down 2, + Down 4, + Forward 5, + Up 7, + Up 7, + Forward 6, + Down 1, + Forward 7 + ] + +{- +>>> list +[Forward 4,Down 7,Down 4,Forward 2,Down 4,Down 9,Down 1,Forward 1,Down 4,Up 5,Forward 5,Down 3,Forward 5,Forward 6,Down 5,Down 3,Forward 8,Up 7,Forward 1,Forward 5,Forward 7,Up 2,Up 7,Down 1,Down 6,Forward 7,Forward 6,Down 7,Up 9,Down 8,Down 3,Down 1,Down 4,Down 3,Forward 7,Down 1,Forward 6,Down 3,Forward 3,Down 6,Forward 5,Down 7,Up 5,Forward 7,Forward 9,Up 1,Forward 5,Down 5,Down 2,Forward 1,Up 7,Down 8,Down 2,Forward 8,Forward 1,Up 1,Up 5,Forward 9,Up 9,Down 5,Down 5,Forward 6,Forward 1,Down 1,Down 3,Up 6,Up 9,Down 6,Down 1,Forward 1,Down 8,Down 3,Down 8,Up 6,Forward 2,Up 8,Down 2,Down 6,Forward 1,Down 2,Down 1,Down 5,Down 8,Forward 8,Down 8,Down 2,Up 1,Up 3,Forward 1,Down 6,Up 5,Forward 5,Up 7,Forward 3,Forward 3,Down 9,Forward 1,Forward 7,Down 1,Down 1,Down 3,Forward 4,Down 4,Forward 2,Up 8,Forward 2,Down 4,Down 2,Up 1,Forward 6,Forward 6,Down 9,Forward 5,Down 3,Forward 5,Down 6,Down 9,Forward 9,Up 6,Down 8,Up 5,Up 2,Forward 5,Down 4,Forward 8,Up 1,Forward 2,Down 9,Down 4,Up 7,Down 1,Down 6,Down 7,Down 7,Down 2,Down 2,Up 7,Down 1,Up 7,Up 4,Forward 9,Down 6,Up 3,Down 5,Forward 8,Up 4,Forward 5,Forward 8,Forward 4,Forward 7,Forward 2,Down 3,Up 4,Forward 7,Forward 3,Down 7,Up 4,Forward 8,Forward 9,Forward 9,Up 6,Up 5,Forward 7,Down 7,Up 5,Down 9,Down 8,Forward 5,Forward 2,Down 1,Down 2,Down 9,Up 5,Down 3,Down 8,Up 6,Forward 9,Down 9,Up 3,Up 9,Forward 3,Forward 9,Down 5,Up 3,Down 6,Forward 5,Down 6,Down 9,Forward 3,Down 7,Down 5,Down 9,Forward 8,Down 8,Forward 8,Forward 2,Up 6,Down 9,Down 7,Down 4,Down 2,Down 9,Forward 7,Up 1,Forward 6,Forward 7,Down 3,Down 5,Down 3,Forward 4,Down 9,Forward 2,Down 1,Forward 4,Up 9,Down 8,Down 9,Forward 5,Up 2,Down 7,Down 2,Down 7,Forward 8,Up 8,Forward 9,Up 8,Down 3,Down 8,Forward 2,Up 7,Forward 4,Up 8,Forward 6,Forward 6,Forward 4,Forward 3,Down 4,Up 7,Down 8,Forward 1,Forward 9,Forward 8,Forward 8,Down 6,Down 6,Down 4,Down 8,Forward 9,Down 8,Down 4,Forward 2,Forward 7,Down 4,Up 1,Down 4,Down 5,Up 5,Up 9,Down 5,Up 5,Forward 2,Down 8,Forward 7,Forward 2,Up 4,Forward 8,Up 7,Down 5,Forward 5,Down 5,Down 6,Down 7,Forward 1,Down 8,Up 8,Up 9,Up 6,Forward 2,Forward 7,Forward 4,Down 6,Up 4,Up 3,Down 3,Forward 7,Up 9,Down 9,Up 5,Forward 1,Up 7,Down 7,Down 7,Up 8,Down 3,Forward 6,Down 6,Down 7,Down 2,Forward 5,Up 8,Up 5,Up 1,Down 5,Forward 9,Forward 9,Forward 2,Forward 6,Up 8,Forward 8,Down 1,Forward 6,Up 7,Up 3,Forward 3,Up 5,Down 8,Forward 2,Down 5,Down 5,Forward 8,Forward 8,Forward 6,Up 7,Up 1,Forward 1,Down 7,Down 1,Up 9,Forward 7,Up 6,Down 2,Down 5,Forward 1,Down 1,Up 1,Forward 7,Forward 7,Forward 1,Up 2,Forward 2,Down 9,Down 8,Forward 9,Forward 8,Up 9,Down 1,Down 3,Up 1,Down 3,Up 9,Down 7,Up 6,Down 1,Down 2,Down 2,Down 4,Forward 4,Forward 3,Down 7,Forward 9,Forward 1,Down 2,Down 2,Down 4,Down 7,Up 7,Forward 6,Forward 2,Down 6,Down 3,Down 3,Down 8,Up 8,Forward 6,Down 8,Down 3,Forward 7,Down 4,Down 4,Up 1,Up 8,Forward 5,Forward 6,Down 8,Down 9,Up 7,Forward 2,Forward 4,Down 4,Forward 6,Forward 5,Down 4,Up 5,Down 5,Forward 1,Down 2,Up 9,Forward 9,Forward 6,Forward 8,Down 2,Down 9,Up 1,Forward 6,Down 3,Forward 6,Forward 7,Down 9,Forward 1,Forward 1,Forward 5,Up 5,Up 6,Forward 8,Forward 6,Forward 6,Forward 8,Down 6,Down 8,Forward 5,Forward 7,Forward 3,Forward 3,Up 5,Forward 8,Up 3,Forward 6,Forward 4,Forward 3,Up 8,Down 1,Down 3,Down 7,Up 8,Forward 5,Down 9,Up 7,Down 2,Down 4,Down 5,Down 2,Up 3,Down 4,Down 4,Forward 4,Forward 6,Up 4,Forward 5,Forward 3,Up 2,Up 3,Down 4,Down 7,Forward 7,Forward 2,Down 2,Forward 5,Up 4,Forward 2,Down 8,Up 5,Up 6,Forward 2,Forward 3,Up 3,Down 2,Forward 7,Down 6,Forward 3,Forward 6,Up 9,Forward 7,Up 1,Forward 1,Forward 2,Down 3,Forward 9,Down 1,Up 5,Up 7,Up 8,Down 5,Up 4,Down 9,Forward 4,Up 8,Forward 3,Forward 7,Down 7,Down 9,Down 5,Up 4,Down 2,Up 5,Down 4,Forward 3,Down 7,Up 5,Forward 9,Forward 9,Forward 7,Up 5,Forward 7,Up 3,Forward 8,Down 1,Down 5,Up 5,Forward 4,Forward 9,Forward 9,Up 5,Down 9,Up 7,Up 3,Forward 5,Down 4,Down 7,Forward 9,Down 9,Up 3,Forward 4,Forward 7,Down 9,Forward 2,Down 2,Forward 1,Down 2,Up 7,Down 6,Forward 9,Forward 1,Down 5,Forward 9,Forward 7,Up 4,Forward 9,Forward 7,Up 4,Down 1,Down 3,Down 3,Down 1,Down 9,Forward 3,Forward 5,Forward 2,Down 1,Forward 9,Down 6,Up 6,Down 7,Down 1,Down 1,Up 1,Up 8,Down 6,Down 3,Down 9,Forward 3,Forward 9,Down 4,Forward 6,Forward 7,Down 3,Forward 5,Up 8,Up 6,Forward 3,Forward 2,Down 6,Up 7,Forward 3,Forward 1,Down 8,Down 1,Forward 1,Forward 2,Up 7,Down 3,Forward 9,Forward 6,Up 8,Forward 3,Down 3,Down 9,Down 3,Down 6,Down 6,Down 2,Forward 8,Down 3,Forward 2,Up 8,Forward 5,Up 6,Down 7,Down 4,Down 2,Up 9,Down 4,Down 9,Down 4,Down 9,Forward 2,Down 5,Down 5,Forward 7,Up 2,Forward 5,Forward 1,Down 4,Forward 6,Forward 9,Down 1,Down 2,Down 6,Forward 5,Up 9,Up 1,Up 9,Up 4,Down 1,Up 3,Down 5,Forward 4,Forward 3,Forward 1,Down 9,Down 9,Down 6,Forward 4,Forward 4,Forward 1,Down 9,Down 8,Down 7,Forward 2,Forward 7,Forward 1,Down 7,Forward 8,Forward 7,Up 4,Down 4,Forward 8,Forward 6,Down 6,Forward 9,Down 2,Forward 7,Forward 1,Down 5,Down 5,Down 3,Down 7,Forward 9,Down 2,Forward 3,Forward 6,Up 4,Up 9,Forward 6,Up 8,Up 2,Forward 9,Forward 3,Forward 9,Down 4,Down 8,Down 1,Down 1,Forward 4,Up 3,Forward 1,Forward 5,Down 5,Down 6,Down 7,Forward 5,Down 3,Up 9,Forward 5,Down 4,Up 9,Forward 1,Down 3,Down 5,Forward 2,Down 7,Up 7,Down 2,Forward 2,Down 2,Down 4,Down 3,Forward 6,Down 7,Down 2,Down 1,Forward 7,Forward 9,Forward 8,Up 8,Forward 5,Down 8,Forward 9,Forward 9,Up 5,Forward 9,Up 8,Up 6,Up 5,Up 3,Down 2,Down 8,Down 4,Up 3,Down 4,Up 8,Down 3,Up 1,Down 4,Down 7,Down 4,Up 7,Down 3,Up 3,Up 8,Forward 3,Down 2,Forward 7,Up 9,Forward 2,Down 1,Down 1,Forward 2,Forward 9,Up 6,Forward 3,Forward 2,Down 1,Down 3,Down 6,Forward 7,Down 4,Forward 2,Down 6,Up 3,Up 5,Forward 6,Up 7,Down 5,Up 5,Forward 5,Down 6,Forward 7,Up 2,Forward 3,Forward 4,Forward 9,Down 4,Down 5,Up 7,Up 1,Down 1,Down 5,Forward 4,Down 2,Down 8,Down 6,Down 3,Down 1,Down 9,Forward 5,Forward 7,Down 5,Forward 7,Down 6,Up 8,Up 9,Down 4,Forward 7,Up 1,Up 5,Down 8,Forward 5,Up 8,Forward 1,Forward 7,Forward 5,Forward 8,Forward 8,Up 1,Down 4,Up 6,Down 1,Down 4,Up 9,Forward 4,Forward 1,Forward 5,Up 7,Forward 5,Forward 7,Up 3,Up 6,Up 3,Forward 4,Down 5,Down 4,Down 2,Down 4,Forward 7,Down 5,Forward 9,Forward 2,Down 3,Up 7,Forward 9,Forward 3,Down 9,Up 8,Forward 1,Forward 3,Up 8,Down 3,Up 9,Down 4,Forward 6,Forward 8,Down 4,Forward 9,Up 1,Forward 3,Forward 1,Up 3,Down 3,Forward 1,Forward 2,Forward 8,Up 3,Down 9,Forward 1,Up 1,Forward 2,Down 4,Forward 7,Up 9,Forward 2,Down 6,Down 8,Up 1,Forward 1,Forward 7,Down 8,Forward 7,Up 9,Up 5,Down 6,Down 2,Down 9,Down 9,Down 7,Down 4,Forward 4,Up 2,Up 8,Forward 5,Down 9,Down 7,Forward 3,Forward 1,Down 3,Down 1,Forward 5,Up 2,Up 9,Up 2,Forward 4,Forward 3,Forward 8,Up 9,Up 6,Up 3,Forward 7,Forward 8,Forward 8,Forward 1,Forward 1,Forward 2,Down 2,Down 7,Forward 4,Up 2,Down 4,Forward 5,Down 3,Forward 1,Down 3,Up 2,Forward 9,Forward 7,Forward 5,Forward 1,Forward 6,Forward 9,Up 3,Down 8,Down 8,Forward 3,Up 4,Up 1,Down 4,Forward 8,Up 9,Down 4,Down 5,Forward 1,Forward 7,Up 2,Down 2,Down 2,Down 1,Up 6,Forward 7,Forward 7,Down 7,Down 8,Forward 1,Forward 8,Down 3,Up 5,Up 9,Down 2,Down 9,Forward 3,Up 8,Up 9,Up 3,Up 3,Forward 9,Up 6,Up 7,Down 6,Down 8,Forward 4,Down 8,Forward 4,Forward 9,Down 9,Forward 4,Up 6,Up 5,Down 2,Forward 6,Down 2,Up 2,Up 4,Forward 4,Forward 6,Down 7,Up 4,Down 8,Forward 1,Down 5,Forward 4,Down 3,Forward 3,Down 5,Forward 7,Down 8,Up 7,Down 7,Forward 1,Forward 6,Up 7,Up 8,Up 2,Forward 1,Forward 1,Down 5,Down 9,Forward 7,Forward 5,Down 7,Up 1,Forward 6,Forward 6,Down 6,Forward 1,Down 1,Down 2,Down 4,Forward 5,Up 7,Up 7,Forward 6,Down 1,Forward 7] + +>>> uncurry (*) $ foldl command (0, 0) list +1250395 + +>>> uncurry (*) $ dropLast $ foldl command2 (0, 0, 0) list +1451210346 + + +-} From 1bd30b2495733d213c0d12cd042b14c6609ab719 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Fri, 3 Dec 2021 09:04:13 +0100 Subject: [PATCH 02/32] Day 3 --- day3.hs | 1082 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1082 insertions(+) create mode 100644 day3.hs diff --git a/day3.hs b/day3.hs new file mode 100644 index 0000000..9fa9fbc --- /dev/null +++ b/day3.hs @@ -0,0 +1,1082 @@ +import Data.Bits + +input = [ + "100100110110", + "101110110110", + "010100010100", + "011001110000", + "000000000111", + "000010110001", + "001111000001", + "100010000001", + "010100110011", + "010000010110", + "010000000011", + "010101001000", + "011011101100", + "011100110111", + "011011000000", + "001010010010", + "011011111010", + "011110110110", + "000000100010", + "111111010101", + "011100010011", + "001110110100", + "110110010100", + "100000001011", + "100100010011", + "011011010001", + "111000101110", + "101100001111", + "110101010101", + "011010001001", + "101111011000", + "011111111011", + "110000100101", + "011101101101", + "011010011001", + "111100100001", + "100000010000", + "111010001010", + "111110010001", + "110110001101", + "000001000001", + "000101011000", + "111101100100", + "110011001001", + "100010100011", + "011011100010", + "011000101101", + "110101101011", + "111110101000", + "010111110100", + "010010101011", + "100001001000", + "101011101011", + "110111000001", + "011100011110", + "011001101101", + "010100001111", + "001111011000", + "100111111010", + "110110000100", + "000010101100", + "111010111011", + "110110110110", + "110111011000", + "100001011001", + "110010011111", + "100000000101", + "110000101100", + "110100010110", + "101001101100", + "111000100011", + "011001001101", + "000111100111", + "001000111110", + "110100100010", + "000110011100", + "010011101111", + "010101000111", + "101100100001", + "100110000010", + "001110100101", + "010100100100", + "001110010110", + "000010111010", + "101011101001", + "101111101010", + "101011100110", + "011001111010", + "100110101010", + "101111101110", + "100000100011", + "000101001011", + "000110110000", + "110111101111", + "111110010110", + "100010010111", + "011100000101", + "101010011010", + "010111110010", + "000011111101", + "000001101000", + "000111001001", + "101000001011", + "011001101000", + "010110101110", + "000111101000", + "101100100011", + "011010010011", + "110101111110", + "101011110001", + "111000011101", + "111110011100", + "100110110001", + "110111110011", + "000000011010", + "000100001110", + "011100001111", + "010110010111", + "011000100110", + "011000111000", + "010001000111", + "100010101110", + "011001111011", + "010111100001", + "011101101110", + "101100010110", + "100010101011", + "110101000010", + "111010001101", + "111001100111", + "101001110111", + "011000110010", + "011001111100", + "010001000110", + "011011100101", + "101111010001", + "100110001011", + "001011000010", + "010100011111", + "101111100110", + "011100000010", + "011001000010", + "010011011111", + "001101000010", + "110110010110", + "001000101001", + "101100011000", + "000111000110", + "000001001101", + "000100100111", + "110111011100", + "010100101001", + "010101110000", + "110000001110", + "010011010000", + "011101110000", + "001111100000", + "010011000011", + "010011010011", + "101110010000", + "101100000110", + "110011101101", + "010011111101", + "101011010110", + "001011011100", + "111111011001", + "011011111000", + "011101110011", + "010011001101", + "100011001000", + "011010111011", + "111101000110", + "100110001101", + "101001101011", + "100000111100", + "111101001011", + "010111010111", + "101110000000", + "010010111111", + "100010110010", + "101111110100", + "111101110111", + "111111110010", + "110110001100", + "101010101111", + "000110101000", + "100110110100", + "100101011010", + "001001011000", + "111010001011", + "000000110100", + "111000010010", + "101010011101", + "010110000010", + "011111110111", + "101011111101", + "011010100111", + "100011011101", + "110110011110", + "101111100111", + "001100001111", + "011111101011", + "100101000100", + "100101010010", + "011110111010", + "010110001001", + "010101101010", + "001111000101", + "010001001100", + "001010000010", + "010000001101", + "110000100110", + "111100001101", + "101010001001", + "110011111111", + "010111011001", + "000111101111", + "111110000011", + "011111000011", + "011110011011", + "100011010100", + "000011000110", + "011100101000", + "010101001111", + "010110010110", + "111111000111", + "101011001001", + "011100010111", + "000000001100", + "011001001000", + "110101101111", + "101101011001", + "000111011000", + "000010001111", + "111110011011", + "010111011111", + "011101110100", + "001011100001", + "001001011100", + "110100111011", + "100101011111", + "000111001101", + "011110100101", + "111100011010", + "111000110100", + "010001010101", + "100001000100", + "000000101110", + "011011001110", + "111010100111", + "100110110000", + "110110110001", + "001110001001", + "110010111111", + "000111001011", + "011001011111", + "000010001011", + "100010101001", + "100101100101", + "010110011001", + "101101011110", + "101011100101", + "101110110101", + "111001100001", + "001000101010", + "101011010011", + "110011101100", + "101100001101", + "101111101001", + "111100000010", + "100111000000", + "010010000100", + "001001001001", + "000110111110", + "011010001110", + "100011110000", + "110111101000", + "110000010101", + "011011101011", + "101000010001", + "110110110010", + "100000000001", + "011011010101", + "000111111101", + "100001111011", + "100100000110", + "001111100010", + "110010000011", + "101101111001", + "110011010110", + "010001011011", + "010010011010", + "001110011110", + "100111010010", + "110011011010", + "111100010011", + "111011011101", + "100001010110", + "101011001111", + "001001000000", + "101011100010", + "010000110011", + "100111110000", + "010010101110", + "110000110011", + "110100010111", + "001011110010", + "101011000111", + "000101110011", + "010001111011", + "000010011011", + "011000001011", + "000000100100", + "001101111001", + "011000000101", + "100100001101", + "101001000011", + "111100100111", + "111000111111", + "011000110011", + "101011011110", + "101110000100", + "010101000001", + "110111111100", + "100000011011", + "101001011010", + "111011000110", + "110111101011", + "011000101100", + "100001111111", + "111011100101", + "110111110101", + "101011111010", + "010001101100", + "111000111001", + "110001000011", + "100000110100", + "010110100010", + "100011011111", + "001111111001", + "101101011011", + "011101011000", + "100000111000", + "101000000110", + "010110011101", + "110010101101", + "011110000010", + "100011011010", + "110010110101", + "110111100011", + "110100110000", + "101010011100", + "001010010110", + "100110011110", + "101100100010", + "110101111001", + "111110000000", + "011000111111", + "100100100000", + "111111101111", + "001011110101", + "101110000001", + "101111011110", + "011010111001", + "011111010111", + "000111000101", + "000100010001", + "111100100101", + "100110000100", + "001000001111", + "001001100100", + "001000000110", + "110010100100", + "111110110101", + "010010111001", + "110111000110", + "011101000000", + "100001101100", + "111101000111", + "000101111111", + "001111010100", + "101001100100", + "111111110001", + "010010010001", + "111011101010", + "001110101000", + "111101100001", + "100001000001", + "111001111010", + "100101000011", + "000001110011", + "000111100001", + "110010011110", + "111111101011", + "000101001001", + "000101111101", + "011101011001", + "100100000111", + "110000001011", + "001111100110", + "110010001101", + "011101011011", + "011100111111", + "000001010000", + "011001010101", + "001111001010", + "010111000000", + "001101101101", + "011111000111", + "000111011101", + "100100111100", + "011101011111", + "111000000111", + "100111101101", + "001001111001", + "010001100101", + "101111100001", + "000010101101", + "010001011111", + "000111101010", + "011101111101", + "111111110101", + "011111001101", + "110101001100", + "011110001110", + "001001010000", + "010001010001", + "011000010010", + "100010000100", + "100100100001", + "011101101111", + "111000101111", + "001101110110", + "110011110000", + "000111011111", + "011100110010", + "001101101110", + "101010100010", + "111110110010", + "111101000100", + "001010101000", + "001100101001", + "111010101000", + "010110000100", + "110111001000", + "111100000110", + "001110001011", + "011111111100", + "110011100000", + "001010011100", + "110011001000", + "001001110101", + "001010110110", + "110010100101", + "100000111111", + "110010000100", + "110110100100", + "001011101101", + "000110000010", + "001100000111", + "010011100110", + "001010101011", + "111001101000", + "110111010010", + "110111001100", + "010000111100", + "010111100110", + "001111111111", + "011010111010", + "100111011100", + "011011011010", + "001000001110", + "111010100101", + "100001011000", + "011001110011", + "100111011000", + "111111101000", + "010111011010", + "111011111000", + "001010101110", + "000010010111", + "110011000010", + "100000111010", + "110000001111", + "011110111101", + "101011011001", + "011110011111", + "100001111010", + "111001010110", + "100001010000", + "011000100000", + "111001011000", + "000100110011", + "010110111101", + "010111000100", + "100111011101", + "100000011010", + "010110111001", + "010101001110", + "110001001001", + "111100010001", + "111010110011", + "100100010100", + "000100100000", + "100100110010", + "111011100000", + "111010110010", + "110110100110", + "001001010001", + "010110100111", + "111001101100", + "000111111001", + "011100000110", + "011011010010", + "001010111000", + "001100000010", + "100001110001", + "011101110111", + "010101010100", + "111100111001", + "010000010101", + "011110011110", + "000011110100", + "000111100100", + "100100011000", + "101000011011", + "111010000100", + "000001000111", + "111111011111", + "000111010101", + "000111100101", + "000000001101", + "000011100001", + "101000100111", + "100111010001", + "001110101010", + "101101110001", + "101000111001", + "001101100011", + "101110000111", + "100111110010", + "010001111111", + "100111000010", + "100111101111", + "111011110110", + "100111000100", + "000011111110", + "001010000101", + "111011111110", + "010010101111", + "010011001110", + "110100000001", + "110101110001", + "000010001101", + "000110001010", + "101111110010", + "111111001000", + "001001100001", + "110001110100", + "110111111011", + "000010000101", + "111100111000", + "010000000010", + "001110111001", + "111000110111", + "000010000001", + "111111010010", + "010010001010", + "001110010111", + "000111111110", + "100101000001", + "000001110111", + "101000010000", + "100011011001", + "010000101101", + "011101000110", + "110011001011", + "110100011010", + "100111000101", + "011000000100", + "110010101110", + "001000101110", + "100010111111", + "100011111011", + "001101010001", + "111000100100", + "100101100100", + "001000100001", + "000011000101", + "001000110001", + "110110011011", + "100110010111", + "110000011101", + "000010000010", + "011111001111", + "000100111000", + "100001011011", + "110010000000", + "000100010100", + "011000101001", + "100111011011", + "110000101101", + "111000110011", + "101100010111", + "101001100110", + "110011111011", + "100110100110", + "100111111111", + "100100101010", + "001000011111", + "000110101101", + "110111100001", + "100111000110", + "100111110110", + "111111100000", + "100010001011", + "100000000010", + "110001001000", + "101101001110", + "111101110011", + "010111001111", + "011111010100", + "110111000101", + "010010001110", + "010111100010", + "111110010011", + "001100011011", + "100000110111", + "010111110110", + "001100011101", + "010010010111", + "110011001101", + "000111101110", + "011000001111", + "010100000000", + "110001101001", + "101101010111", + "110111010100", + "101010010111", + "001110111110", + "101001000111", + "011010010110", + "010000101100", + "110000111111", + "011101010000", + "010110001101", + "010101000101", + "101101001000", + "100010110001", + "101000110101", + "111101010101", + "010011111100", + "100010100110", + "011001111001", + "000110001001", + "101100000101", + "000101011010", + "100100011110", + "100000110011", + "100011000101", + "110001011010", + "111100000111", + "001100110111", + "011010110000", + "011000000111", + "111101101011", + "011101110101", + "010100011110", + "010101100011", + "000100010000", + "010111001011", + "101110100001", + "110110110101", + "000010100111", + "001011000110", + "110110101100", + "010001100010", + "011110110111", + "110100001100", + "000110001100", + "001101001100", + "101010111011", + "000001101100", + "011110101101", + "110100000000", + "011011100111", + "000010101001", + "100111110101", + "000101000101", + "010011010111", + "111101010100", + "110110101101", + "110011011100", + "110011011001", + "000111010111", + "110100011110", + "000110011010", + "110110100000", + "111110101101", + "001100111000", + "101111000110", + "011011000010", + "000000001001", + "010100111110", + "100010000110", + "001110111101", + "010001011100", + "101001111101", + "110101110000", + "001111101111", + "100101111101", + "110100111101", + "000100001000", + "111011110101", + "001011001000", + "011101010111", + "101000000101", + "011111111001", + "111001011011", + "010001110001", + "010001110011", + "001100111001", + "100101110010", + "100010100010", + "110011111001", + "111011101011", + "001101110011", + "000011011010", + "101000010111", + "000011010001", + "101000010010", + "000011101111", + "011100001100", + "110110011001", + "011101000001", + "010011011001", + "111010010011", + "001011000111", + "010111100101", + "101001111011", + "101111011011", + "011011001100", + "010100110000", + "000111100011", + "010111111011", + "100101101010", + "010001100000", + "111110100011", + "100101011101", + "011011011011", + "111101011001", + "001000011101", + "101111110011", + "100100100110", + "010111100111", + "110000001101", + "000001010011", + "100001010100", + "101101000100", + "110100101101", + "110001100011", + "110000111010", + "101111011010", + "011011100000", + "100101100110", + "010110010001", + "101000011000", + "011001110100", + "001110001100", + "100100110100", + "001110101011", + "000110000100", + "101000110001", + "101101110010", + "011100001110", + "111011010011", + "011001110001", + "110101100000", + "110000010011", + "010111101111", + "101111101111", + "010110100000", + "111111011011", + "100110000110", + "011000111100", + "110010111110", + "001000001101", + "011101011010", + "001010110101", + "100110001100", + "101000000011", + "110001010011", + "100001011111", + "000001101001", + "100001101101", + "011100100000", + "011011000110", + "001110111011", + "100111100101", + "001110000000", + "110101001101", + "101100101100", + "001110000001", + "110000011010", + "101100110010", + "011010100010", + "111101111010", + "110001111100", + "011110101000", + "110011011000", + "110100000111", + "100000000111", + "101100101110", + "011001000011", + "011011100110", + "101001000001", + "001100100101", + "011011110011", + "110110001011", + "000101011110", + "010011110101", + "110101001010", + "111110101010", + "110111010001", + "010111001101", + "001101011000", + "011001011011", + "011101000010", + "101010111110", + "010010000101", + "100100010010", + "110010000010", + "000000001110", + "100110011100", + "000110100111", + "000000011011", + "111011010000", + "100001111000", + "000010010011", + "110010000101", + "011001101111", + "010010000110", + "101000110100", + "010111010110", + "011010110010", + "001101110001", + "001101000100", + "001001101011", + "010111110101", + "100100110101", + "000101011011", + "110110000111", + "000100110001", + "111111110011", + "111010110000", + "000000110001", + "010011101001", + "111110011101", + "101011100100", + "001010100101", + "010111111100", + "010101100100", + "011010000000", + "010111111111", + "111001100011", + "111001011110", + "010011111010", + "001010101100", + "110001100101", + "011000011110", + "001111010010", + "010110001110", + "011101011110", + "101100011010", + "000000001000", + "000000000000", + "100000011000", + "101000001111", + "101101010010", + "001010110010", + "101111001001", + "111000000001", + "010011001010", + "110000000101", + "101101101000", + "110011110111", + "111101010110", + "111011011011", + "100011101100", + "101100011011", + "000011010110", + "111100111101", + "110000100100", + "001111110111", + "011110011101", + "010001010110", + "101000001110", + "101010101110", + "010110000011", + "100001100000", + "000001000011", + "110011100010", + "000101000110", + "010010110110", + "100111100011", + "000110100100", + "110100101110", + "001101100101", + "110000100000", + "110001100110", + "110010000001", + "010010011011", + "101111001010", + "101010010110", + "001001001100", + "011001110010", + "111010100100", + "000001000100", + "010101101001", + "101001100011", + "010010100100", + "010111001010", + "101000000100", + "011001010100", + "101110000110", + "011010000111", + "101001110001", + "111000011110", + "010011000111", + "111000100111", + "010101010011", + "010010101010", + "011110011001", + "110010011101", + "110100001001", + "011111001001", + "011110100011", + "100100001111", + "000010100101", + "110010010111", + "111111000101", + "000001010100", + "001000011110", + "011101001110", + "111001100101", + "111110011110", + "000110111100", + "110001001110", + "011001010111", + "101100011101", + "010000001001", + "100100011101", + "101001101000", + "100011110101", + "100110101101", + "000001111001", + "011110010001", + "001001011001", + "110111100111", + "100000010111", + "011110010010", + "000011110000", + "111011110111", + "101110110010", + "000111010100", + "010010101001", + "000011010010", + "101010001011", + "111100101100", + "111101101111", + "101001011111", + "101110001010", + "010101011110", + "101001001101", + "100010111100", + "011110111001", + "110100111110", + "001101000011", + "000111100010", + "100111110011", + "010111011011", + "101010111100", + "100111111110", + "001111001011", + "000101110001", + "001000001010", + "010001101101", + "101101001101", + "111100010000", + "011110000110", + "100011101110", + "111100001110", + "101000100010", + "101010100101", + "110011010111", + "110010101010", + "110110111111", + "010010110011", + "000111110010", + "111100110011" + ] + +example = [ + "00100", + "11110", + "10110", + "10111", + "10101", + "01111", + "00111", + "11100", + "10000", + "11001", + "00010", + "01010" + ] + +count :: Int -> (Int, Int) -> [Char] -> (Int, Int) +count pos (zero, one) num = case num !! pos of + '0' -> (zero + 1, one) + '1' -> (zero, one + 1) + _ -> (zero, one) +gammaBit (zero, one) + | zero > one = False + | otherwise = True + + +bitsAt input pos = bits $ foldl (count pos) (0, 0) input + where bits a = (gammaBit a, not $ gammaBit a) + + +toNumber :: Foldable t => t Bool -> Int +toNumber = foldl addBit 0 + where addBit a b = a `shiftL` 1 + (if b then 1 else 0) + +{- + +>>> import Data.Bits +>>> 1 `shiftL` 1 +2 + +>>> (toNumber $ snd <$> (bitsAt input) <$> [0..11]) * (toNumber $ fst <$> (bitsAt input) <$> [0..11]) +2003336 + +>>> toNumber [False, True, True, False] +6 + +-} + + +filterUntilLast pred list = filterUntilLastAt 0 pred list + where filterUntilLastAt pos pred list + | [item] <- list = item + | otherwise = filterUntilLastAt (pos + 1) pred (filter (pred pos list) list) + + +oxyFilter pos list item = + item !! pos == keep + where + (zero, one) = foldl (count pos) (0, 0) list + keep = if zero > one then '0' else '1' + +co2Filter pos list item = + item !! pos == keep + where + (zero, one) = foldl (count pos) (0, 0) list + keep = if zero > one then '1' else '0' + + +{- + +>>> toNumber [x == '1' | x <- filterUntilLast co2Filter input] +737 +>>> toNumber [x == '1' | x <- filterUntilLast oxyFilter input] +2547 + +>>> 737 * 2547 +1877139 +-} From 609cf69d407c41a17fe920f8fed4686c71e82fe7 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Fri, 3 Dec 2021 22:06:32 +0100 Subject: [PATCH 03/32] Improvements --- day3.hs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/day3.hs b/day3.hs index 9fa9fbc..2dfa0fb 100644 --- a/day3.hs +++ b/day3.hs @@ -1034,20 +1034,18 @@ bitsAt input pos = bits $ foldl (count pos) (0, 0) input toNumber :: Foldable t => t Bool -> Int toNumber = foldl addBit 0 - where addBit a b = a `shiftL` 1 + (if b then 1 else 0) + where addBit a b = a `shiftL` 1 .|. (if b then 1 else 0) + +combineList [] = ([], []) +combineList ((a, b) : rest) = (a : arest, b: brest) + where (arest, brest) = combineList rest {- ->>> import Data.Bits ->>> 1 `shiftL` 1 -2 - ->>> (toNumber $ snd <$> (bitsAt input) <$> [0..11]) * (toNumber $ fst <$> (bitsAt input) <$> [0..11]) +>>> (a, b) = ( combineList $ (bitsAt input) <$> [0..(length $ head input) - 1]) +>>> toNumber a * toNumber b 2003336 ->>> toNumber [False, True, True, False] -6 - -} @@ -1057,23 +1055,23 @@ filterUntilLast pred list = filterUntilLastAt 0 pred list | otherwise = filterUntilLastAt (pos + 1) pred (filter (pred pos list) list) -oxyFilter pos list item = +oxyFilter :: Int -> [[Char]] -> [Char] -> Bool +oxyFilter = makeFilter (<=) + +co2Filter :: Int -> [[Char]] -> [Char] -> Bool +co2Filter = makeFilter (>) + +makeFilter predicate pos list item = item !! pos == keep - where + where (zero, one) = foldl (count pos) (0, 0) list - keep = if zero > one then '0' else '1' + keep = if predicate zero one then '1' else '0' -co2Filter pos list item = - item !! pos == keep - where - (zero, one) = foldl (count pos) (0, 0) list - keep = if zero > one then '1' else '0' - - -{- +{- | >>> toNumber [x == '1' | x <- filterUntilLast co2Filter input] 737 + >>> toNumber [x == '1' | x <- filterUntilLast oxyFilter input] 2547 From 8798f1b1e4100c9b53957c6062d4eae285bae2c3 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Fri, 3 Dec 2021 22:18:59 +0100 Subject: [PATCH 04/32] Changes --- day3.hs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/day3.hs b/day3.hs index 2dfa0fb..fb7ef41 100644 --- a/day3.hs +++ b/day3.hs @@ -1023,13 +1023,11 @@ count pos (zero, one) num = case num !! pos of '0' -> (zero + 1, one) '1' -> (zero, one + 1) _ -> (zero, one) -gammaBit (zero, one) - | zero > one = False - | otherwise = True +bitsAt :: Foldable t => t [Char] -> Int -> (Bool, Bool) bitsAt input pos = bits $ foldl (count pos) (0, 0) input - where bits a = (gammaBit a, not $ gammaBit a) + where bits (zero, one) = (zero <= one, zero > one) toNumber :: Foldable t => t Bool -> Int From 8a50c9ab9dfab085a664ca423bf5e2885f08036c Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 4 Dec 2021 23:24:18 +0100 Subject: [PATCH 05/32] Day 4 --- day4.hs | 788 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 788 insertions(+) create mode 100644 day4.hs diff --git a/day4.hs b/day4.hs new file mode 100644 index 0000000..ae34442 --- /dev/null +++ b/day4.hs @@ -0,0 +1,788 @@ +import Data.Maybe ( catMaybes ) +import Data.List ( find ) + +randoms :: [Int] +randoms = [13,47,64,52,60,69,80,85,57,1,2,6,30,81,86,40,27,26,97,77,70,92,43,94,8,78,3,88,93,17,55,49,32,59,51,28,33,41,83,67,11,91,53,36,96,7,34,79,98,72,39,56,31,75,82,62,99,66,29,58,9,50,54,12,45,68,4,46,38,21,24,18,44,48,16,61,19,0,90,35,65,37,73,20,22,89,42,23,15,87,74,10,71,25,14,76,84,5,63,95] + +update 0 (_:rest) newValue = newValue : rest +update n (first:rest) newValue = first : update (n - 1) rest newValue +update _ [] _ = [] + +updateMatrix :: (Int, Int) -> [[t]] -> t -> [[t]] +updateMatrix (x, y) matrix newValue = update y matrix $ update x (matrix !! y) newValue + + +newtype Board = Board [[Maybe Int]] deriving Show + +board :: [[Int]] -> Board +board list = Board $ (map $ map Just) list + +boards :: [Board] +boards = [ + board [ + [88, 67, 20, 19, 15], + [22, 76, 86, 44, 73], + [ 7, 42, 6, 69, 25], + [12, 68, 92, 21, 75], + [97, 45, 13, 52, 70] + ], + board [ + [75, 98, 24, 18, 77], + [17, 93, 46, 49, 13], + [92, 56, 97, 57, 66], + [44, 0, 65, 54, 74], + [23, 6, 53, 42, 20] + ], + board [ + [92, 94, 9, 27, 41], + [73, 28, 62, 90, 40], + [78, 3, 12, 37, 32], + [ 8, 86, 91, 16, 30], + [84, 38, 68, 11, 19] + ], + board [ + [51, 5, 12, 76, 97], + [72, 31, 15, 61, 71], + [38, 32, 55, 87, 10], + [91, 4, 85, 84, 53], + [59, 79, 28, 69, 23] + ], + board [ + [35, 48, 10, 81, 60], + [25, 86, 24, 43, 15], + [44, 55, 12, 54, 62], + [94, 89, 95, 2, 23], + [64, 63, 45, 50, 66] + ], + board [ + [80, 87, 49, 88, 39], + [33, 81, 95, 68, 55], + [83, 46, 36, 41, 54], + [90, 74, 3, 52, 7], + [71, 40, 35, 8, 77] + ], + board [ + [34, 21, 24, 8, 97], + [99, 23, 94, 70, 9], + [14, 98, 2, 11, 10], + [16, 38, 92, 13, 35], + [82, 25, 76, 42, 39] + ], + board [ + [52, 76, 98, 25, 3], + [24, 41, 13, 39, 56], + [11, 72, 77, 47, 86], + [50, 32, 26, 88, 48], + [18, 99, 22, 78, 58] + ], + board [ + [22, 24, 53, 84, 80], + [26, 97, 42, 95, 11], + [ 1, 59, 81, 7, 35], + [47, 14, 40, 63, 30], + [37, 31, 10, 50, 43] + ], + board [ + [26, 86, 85, 69, 45], + [81, 43, 96, 12, 42], + [ 7, 36, 5, 28, 95], + [55, 90, 54, 4, 46], + [52, 30, 79, 59, 87] + ], + board [ + [76, 87, 62, 13, 38], + [40, 44, 75, 2, 37], + [51, 22, 58, 84, 57], + [ 1, 29, 82, 67, 35], + [39, 20, 31, 77, 32] + ], + board [ + [53, 62, 61, 26, 95], + [78, 44, 9, 5, 97], + [83, 11, 18, 69, 2], + [94, 66, 8, 14, 27], + [ 1, 6, 7, 73, 76] + ], + board [ + [87, 34, 62, 93, 43], + [49, 20, 63, 29, 22], + [30, 94, 11, 5, 69], + [74, 9, 89, 41, 37], + [98, 38, 72, 13, 97] + ], + board [ + [69, 39, 15, 59, 14], + [42, 84, 56, 23, 1], + [99, 16, 62, 83, 89], + [32, 36, 33, 24, 3], + [22, 31, 55, 10, 13] + ], + board [ + [22, 44, 75, 3, 17], + [51, 79, 37, 59, 19], + [98, 4, 86, 35, 34], + [36, 20, 85, 5, 23], + [62, 92, 43, 7, 90] + ], + board [ + [ 3, 68, 95, 51, 71], + [20, 62, 47, 4, 30], + [60, 91, 27, 86, 73], + [14, 76, 66, 97, 85], + [79, 65, 8, 11, 36] + ], + board [ + [38, 63, 1, 64, 14], + [10, 52, 17, 90, 98], + [28, 61, 29, 20, 55], + [49, 58, 43, 3, 7], + [88, 81, 67, 32, 68] + ], + board [ + [63, 65, 15, 61, 57], + [47, 94, 6, 14, 49], + [42, 3, 83, 96, 31], + [23, 77, 1, 70, 18], + [45, 36, 64, 48, 35] + ], + board [ + [ 8, 92, 88, 32, 95], + [26, 41, 34, 11, 48], + [81, 35, 36, 62, 28], + [64, 33, 52, 97, 82], + [ 7, 37, 78, 0, 86] + ], + board [ + [75, 64, 84, 80, 60], + [17, 5, 55, 95, 70], + [ 0, 90, 68, 53, 93], + [ 8, 50, 38, 9, 65], + [97, 35, 26, 30, 6] + ], + board [ + [76, 87, 36, 5, 74], + [79, 94, 82, 48, 20], + [24, 88, 90, 62, 45], + [ 9, 40, 78, 22, 68], + [73, 71, 35, 42, 66] + ], + board [ + [52, 11, 17, 9, 72], + [45, 13, 90, 0, 80], + [93, 77, 37, 51, 96], + [ 3, 68, 94, 61, 1], + [26, 78, 12, 29, 66] + ], + board [ + [49, 14, 74, 11, 48], + [73, 58, 54, 3, 40], + [90, 38, 62, 18, 26], + [53, 93, 19, 41, 94], + [35, 63, 7, 80, 68] + ], + board [ + [13, 19, 92, 38, 1], + [63, 69, 4, 99, 36], + [53, 57, 54, 21, 80], + [97, 17, 2, 44, 29], + [ 0, 3, 89, 45, 58] + ], + board [ + [ 9, 26, 45, 57, 67], + [72, 10, 59, 79, 88], + [32, 2, 87, 42, 60], + [62, 98, 1, 93, 28], + [24, 99, 41, 44, 29] + ], + board [ + [57, 60, 54, 36, 84], + [28, 5, 32, 66, 77], + [13, 19, 42, 39, 37], + [56, 89, 74, 50, 55], + [ 8, 71, 78, 0, 80] + ], + board [ + [62, 38, 24, 44, 4], + [17, 8, 70, 77, 86], + [98, 6, 9, 88, 23], + [85, 63, 78, 60, 72], + [71, 48, 36, 69, 81] + ], + board [ + [84, 19, 8, 20, 17], + [40, 88, 33, 52, 81], + [29, 83, 11, 36, 92], + [66, 6, 73, 32, 82], + [44, 39, 26, 60, 63] + ], + board [ + [13, 73, 45, 32, 42], + [69, 20, 83, 8, 84], + [82, 61, 11, 89, 25], + [ 6, 92, 99, 52, 57], + [88, 22, 46, 26, 86] + ], + board [ + [74, 65, 17, 64, 94], + [19, 34, 40, 69, 80], + [43, 83, 45, 77, 87], + [41, 49, 13, 51, 89], + [91, 72, 54, 1, 60] + ], + board [ + [38, 10, 16, 51, 46], + [80, 60, 64, 62, 70], + [89, 71, 58, 49, 39], + [37, 5, 35, 88, 40], + [93, 72, 98, 42, 13] + ], + board [ + [ 9, 47, 91, 69, 68], + [27, 1, 49, 60, 13], + [ 6, 17, 95, 59, 35], + [26, 14, 75, 57, 11], + [15, 18, 19, 46, 74] + ], + board [ + [88, 66, 25, 89, 6], + [63, 4, 56, 73, 8], + [57, 0, 51, 5, 36], + [68, 96, 84, 67, 53], + [49, 82, 7, 32, 9] + ], + board [ + [81, 75, 92, 1, 62], + [ 0, 96, 27, 63, 46], + [76, 31, 93, 67, 12], + [74, 78, 59, 5, 60], + [69, 33, 25, 94, 43] + ], + board [ + [40, 72, 79, 58, 22], + [16, 24, 99, 96, 44], + [69, 4, 87, 90, 26], + [34, 43, 56, 15, 35], + [63, 88, 89, 52, 54] + ], + board [ + [43, 7, 44, 31, 24], + [71, 18, 84, 17, 64], + [ 8, 47, 93, 85, 36], + [72, 29, 22, 67, 74], + [69, 41, 58, 98, 61] + ], + board [ + [55, 21, 72, 14, 3], + [75, 12, 69, 91, 54], + [80, 40, 78, 39, 4], + [44, 88, 84, 76, 25], + [96, 57, 49, 52, 28] + ], + board [ + [54, 74, 32, 40, 64], + [ 5, 94, 71, 80, 22], + [82, 92, 79, 93, 16], + [53, 33, 98, 85, 14], + [29, 49, 9, 47, 12] + ], + board [ + [98, 67, 8, 10, 6], + [58, 13, 77, 99, 81], + [ 5, 55, 21, 19, 1], + [ 0, 26, 44, 70, 93], + [41, 96, 31, 91, 27] + ], + board [ + [50, 28, 48, 13, 18], + [96, 43, 25, 2, 78], + [88, 60, 0, 16, 73], + [12, 32, 15, 68, 22], + [95, 74, 10, 80, 21] + ], + board [ + [18, 49, 85, 55, 21], + [11, 68, 80, 59, 41], + [56, 94, 14, 62, 60], + [32, 20, 40, 6, 2], + [42, 66, 98, 71, 17] + ], + board [ + [13, 38, 1, 63, 82], + [33, 55, 54, 53, 92], + [36, 20, 39, 84, 83], + [67, 43, 70, 73, 75], + [94, 77, 76, 29, 16] + ], + board [ + [82, 27, 25, 18, 86], + [73, 3, 36, 28, 1], + [11, 96, 40, 23, 93], + [58, 90, 88, 35, 64], + [ 9, 38, 69, 8, 43] + ], + board [ + [87, 90, 16, 56, 67], + [41, 75, 89, 1, 80], + [22, 62, 5, 45, 69], + [28, 36, 19, 96, 71], + [26, 63, 88, 76, 31] + ], + board [ + [68, 80, 83, 95, 20], + [75, 0, 16, 38, 21], + [34, 89, 87, 36, 14], + [94, 47, 53, 73, 71], + [63, 8, 61, 96, 50] + ], + board [ + [81, 23, 6, 14, 26], + [86, 42, 82, 95, 85], + [77, 52, 38, 2, 33], + [69, 98, 54, 37, 4], + [78, 39, 3, 75, 80] + ], + board [ + [92, 99, 93, 28, 44], + [ 5, 8, 67, 45, 10], + [61, 79, 63, 85, 81], + [ 2, 87, 76, 68, 18], + [69, 52, 22, 16, 12] + ], + board [ + [95, 50, 21, 82, 60], + [ 5, 8, 6, 28, 26], + [52, 3, 38, 70, 74], + [75, 0, 53, 51, 44], + [10, 30, 34, 47, 71] + ], + board [ + [71, 44, 65, 48, 51], + [78, 57, 75, 6, 86], + [95, 58, 66, 12, 92], + [22, 61, 68, 88, 50], + [ 4, 36, 45, 28, 54] + ], + board [ + [37, 17, 5, 2, 52], + [57, 47, 4, 53, 39], + [11, 72, 66, 81, 46], + [27, 0, 67, 40, 83], + [98, 19, 10, 35, 84] + ], + board [ + [43, 59, 30, 72, 17], + [66, 50, 12, 84, 65], + [49, 60, 14, 1, 29], + [89, 75, 62, 82, 47], + [33, 5, 9, 58, 45] + ], + board [ + [14, 9, 3, 47, 74], + [69, 29, 57, 62, 22], + [ 4, 90, 40, 64, 15], + [21, 27, 30, 2, 63], + [97, 96, 99, 55, 41] + ], + board [ + [75, 73, 3, 59, 80], + [65, 34, 52, 20, 72], + [50, 84, 81, 69, 41], + [97, 77, 19, 85, 39], + [88, 46, 15, 35, 87] + ], + board [ + [96, 42, 74, 38, 78], + [58, 73, 67, 70, 10], + [62, 8, 82, 64, 16], + [65, 25, 13, 3, 89], + [40, 30, 53, 95, 51] + ], + board [ + [ 7, 16, 92, 88, 38], + [14, 52, 46, 93, 64], + [49, 48, 8, 76, 51], + [97, 67, 89, 75, 19], + [69, 9, 29, 43, 82] + ], + board [ + [81, 51, 24, 57, 9], + [46, 43, 77, 11, 35], + [83, 5, 14, 25, 84], + [70, 99, 47, 37, 16], + [ 3, 39, 75, 97, 80] + ], + board [ + [18, 74, 64, 6, 94], + [12, 59, 46, 48, 31], + [73, 77, 33, 1, 39], + [ 0, 69, 10, 24, 56], + [83, 66, 5, 76, 58] + ], + board [ + [40, 48, 72, 65, 2], + [19, 28, 93, 53, 44], + [75, 85, 42, 68, 66], + [99, 49, 55, 31, 41], + [94, 35, 78, 13, 61] + ], + board [ + [ 4, 20, 54, 33, 21], + [50, 61, 17, 53, 64], + [69, 30, 24, 90, 95], + [82, 51, 39, 52, 67], + [43, 73, 44, 62, 83] + ], + board [ + [31, 32, 63, 42, 60], + [39, 41, 28, 51, 53], + [15, 20, 24, 54, 5], + [ 9, 65, 70, 57, 99], + [50, 29, 35, 4, 47] + ], + board [ + [40, 99, 95, 72, 35], + [10, 14, 52, 83, 19], + [ 5, 51, 87, 49, 16], + [60, 66, 13, 63, 93], + [68, 57, 31, 6, 78] + ], + board [ + [58, 96, 49, 87, 28], + [95, 50, 54, 53, 52], + [24, 16, 64, 9, 5], + [ 7, 63, 8, 4, 17], + [59, 98, 3, 31, 25] + ], + board [ + [31, 83, 61, 58, 93], + [94, 52, 97, 30, 98], + [99, 2, 13, 66, 73], + [69, 71, 68, 40, 19], + [74, 84, 45, 25, 77] + ], + board [ + [58, 85, 45, 64, 74], + [18, 88, 91, 53, 2], + [93, 0, 92, 55, 39], + [75, 49, 87, 80, 4], + [89, 97, 57, 14, 54] + ], + board [ + [20, 92, 64, 50, 25], + [52, 90, 80, 31, 38], + [55, 54, 10, 76, 21], + [95, 97, 4, 77, 19], + [30, 26, 12, 39, 11] + ], + board [ + [71, 10, 84, 68, 77], + [48, 82, 69, 75, 3], + [93, 24, 16, 42, 60], + [15, 62, 76, 36, 20], + [21, 18, 94, 22, 45] + ], + board [ + [10, 91, 66, 56, 75], + [ 6, 8, 45, 59, 83], + [52, 93, 48, 81, 87], + [99, 78, 43, 64, 84], + [21, 12, 61, 71, 9] + ], + board [ + [98, 77, 95, 63, 15], + [30, 14, 39, 12, 20], + [13, 32, 27, 0, 5], + [86, 80, 51, 40, 99], + [68, 44, 26, 29, 91] + ], + board [ + [92, 79, 49, 44, 33], + [88, 4, 34, 3, 90], + [51, 46, 31, 50, 47], + [61, 11, 94, 6, 24], + [72, 18, 98, 65, 57] + ], + board [ + [88, 94, 93, 11, 33], + [75, 77, 53, 54, 51], + [97, 15, 89, 38, 76], + [47, 64, 55, 22, 0], + [40, 56, 34, 19, 3] + ], + board [ + [36, 55, 51, 86, 91], + [49, 21, 78, 6, 58], + [90, 1, 88, 45, 33], + [37, 69, 75, 41, 50], + [81, 24, 34, 38, 93] + ], + board [ + [21, 73, 99, 50, 65], + [72, 77, 86, 7, 68], + [24, 63, 71, 26, 25], + [ 9, 12, 29, 93, 87], + [81, 23, 22, 94, 67] + ], + board [ + [37, 47, 66, 89, 73], + [49, 23, 79, 31, 86], + [58, 52, 21, 39, 15], + [60, 38, 82, 50, 36], + [74, 30, 25, 35, 99] + ], + board [ + [ 6, 18, 53, 36, 87], + [ 3, 59, 50, 2, 75], + [69, 61, 57, 19, 63], + [44, 77, 42, 22, 7], + [89, 29, 45, 35, 71] + ], + board [ + [28, 26, 53, 47, 21], + [31, 71, 27, 58, 85], + [10, 20, 74, 59, 42], + [89, 44, 12, 91, 54], + [32, 87, 36, 22, 7] + ], + board [ + [62, 17, 61, 75, 51], + [44, 60, 37, 14, 76], + [96, 0, 1, 52, 5], + [57, 42, 97, 66, 90], + [12, 23, 50, 98, 25] + ], + board [ + [42, 30, 86, 89, 66], + [41, 98, 39, 29, 23], + [75, 73, 20, 79, 90], + [38, 60, 45, 16, 18], + [17, 10, 47, 5, 13] + ], + board [ + [ 4, 10, 26, 74, 38], + [66, 84, 60, 23, 57], + [30, 59, 58, 2, 49], + [83, 82, 70, 64, 43], + [71, 31, 35, 90, 0] + ], + board [ + [27, 99, 33, 56, 3], + [41, 97, 1, 68, 88], + [43, 63, 81, 89, 22], + [30, 32, 59, 64, 12], + [84, 58, 10, 39, 76] + ], + board [ + [98, 16, 75, 27, 57], + [ 0, 9, 3, 79, 50], + [ 5, 34, 93, 6, 21], + [52, 70, 87, 31, 49], + [58, 46, 24, 20, 45] + ], + board [ + [78, 24, 19, 13, 30], + [83, 59, 79, 37, 72], + [84, 81, 99, 17, 77], + [10, 93, 3, 33, 70], + [29, 35, 49, 6, 5] + ], + board [ + [47, 6, 82, 94, 53], + [83, 19, 25, 54, 64], + [ 9, 56, 39, 31, 96], + [ 1, 81, 66, 41, 5], + [55, 48, 43, 12, 14] + ], + board [ + [47, 55, 86, 31, 17], + [89, 45, 65, 34, 56], + [99, 88, 18, 97, 3], + [52, 21, 14, 68, 13], + [ 9, 26, 22, 7, 32] + ], + board [ + [75, 27, 62, 19, 72], + [20, 49, 7, 21, 85], + [53, 46, 1, 59, 99], + [61, 71, 87, 24, 83], + [ 5, 77, 41, 51, 73] + ], + board [ + [57, 59, 82, 77, 52], + [99, 49, 81, 37, 54], + [70, 89, 23, 20, 90], + [31, 1, 21, 98, 66], + [86, 35, 46, 36, 18] + ], + board [ + [37, 39, 70, 76, 27], + [68, 84, 25, 1, 33], + [50, 82, 77, 20, 44], + [18, 11, 51, 62, 54], + [80, 67, 35, 89, 30] + ], + board [ + [85, 96, 1, 3, 73], + [25, 47, 10, 46, 98], + [ 2, 33, 91, 71, 35], + [ 0, 32, 11, 55, 67], + [14, 81, 17, 5, 94] + ], + board [ + [68, 84, 46, 43, 81], + [42, 35, 48, 89, 30], + [ 4, 17, 65, 77, 6], + [49, 97, 85, 12, 66], + [75, 25, 13, 90, 51] + ], + board [ + [94, 42, 46, 58, 56], + [97, 50, 86, 84, 15], + [52, 9, 28, 32, 59], + [26, 96, 91, 57, 83], + [29, 99, 18, 31, 43] + ], + board [ + [ 2, 19, 31, 10, 32], + [81, 16, 50, 59, 7], + [76, 30, 63, 44, 95], + [82, 54, 61, 75, 36], + [85, 78, 12, 67, 9] + ], + board [ + [92, 1, 72, 27, 37], + [22, 13, 91, 4, 34], + [53, 82, 76, 70, 19], + [99, 38, 59, 33, 52], + [ 0, 61, 36, 67, 75] + ], + board [ + [94, 41, 5, 57, 1], + [37, 36, 99, 34, 47], + [40, 93, 62, 32, 76], + [61, 75, 48, 42, 73], + [35, 69, 54, 13, 50] + ], + board [ + [76, 12, 51, 11, 74], + [30, 83, 73, 33, 78], + [95, 77, 15, 14, 80], + [86, 37, 91, 50, 10], + [52, 67, 3, 60, 17] + ], + board [ + [68, 7, 42, 81, 15], + [46, 58, 9, 31, 18], + [91, 1, 28, 34, 37], + [17, 57, 6, 2, 70], + [97, 54, 20, 27, 44] + ], + board [ + [44, 80, 45, 28, 14], + [94, 47, 29, 50, 54], + [64, 67, 96, 95, 93], + [76, 36, 82, 39, 43], + [30, 55, 97, 3, 2] + ], + board [ + [43, 2, 15, 99, 34], + [97, 75, 26, 9, 67], + [30, 63, 74, 12, 82], + [18, 6, 49, 48, 55], + [47, 36, 41, 56, 83] + ], + board [ + [26, 70, 87, 80, 89], + [17, 50, 61, 21, 96], + [43, 83, 85, 46, 64], + [66, 75, 23, 47, 69], + [22, 72, 55, 52, 8] + ], + board [ + [67, 54, 11, 29, 42], + [16, 45, 56, 86, 66], + [ 4, 80, 43, 72, 91], + [90, 87, 63, 39, 50], + [32, 6, 59, 27, 89] + ], + board [ + [14, 92, 78, 47, 59], + [98, 0, 63, 85, 31], + [52, 8, 84, 70, 91], + [43, 3, 76, 65, 57], + [87, 22, 99, 94, 58] + ], + board [ + [26, 53, 58, 52, 1], + [82, 57, 32, 40, 20], + [72, 21, 74, 46, 43], + [41, 15, 98, 2, 11], + [ 5, 96, 22, 18, 55] + ] + ] + +row n (Board board) = board !! n +column n (Board board) = [ x !! n | x <- board ] + +score :: Board -> Int +score (Board board) = sum $ catMaybes $ concat board + +mark :: Board -> (Int, Int) -> Either Board Int +mark (Board board) pos = if isWin then Right $ score newBoard else Left newBoard + where + newBoard = Board $ updateMatrix pos board Nothing + isWin = row y newBoard == cleared || column x newBoard == cleared + (x, y) = pos + cleared = replicate 5 Nothing + +findPosition :: Board -> Int -> Maybe (Int, Int) +findPosition (Board board) value = find (\(x,y) -> board !! y !! x == Just value) indexes + where + indexes = [(x,y) | x <- [0..4], y <- [0..4]] + +markTile :: Board -> Int -> Either Board Int +markTile board num = case pos of + Nothing -> Left board + Just pos -> mark board pos + where + pos = findPosition board num + +markAll :: [Board] -> Int -> Either [Board] Int +markAll boards num = getResult $ map (`markTile` num) boards + where + getResult [] = Left [] + getResult (Right score : rest) = Right score + getResult (Left board : rest) = case getResult rest of + Left boards -> Left $ board : boards + Right score -> Right score + + +markAllUntilLast :: [Board] -> Int -> Either [Board] Int +markAllUntilLast boards num = getResult $ map (`markTile` num) boards + where + getResult [] = Left [] + getResult [Right score] = Right score + getResult (Right _ : rest) = getResult rest + getResult (Left board : rest) = case getResult rest of + Left boards -> Left $ board : boards + Right _ -> Left [board] + + +play :: [Board] -> [Int] -> ([Board] -> Int -> Either [Board] Int) -> Maybe Int +play boards numbers markAll = playRound numbers boards + where + playRound [] _ = Nothing + playRound (cur:rest) boards = case markAll boards cur of + Left newBoards -> playRound rest newBoards + Right score -> Just (score * cur) + + +{- + +>>> play boards randoms markAll +Just 49686 + +>>> play boards randoms markAllUntilLast +Just 26878 + +-} From 31d96b280afd20cf620f6d104b5562a1a7731a25 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 12 Dec 2021 11:21:58 +0100 Subject: [PATCH 06/32] Day 5 - 10 --- .gitignore | 1 + AoC21.xcodeproj/project.pbxproj | 675 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + common.swift | 18 + day10.swift | 170 +++++ day5.hs | 554 ++++++++++++++ day5part2.swift | 584 +++++++++++++++ day6.swift | 36 + day8part2.swift | 267 +++++++ day9.swift | 186 +++++ 11 files changed, 2506 insertions(+) create mode 100644 .gitignore create mode 100644 AoC21.xcodeproj/project.pbxproj create mode 100644 AoC21.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 AoC21.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 common.swift create mode 100644 day10.swift create mode 100644 day5.hs create mode 100644 day5part2.swift create mode 100644 day6.swift create mode 100644 day8part2.swift create mode 100644 day9.swift diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c545112 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +xcuserdata/ diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj new file mode 100644 index 0000000..62bf1b8 --- /dev/null +++ b/AoC21.xcodeproj/project.pbxproj @@ -0,0 +1,675 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 55; + objects = { + +/* Begin PBXBuildFile section */ + 261F317F2763DACC0018D49C /* day10.swift in Sources */ = {isa = PBXBuildFile; fileRef = 261F317E2763DACC0018D49C /* day10.swift */; }; + 261F31802763DACE0018D49C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 263BA5B3275E97AA00839C92 /* day5part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263BA5B2275E97AA00839C92 /* day5part2.swift */; }; + 263BA5C0275EA5BD00839C92 /* day6.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263BA5BF275EA5BD00839C92 /* day6.swift */; }; + 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; + 269BE5CA2762A05300871C85 /* day9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5C82762A04F00871C85 /* day9.swift */; }; + 269BE5CC2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 269BE5CD2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 269BE5CE2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 269BE5CF2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXCopyFilesBuildPhase section */ + 261F31752763DAB60018D49C /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; + 263BA5A9275E978400839C92 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; + 263BA5B6275EA5A600839C92 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; + 269BE5B22762958800871C85 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; + 269BE5BF2762A03F00871C85 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; +/* End PBXCopyFilesBuildPhase section */ + +/* Begin PBXFileReference section */ + 261F31772763DAB60018D49C /* Day10 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day10; sourceTree = BUILT_PRODUCTS_DIR; }; + 261F317E2763DACC0018D49C /* day10.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day10.swift; sourceTree = ""; }; + 263BA5AB275E978400839C92 /* Day5Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day5Part2; sourceTree = BUILT_PRODUCTS_DIR; }; + 263BA5B2275E97AA00839C92 /* day5part2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day5part2.swift; sourceTree = ""; }; + 263BA5B8275EA5A600839C92 /* Day6 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day6; sourceTree = BUILT_PRODUCTS_DIR; }; + 263BA5BF275EA5BD00839C92 /* day6.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day6.swift; sourceTree = ""; }; + 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; + 269BE5BB2762959B00871C85 /* day8part2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day8part2.swift; sourceTree = ""; }; + 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; + 269BE5C82762A04F00871C85 /* day9.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day9.swift; sourceTree = ""; }; + 269BE5CB2762A08800871C85 /* common.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = common.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 261F31742763DAB60018D49C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 263BA5A8275E978400839C92 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 263BA5B5275EA5A600839C92 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 269BE5B12762958800871C85 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 269BE5BE2762A03F00871C85 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 263BA594275E974800839C92 = { + isa = PBXGroup; + children = ( + 261F317E2763DACC0018D49C /* day10.swift */, + 269BE5CB2762A08800871C85 /* common.swift */, + 269BE5C82762A04F00871C85 /* day9.swift */, + 269BE5BB2762959B00871C85 /* day8part2.swift */, + 263BA5BF275EA5BD00839C92 /* day6.swift */, + 263BA5B2275E97AA00839C92 /* day5part2.swift */, + 263BA59E275E974800839C92 /* Products */, + ); + sourceTree = ""; + }; + 263BA59E275E974800839C92 /* Products */ = { + isa = PBXGroup; + children = ( + 263BA5AB275E978400839C92 /* Day5Part2 */, + 263BA5B8275EA5A600839C92 /* Day6 */, + 269BE5B42762958800871C85 /* Day8Part2 */, + 269BE5C12762A03F00871C85 /* Day9 */, + 261F31772763DAB60018D49C /* Day10 */, + ); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 261F31762763DAB60018D49C /* Day10 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 261F317D2763DAB60018D49C /* Build configuration list for PBXNativeTarget "Day10" */; + buildPhases = ( + 261F31732763DAB60018D49C /* Sources */, + 261F31742763DAB60018D49C /* Frameworks */, + 261F31752763DAB60018D49C /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day10; + productName = Day10; + productReference = 261F31772763DAB60018D49C /* Day10 */; + productType = "com.apple.product-type.tool"; + }; + 263BA5AA275E978400839C92 /* Day5Part2 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 263BA5AF275E978400839C92 /* Build configuration list for PBXNativeTarget "Day5Part2" */; + buildPhases = ( + 263BA5A7275E978400839C92 /* Sources */, + 263BA5A8275E978400839C92 /* Frameworks */, + 263BA5A9275E978400839C92 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day5Part2; + productName = Day5Part2; + productReference = 263BA5AB275E978400839C92 /* Day5Part2 */; + productType = "com.apple.product-type.tool"; + }; + 263BA5B7275EA5A600839C92 /* Day6 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 263BA5BC275EA5A600839C92 /* Build configuration list for PBXNativeTarget "Day6" */; + buildPhases = ( + 263BA5B4275EA5A600839C92 /* Sources */, + 263BA5B5275EA5A600839C92 /* Frameworks */, + 263BA5B6275EA5A600839C92 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day6; + productName = Day6; + productReference = 263BA5B8275EA5A600839C92 /* Day6 */; + productType = "com.apple.product-type.tool"; + }; + 269BE5B32762958800871C85 /* Day8Part2 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; + buildPhases = ( + 269BE5B02762958800871C85 /* Sources */, + 269BE5B12762958800871C85 /* Frameworks */, + 269BE5B22762958800871C85 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day8Part2; + productName = Day8Part2; + productReference = 269BE5B42762958800871C85 /* Day8Part2 */; + productType = "com.apple.product-type.tool"; + }; + 269BE5C02762A03F00871C85 /* Day9 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 269BE5C52762A03F00871C85 /* Build configuration list for PBXNativeTarget "Day9" */; + buildPhases = ( + 269BE5BD2762A03F00871C85 /* Sources */, + 269BE5BE2762A03F00871C85 /* Frameworks */, + 269BE5BF2762A03F00871C85 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day9; + productName = Day9; + productReference = 269BE5C12762A03F00871C85 /* Day9 */; + productType = "com.apple.product-type.tool"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 263BA595275E974800839C92 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastSwiftUpdateCheck = 1310; + LastUpgradeCheck = 1310; + TargetAttributes = { + 261F31762763DAB60018D49C = { + CreatedOnToolsVersion = 13.1; + LastSwiftMigration = 1310; + }; + 263BA5AA275E978400839C92 = { + CreatedOnToolsVersion = 13.1; + LastSwiftMigration = 1310; + }; + 263BA5B7275EA5A600839C92 = { + CreatedOnToolsVersion = 13.1; + LastSwiftMigration = 1310; + }; + 269BE5B32762958800871C85 = { + CreatedOnToolsVersion = 13.1; + LastSwiftMigration = 1310; + }; + 269BE5C02762A03F00871C85 = { + CreatedOnToolsVersion = 13.1; + }; + }; + }; + buildConfigurationList = 263BA598275E974800839C92 /* Build configuration list for PBXProject "AoC21" */; + compatibilityVersion = "Xcode 13.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 263BA594275E974800839C92; + productRefGroup = 263BA59E275E974800839C92 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 263BA5AA275E978400839C92 /* Day5Part2 */, + 263BA5B7275EA5A600839C92 /* Day6 */, + 269BE5B32762958800871C85 /* Day8Part2 */, + 269BE5C02762A03F00871C85 /* Day9 */, + 261F31762763DAB60018D49C /* Day10 */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 261F31732763DAB60018D49C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 261F31802763DACE0018D49C /* common.swift in Sources */, + 261F317F2763DACC0018D49C /* day10.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 263BA5A7275E978400839C92 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 269BE5CC2762A08800871C85 /* common.swift in Sources */, + 263BA5B3275E97AA00839C92 /* day5part2.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 263BA5B4275EA5A600839C92 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 269BE5CD2762A08800871C85 /* common.swift in Sources */, + 263BA5C0275EA5BD00839C92 /* day6.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 269BE5B02762958800871C85 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 269BE5CE2762A08800871C85 /* common.swift in Sources */, + 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + 269BE5BD2762A03F00871C85 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 269BE5CF2762A08800871C85 /* common.swift in Sources */, + 269BE5CA2762A05300871C85 /* day9.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 261F317B2763DAB60018D49C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 261F317C2763DAB60018D49C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; + 263BA5A2275E974800839C92 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 12.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + }; + name = Debug; + }; + 263BA5A3275E974800839C92 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 12.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = macosx; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + }; + name = Release; + }; + 263BA5B0275E978400839C92 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 263BA5B1275E978400839C92 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; + 263BA5BD275EA5A600839C92 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 263BA5BE275EA5A600839C92 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; + 269BE5B82762958800871C85 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 269BE5B92762958800871C85 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; + 269BE5C62762A03F00871C85 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 269BE5C72762A03F00871C85 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 261F317D2763DAB60018D49C /* Build configuration list for PBXNativeTarget "Day10" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 261F317B2763DAB60018D49C /* Debug */, + 261F317C2763DAB60018D49C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 263BA598275E974800839C92 /* Build configuration list for PBXProject "AoC21" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 263BA5A2275E974800839C92 /* Debug */, + 263BA5A3275E974800839C92 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 263BA5AF275E978400839C92 /* Build configuration list for PBXNativeTarget "Day5Part2" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 263BA5B0275E978400839C92 /* Debug */, + 263BA5B1275E978400839C92 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 263BA5BC275EA5A600839C92 /* Build configuration list for PBXNativeTarget "Day6" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 263BA5BD275EA5A600839C92 /* Debug */, + 263BA5BE275EA5A600839C92 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 269BE5B82762958800871C85 /* Debug */, + 269BE5B92762958800871C85 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 269BE5C52762A03F00871C85 /* Build configuration list for PBXNativeTarget "Day9" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 269BE5C62762A03F00871C85 /* Debug */, + 269BE5C72762A03F00871C85 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 263BA595275E974800839C92 /* Project object */; +} diff --git a/AoC21.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/AoC21.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/AoC21.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/AoC21.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/AoC21.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/AoC21.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/common.swift b/common.swift new file mode 100644 index 0000000..1df8bf4 --- /dev/null +++ b/common.swift @@ -0,0 +1,18 @@ +protocol Puzzle { + mutating func run() + init() +} + +extension Puzzle { + static func main() { + var instance = Self() + instance.run() + } +} + +extension RangeReplaceableCollection { + mutating func removeFirst(where predicate: (Element) -> Bool) -> Element? { + guard let index = firstIndex(where: predicate) else { return nil } + return remove(at: index) + } +} diff --git a/day10.swift b/day10.swift new file mode 100644 index 0000000..e9dbed6 --- /dev/null +++ b/day10.swift @@ -0,0 +1,170 @@ +@main +struct Day10: Puzzle { + func run() { + var rest = input[...] + var score = 0 + var completeionScores: [Int] = [] + loop: while true { + if let complete = parse(&rest) { + completeionScores.append(complete) + } + + switch rest.first { + case "\n": break + case ")": score += 3 + case "]": score += 57 + case "}": score += 1197 + case ">": score += 25137 + case let ch?: print("Unknown char", ch) + case nil: break loop + } + + rest = rest.drop { !$0.isNewline }.dropFirst() + } + + print(score) + + completeionScores.sort() + let completeScore = completeionScores[completeionScores.count / 2] + print("Part 2:", completeScore) + } + + func end(for start: Character) -> Character? { + switch start { + case "(": return ")" + case "[": return "]" + case "{": return "}" + case "<": return ">" + default: return nil + } + } + func parse(_ string: inout Substring) -> Int? { + var stack: [Character] = [] + while true { + guard let current = string.first else { return nil } + + if current == stack.last { + stack.removeLast() + } else if let closing = end(for: current) { + stack.append(closing) + } else if current == "\n" { + + return stack.reversed().reduce(0) { partialResult, ch -> Int in + let characterScore: Int + switch (ch) { + case ")": characterScore = 1 + case "]": characterScore = 2 + case "}": characterScore = 3 + case ">": characterScore = 4 + default: fatalError() + } + + return partialResult * 5 + characterScore + } + } else { return nil } + + string = string.dropFirst() + } + } + + let input = """ +{<[[<<<[[{[[<<()()>>[([]<>){{}{}}]]]<(<{{}<>}{{}{}}><{<>[]}[{}{}]>)({(())}<[(){}][(){}]>)>}]<<<(<<{} +{[({({([({(((<[]()>[()<>]){[<>[]](<>[])})<<<[]()}>(<()()>)>)}[{((<{}[]>{<>()}){[<>{}]<<><>>})(({[]()}<<>()>)< +(((([{{{<{<[[<()<>><<>{}>]]{<(<>())>}>{<[([]<>)<{}{}>][[<><>][<>[]]]>[{(<>{})[<>()]}<<()[]>[[]< +<<[<([[{{{[({<<>()>}<<<>[]>([]())>){{{{}[]}<(){}>)}]{{[(()())[(){}]]<<[]{}>({}[])>}({<<><>>}<([]<>){()[]}>)} +<[(({[({<<[[[{<>{}}(<><>))<(<>[])[{}[]]>]<[([][])[(){}]]>]([<[()()](<>[])>]<(<{}()>[{}<>])({<>[ +(<[[{[[[(<((<<{}{}><[]<>>><[{}<>][<>[]]>)<(<{}<>><{}{}>)[{()[]}<<>()>]>)>[(<{({}{})}[{()[]}{[]<>}]>){<(([] +(([[({{{(<([({[]{}}){{[][]]}])>)[<([[[{}]{(){}}][([]())]]([{[][]}{<>()}](({}()){<>{}})))[{({ +{(<<([([(<<[{((){})([]<>)}]<{{{}{}}{{}[]}}<{(){}}{()[]})>><([<[]{}>([]<>)](<()<>>)){<([]<> +{[<<[<{{((({<[[]{}]{{}}>{<{}<>>[<>[]]}})<{[(<><>><{}<>>][{[]{}}]}[[[[][]]<<>{}>]{[[]()][[]<>]}]>)[( +({<(<([{<{<[(((){}){(){}})]>}[{{{{()()}<[]()>}{{<>()}<{}{}>}}[[{{}{}}[<><>]]{({}[]>[[]{}]}]}({{{()() +{[[{([{<<{<(<<{}()>[()]>)<{{()()}}[<[][]>(<>{})]>>{[<{<>()){()<>}>{{()()}[<>()]}][[[{}[]]]<{{}[]}<()( +{{<<<<<[[([{<[{}[]]{()<>}>{<<>()>{{}<>}}}<<{{}()}{()()}>{[()<>]({}[])]>]{[(<{}{}>)<({}[])>]<{({}<>)}{({}()) +({(<<{[<<{<(<[<>()][[][]]><[{}<>]({}[])>)[[[<>[]][[]()]]<{[]{}}[{}{}]>]>{[(<[]()>{<>{}})][<{{}}<{ +(<<<[[({<[[(<<[]{}>>[(()<>)[<>{}]])<{(<>[])}(<<>()>[[]{}])>]<[({[]()}{{}{}})<{{}()}<{}<>>>]>][[({<<>()>{[]()} +{{{[<[([<(<{[([][])[{}[]]]{[()()]{{}()}}}><[<([][])(<>())><{()[]}({}<>)>]<<<(){}><<><>>>((()()){ +[(((([{({<(<(<[]<>>{()[]})><{(<><>){{}[]}}{(()<>)([][])}>)>})}][({{[[{<(<><>){{}()}>((<>()){<>[] +([{<{<({(<[(<({}<>)><{[]}[{}[]]>)(<[[][]]{[]{}}>{{[]}((){})})]{(((()){{}()})(([][])({}{})))[(<()[]><( +[([{{<{(<[{{((()())<[]{}>){{{}<>}<<><>>}}[<([]())>[(()[])<{}{}>]]}{[({(){}}{(){}])<<[]()>{{}()}> +(<[<{{[<<({<<{<><>}><<()[]>[<><>]>>}{[(((){}){()<>}}<<[]()>{(){}}>]{{{{}[]}[{}<>]}[{{}{}}<(){}>]}}){{({ +<([([[<[({<<{{[][]}<{}[]>}<<()[]>[()<>]>>>})((<(<[[]]((){})>{([]()){<><>}})>)(<<<<[]<>>[[]()])>>([[ +[<<{<(<<{{[({<<>()><{}[]>})<{[<>()](<><>)}[{{}()}{[]()}]>]}}({<[{<()<>>}[<<>>]]((<[][]>{[][ +{{(<({[((<([({()<>})[{{}[]}(()<>))]{[{[]<>}][{()<>}]})([({<><>}{{}()})])>)({<(((()<>)(<><>)))>(( +[(<{({[<{<[<([{}()](<>{}))>]([([[]<>]<()<>>){<()>}]{{[[]()]{{}{}}}[{[]}{{}<>}]})>}<([<[<<>[]>]{<(){}>{ +[[[[<(<({<[[<{()}{{}[]}><[<><>]<()[]>>](<([]{})[{}]>)]>}<[{[[{(){}}]{(<><>)}][<<[][]>{(){} +{(({({[{{((<[([]())((){})]>){[{(<>[])}<([]{})[[]<>]>]({<()<>>[[]<>]}[<()>{<>[]}])})({<([[][]]((){}))([<>{}]) +<(<(<<{[<<{<<[()[]][[]()]><([][])<{}()>>}}>[<<<<{}<>>>{[{}][{}{}]}>[<[()()]{{}[]}>[{[][]}([])]]>[[<<[][]>( +<<{{({<<(([{{([][])(()<>)}}(<<[]{}>>)])([{[(()[])]}<{{<><>}<[]<>>}[<{}<>><[]<>)]>]{{<[{}[]] +({[[[[(([[[{<({})([]())>{{<>{}}}}][[<<[]>[<>()]>]{[[[]<>][<>{}]]{[<>[]]}})][[<(<(){}>{()[]})><[[()[ +(((((([{{{(<<(<>{}){<>}><(()[])<()[]>>>{[<{}<>>(<>())}<{{}()}>})((([(){}]{<>})[{[]{}}{{}[] +(([([(((<[(({[<>()]{[][]}})[{{[]()>[[]{}]}(<<>{}><{}<>>)]){[({[]}(<>[]))([()()][<>{}])]({({} +[(<({{([[<(((<<>()>[[]<>])({{}()}<()<>>))[[{[]()}<()<>>][{()()}<{}[]>]])>({(<({}{})<()[]}>[{() +{{{[([<{{{([<<<>{}>{()<>}>{<{}()><<><>>}]([{()}]))<{[<()()>{()[]}]{({}[])[<>()]}}>}}}({<(({({ +[([([<[<(<[{{{<>()}{()[]}}({()[]}[{}<>])}{[{<>}]([[][]]<(){}>)}]>[[{<[<>()][{}()]>(((){}>[<>[]])}{{{<>( +{{[{{[[<{(<([[[][]]([]<>)])>)<{<<{<>()}[<>()]><((){})<<><>>>>([<<><>>(()<>)]([(){}}{[]<>}))}>}(({{{{[]}{ +[[[[({[<({{[[(<><>)]{<<>[]>([]{})}]<[{[]}{()()}]([<>()]([]<>>)>}})>{{<([{<{}{}>{()}}])({<[<>()](<><>)> +{[({<[({[{[({[{}()][[]<>]}((()<>)<{}<>>))<[{{}}({}{})]>]{[<<<>[]>[[][]]>(({}[])(()[]))>{(([]{})({}[]))([ +[<<([{{(<<[<[[{}[]]](<[]{}>[<>{}])>[[<{}[]>(<>[])]{([][])[()[]]}]][[{{()[]}[<>{}]}(<<>{}>[[] +<[(([{([([<{[[(){}]<<>{}>]}[[[()()]{{}<>}]]>[[{({}{})[()()]}([()()][<>[]])]{<[[]<>][[][]]>}]]{{<{[<> +[<[[[[([[{[[<[{}{}]([]<>)>[({}[])<[]<>>]][<{()()}[<>[]]>([[]]<<><>>)]]}([<{{{}[]}}{(()<>>({}[ +<[<{[(<{({[<[[[][]]([][])]<([]){{}<>}>>[({<>{}}<[]>)]]([[([]{})[[]()]][[[]{}]({}{})]](<[{}<> +({[<([(<[[<([[(){}]<<>{}>])({[[]{}]<()[]>}<<[]<>>{[][]}>)>{<[<[]()><{}[]>]([<>()]({}[]))><( +({<<<{<({[[{[{<>{}}[<><>]]}{(({})[()()])<<()<>>([]{})>}]]]{<{{([[]<>](<>[]))[[{}()]({}())]}}>((<([{}<>])>{{<[ +[{([{[[{{<{<[{[]()}<[]{}>]>({<{}[]>{<>[]}})}[(({{}<>}<{}<>]){[[]{}]({}[])})]>}}]({([[([<()()>{[] +<[{([([{[{<(([<>[]]{<>()})<{{}<>}>){([<><>])<(()[])<<>{}>>}><{{[{}<>]{<>}}[(())[()<>]]}<{[{}{}]{{}{}}}<{{}<> +[{<<([{{(((<{<<>()>[{}()]}>{<<<>[]><[]()>>}){[(([][])([]()))<{{}[]}[[]()]>][[{{}<>}({}{})]{({}{})(< +{({[((<([[[{{([])[()()]}({{}{}}))<(<[]<>>[()[]])>]<<([[]<>])<[<>()][[]<>]>>({[<><>]{{}[]}}({()[] +{[(<(<{[({[(<<[]>{<>{}}>[{<><>}([]())])(<[[][]]<()<>>>[(()[])])]<{[{()[]}[<>{}]]}[{<(){}>(<>{})}<<<>()>>] +(<{<[<({{{{([({}{})[[]{}]]({{}<>}<()[]}))}(<<{<>()}{()<>}>{[<>{}][()[]]}>{<<[][]>{<>()}><(<>{})<{}[]>>})} +{(<<{[({<<[(<{{}{}}<[]<>>>((<>())([]<>)))]>><<({<{[]<>}([]<>)>{[()()]}}<<{{}<>}<()()>>(<()() +<([([<(<[({[[({}()){()<>}](({}{})(<><>))][{({}<>)<{}{}>}{([]){(){}}}]})]>)<[[{<{{(()){<>[]}}({{}()} +<<<([{[({{{((<{}()>{()()})[[<>()]])[[[{}()]<{}()>]{{[]<>}{{}())}]}<<{<<>><<><>>}[{{}[]}(<><>) +<([<[<{[[[{[{[<><>][{}<>]}(<[]{}>)]}([{{<>()}<{}[]>}{([]<>)[(){}]}]<{((){})<[]()>}[<[]()><()[]>]> +[([[[[[({{[(<[{}<>]<{}()>><[<><>]{<>()}>)]}<{(<(()<>)<{}{}>>{{()<>}(()()}})}>})(<[<([<<>[]>((){})]([[]<>]<() +{(<[[{(((([[([{}()]<{}()))[({}{}){[]}]](<<<>[]>>([<>{}]))][(<({}{})<()[]>><<[]{}><{}[]>>)])(<([([][])[ +({(([{{<{{{<[<<>[]>{()<>}][([][]){()<>}]>([<()>(<><>)]<{<><>}<[]>>)}<{[[<>[]][<>{}]]<{<>()}{[][] +{[([[[(([<{<{[[]<>]<<>()>}<<()()>([]{})>>[<<()[]>(<>())>[(()<>)]]}>]{{({{{{}[]}({})}<{{}<>}[[]()]>}(( +[<([[<[{{{[[[<<>{}>]{<()[]>{[]<>}}]{([[]{}]({}()))}]}}}(<{<[[({}{})<()()>]]({({}<>)[[]]}{[<><>]{<>{}}})>(<[ +<<(<<{{(({<[<<[]{}>{()()}}[[()[]]]]{[(<>{}){<>}]{[{}](<>)}}><{<(()())<[]<>>>[<(){}>(<>[])] +{<(<[<[(((<{((()[])([]<>))([[]<>]({}[]))}><<([<>())<[]<>>)(<{}[]>({}<>))>[[([]())]({()()}{{}<>}) +[[((<[[[{{<{<{{}()}[{}()]>{{[]{}}[<><>]}}>}<{[<[(){}]<<><>>]([{}<>]<<><>>)]}(<<[{}<>]<<>{}> +<[{{[[{<[{(({[(){}]({}{})})[([{}<>][()()])[<[][]><{}<>>]])}](<({(([]<>){()[]})[([]())((){})]}{{{<>{}}<[]()>}< +<{((<([[{<<[(<{}()><{}[]>)]>[([{<>()}<()()>]<{()[]><()[]>>){<[{}[]]>[{[][]}(<><>)]}]>{{[{{{}<>}{<>}}[[(){}]<( +{<[({(<{{[([{{[]{}}((){})}[[{}[]]<{}()>]]((<[]<>>)<([]()){{}[]}>))(<<{()[]}{[]})>[[(()())]{[()<>][<>( +<<{[[[[{<({<[[{}()]]{{[]<>}{[]}}><{{{}()}({}{})}>}{([({}[])({}())](({}[])))[<{[]<>}[<>()]><<[]<>>(<>[])>]} +[(([<<[{[{<({{()[]}<<><>>})[[([]())[[]{}])(<<>{}>)]>(<{{[][]}[{}()]}([{}{}][(){}])>((<<>[]>(() +[({<<[[<{[<<({[]()}<()<>>)>]]{{<[[[]][{}<>]]>}{([[<>{}][{}{}]]<((){})(()[])>)}}}([{((<{}[]>)[<{}[]><{}()>])} +{[[((<[{<<([{<<><>>([]{})}<(<><>)<<>()>>][{[[]{}]{()()}}<({}{})([][])>])><<{({<>{}}[<>()])(( +[[[<{{(<((([{<[]{}>({}<>)}[<<>()>]]<<(()[]){{}[]}>{(()<>){{}<>}}>){{{(()())[<><>]}[[[]<>]{<>[]}]}}))[ +(<({[({[[({{<{<>{}}<()[]>>[{(){}}<<>[]>]}[[<[][]><()<>>]({<>{}}{[]()})]}{(([[]<>][{}[]])(<[]<>>([ +{([<{[<({{{({[[][]][()()])[<<>()><[][]>])}<{{{[]()}[{}()]}{{<>()}{<>}}}((({}())<{}{}>){(<>{})[<>{}]})>}})((( +([[[[[[{(((((<<>()>[[]<>])<{()()}[()<>]>){<[()[]]<<>[]>>(<<>{}}{{}[]})}))<<[([()()][[]()])[{()[]}([]())]][<([ +({{((((<{<[{[(<>{})][<()()>[[]()]]}]{({(<>())[[]()]}{<[][]><[][]>})}>}>{<<{{(({}())[<>])((()[])<[]<>>)}}[ +<(<[[[<<{([<<[[]<>]{<>[]}><{()()}([]<>)>>]{<{(<>())((){})}{[{}{}]<<>{}>}>})({([[{}{}]])[(<<>()>[ +{[(<{<[[{<{{(<()[]><<>{}>){[()<>]<{}<>>}}[[[<>{}][[]<>]]]}<[[({})<<>[]>]]{{<()<>><<>[]>}<{()[]}((){} +({[{(<<{[<{({[()()]{{}}}<({}{}){()()}>)}<{(({}{})[<>[]]){([]())[[][]]}}{(<[]<>)[()])[[<>()]]}>> +(({<<[<[<<<({<{}[]>({}{})}{<{}[]><{}[]>})>>(<<(<()()>{[]{}})({()<>}[()()])>[{[{}()][[]]}({<>()}}]>( +<<(<(<[{([{<[[<>{}]<{}<>>]>{<(()[])(()())><{<>[]}{()()}>}}([{({}<>){<>{}}}{{<>[]}[{}{}]}]([<[]()>((){}) +{[{([({[[<<<{{[]{}}<[]{}>}[<()[]><[]<>>]>([{{}[]][[][]]]{{(){}}({}[])})>(<{[<>{}](()<>)}>{[<[]()> +([[[<(<{[({<({()()}{<>()})((<>[])<[]<>>)>}<{<<{}()><<>>>}({{{}[]}}[(()[])[()<>]])>)<[<((()())[<>{}])( +{<(((<[((((({<[]>[[]{}]}))[(<[[][]][{}{}]>){{[<>()][[]<>]}([()()](<><>))}])){(<[{[<>()][{}()] +({([{<(((<{[<[{}[]]<<>>>({{}[]}[[]()])]([<(){}>])}{<[{<>[]}{[]<>}][<<><>){<>{}}]>[<((){})([]{})>[[{}{}][() +<[{[<[{[({{<(({}<>}[[]()])>({{[][]}}<[(){}](()[])>)}})([[{{<<>()>([]{})}[<<>{}>[<><>]]}]])] +{(<{([{[[({[(<[]{}>)({{}[]}{()()}))<{(()[])}[[()()]{{}[]}]>}<{<(()<>)>{{[]{}}({}())}}({{{}[]}{< +[<<{<{<<(<([<(<>()){[]()}>[({}<>)({}<>)]][(([]{}){()[]})[{{}()}(()())]])>{([{{()[]}<<>[]>}<{<>[ +{{[[(([([({([([]<>)(()())][{<>()}{(){}}])})])]([{{[<{{{}<>}(<>())>{[()()]<<>[]>}>[{(()){{}[ +[({({({(({{(<[()()][{}<>]>[[{}()]{(){}}])<[<<>[]>][<{}>(<>{})]>}(((({}())({}{}))){{(()[])<[][ +<(<(<[([<[{[<(()<>)<()[]>><(<>())([][])>]{<[{}<>]{{}()}>{[[]{}](<>{})}}}<[<([]{})><{{}<>}([]())>]>]> +<<<(<<[({{{<{({}<>)<()[]>}<[()<>]{(){}}>><{[[]()]<[]{}>}[({}{})<(){}>]>}(<[<<>[]>{()[]}]>)}[<{[<{}>[{}{ +{(<((<[{{[[[[<{}{}]<{}()>]][{({}{})<<>{}>}[<()[]>(()())]]]{([<[]()><[]{}>](<{}()>))({<<><>>(<>())}{ +{({{[[<[{<{{(<<>>)(<[]()>[()])}([({}<>)])}[[{[()<>][()[]]}]({<[]<>>{<>()}})]><<(<<{}()>{<>[] +{{{[{[<<{[[{[{{}<>}<[][]>]{(()[])<<>()>}}[<{()()}[(){}]><<()[]>>]]{<((<>{})({}()))[<(){}>([][])]>{({{}[]}< +<{{[<{({{{[<{{<>()}{{}()}}>]<{<[()[]](<><>)><[<>[]]{(){}}>}<([(){}]<{}{}>){(())}>>}[([{[[]<>][{}<>]}[{()}( +[<<[{{(({({<{{{}{}}{<>[]}}({[][]}{<>[]})>[{[{}<>][<>[]]}[({}[])[[]<>]]]})}))<(<[[<(({}()))[<<>()>{( +{<<([<{({[{{[({}[])<<>{}>]([()<>]<(){}>)}<([{}[]]({}{}))>}[<<<<>{}>[[][]]>{<<>[]><()>}>[<<<>><[][]> +[({(<({[<[{{[<<>[]>[()[]]]<<<>[]>>}}<{([{}[]](<>{}))[{<>}{{}<>}]}(<<[][]>>[{[]<>}])>]>[(<(({<>[]}{<><>}))(< +{<[[{{<<{(<(<([]<>)[[][]]>[([]{})[<>{}]])<[{<>()}{[]<>}]>>>[{(({()()}[<>[]]){({}())<[]<>>})}[(<{<> +{[[{{{<[<[{<(({}{})[{}()])[(<><>)({}())]>}{[<{[]{}}[()[]]>]<{[()()]<[]()>}<[[][]]>>}]>{{({{{()[]}{{}( +<{[<([{{[[<(<{<><>}[{}{}]><((){})>){[[()()][()[]]]{[[]<>]}}>]<<(<({}[])<{}[]>>{(<><>)<[]()>}){(< +""" +} diff --git a/day5.hs b/day5.hs new file mode 100644 index 0000000..2581951 --- /dev/null +++ b/day5.hs @@ -0,0 +1,554 @@ +example :: [((Int, Int), (Int, Int))] +example = [ + ((0,9), (5,9)), + ((8,0), (0,8)), + ((9,4), (3,4)), + ((2,2), (2,1)), + ((7,0), (7,4)), + ((6,4), (2,0)), + ((0,9), (2,9)), + ((3,4), (1,4)), + ((0,0), (8,8)), + ((5,5), (8,2)) + ] + +isHorizontalOrVertical :: ((Int, Int), (Int, Int)) -> Bool +isHorizontalOrVertical ((x1, y1), (x2, y2)) = x1 == x2 || y1 == y2 + +atPoint (x,y) p = minX p <= x && x <= maxX p && minY p <= y && y <= maxY p + +minX ((x1, _), (x2, _)) = min x1 x2 +minY ((_, y1), (_, y2)) = min y1 y2 + + +maxX ((x1, _), (x2, _)) = max x1 x2 +maxY ((_, y1), (_, y2)) = max y1 y2 + + +bounds :: (Ord b, Ord a) => [((a, b), (a, b))] -> ((a, b), (a, b)) +bounds input = ((x0, y0), (x1, y1)) + where + x0 = minimum $ map minX input + y0 = minimum $ map minY input + x1 = maximum $ map maxX input + y1 = maximum $ map maxY input + +grid ((x0, y0), (x1, y1)) = [ (x, y) | x <- [x0..x1], y <- [y0..y1] ] + +counts input = length $ filter (>= 2) intersectionCounts + where + intersectionCounts = map (\pos -> length $ filter (atPoint pos) input) gridIndexes + gridIndexes = grid $ bounds input + + +inputs :: [((Int, Int), (Int, Int))] +inputs = [ + ((911, 808), (324, 221)), + ((161, 890), (808, 243)), + ((174, 59), (174, 760)), + ((983, 983), (10, 10)), + ((321, 12), (870, 12)), + ((66, 936), (941, 61)), + ((670, 141), (670, 550)), + ((783, 935), (496, 648)), + ((973, 651), (635, 989)), + ((535, 47), (535, 154)), + ((355, 183), (754, 582)), + ((172, 111), (892, 111)), + ((353, 66), (907, 620)), + ((741, 960), (741, 805)), + ((113, 895), (946, 895)), + ((777, 280), (563, 280)), + ((679, 815), (626, 815)), + ((651, 848), (651, 673)), + ((205, 834), (205, 599)), + ((895, 118), (82, 931)), + ((685, 303), (93, 895)), + ((973, 38), (62, 949)), + ((867, 23), (867, 300)), + ((784, 947), (784, 47)), + ((96, 168), (755, 827)), + ((909, 321), (909, 716)), + ((59, 881), (692, 881)), + ((964, 19), (69, 914)), + ((752, 869), (67, 184)), + ((974, 877), (138, 41)), + ((432, 389), (137, 684)), + ((458, 822), (458, 402)), + ((818, 852), (308, 342)), + ((882, 484), (441, 925)), + ((82, 959), (976, 65)), + ((117, 487), (117, 429)), + ((214, 673), (429, 673)), + ((72, 955), (72, 829)), + ((587, 109), (587, 368)), + ((576, 17), (576, 872)), + ((685, 102), (685, 905)), + ((563, 394), (716, 394)), + ((966, 145), (150, 961)), + ((555, 582), (555, 385)), + ((453, 31), (453, 207)), + ((639, 815), (547, 723)), + ((431, 869), (431, 811)), + ((646, 938), (599, 938)), + ((215, 513), (900, 513)), + ((809, 82), (798, 82)), + ((768, 344), (244, 868)), + ((39, 962), (39, 601)), + ((675, 186), (61, 186)), + ((861, 967), (28, 967)), + ((860, 550), (538, 550)), + ((283, 740), (571, 740)), + ((72, 297), (72, 645)), + ((727, 801), (727, 526)), + ((799, 519), (799, 497)), + ((915, 24), (174, 765)), + ((795, 943), (136, 943)), + ((518, 971), (599, 971)), + ((594, 676), (594, 461)), + ((850, 799), (363, 799)), + ((958, 575), (958, 231)), + ((752, 576), (398, 576)), + ((891, 433), (398, 433)), + ((524, 126), (397, 126)), + ((10, 890), (796, 104)), + ((57, 228), (168, 228)), + ((168, 521), (338, 691)), + ((230, 83), (777, 83)), + ((865, 677), (640, 452)), + ((866, 821), (825, 821)), + ((17, 143), (17, 596)), + ((113, 916), (113, 601)), + ((268, 187), (551, 470)), + ((794, 167), (220, 167)), + ((459, 17), (459, 931)), + ((211, 31), (526, 31)), + ((680, 57), (756, 57)), + ((926, 190), (926, 800)), + ((85, 284), (63, 284)), + ((44, 988), (44, 701)), + ((110, 941), (176, 941)), + ((480, 163), (480, 112)), + ((574, 538), (574, 371)), + ((584, 473), (69, 473)), + ((303, 621), (303, 380)), + ((762, 652), (762, 89)), + ((286, 195), (276, 185)), + ((957, 87), (217, 827)), + ((561, 858), (561, 437)), + ((384, 55), (81, 55)), + ((19, 977), (981, 15)), + ((454, 747), (938, 263)), + ((425, 836), (425, 617)), + ((860, 135), (775, 50)), + ((633, 131), (633, 651)), + ((904, 912), (242, 250)), + ((880, 177), (480, 577)), + ((470, 162), (964, 656)), + ((585, 376), (585, 470)), + ((696, 760), (594, 862)), + ((534, 225), (534, 717)), + ((258, 816), (258, 847)), + ((990, 244), (990, 93)), + ((463, 462), (463, 533)), + ((434, 928), (537, 825)), + ((813, 734), (533, 734)), + ((498, 673), (395, 673)), + ((564, 312), (55, 312)), + ((280, 550), (939, 550)), + ((591, 247), (396, 52)), + ((127, 516), (127, 235)), + ((850, 425), (552, 127)), + ((894, 428), (894, 598)), + ((366, 960), (592, 960)), + ((579, 488), (170, 488)), + ((775, 92), (775, 586)), + ((49, 909), (930, 28)), + ((856, 113), (284, 685)), + ((263, 175), (120, 175)), + ((332, 592), (276, 592)), + ((920, 157), (141, 157)), + ((349, 776), (316, 776)), + ((187, 863), (279, 863)), + ((218, 872), (83, 872)), + ((465, 430), (410, 430)), + ((710, 218), (857, 218)), + ((797, 314), (184, 314)), + ((387, 327), (49, 665)), + ((950, 812), (205, 67)), + ((803, 133), (803, 682)), + ((125, 972), (545, 552)), + ((353, 901), (840, 414)), + ((936, 843), (202, 109)), + ((11, 904), (856, 59)), + ((725, 757), (954, 986)), + ((227, 697), (345, 697)), + ((187, 520), (187, 441)), + ((860, 262), (135, 987)), + ((700, 95), (976, 371)), + ((86, 946), (869, 163)), + ((898, 806), (461, 806)), + ((717, 796), (717, 195)), + ((882, 127), (835, 127)), + ((133, 48), (133, 191)), + ((521, 51), (521, 927)), + ((384, 806), (957, 233)), + ((570, 139), (570, 842)), + ((949, 819), (949, 350)), + ((592, 230), (283, 230)), + ((315, 856), (741, 856)), + ((870, 674), (549, 353)), + ((857, 306), (857, 889)), + ((428, 217), (267, 217)), + ((47, 93), (898, 944)), + ((636, 238), (665, 238)), + ((202, 910), (202, 737)), + ((246, 432), (617, 803)), + ((985, 24), (48, 961)), + ((965, 876), (956, 867)), + ((618, 650), (810, 458)), + ((292, 356), (575, 356)), + ((394, 585), (910, 585)), + ((137, 453), (137, 178)), + ((509, 737), (509, 665)), + ((193, 350), (531, 688)), + ((805, 219), (107, 219)), + ((975, 506), (907, 506)), + ((435, 303), (435, 380)), + ((344, 83), (344, 224)), + ((47, 66), (47, 115)), + ((570, 516), (857, 516)), + ((162, 91), (926, 91)), + ((759, 417), (759, 460)), + ((445, 942), (445, 699)), + ((421, 340), (421, 743)), + ((590, 590), (434, 434)), + ((453, 38), (453, 327)), + ((865, 134), (865, 773)), + ((842, 609), (18, 609)), + ((662, 282), (62, 882)), + ((489, 32), (344, 32)), + ((135, 496), (93, 454)), + ((552, 211), (421, 211)), + ((620, 678), (642, 678)), + ((782, 158), (585, 355)), + ((733, 509), (733, 574)), + ((932, 383), (369, 946)), + ((843, 705), (843, 725)), + ((747, 414), (676, 343)), + ((294, 218), (962, 886)), + ((844, 175), (844, 420)), + ((255, 489), (531, 213)), + ((555, 532), (821, 532)), + ((533, 15), (533, 161)), + ((631, 778), (631, 401)), + ((75, 282), (468, 282)), + ((903, 838), (903, 957)), + ((46, 293), (543, 790)), + ((30, 834), (30, 948)), + ((591, 720), (591, 965)), + ((624, 36), (339, 36)), + ((425, 323), (425, 442)), + ((234, 939), (234, 963)), + ((482, 912), (968, 912)), + ((228, 614), (189, 614)), + ((969, 472), (969, 692)), + ((871, 494), (871, 172)), + ((101, 624), (848, 624)), + ((424, 918), (69, 563)), + ((929, 671), (93, 671)), + ((81, 187), (707, 813)), + ((348, 923), (348, 924)), + ((921, 524), (921, 828)), + ((678, 454), (678, 364)), + ((904, 227), (904, 596)), + ((163, 344), (609, 790)), + ((206, 180), (206, 59)), + ((145, 519), (145, 717)), + ((317, 679), (317, 417)), + ((503, 724), (221, 724)), + ((353, 448), (413, 448)), + ((363, 643), (837, 643)), + ((594, 54), (359, 54)), + ((866, 117), (45, 938)), + ((939, 210), (284, 865)), + ((410, 556), (410, 801)), + ((905, 111), (673, 111)), + ((983, 167), (574, 167)), + ((595, 758), (97, 758)), + ((785, 10), (437, 10)), + ((517, 414), (517, 734)), + ((691, 567), (186, 62)), + ((842, 51), (31, 862)), + ((36, 199), (282, 199)), + ((864, 758), (864, 610)), + ((639, 918), (951, 918)), + ((245, 516), (245, 474)), + ((951, 203), (557, 203)), + ((176, 728), (176, 171)), + ((322, 217), (387, 217)), + ((149, 208), (836, 895)), + ((661, 298), (609, 298)), + ((46, 47), (981, 982)), + ((769, 45), (769, 610)), + ((988, 932), (988, 459)), + ((901, 97), (901, 908)), + ((195, 395), (121, 395)), + ((197, 403), (327, 533)), + ((159, 456), (857, 456)), + ((480, 981), (881, 580)), + ((86, 958), (962, 82)), + ((375, 198), (763, 198)), + ((950, 381), (341, 381)), + ((504, 679), (504, 598)), + ((756, 659), (680, 583)), + ((146, 328), (886, 328)), + ((930, 412), (492, 850)), + ((954, 54), (954, 940)), + ((790, 498), (790, 305)), + ((83, 270), (83, 242)), + ((939, 268), (939, 563)), + ((423, 756), (916, 263)), + ((583, 756), (583, 34)), + ((957, 639), (614, 639)), + ((484, 523), (521, 560)), + ((497, 809), (497, 419)), + ((76, 17), (979, 920)), + ((49, 39), (943, 933)), + ((110, 289), (110, 247)), + ((874, 868), (874, 172)), + ((576, 127), (53, 650)), + ((871, 879), (12, 20)), + ((436, 711), (592, 711)), + ((132, 285), (225, 285)), + ((245, 147), (514, 147)), + ((158, 882), (956, 84)), + ((21, 984), (937, 68)), + ((42, 275), (219, 275)), + ((877, 143), (889, 143)), + ((593, 841), (508, 756)), + ((414, 289), (132, 289)), + ((687, 655), (767, 655)), + ((453, 981), (459, 987)), + ((635, 433), (635, 324)), + ((671, 347), (170, 848)), + ((412, 579), (915, 579)), + ((269, 677), (269, 596)), + ((587, 121), (367, 341)), + ((153, 883), (153, 709)), + ((524, 580), (508, 580)), + ((541, 232), (651, 232)), + ((93, 948), (284, 757)), + ((168, 745), (872, 41)), + ((831, 657), (925, 563)), + ((908, 389), (442, 389)), + ((462, 445), (234, 445)), + ((735, 493), (895, 493)), + ((274, 624), (296, 646)), + ((153, 130), (153, 160)), + ((466, 214), (466, 769)), + ((474, 499), (686, 711)), + ((540, 428), (788, 676)), + ((858, 215), (959, 215)), + ((788, 91), (788, 410)), + ((552, 505), (988, 505)), + ((978, 312), (978, 202)), + ((108, 321), (616, 829)), + ((903, 359), (903, 770)), + ((480, 331), (480, 769)), + ((503, 842), (34, 842)), + ((613, 732), (323, 442)), + ((767, 949), (654, 949)), + ((514, 589), (386, 589)), + ((38, 554), (284, 308)), + ((689, 268), (689, 711)), + ((860, 66), (190, 736)), + ((253, 865), (622, 865)), + ((87, 658), (698, 47)), + ((506, 892), (829, 569)), + ((680, 910), (594, 824)), + ((824, 603), (958, 603)), + ((576, 802), (562, 802)), + ((67, 27), (67, 489)), + ((969, 461), (517, 913)), + ((674, 763), (674, 226)), + ((223, 955), (218, 955)), + ((147, 540), (569, 962)), + ((455, 703), (596, 703)), + ((746, 899), (746, 403)), + ((516, 476), (756, 476)), + ((897, 674), (373, 150)), + ((120, 395), (120, 49)), + ((722, 443), (722, 244)), + ((724, 924), (724, 39)), + ((809, 930), (109, 930)), + ((822, 816), (874, 816)), + ((796, 539), (895, 539)), + ((340, 88), (560, 88)), + ((223, 158), (593, 158)), + ((779, 977), (856, 900)), + ((617, 461), (973, 817)), + ((515, 62), (515, 140)), + ((12, 586), (724, 586)), + ((870, 50), (391, 50)), + ((308, 123), (308, 696)), + ((119, 164), (863, 908)), + ((755, 599), (448, 599)), + ((129, 526), (633, 526)), + ((478, 668), (102, 668)), + ((237, 637), (237, 743)), + ((270, 102), (72, 300)), + ((115, 470), (115, 427)), + ((948, 233), (948, 731)), + ((983, 135), (468, 650)), + ((748, 439), (748, 642)), + ((62, 862), (352, 572)), + ((765, 901), (660, 901)), + ((917, 807), (917, 587)), + ((55, 81), (116, 81)), + ((954, 972), (102, 120)), + ((340, 503), (294, 549)), + ((970, 661), (522, 213)), + ((618, 92), (618, 247)), + ((688, 965), (965, 965)), + ((94, 241), (94, 292)), + ((15, 132), (15, 492)), + ((979, 927), (488, 927)), + ((509, 26), (984, 26)), + ((840, 530), (840, 95)), + ((55, 956), (849, 162)), + ((297, 297), (297, 472)), + ((338, 780), (369, 780)), + ((487, 292), (37, 292)), + ((122, 117), (206, 201)), + ((66, 807), (564, 309)), + ((643, 242), (906, 242)), + ((909, 833), (909, 441)), + ((129, 128), (818, 817)), + ((406, 42), (406, 297)), + ((53, 20), (967, 934)), + ((235, 285), (601, 285)), + ((275, 625), (275, 539)), + ((199, 732), (430, 963)), + ((639, 187), (639, 265)), + ((549, 740), (549, 824)), + ((603, 140), (603, 748)), + ((35, 455), (176, 455)), + ((888, 611), (888, 271)), + ((134, 154), (484, 154)), + ((694, 820), (694, 814)), + ((535, 584), (187, 932)), + ((642, 510), (642, 249)), + ((191, 886), (268, 886)), + ((918, 353), (881, 390)), + ((977, 13), (343, 13)), + ((380, 243), (271, 134)), + ((410, 758), (410, 670)), + ((613, 551), (519, 645)), + ((963, 84), (124, 923)), + ((702, 252), (821, 252)), + ((405, 237), (405, 22)), + ((21, 139), (21, 510)), + ((548, 499), (132, 499)), + ((196, 104), (196, 680)), + ((739, 145), (476, 145)), + ((751, 746), (91, 746)), + ((975, 628), (975, 847)), + ((935, 520), (935, 450)), + ((803, 372), (803, 393)), + ((872, 77), (872, 373)), + ((339, 130), (339, 103)), + ((226, 886), (226, 45)), + ((794, 647), (794, 257)), + ((90, 922), (889, 123)), + ((615, 971), (615, 574)), + ((26, 278), (26, 719)), + ((838, 88), (806, 88)), + ((263, 691), (804, 150)), + ((309, 721), (910, 721)), + ((510, 496), (960, 946)), + ((195, 236), (46, 236)), + ((610, 143), (610, 610)), + ((891, 412), (891, 268)), + ((714, 21), (156, 579)), + ((320, 935), (320, 96)), + ((240, 782), (449, 782)), + ((754, 472), (48, 472)), + ((105, 481), (529, 57)), + ((451, 301), (451, 965)), + ((796, 638), (796, 185)), + ((908, 553), (771, 553)), + ((98, 543), (490, 935)), + ((481, 159), (762, 159)), + ((593, 527), (419, 353)), + ((86, 391), (216, 521)), + ((260, 716), (42, 716)), + ((734, 538), (375, 179)), + ((24, 974), (975, 23)), + ((402, 466), (787, 851)), + ((344, 409), (262, 327)), + ((803, 443), (685, 443)), + ((986, 152), (249, 152)), + ((125, 738), (90, 773)), + ((184, 772), (184, 746)), + ((729, 829), (729, 340)), + ((226, 527), (226, 375)), + ((936, 231), (222, 945)), + ((254, 333), (254, 167)), + ((451, 234), (451, 273)), + ((915, 790), (568, 443)), + ((869, 794), (504, 429)), + ((11, 878), (836, 53)), + ((821, 231), (522, 530)), + ((285, 419), (732, 866)), + ((191, 272), (191, 679)), + ((41, 651), (225, 651)), + ((30, 13), (879, 862)), + ((980, 488), (20, 488)), + ((27, 187), (27, 348)), + ((53, 238), (53, 514)), + ((778, 306), (379, 705)), + ((425, 399), (425, 60)), + ((162, 859), (57, 859)), + ((652, 926), (652, 589)), + ((962, 489), (555, 896)), + ((197, 378), (436, 617)), + ((310, 190), (310, 760)), + ((678, 20), (678, 713)), + ((390, 653), (985, 58)), + ((938, 351), (656, 69)), + ((881, 39), (18, 902)), + ((313, 681), (323, 681)), + ((910, 907), (288, 907)), + ((739, 977), (739, 132)), + ((856, 479), (154, 479)), + ((893, 785), (761, 785)), + ((405, 247), (405, 901)), + ((58, 933), (808, 183)), + ((643, 156), (676, 189)), + ((149, 773), (357, 773)), + ((479, 518), (434, 518)), + ((389, 518), (556, 685)), + ((858, 449), (533, 774)), + ((503, 133), (409, 133)), + ((340, 315), (219, 194)), + ((183, 701), (183, 242)), + ((810, 151), (195, 151)), + ((446, 686), (446, 912)), + ((968, 482), (49, 482)), + ((203, 20), (203, 667)), + ((493, 516), (647, 516)), + ((900, 91), (634, 91)), + ((660, 554), (119, 13)), + ((964, 864), (964, 919)), + ((871, 293), (344, 293)), + ((895, 258), (972, 258)) + ] + +{- +5167 + + +-} + + diff --git a/day5part2.swift b/day5part2.swift new file mode 100644 index 0000000..7964479 --- /dev/null +++ b/day5part2.swift @@ -0,0 +1,584 @@ +import CoreGraphics +import ImageIO +import Foundation +import UniformTypeIdentifiers + +@main +struct Day5Part2 { + let input: [(Point, Point)] = [ + ((911, 808), (324, 221)), + ((161, 890), (808, 243)), + ((174, 59), (174, 760)), + ((983, 983), (10, 10)), + ((321, 12), (870, 12)), + ((66, 936), (941, 61)), + ((670, 141), (670, 550)), + ((783, 935), (496, 648)), + ((973, 651), (635, 989)), + ((535, 47), (535, 154)), + ((355, 183), (754, 582)), + ((172, 111), (892, 111)), + ((353, 66), (907, 620)), + ((741, 960), (741, 805)), + ((113, 895), (946, 895)), + ((777, 280), (563, 280)), + ((679, 815), (626, 815)), + ((651, 848), (651, 673)), + ((205, 834), (205, 599)), + ((895, 118), (82, 931)), + ((685, 303), (93, 895)), + ((973, 38), (62, 949)), + ((867, 23), (867, 300)), + ((784, 947), (784, 47)), + ((96, 168), (755, 827)), + ((909, 321), (909, 716)), + ((59, 881), (692, 881)), + ((964, 19), (69, 914)), + ((752, 869), (67, 184)), + ((974, 877), (138, 41)), + ((432, 389), (137, 684)), + ((458, 822), (458, 402)), + ((818, 852), (308, 342)), + ((882, 484), (441, 925)), + ((82, 959), (976, 65)), + ((117, 487), (117, 429)), + ((214, 673), (429, 673)), + ((72, 955), (72, 829)), + ((587, 109), (587, 368)), + ((576, 17), (576, 872)), + ((685, 102), (685, 905)), + ((563, 394), (716, 394)), + ((966, 145), (150, 961)), + ((555, 582), (555, 385)), + ((453, 31), (453, 207)), + ((639, 815), (547, 723)), + ((431, 869), (431, 811)), + ((646, 938), (599, 938)), + ((215, 513), (900, 513)), + ((809, 82), (798, 82)), + ((768, 344), (244, 868)), + ((39, 962), (39, 601)), + ((675, 186), (61, 186)), + ((861, 967), (28, 967)), + ((860, 550), (538, 550)), + ((283, 740), (571, 740)), + ((72, 297), (72, 645)), + ((727, 801), (727, 526)), + ((799, 519), (799, 497)), + ((915, 24), (174, 765)), + ((795, 943), (136, 943)), + ((518, 971), (599, 971)), + ((594, 676), (594, 461)), + ((850, 799), (363, 799)), + ((958, 575), (958, 231)), + ((752, 576), (398, 576)), + ((891, 433), (398, 433)), + ((524, 126), (397, 126)), + ((10, 890), (796, 104)), + ((57, 228), (168, 228)), + ((168, 521), (338, 691)), + ((230, 83), (777, 83)), + ((865, 677), (640, 452)), + ((866, 821), (825, 821)), + ((17, 143), (17, 596)), + ((113, 916), (113, 601)), + ((268, 187), (551, 470)), + ((794, 167), (220, 167)), + ((459, 17), (459, 931)), + ((211, 31), (526, 31)), + ((680, 57), (756, 57)), + ((926, 190), (926, 800)), + ((85, 284), (63, 284)), + ((44, 988), (44, 701)), + ((110, 941), (176, 941)), + ((480, 163), (480, 112)), + ((574, 538), (574, 371)), + ((584, 473), (69, 473)), + ((303, 621), (303, 380)), + ((762, 652), (762, 89)), + ((286, 195), (276, 185)), + ((957, 87), (217, 827)), + ((561, 858), (561, 437)), + ((384, 55), (81, 55)), + ((19, 977), (981, 15)), + ((454, 747), (938, 263)), + ((425, 836), (425, 617)), + ((860, 135), (775, 50)), + ((633, 131), (633, 651)), + ((904, 912), (242, 250)), + ((880, 177), (480, 577)), + ((470, 162), (964, 656)), + ((585, 376), (585, 470)), + ((696, 760), (594, 862)), + ((534, 225), (534, 717)), + ((258, 816), (258, 847)), + ((990, 244), (990, 93)), + ((463, 462), (463, 533)), + ((434, 928), (537, 825)), + ((813, 734), (533, 734)), + ((498, 673), (395, 673)), + ((564, 312), (55, 312)), + ((280, 550), (939, 550)), + ((591, 247), (396, 52)), + ((127, 516), (127, 235)), + ((850, 425), (552, 127)), + ((894, 428), (894, 598)), + ((366, 960), (592, 960)), + ((579, 488), (170, 488)), + ((775, 92), (775, 586)), + ((49, 909), (930, 28)), + ((856, 113), (284, 685)), + ((263, 175), (120, 175)), + ((332, 592), (276, 592)), + ((920, 157), (141, 157)), + ((349, 776), (316, 776)), + ((187, 863), (279, 863)), + ((218, 872), (83, 872)), + ((465, 430), (410, 430)), + ((710, 218), (857, 218)), + ((797, 314), (184, 314)), + ((387, 327), (49, 665)), + ((950, 812), (205, 67)), + ((803, 133), (803, 682)), + ((125, 972), (545, 552)), + ((353, 901), (840, 414)), + ((936, 843), (202, 109)), + ((11, 904), (856, 59)), + ((725, 757), (954, 986)), + ((227, 697), (345, 697)), + ((187, 520), (187, 441)), + ((860, 262), (135, 987)), + ((700, 95), (976, 371)), + ((86, 946), (869, 163)), + ((898, 806), (461, 806)), + ((717, 796), (717, 195)), + ((882, 127), (835, 127)), + ((133, 48), (133, 191)), + ((521, 51), (521, 927)), + ((384, 806), (957, 233)), + ((570, 139), (570, 842)), + ((949, 819), (949, 350)), + ((592, 230), (283, 230)), + ((315, 856), (741, 856)), + ((870, 674), (549, 353)), + ((857, 306), (857, 889)), + ((428, 217), (267, 217)), + ((47, 93), (898, 944)), + ((636, 238), (665, 238)), + ((202, 910), (202, 737)), + ((246, 432), (617, 803)), + ((985, 24), (48, 961)), + ((965, 876), (956, 867)), + ((618, 650), (810, 458)), + ((292, 356), (575, 356)), + ((394, 585), (910, 585)), + ((137, 453), (137, 178)), + ((509, 737), (509, 665)), + ((193, 350), (531, 688)), + ((805, 219), (107, 219)), + ((975, 506), (907, 506)), + ((435, 303), (435, 380)), + ((344, 83), (344, 224)), + ((47, 66), (47, 115)), + ((570, 516), (857, 516)), + ((162, 91), (926, 91)), + ((759, 417), (759, 460)), + ((445, 942), (445, 699)), + ((421, 340), (421, 743)), + ((590, 590), (434, 434)), + ((453, 38), (453, 327)), + ((865, 134), (865, 773)), + ((842, 609), (18, 609)), + ((662, 282), (62, 882)), + ((489, 32), (344, 32)), + ((135, 496), (93, 454)), + ((552, 211), (421, 211)), + ((620, 678), (642, 678)), + ((782, 158), (585, 355)), + ((733, 509), (733, 574)), + ((932, 383), (369, 946)), + ((843, 705), (843, 725)), + ((747, 414), (676, 343)), + ((294, 218), (962, 886)), + ((844, 175), (844, 420)), + ((255, 489), (531, 213)), + ((555, 532), (821, 532)), + ((533, 15), (533, 161)), + ((631, 778), (631, 401)), + ((75, 282), (468, 282)), + ((903, 838), (903, 957)), + ((46, 293), (543, 790)), + ((30, 834), (30, 948)), + ((591, 720), (591, 965)), + ((624, 36), (339, 36)), + ((425, 323), (425, 442)), + ((234, 939), (234, 963)), + ((482, 912), (968, 912)), + ((228, 614), (189, 614)), + ((969, 472), (969, 692)), + ((871, 494), (871, 172)), + ((101, 624), (848, 624)), + ((424, 918), (69, 563)), + ((929, 671), (93, 671)), + ((81, 187), (707, 813)), + ((348, 923), (348, 924)), + ((921, 524), (921, 828)), + ((678, 454), (678, 364)), + ((904, 227), (904, 596)), + ((163, 344), (609, 790)), + ((206, 180), (206, 59)), + ((145, 519), (145, 717)), + ((317, 679), (317, 417)), + ((503, 724), (221, 724)), + ((353, 448), (413, 448)), + ((363, 643), (837, 643)), + ((594, 54), (359, 54)), + ((866, 117), (45, 938)), + ((939, 210), (284, 865)), + ((410, 556), (410, 801)), + ((905, 111), (673, 111)), + ((983, 167), (574, 167)), + ((595, 758), (97, 758)), + ((785, 10), (437, 10)), + ((517, 414), (517, 734)), + ((691, 567), (186, 62)), + ((842, 51), (31, 862)), + ((36, 199), (282, 199)), + ((864, 758), (864, 610)), + ((639, 918), (951, 918)), + ((245, 516), (245, 474)), + ((951, 203), (557, 203)), + ((176, 728), (176, 171)), + ((322, 217), (387, 217)), + ((149, 208), (836, 895)), + ((661, 298), (609, 298)), + ((46, 47), (981, 982)), + ((769, 45), (769, 610)), + ((988, 932), (988, 459)), + ((901, 97), (901, 908)), + ((195, 395), (121, 395)), + ((197, 403), (327, 533)), + ((159, 456), (857, 456)), + ((480, 981), (881, 580)), + ((86, 958), (962, 82)), + ((375, 198), (763, 198)), + ((950, 381), (341, 381)), + ((504, 679), (504, 598)), + ((756, 659), (680, 583)), + ((146, 328), (886, 328)), + ((930, 412), (492, 850)), + ((954, 54), (954, 940)), + ((790, 498), (790, 305)), + ((83, 270), (83, 242)), + ((939, 268), (939, 563)), + ((423, 756), (916, 263)), + ((583, 756), (583, 34)), + ((957, 639), (614, 639)), + ((484, 523), (521, 560)), + ((497, 809), (497, 419)), + ((76, 17), (979, 920)), + ((49, 39), (943, 933)), + ((110, 289), (110, 247)), + ((874, 868), (874, 172)), + ((576, 127), (53, 650)), + ((871, 879), (12, 20)), + ((436, 711), (592, 711)), + ((132, 285), (225, 285)), + ((245, 147), (514, 147)), + ((158, 882), (956, 84)), + ((21, 984), (937, 68)), + ((42, 275), (219, 275)), + ((877, 143), (889, 143)), + ((593, 841), (508, 756)), + ((414, 289), (132, 289)), + ((687, 655), (767, 655)), + ((453, 981), (459, 987)), + ((635, 433), (635, 324)), + ((671, 347), (170, 848)), + ((412, 579), (915, 579)), + ((269, 677), (269, 596)), + ((587, 121), (367, 341)), + ((153, 883), (153, 709)), + ((524, 580), (508, 580)), + ((541, 232), (651, 232)), + ((93, 948), (284, 757)), + ((168, 745), (872, 41)), + ((831, 657), (925, 563)), + ((908, 389), (442, 389)), + ((462, 445), (234, 445)), + ((735, 493), (895, 493)), + ((274, 624), (296, 646)), + ((153, 130), (153, 160)), + ((466, 214), (466, 769)), + ((474, 499), (686, 711)), + ((540, 428), (788, 676)), + ((858, 215), (959, 215)), + ((788, 91), (788, 410)), + ((552, 505), (988, 505)), + ((978, 312), (978, 202)), + ((108, 321), (616, 829)), + ((903, 359), (903, 770)), + ((480, 331), (480, 769)), + ((503, 842), (34, 842)), + ((613, 732), (323, 442)), + ((767, 949), (654, 949)), + ((514, 589), (386, 589)), + ((38, 554), (284, 308)), + ((689, 268), (689, 711)), + ((860, 66), (190, 736)), + ((253, 865), (622, 865)), + ((87, 658), (698, 47)), + ((506, 892), (829, 569)), + ((680, 910), (594, 824)), + ((824, 603), (958, 603)), + ((576, 802), (562, 802)), + ((67, 27), (67, 489)), + ((969, 461), (517, 913)), + ((674, 763), (674, 226)), + ((223, 955), (218, 955)), + ((147, 540), (569, 962)), + ((455, 703), (596, 703)), + ((746, 899), (746, 403)), + ((516, 476), (756, 476)), + ((897, 674), (373, 150)), + ((120, 395), (120, 49)), + ((722, 443), (722, 244)), + ((724, 924), (724, 39)), + ((809, 930), (109, 930)), + ((822, 816), (874, 816)), + ((796, 539), (895, 539)), + ((340, 88), (560, 88)), + ((223, 158), (593, 158)), + ((779, 977), (856, 900)), + ((617, 461), (973, 817)), + ((515, 62), (515, 140)), + ((12, 586), (724, 586)), + ((870, 50), (391, 50)), + ((308, 123), (308, 696)), + ((119, 164), (863, 908)), + ((755, 599), (448, 599)), + ((129, 526), (633, 526)), + ((478, 668), (102, 668)), + ((237, 637), (237, 743)), + ((270, 102), (72, 300)), + ((115, 470), (115, 427)), + ((948, 233), (948, 731)), + ((983, 135), (468, 650)), + ((748, 439), (748, 642)), + ((62, 862), (352, 572)), + ((765, 901), (660, 901)), + ((917, 807), (917, 587)), + ((55, 81), (116, 81)), + ((954, 972), (102, 120)), + ((340, 503), (294, 549)), + ((970, 661), (522, 213)), + ((618, 92), (618, 247)), + ((688, 965), (965, 965)), + ((94, 241), (94, 292)), + ((15, 132), (15, 492)), + ((979, 927), (488, 927)), + ((509, 26), (984, 26)), + ((840, 530), (840, 95)), + ((55, 956), (849, 162)), + ((297, 297), (297, 472)), + ((338, 780), (369, 780)), + ((487, 292), (37, 292)), + ((122, 117), (206, 201)), + ((66, 807), (564, 309)), + ((643, 242), (906, 242)), + ((909, 833), (909, 441)), + ((129, 128), (818, 817)), + ((406, 42), (406, 297)), + ((53, 20), (967, 934)), + ((235, 285), (601, 285)), + ((275, 625), (275, 539)), + ((199, 732), (430, 963)), + ((639, 187), (639, 265)), + ((549, 740), (549, 824)), + ((603, 140), (603, 748)), + ((35, 455), (176, 455)), + ((888, 611), (888, 271)), + ((134, 154), (484, 154)), + ((694, 820), (694, 814)), + ((535, 584), (187, 932)), + ((642, 510), (642, 249)), + ((191, 886), (268, 886)), + ((918, 353), (881, 390)), + ((977, 13), (343, 13)), + ((380, 243), (271, 134)), + ((410, 758), (410, 670)), + ((613, 551), (519, 645)), + ((963, 84), (124, 923)), + ((702, 252), (821, 252)), + ((405, 237), (405, 22)), + ((21, 139), (21, 510)), + ((548, 499), (132, 499)), + ((196, 104), (196, 680)), + ((739, 145), (476, 145)), + ((751, 746), (91, 746)), + ((975, 628), (975, 847)), + ((935, 520), (935, 450)), + ((803, 372), (803, 393)), + ((872, 77), (872, 373)), + ((339, 130), (339, 103)), + ((226, 886), (226, 45)), + ((794, 647), (794, 257)), + ((90, 922), (889, 123)), + ((615, 971), (615, 574)), + ((26, 278), (26, 719)), + ((838, 88), (806, 88)), + ((263, 691), (804, 150)), + ((309, 721), (910, 721)), + ((510, 496), (960, 946)), + ((195, 236), (46, 236)), + ((610, 143), (610, 610)), + ((891, 412), (891, 268)), + ((714, 21), (156, 579)), + ((320, 935), (320, 96)), + ((240, 782), (449, 782)), + ((754, 472), (48, 472)), + ((105, 481), (529, 57)), + ((451, 301), (451, 965)), + ((796, 638), (796, 185)), + ((908, 553), (771, 553)), + ((98, 543), (490, 935)), + ((481, 159), (762, 159)), + ((593, 527), (419, 353)), + ((86, 391), (216, 521)), + ((260, 716), (42, 716)), + ((734, 538), (375, 179)), + ((24, 974), (975, 23)), + ((402, 466), (787, 851)), + ((344, 409), (262, 327)), + ((803, 443), (685, 443)), + ((986, 152), (249, 152)), + ((125, 738), (90, 773)), + ((184, 772), (184, 746)), + ((729, 829), (729, 340)), + ((226, 527), (226, 375)), + ((936, 231), (222, 945)), + ((254, 333), (254, 167)), + ((451, 234), (451, 273)), + ((915, 790), (568, 443)), + ((869, 794), (504, 429)), + ((11, 878), (836, 53)), + ((821, 231), (522, 530)), + ((285, 419), (732, 866)), + ((191, 272), (191, 679)), + ((41, 651), (225, 651)), + ((30, 13), (879, 862)), + ((980, 488), (20, 488)), + ((27, 187), (27, 348)), + ((53, 238), (53, 514)), + ((778, 306), (379, 705)), + ((425, 399), (425, 60)), + ((162, 859), (57, 859)), + ((652, 926), (652, 589)), + ((962, 489), (555, 896)), + ((197, 378), (436, 617)), + ((310, 190), (310, 760)), + ((678, 20), (678, 713)), + ((390, 653), (985, 58)), + ((938, 351), (656, 69)), + ((881, 39), (18, 902)), + ((313, 681), (323, 681)), + ((910, 907), (288, 907)), + ((739, 977), (739, 132)), + ((856, 479), (154, 479)), + ((893, 785), (761, 785)), + ((405, 247), (405, 901)), + ((58, 933), (808, 183)), + ((643, 156), (676, 189)), + ((149, 773), (357, 773)), + ((479, 518), (434, 518)), + ((389, 518), (556, 685)), + ((858, 449), (533, 774)), + ((503, 133), (409, 133)), + ((340, 315), (219, 194)), + ((183, 701), (183, 242)), + ((810, 151), (195, 151)), + ((446, 686), (446, 912)), + ((968, 482), (49, 482)), + ((203, 20), (203, 667)), + ((493, 516), (647, 516)), + ((900, 91), (634, 91)), + ((660, 554), (119, 13)), + ((964, 864), (964, 919)), + ((871, 293), (344, 293)), + ((895, 258), (972, 258)), + ] + + func run() { + var minX = 0 + var maxX = 0 + var minY = 0 + var maxY = 0 + for line in input { + minX = min(minX, line.0.x, line.1.x) + maxX = max(maxX, line.0.x, line.1.x) + minY = min(minY, line.0.y, line.1.y) + maxY = max(maxY, line.0.y, line.1.y) + } + + var grid = Grid(minX: minX, minY: minY, maxX: maxX, maxY: maxY) + for line in input { grid.line(line) } + + + print(grid.grid.lazy.filter { $0 >= 2 }.count) + + let data = CGDataProvider(dataInfo: nil, data: &grid.grid, size: grid.grid.count, releaseData: {_,_,_ in }) + let image = CGImage(width: maxX - minX, height: maxY - minY, bitsPerComponent: 8, bitsPerPixel: 8, bytesPerRow: maxX - minX, space: .init(name: CGColorSpace.linearGray)!, bitmapInfo: [], provider: data!, decode: nil, shouldInterpolate: false, intent: .defaultIntent) + + let url = URL(fileURLWithPath: "/Users/sven/test.png") + let dest = CGImageDestinationCreateWithURL(url as CFURL, UTType.png.identifier as CFString, 1, nil) + CGImageDestinationAddImage(dest!, image!, nil) + CGImageDestinationFinalize(dest!) + } + + struct Grid { + let minX: Int + let minY: Int + let width: Int + var grid: [UInt8] + + init(minX: Int, minY: Int, maxX: Int, maxY: Int) { + self.minX = minX + self.minY = minY + width = maxX - minX + 1 + let height = maxY - minY + 1 + + grid = Array(repeating: 0, count: width * height) + } + + subscript(x: Int, y:Int) -> UInt8 { + get { grid[(y - minY) * width + x - minX] } + set { grid[(y - minY) * width + x - minX] = newValue } + } + + mutating func line(_ line: (Point, Point)) { + let ((minX, minY), (maxX, maxY)) = line + + var x = minX + var y = minY + + let xStep = minX == maxX ? 0 : minX < maxX ? 1 : -1 + let yStep = minY == maxY ? 0 : minY < maxY ? 1 : -1 + + while true { + self[x, y] += 1 + if x == maxX && y == maxY { + break + } + x += xStep + y += yStep + } + + } + } + + typealias Point = (x: Int, y: Int) + + static func main() { + Self().run() + } +} diff --git a/day6.swift b/day6.swift new file mode 100644 index 0000000..8e3a8a8 --- /dev/null +++ b/day6.swift @@ -0,0 +1,36 @@ +@main +struct Day6 { + let input = [3,3,2,1,4,1,1,2,3,1,1,2,1,2,1,1,1,1,1,1,4,1,1,5,2,1,1,2,1,1,1,3,5,1,5,5,1,1,1,1,3,1,1,3,2,1,1,1,1,1,1,4,1,1,1,1,1,1,1,4,1,3,3,1,1,3,1,3,1,2,1,3,1,1,4,1,2,4,4,5,1,1,1,1,1,1,4,1,5,1,1,5,1,1,3,3,1,3,2,5,2,4,1,4,1,2,4,5,1,1,5,1,1,1,4,1,1,5,2,1,1,5,1,1,1,5,1,1,1,1,1,3,1,5,3,2,1,1,2,2,1,2,1,1,5,1,1,4,5,1,4,3,1,1,1,1,1,1,5,1,1,1,5,2,1,1,1,5,1,1,1,4,4,2,1,1,1,1,1,1,1,3,1,1,4,4,1,4,1,1,5,3,1,1,1,5,2,2,4,2,1,1,3,1,5,5,1,1,1,4,1,5,1,1,1,4,3,3,3,1,3,1,5,1,4,2,1,1,5,1,1,1,5,5,1,1,2,1,1,1,3,1,1,1,2,3,1,2,2,3,1,3,1,1,4,1,1,2,1,1,1,1,3,5,1,1,2,1,1,1,4,1,1,1,1,1,2,4,1,1,5,3,1,1,1,2,2,2,1,5,1,3,5,3,1,1,4,1,1,4] + + var population: [Int] = .init(repeating: 0, count: 9) + + func step() -> [Int] { + var result = Array(repeating: 0, count: 9) + for (age, count) in population.enumerated() { + if age == 0 { + result[6] += count + result[8] += count + } else { + result[age - 1] += count + } + } + return result + } + + mutating func run() { + for age in input { + population[age] += 1 + } + + for day in 0..<256 { + population = step() + } + + print(population.reduce(0) { $0 + $1 }) + } + + static func main() { + var x = Self() + x.run() + } +} diff --git a/day8part2.swift b/day8part2.swift new file mode 100644 index 0000000..3b8e185 --- /dev/null +++ b/day8part2.swift @@ -0,0 +1,267 @@ + +@main +struct Day8Part2 { + + mutating func run() { + var sum = 0 + + for line in input.split(separator: "\n") { + let parts = line.split(separator: " ") + .filter { $0 != "|" } + .map { Set($0) } + + let mapping = self.findMapping(parts[0..<10]) + sum += self.decode(parts[10...], with: mapping) + } + + print(sum) + } + + func findMapping(_ input: ArraySlice>) -> [String: Int] { + var result: [String: Int] = [:] + var input = input + + let one = input.removeFirst { $0.count == 2 }! + let seven = input.removeFirst { $0.count == 3 }! + let four = input.removeFirst { $0.count == 4 }! + + let three = input.removeFirst { $0.count == 5 && $0.isSuperset(of: one) }! + let nine = input.removeFirst { $0.count == 6 && $0.isSuperset(of: three) }! + + let five = input.removeFirst { $0.count == 5 && $0.subtracting(nine).isEmpty }! + let two = input.removeFirst { $0.count == 5 }! + + let zero = input.removeFirst { $0.count == 6 && $0.subtracting(one).count == 4 }! + let six = input.removeFirst { $0.count == 6 }! + + result[String(zero.sorted())] = 0 + result[String(one.sorted())] = 1 + result[String(two.sorted())] = 2 + result[String(three.sorted())] = 3 + result[String(four.sorted())] = 4 + result[String(five.sorted())] = 5 + result[String(six.sorted())] = 6 + result[String(seven.sorted())] = 7 + result["abcdefg"] = 8 + result[String(nine.sorted())] = 9 + + return result + } + + + func decode(_ input: ArraySlice>, with mapping: [String: Int]) -> Int { + var result = 0 + for digit in input { + result = result * 10 + mapping[String(digit.sorted())]! + } + return result + } + + static func main() { + var result = Self() + result.run() + } + + let input = """ +fgcae ebafc cabdef eg abecfg abgfed feg gafdc bceg ebgcadf | defagbc faecg cfdag gecb +eagd cad fgadbc aefdcg dcebfg fcegd cbeaf ad dbgfeca defca | cfdeg gdcabf fcgde afgced +gfdeca aeb eb fbdag eafdc adfbe cefdab bdaegcf efbc ecadgb | eb dbafe eab faecd +facbdge efdg gcafd daegc caegb aecfdb ade ed gafbdc dgfeac | de abcdef faedcg dfgca +dcaegbf bc dcgeab cbgad cebg cdfaeb fbadg acdge cefdga cdb | gdcfae gbec gdace cgadfe +ceagfbd fbaeg fadge fbegad abgecf dafcg ed edab ged dfcbeg | gebfa de gbafec ed +acedbf adfbg cgafedb geac cgb befcgd bcgaf bfgace cg bfeac | bcdfeg fecba cafgeb bgdfa +egadc cea ac agbcedf cgab bgcade fbcaed eadfbg fegdc ebdag | fdecabg fcged cdbage efgdab +cbfge gdc cdea efdag dc cdfeg gfbcad gcdeaf cdeagbf egfadb | dfcge egcfd fcdbag dc +gaedb fbced gcdbef cge gc decgb dcbgfea cgfb cgdfea fbcade | fdegbac fecbd ecg degba +geaf bcfedg agbcd bfeagd dgfbe cbdaef beacdfg abe agebd ea | fbecdg geaf gebfad adgbc +bed afgebd adecf bafgedc dbca cfegb becdf fabdce bd gcfdea | gfdaeb ebfgad dbefc cbda +gacfebd ag bedfca agbfde egfcab dfga degab fbade bag bdecg | dgaf abcfed agb bfecag +afebg afdeg gcbe acebdf cfbage cafeb gab decgabf bg gcfdba | gb caebf abgef acfebd +efdab fabdgce gcbfa eg gacbfe beg ecag beafg dfabcg bgecfd | bge fgdcbe gcfdba ebdaf +aecbd bdagef gd bfega aefgcbd gfde gfdcab dbg edgba febagc | dg dagfbc dgfe eabdgf +gdfba egfacbd gdcefa baf gcba fdagcb dgfac ab dcfeba ebgdf | dcfag bfa gafcd cbag +afbe bdagc cedfb defcab ebfdgac gfedbc cbfad af adgcfe adf | dafbc dfbac fdcegb gadcb +fcgedb ca fdcage cagdbef ceab acedfb fdbce cafdb cda afgbd | ac dcafb efgdac ebcfda +fbad gbcda dcefgb cfeadg dgbcaf bcafdeg ad gcabe adc dfbcg | gbcdef bcfdg cgbfd bfadgc +afcgeb bedag bdecafg agb bedfga fdagce bg bgfd dfgea cdabe | badge cagdfe efdag afegbd +gfed dcaef ge bafdce daceg acebfg cefadg egc ecdabfg gacdb | bcgad bcegaf agecd degcafb +deacb adcge ab acb fbdce faedbgc fgadec gabd deabgc ecbfag | dfbec ab cbagfe bcfgae +gcabfde ecf cbdeg bacgf fedg ecfgb gfedcb fcaedb cebdag fe | dbcegf bcgdfe gfde defg +daefb bfg gedafb fabge dbafec cfega bdga cgbdfe gb gbacedf | aecfg bcedaf begdcf dgba +bagce bf bdacfeg efbg fcb cefab gbfadc cafebg ecdfa bacdge | fegb cegba bagce efbg +becag cfeg cefgbad fbace gfeacb acdegb bef ef adbcf fdabge | ef bdfac cbegad gbeac +egb afcbe fbaeg eabgfdc gcab gb gfdea fgbecd bcdafe gafbec | fcdgeb edcgbfa gbe fgeba +dga caebd ecdag cegfd abdfce dafbge ecbgda bcdgefa ga agbc | afdbec ecfdab gdaec gad +fc gbdefc cfe ebdaf fcbeda gbafde edacg bfcdgae dacef cabf | decgfba cdbefg cf ecdabf +febgd dafgbc caed cgd ceadgf fbecga dcgfe aegfc gbedfca cd | dgfeb fgced dcafebg agdfce +ecdafgb bgade bgd fabd gbaef bd egadbf ceadg cfbgea dgbfec | edagc bdage bfda dbgae +cgeba aefgcd dfceagb dc ced facbed cdfb dbaec adfeb fgedba | fbdea bgcae efbda fcdeag +caegbf dcfga afcedgb afgdcb fcedag ab edbcg dbfa gdcab abg | bag cbdag agb ab +gadfecb befc fgdbea cf fedagc fac bcgfa agbfe adgcb feagcb | gbcda cgbad bcgfa cf +dae aefcb dgabec dacfg afebdc ebfd afecgb aedcf fbcagde ed | ecabfgd bedf bcadef egdacfb +abcf fcega fdgce fegbda eafdcgb ac gabecd gbfae beagfc eca | edbgca edbfga cae gface +bgedcf afcdbg ag gcbaef fcedgab afgdb cfgbd befda acdg agf | fdbga ceadbfg egfbac fag +bad edafc bdeg abcgde ecafgbd gfadcb bgaec db edcba geafcb | cdafe fdcabg cdfageb dbge +geb bgcf agfcde gb befag gfbcae defab gabdec eacfg gabecfd | afbedgc gceabf abgef egacf +fdeac acg cfegda cbeadf fegdbca ag acefg adge bfdgca gbcfe | gecdaf efgca cgbafd cfgeb +begdf bdfgc ebcf adfcg cdegbf dgeafb cbg cb bfeagdc gbdcea | acgedb fagdbe bgfde dfgac +afcgdb aedc bedgf egafcd afdge gecaf fcgebda gda da ceabfg | gedbf gacfe adg agdef +bdeafg acgebdf dgcfa fa cafe dgfbc gaf edagfc cgabde agedc | fadbge bdgfcea dgcefa cgeda +gacfbde defag cbgef gbca efadcb afcbeg cgbdfe ba eba gefab | fegab abe ebfcgd fbgae +gfbdec fbcade cabdegf ebdg bfd dcfgb cadgf gebcf db fcabge | eacdgfb bdge ebdg ecafdb +egcfba fdcae fcdaeb gcfdae fabd ba cdeab fcdbaeg eba decbg | cabed facbed cbade dfcea +agdfe acfdeg fcdbeg dgf gdaec df cadf ceagbfd abgfe gbdaec | bgcdef abfge gbefcd df +dgebf afgdb bfa fcbade af dcafgb agcf gcbda dabgecf gadecb | dbcga abcdge fadgbce eagcbd +dbcga bea egacdf dageb be efbd abgfce gadfe fcbegad aebfdg | eba be bdage bedga +gcbd egbdafc fgc fbgedc gc aedbfg cefgb fedbg cfaegd efabc | bface egfbc bgdacfe gc +fdgae dc ced gbcd gbcefd decfba becfg ecfabg cgfde gbcadfe | edbcfg cd cafgbe cdegbf +geadcf efg gadef fg fecad acbgfe dagecbf dcgf gadbe afbdce | edafcg bgcfade gfdc abedg +edcba ecgdb aedbcf aebf dabfc fdacge badgfc ae ead gfcbade | gcebdaf degbc ebfadc efdcba +caedbg dbgec dcbf ecgdbf cgdef dgf bgaedcf egacf afebgd fd | cdfbeg ecfbgda gcbfed cbgedf +bdag acdeb ecgdb egbcfa fcdbega gb dbecag cbdafe ebg gdcef | bedgc abdfcge efagcb cdgef +bfade bfdga dfbgec cagbfd dfgacbe gba gacf ag gdcbf abgedc | cfag abdcge ebafd abefd +agcdef bcfda bdgca aebdfcg eagdc gb dbg gbea cedagb bfgdec | dfabgec eafdgc acdge gdecab +ebacdfg cgbfad egdbc bdgfa daef bae gabed febadg ae gacbef | fagdb daebgf fgdab bea +dcgbf cbedf degc bgc fcgdbea fbadg cfdgbe gc efacgb fdbace | edfcb cfeabg cg edbacf +bcdagfe dfacb cbfdge bgcdf fdgeac acd agbd acbef da dcgfba | cbgdf dgab ad gbdcaf +edgfbca gbe be ceba bdaecg gefcd dbagc egbadf dcegb dfgabc | gcfde gfceabd gfbcad eagfdb +dfcbg fcdga efgb gecbfd gecbda dfbgaec bedfc dgb bfadce bg | dbcgf bfge dacgf dfgcbe +cga ebdgaf bfdag cbgfe bfadgc gbfeadc ac cagfb cdba cgadef | bfdag agedcf aegcbfd gcfab +gcead gfdbec cbdge gabfce eb dfgcba ecfdgab befd dgcbf ebg | cdgae fbdegc bcdge gcdfb +da ebgfda fcdeagb acgbfe gedba dbegc agdecf dae bafeg bfda | gdafec fegba edfbga aed +fbagec gdfba bcg gebcfd cbafg bcdfgea ebac cefag faedcg bc | gfbac baegcfd fabegc dgeacbf +dfbeag fabdg fdbe abdeg eb dcega gcefba cfdbag dgcfbae geb | cgbafd bdef dcage eb +abdcfe gba dagf cagbefd fdbea bfega ag dafbge dcbeag gfceb | afbcde ecgfb dfbega bdgeca +dg dge gdbc cfdae bcgae cbdage abgfde cfedagb cdgae eacgbf | eabgc bgeac bcdg cagbe +bfd dgaeb gcabde fd bfgce dgfa dbefg gadbfce dbafge fabcde | edfcba efbcg df bdf +gcbfd agbecd fedc edcbfg fdb eadfbg bcfag fd gedcb gbacdef | fgbac bdagfec gfbdaec aegbdc +cgbdfe ce acedg adcfeg bcfegad fcea ceg abdeg afbdcg fcgda | ecg aefdcg dacge ec +egbdfca efacb agcdbe cbaed bdafgc dfaecg debg bd bda agced | geadbc efbca gabfdc bd +edcfb bfgecd ecabfd eca abcd fgeab fedcag abfedgc ac bfeca | bgfae eca cdgfeb bfaec +cadef cebdga ebd bacdfe dagfec dbfa fcebg ecbgfad db fdecb | befcd acgdeb abdf dbfa +gbfecda bfd cdagb fbdacg fgdbec gbaf cbadf aefdc eadcgb bf | adcgb dfbca agbf dafcb +abedg acbgf fd gaefcb cagbdef cdaf dcbfeg bdagf dgcbfa bfd | gbadcef df fadgb gabcdf +fbgadc dbcfg dcega cgdfe cfedbg caefgb ef gef gfaebcd fedb | fcedg febd gbcdfa cdgfab +bg adgb fcaedbg cgfda fcbae bcg dfacge fgcdba decgfb bcagf | cgafb dbfcage bdgcfe dbga +ed afbdg acefb aecbfg cfdabeg gedfcb fcdabe ecda def ebdfa | gbafd gedfcb dfegcb dfgba +gcdfae efgdb abdfcg fde gdabf dbcegfa fedgab efab ef ebdcg | eafb gbfda gacedf cbdge +edagfc gfaecbd bgced cbegfd cb gefdc badgfc bdc efbc adegb | dgbec cegafd dcbgef dgecb +gedcab beg eg cafgb fbegc gdfe bdgfec gabcedf bdafce bcdfe | gcefb afcbed ge gdfe +fdbagc cdgefab edacf ceba ea cfbaed ecfdg abfegd adfbc eda | ea beadfc abcgdef ecdgf +fbc gfbacd fecda cbdfa dbgf dceabg gbcdfea efbgac cdabg bf | gbdf agbced gaedbc gfdb +aegcb bfgecd fbegad fcagdbe dfgce fcbd gdeacf fb bgf bgfce | bgafedc gcdaef dgfcbe eadgcf +def dbce agdfeb ed fdgbcea dgcfb aegfc dfgbac gecfd cdfgbe | cdfegb ed cdgfaeb ebdc +ecbfad afdb eagbfc caebgdf edfcb dgfec cgdbae bd bcafe bdc | bfacegd fbdcae dbc bgfdcea +dfgba eg cfbead gde bfedgc dceab cabfdge gdbae aecg aedgcb | egd adebg gde debcfa +cadfb egfb afgbc adbegc ecgadf ecagb fga ecfgab bgacfde fg | afcbg cgfba gf dacbge +cegbf dec efcdg facdge dfae dcbafge ed adgfc fdbagc cgaebd | dce gbcfe afcdgbe dcgabf +cfabe fa eabgfc cgaf fba gfbce dcfbge edbgcfa efbadg acebd | cfebga bfa fedcagb abfce +acdfb dbagf afcebgd beag afgedb gfdea gbd fgdcbe gb cdeagf | dafge gafed gfacde gb +gdfeac dfgce ebfadcg edagc dacf baegdc fd gdf egcfb dabgef | degfbac cfdeg daebgf cdeagb +fcgabe ebcd ecfdg dabcgf gfecabd cfd ebcfg dc bcfegd adefg | agedf cd bcefg dc +cbaeg bcfea fb feb gfaceb gdfbec dagbec gfab deacf bgcaedf | acbfge bf afgb efb +egdba cadgfbe efadbg cg begc bfgdca caegd abcegd dcg acdfe | fbegcad bceg gcd cdgebaf +agbcd dafcgb cfdg ceadb fdgeba eafbcg gfdab gbc bgdcafe cg | gfadcbe fbagd decba gc +gfbec adgc dge egdcfa abgfde abgdfce dg eacfdb fdaec gdfce | fecgd gd fgcbe acdef +fdbceg bfedc af fegcad cbdag daf efab dabcef fbeadgc cbfad | fcabd cbfad gcfdbe gadbc +ebgac fabdeg bedca cg aegdcfb geacbf ceg efgba facg fbedcg | gecdfb aebfg fagc ceg +eagb dcgeb ba abd cbeagd cdgafb fecbdga fgedcb dacfe dacbe | bdfeacg cadgbf bgedfc fcdae +dbac cfgae cbfgde ba bga gcfbad dcfebga gcdbf feagbd cgbaf | cgafb fagbdc dcfbg afgebd +adbgfe fgb egdafc cbafd dfegbc edgafbc egfcd cegb gfcdb gb | bg fdacge efcadg egdfba +agfbe cgaefdb gbdf edagb afg cdgafe gfedab fg egcdba fabec | fcbdega fag agf agf +cae edabfc cdfega ca cfba gadeb fdecb ecagfbd cbgdef acdeb | facdeb fgecad bfadce efcgbad +facb dbfec dceabf efgcd cb aecfbgd dgfbae eafdb ebdagc bcd | bfedc cdegab cbdfe adefb +fecbga badgc dga bfgadec dg geacb cgedba dfebag gcde fcabd | decg gda baceg gd +gecbd gcbdfea cbfg bg bge egbcdf dcbef beafdc cdeag bfgeda | agcde dceag gfabde beg +gefba gacfbe facdeg cg cgab gfc efbdcag cfgeb cefbd afegbd | gfc afbge dafegb aebcgf +egd aebfgd gefc ge gedbfca bdcfe dfcegb gecdb edbacf cdbag | dacbg edcfbg bcdfe ge +df ebdga gcdefa cfbd bgdfa afgcb dfeagbc bfacgd fgd facebg | gaebd gecbaf fdcb dfbag +fag cegfdab cadebg dabcgf abefg eafc ecafgb ebacg fbdge fa | ebfag dbcgfae gbecad fdbgac +gecbd gefa gfc fabdcg bfaecd ecgafbd fg befgc efabc gecbfa | gfae gcf egbdafc edgbc +bfgae fcebag fdbe fd fedag adgfbc fdbgea ecagd agcbfed fad | fgabe dfeb ebfd gefba +ecdgfb afge bdage ebcda eg abdgef fgadb gabcfd dge bcdagef | gdebfa eg agfbdec afeg +dbc acefd cegadbf fbcde afcb fdcaeg bc acdbge febdac debfg | ceafd caefd gfbaced dcb +fecgb fgacd gdcefba de gbafce febd gecfd edc fecdbg ecbgda | becdfg ed becgad gcdeba +agfdbe fbac bcd edacfb bgecfd cb cdeba cfgdeab acged edfab | cb dbc adbce bdfae +dgacef bdfa ecgbf ab fegba dgafe efdbgca abg acebdg afbdge | gefda ecgafd ecfbg begcad +gbfcea bgfa afdebc aegdcb af bgcdfea bgeac gecdf afc fgace | bfgecad fca afc abfg +cfe dgcfe fdagcb gebdc egfa gbcdefa dcebfa fe agdfc fgaedc | dfgac fe fce fcdbega +fecgab gcbdef cbaefgd dfgbe dgcfe eb bfe dbfga edbc acfegd | fbdeg fdgeca agefbc egcfd +aecd bac dfacbe cafdb ac gefcab gfcdbe fcbde gbdaf cbdegaf | gcdbefa fbgcae ecad fagecb +eagbf cagedf geafdbc fdgbce adefb aecfd adcb deb faecbd bd | ebd deabf fadcge cdagfe +gadefcb bfcga eadcgb egdbfc deag ecg edabc acedbf eg abgce | gdea bagce gce gce +agcfe bec gecab gbeda facb dbfgec fcegab gfacde gfdaceb cb | cb cefdgb cadefgb bc +fdgb df egabcf fegda cedga fegab efdbac def cgdefba gafedb | facdbe bafceg agedf fdebga +fdaecg dbcafg abfgec dc caefg cedg aecfd abdgfce eadbf fcd | cgefab gbfacd fbegac fabecg +dfecabg bcfeag gdafce dagbfc cdefb dca gafbc dbag da bcadf | dca bagfce cbfda cdagbf +dabfgec febadg gbcdae gfbe adebf ef cabdf edfgca ebgda eaf | gfacbed abgced egdfca egfb +cfdea dc fdagebc fegcbd agdc gcfade fcabe cfd efdgba gfead | bdfcge agdc dgefa cfd +gbfca febadcg bc bdegcf beca begaf gabedf cgfbae cgfad cbg | bcg dcaefgb dfgceb afebdg +cg cgbf bedcg fcagde adefbcg efdgcb ebacd cgd edbfg egdafb | cfgedb bgcf acegdf cfgb +bfad adcgbe faceg fagbdc df afdgc efcbdag dfbegc gfd badcg | gfd abgcd fgd abfd +bcadgf cgdfb adgcb gf gafc dcfeabg bdagce dgf dfbega cebdf | adcbfg gfca fdcbe gfac +daebf fcabgde bgdcef gab ga edbcga afcg ebfga aefbgc egbcf | ebcfg ebcgfa fcgebd agcf +gedab fg afgecb cfgd feg cfabed befdc fgdcbe gdceafb befgd | ecafbg ecfbd febdc gf +gcbadf ce gfcdb fec feabd gbecdf dbcfage febdc agcdfe egbc | bafed gcbe gefcbd egcb +cbdfg gbcafd feb gaecf gcbfe gfbdae be fcgabde bdgcef becd | abgedf gdfebc aedgfb fbe +edacgbf fbdae edgac bg gab bagdec dagfce fadbgc cebg bgdea | aedgb bg cebg adcge +bda gbfea gedcab gfbceda bgdfa bfcdg febcdg bagcfd cfad da | dbecag bgfdc gdbfa facd +ceabd dabfe acgde bc fbca cgbfde febgda dfecagb ebafdc bdc | cbdae dbfea fgdceb gbfedc +acg fadbgce cfgdeb dbgce efdca ag dbga cagbef egacd cdgbea | gdecabf gcbfea ecbdg gac +dfgacb ged egfdab efcgbda gcefbd debga ebadc ge aegf gfdab | cdfgab cdafgb bfcgda ge +gdbeca bcfde eafg cefbgad dgafcb ge degfb adefbg dgfba bge | bdfeg begdf ebfadg eabdfgc +cfbdgae gbaec bagfcd gfcba dcaeg bacfeg dbeacf bec efgb eb | ceb egcab cagfb dgcae +fdcagb gdaecb fcadg bcdgaef gd dfcea gbcaf aecgfb cgd dgbf | gfadc gfdb badgfc gbfca +bcafed defgcab fgcb cfbae cagde agebfc gbcae gbe gadefb gb | bg gfcaeb bg aedgc +bcfadeg gbfd eacfbd gdafe afebg bafegd geafcd gebca fb bfa | agdefc eafbdg fgead gedaf +fcgeabd dgeaf ebgfad gceafb geadc ac aec ebcdg cafd adgefc | gbfadec egcbd acfd dgabef +eac fbdgec ea eadb cbaeg agfdecb eafdgc dcgbe bcfga adcbeg | aec abde eac ea +cedb gcadbe ed edgfbac fbega abdge gacdb fdacgb dcefag aed | cdagb gbcafd gdefac gedafc +cae agfbec ae begcd feab fadgbc ecfadg ecabg cdgbefa cabfg | ecgba afcgb efba dgeafc +fecgabd badf efadg abedfg fea fdgceb fedgb aebfgc af gdcae | ebfgca fae fea af +bgedac gbfae dcbefg acbf fb gaedf cagedbf aebgc gebfca fgb | aedgf fdgea bgfae bfdgeac +bagcfe eadcb gb facegdb bgc fdcag efbcad beadgc bgadc ebgd | gbc fbcage becad gbfcae +cagde gbca dcgbea gc efcad dbgfea dfgecb caedbfg adgbe cdg | gc egdfbc bcga cg +efgcbd aebgcd dfe eadfb abged abdcefg bacfd ef bdfaeg faeg | fgae cgdaeb adefb afcbd +cgbfea gcbda gefdcb eadf febag bdf fd febgad fadbegc bgadf | gebcfd fgdbea dfb dbf +dg cdgfe gecfad dfbce bgefda fgd eafcg efcbag dgac bcdagfe | afdebg gfd aecgf gfcea +edfga cgefda gcdbe fbae ab egdabf cfgbda deabg bad adefcgb | ebadg agdeb fbae gbdae +gbdcfa gbade ebgac ecbfa adfgbec badecf bgc gcef fbgace gc | gc fcge gacefdb ebgca +acefgd ebgaf egfda egcd bgfaecd dgcfa ed abcefd efd abdgcf | def ebgaf fed befcda +fecag cag cfagbd agfcbde fabec eabg fdcge cgbfea fadebc ag | cfgbda dfgbeca efdcg dfgcaeb +bgecfda ecdag dgfcbe bdgac adbgf afgdbe bfac gdcabf cbd bc | bc acbgefd fagcdb fcba +ba efacg cfbgde cfdbg bacgf bgfdea abf bcad cfbeadg bgcfad | bcdgef fdgaeb ab cagfe +cbdaf bafdgc eacbd fd dcf fcabg bcdfeg bedgcfa dgfa agbecf | dfga bgfac df agdf +edcabf cadbf bgfdcea bgcfad bgcde dfg fg bdfgc cgadfe abfg | adfecg gfbcd acgdfeb cbgdaf +badgfc dg bagcde bceda dbcfea beafg debag adg fcbadeg edgc | gebda dcagbf dceba egfab +edcf dacfbg dbcefag fbd df bdegfc ebdagc bgfea begcd bfedg | dgebc cbgde dgfbac bdf +bdcfg acegfb acebfd fdcag egadcf da daf cgeaf edag bafcegd | gdcaf fgaec bafdec fdaecb +cagefd fde gdfba fcgaedb abefd ef dafbeg ebfg afcbgd ebdca | egdbfa bceda dfabg bdace +fcgb gcdae bg gfadeb cbfegd fcabed bdg gcdbe fdcbe adbefcg | dgb gfbdec befdc gfbc +fabde bgfde bgecda cedgb gf cabfdeg ebfdgc fgbeac fcdg gfb | fgbde efdgb edgbc afedb +ebacf cedfab fde eabfd fd baecgf bagde agdebfc dfcb egfadc | ebfdgac acdegf abdefc ebfad +gdcb cd cfgae cgbedf fbegd efgdc fadcbe gfeacdb dce dfagbe | gbcd cde gacfe gefdbc +geb afedgb bfegca bgade eg dgef gadbc eafdb afebdcg fcbead | cagbd defcba dbafec geafcb +fgbdc gbfade gba gefa dbafg cbadge bfgecda ga edbafc dfabe | afbdeg dgfba bgdfa dcfgb +agef badce ef febad cabfdg gdfab cbgeafd egfbdc defgba fbe | abefd geaf gafdbe ebcad +gd gad bfdg gecba fedabcg fcgeda aedfbc adebf agbdfe dgeab | ebcdaf agd gcfbdae egacb +cedbfa ba cgbfa abf dfegac cafeg gabe befgca ebdfcag cbfdg | fab aegcf faecbd fdcebga +daefg febdga aegfdbc efgdca eafb bge eb agbdec febgd gbdfc | gbcfd edbfg egb bacegd +fbdecg bdafec cfaeb gedac def dceaf abfd fd gfecba gfdaecb | ebfac bdfa gbcdef fd +fcbga cbdfeag bafgce fdacg dc fcd fcgbad fadge efcdgb cdab | gaefcb bafegcd acdb abcgf +ecgabd acgdf abge ae bcfead fgdaceb ead daegc bcdefg gcbed | egba gaecd cgadbe gdbce +aegfbc gf afebdc agebdfc ebcdf feg dcefg bdfg dcgea bgedcf | edbfcg fdgb bdgafce febgcd +dcg bcdefa dbfceg fdeacg acbeg bfeagcd cfdae dg dfag agdec | dfcgea dg egdca edagc +agcfeb fagbed fdbceg bag gebfd afgd agbfcde acbed ag badeg | cbdea fcedgb gfebac abcegf +adbegfc fgdace cadge afcg ebdfca ebgad eac bfgdce cgefd ca | acged fcedg ebagd dgacefb +gfeadb gfeab bcfa agebcfd aebgfc fecgbd efc fc gadec egcfa | fgbace fc debgcf afgceb +bacg ab cfgbd bfadge acfed fgdecb agedcbf fcabgd bfa dbafc | gfdcb cbga abcg afecbdg +ge beagc cfgabd bgaced bgcda fbadgec age ecdagf egbd fcbae | aeg gedbac cadfeg fbgcda +dgbafe abfdgec gdf fd agebcf gadec dfcgba befd edfga aebfg | abcgefd dagec dfg df +edfcga bcegd baeg cbgdae agdbc gce acgdebf eg gfadbc cefbd | dcbeg bgedc cedbg edcbg +adfbe dg cagef dfgb gdbeca aefdg gda edcbaf fdgeba gdecbfa | cdabef eafbd bafcegd dg +cd edcbfg dbage fcaebdg facd bgcfea afbedc afbec dbc adecb | adegb bcefag abegd cbgedf +gdcbf da ecgbfda adc adgb bagcfd dfbcea egfac dfbceg gdacf | gdba cfdbg adbfec fcaeg +bcg bc gdcbae dbca abfgde gdeba ebfacdg egcba faceg dgfbce | geadb badecg ecbfgd baecg +""" +} diff --git a/day9.swift b/day9.swift new file mode 100644 index 0000000..078a9f6 --- /dev/null +++ b/day9.swift @@ -0,0 +1,186 @@ +@main +struct Day9: Puzzle { + + let matrix = Self.input.compactMap { $0.wholeNumberValue } + let width = 100 + let height = 100 + + subscript(x: Int, y: Int) -> Int { + get { + return matrix[x + width * y] + } + } + + func isMinimum(x: Int, y: Int) -> Bool { + let value = self[x, y] + let smallerLeft = x == 0 || self[x - 1, y] > value + let smallerRight = x == width - 1 || self[x + 1, y] > value + let smallerTop = y == 0 || self[x, y - 1] > value + let smallerBottom = y == height - 1 || self[x, y + 1] > value + + return smallerLeft && smallerRight && smallerTop && smallerBottom + } + + func run() { + var lowPoints: [(Int, Int)] = [] + var riskSum = 0 + for y in 0.. $1 }) + .prefix(3) + .reduce(1, *) + + print("Part 2:", basins) + } + + func basinSize(x: Int, y: Int) -> Int { + basinAt(Point(x: x, y: y)).count + } + + subscript(point: Point) -> Int { + self[point.x, point.y] + } + + func basinAt(_ p: Point) -> Set { + var set: Set = [] + findBasin(at: p, value: self[p] - 1, visited: &set) + return set + } + + func findBasin(at point: Point, value: Int, visited: inout Set) { + guard !visited.contains(point) else { return } + guard point.x >= 0 && point.y >= 0 && point.x < width && point.y < height else { return } + let currentValue = self[point] + guard currentValue != 9 && currentValue > value else { return } + + visited.insert(point) + findBasin(at: point.left, value: currentValue, visited: &visited) + findBasin(at: point.up, value: currentValue, visited: &visited) + findBasin(at: point.right, value: currentValue, visited: &visited) + findBasin(at: point.down, value: currentValue, visited: &visited) + } + + struct Point: Hashable { + var x: Int + var y: Int + + var left: Point { .init(x: x - 1, y: y) } + var right: Point { .init(x: x + 1, y: y) } + var up: Point { .init(x: x, y: y - 1) } + var down: Point { .init(x: x, y: y + 1) } + } + + + static let input = """ +5796798621237995498765434567987542999765679987545679109878999877899789876532123456998999876887899921 +4645976434456789349654321298997679898654698987635678998767897656789698765432012347897899865676798799 +3234987545978993298795410989998989789543256897646789498756789546896579877842123456976789954345985678 +4356798679989999019987329878999798679765345689856991296545890134789467998956899967895698643239874579 +6467999789999898934976598967987676568996457999967999987636789345691346789769987898934987651098763456 +7598997999987796899989987959876543456789569898998998765125678956910298899898776799123998862987652345 +8789876789865685578999876645987665567998689656789876554034899999891989998987564679099789879876543456 +9898765498974324456799865534598786788998798645889985432126954987789878987654323567988698989987664678 +9989997987543212367987654323459897899549895434569876556437899876599967996543212459876587898998798789 +9877989997662101456798765434567998967932987624778989787548999989679459987687301349875456967899899893 +8765678998983232367899896765788939346891295434999999899659879699894349899796532656954344756910923964 +9874789019894343456789949878999321235799987545678945998778964578942139769898543769843212345891949765 +7965678998765764567893433989876542346987898656789236799889653989943298652999678989754523456789898976 +6598789109976975698921012398999763457896559867997647989998799899874569541098799798765676579896687897 +5439898929989989789962123457899894568989435998998799878999989798765798432149898649987787899965456989 +5212987898999999899854336568999989879979326569109989567898879689878987544234995434599898999874345679 +4309875987999876998765687679989878998765412458929875467987854599989698955679989323459979899993214589 +5996954676799984329877788989878969899874324567899765359876743489996549877789878912398767789984323456 +9875432545789743212989899998967659789985595678998868249965312679865435998898767899598954698765467567 +2994321036897654101996936987654543679999989789997854198764301569979423459999954678997643789978578978 +0987432128998763219875424698763212478998978999876543298773212458998901267898767889598732345988789989 +9876544346789979329876512349998401569897569234987994987654343457897892478999878993349891234899998695 +9989875679899898939984701459886212398786456946799889998795464567896789989896999321234989345678987544 +9997987889998797998743212598765323987654397899989778999989978679945698999765678930349878956999898433 +8986798999987676789655329679876439876541289998878656899867898791239956799954345959499867897899765321 +7845679219878565678976598999987556998732378997667545798656789892398745999765237898987656789998654310 +6434798998765454567897987898798677899543467896553234987545878989499636878992156987975345899219964324 +0123987999874343456789876799549789987654598998421056989434767879976521767989249876543234998909878434 +4339876798955102367898985678939891099967899876542159875323854568965410156978956997684456797899989545 +5498765986543234488987894799012999129878999987653249986210123467894321249769767898795867956789199656 +6599754397654545589996789899993998949999998799767998975351237998995432398758998989986788946999098789 +7988732198765657678975698999879876898946999544979877989876456789987643459647899876097899534878999899 +9876546019878767889984567899968765667939896532989966592987887893298754598756789965198997323456799989 +0998757898989899992099789988754543457898789540199854301298998984129866789998999954249986412345899878 +1299898987699954954299999876543212348987698921598763212349999873234977995679439765356897601234988767 +2989999876569999895989986987654103467896567892349854334568989964345698934678921976897996532349875756 +9879899988698988789878995498976215989995456789498999965689879899456789323567890197998987656756994345 +8765677899987677697667896329865423499989997995987878897798768778968895438678991298969398967899873201 +8654546789766563459548994219876534578977789104986567789949654567899976657899789349543219988932965412 +8643437899854312998435689101987645689665679323975345678959868778957987798934679959654301299549876723 +6532126778969409876424578912498756796553568939863203456899979989646799899012567898976432358956987894 +8544434567998912998535689843569867989432459949954212568999989594535798989123467987976563567969799985 +9655566789897893479697897654567979879421267898767343467898795443123497679935679876899674789997659876 +9776789998656799567989998765678998967992349929876556578987654321012989567896798765678985999876642989 +9988899875434678979878999879889987657889458919997967689798865452199867456987987654567896798765431096 +8799943989545789999868799989999876545678967898789898797659979874987654347899999753459987979876532145 +7679969998656789987657678994323965432789979987679789896535989995998321234568939894567899764987687236 +7567898959767893976543467995909876645678989996545678965423599989899754359679421986789968973199875345 +3467987644978912987784989879899987856789999987434569653213679876789866468789439898993459792012989466 +6569999533989999798896797867789898987897999876424678962102398765898977578996598769322375679933496578 +8678998921296789679987896545698769399986789764312398993923499654567897689398999954201234567894987689 +9799867890145679542998987896789954234995698765101256789894987543458999791249899895312347899995798789 +2988656891237789869879998998996895999876789864212345996789987652377899910299756789436556799989999890 +3977545789347899998767899989434999879989898987423467895678993210466789321987645679987867989767899921 +9865437896556789999946799876545998767999987976534789954589997621245679459876437898798979876545978943 +9976545789697899987834689989699767656899876989675678932679876432556789598943218997659989765434567894 +9989867899989999986545678995987654545998965498797889321996997543457898797654323789943596986546789965 +8896978999879878997668789213986543234987894349898996549875698665678909998965534567892345697656798987 +7645989898765655689779898901987662129876789234989998698954539987899919879877645689931257898767967899 +9869998789964334578995937892397654398945689345678979987643012398967898767998776798890234999898957898 +6998789679892123457894325943498765987834568956889467898952134579656899656549887897789656799999546457 +5987654597651012349975434799579989865423567897992378999763245678947999743435998996549767987989432347 +4398753299843123457896545678992099654512455789209989398754356789439879842124999987638989876678954456 +1239854987653238768998956999989298743101234678998994298765768899598765431029892196547898965466795567 +0123995799768545678969869878979349543213455789997899109976779998789987532139789987656987654345789979 +3235986999879656789756999867668956975434696899986987912987899999894697643298678999987898543234567898 +9945799876998798897645987654456899876595989989765976899998999886989987659987567998998999864365788977 +8799895995439899986534598732367968988989878979954695678919998785468998798766456997899898765479899766 +5678923994323999876545987543478957999976556767893254589101987674378789899854349876799649876678987645 +4567919876214899987689998954989546898765432356789123678919986543245679998765467965678999987789298756 +3467898765436789998998999879995434999896521237895019789998997532135998899977569876789988798992109867 +2369929876545699999987899999876545689943210235994298999997989949239876789989778989899976569993298989 +3458910997676789899876989212987676899656432346789987889886979898949965679999899998998665478989987897 +4567899998787895798765678923598789998789545497899996779765765787898764567878921987999543289567976546 +6878978999898934989894569654589890249899876569999884568954654656989843458967899876998992123458997997 +7989569899919129878989678979678931239964998978998763477943212349879932123458998765876789012567919989 +8991356779909019767678989989899842498643239989987542356799302345965431014568919954345692139898929879 +9410124567898998654567891294998753679654134699876521245678913459876432323589109765457789256799598767 +4321267898987549876899910123987654598763245698776410234569865567997943434578999988767899767895349654 +6532356789876434987898943235698765679854659987654321345678976778999894565689989999878949878943298765 +7656467893987545998967896545699887998767998998765432456889988999998789689789878989999432989752129976 +8767589954899756899459987756989998919879897899876548668994599989987678999898769878997643496543234988 +9879678975798967987598999899878959102998766999988767899123678978986567894987653967898765789759449999 +2989789996987898998987899954969643213459945789799878943239989865454478923986542656949896899898998931 +1299899989776999129976899769878965425678996999656989965398798754342349435965421248956997899987687899 +0467999876545789098865689879989876566789987898768997897987659983210456949878543367899898999876546797 +2378988965434678987674778998999998987893598999879876798965434975672569898987665456789789998695435896 +4499877994323569876523467897988999999912459999989965689896323497883479767999787578995679876564326345 +9987656789212489986313568966767897899106598989999984878789212598965998956799898989664868975432101234 +8798878994323478965423459954456986798919987678999873165678943679879876545689939996543459876875212345 +9659989765434568976796567893299765987898765569898762054567894578998765434696549987654667998994323476 +8934599879876899997898879954987654496987654346789943123489965679539854323589998998767898989889456567 +7895678998987899998999998769876543345798765767898894344567896789329875434567897689998999878778967678 +6796989987698969799998789878998632134899878998946799465679987898919987645679976578999098767669898799 +5789995799549347689987678999986721023999999769434878987989998967898998767989787459989298653456789910 +4567894698432134567988566789875432335698798653212967999997859456987679878998643212578987832345679891 +3779992976553235679977455698998765487987689864343458999876543237897569989797652101459876543456789789 +9889689987664346798765323456789887569876598765454569986987652145789698795698768892368987956567895678 +4994567899865457999974313345699998798765439879875678955698767234999987654229878765456799767878934589 +2123456999876567899875101256789109999984321989989899543249878945678996542101989877667899878989123699 +""" + +} From 534ae9c20afb9b178a80bf807fe7e6e02dfd0cdf Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 11 Dec 2021 23:55:58 +0100 Subject: [PATCH 07/32] Day 11 --- day11.swift | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 day11.swift diff --git a/day11.swift b/day11.swift new file mode 100644 index 0000000..2d16909 --- /dev/null +++ b/day11.swift @@ -0,0 +1,70 @@ + +struct Day11 { + mutating func run() { + var count = 0 + var currentStep = 0 + repeat { + let stepCount = step() + count += stepCount + currentStep += 1 + if (currentStep == 100) { + print("Part 1:", count) + } + + if stepCount == width * height { + print("Part 2:", currentStep) + break + } + } while true + } + + subscript(x: Int, y: Int) -> Int { + get { data[x + width * y] } + set { data[x + width * y] = newValue } + } + + mutating func step() -> Int { + var flashes: Set = [] + + func up(x: Int, y: Int) { + self[x, y] += 1 + if self[x, y] > 9, flashes.insert(Point(x: x, y: y)).inserted { + for i in -1...1 where 0.. Date: Sun, 12 Dec 2021 11:28:18 +0100 Subject: [PATCH 08/32] Add day 11 to Xcode project --- AoC21.xcodeproj/project.pbxproj | 83 +++++++++++++++++++++++++++++++++ day11.swift | 4 +- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 62bf1b8..3b59b84 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -11,6 +11,8 @@ 261F31802763DACE0018D49C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 263BA5B3275E97AA00839C92 /* day5part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263BA5B2275E97AA00839C92 /* day5part2.swift */; }; 263BA5C0275EA5BD00839C92 /* day6.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263BA5BF275EA5BD00839C92 /* day6.swift */; }; + 263CCF68276605F40061B353 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 263CCF6A276606010061B353 /* day11.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263CCF69276606010061B353 /* day11.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; 269BE5CA2762A05300871C85 /* day9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5C82762A04F00871C85 /* day9.swift */; }; 269BE5CC2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; @@ -47,6 +49,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 263CCF5F276605320061B353 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 269BE5B22762958800871C85 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -74,6 +85,8 @@ 263BA5B2275E97AA00839C92 /* day5part2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day5part2.swift; sourceTree = ""; }; 263BA5B8275EA5A600839C92 /* Day6 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day6; sourceTree = BUILT_PRODUCTS_DIR; }; 263BA5BF275EA5BD00839C92 /* day6.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day6.swift; sourceTree = ""; }; + 263CCF61276605320061B353 /* Day11 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day11; sourceTree = BUILT_PRODUCTS_DIR; }; + 263CCF69276606010061B353 /* day11.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day11.swift; sourceTree = ""; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5BB2762959B00871C85 /* day8part2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day8part2.swift; sourceTree = ""; }; 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -103,6 +116,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263CCF5E276605320061B353 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B12762958800871C85 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -123,6 +143,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 263CCF69276606010061B353 /* day11.swift */, 261F317E2763DACC0018D49C /* day10.swift */, 269BE5CB2762A08800871C85 /* common.swift */, 269BE5C82762A04F00871C85 /* day9.swift */, @@ -141,6 +162,7 @@ 269BE5B42762958800871C85 /* Day8Part2 */, 269BE5C12762A03F00871C85 /* Day9 */, 261F31772763DAB60018D49C /* Day10 */, + 263CCF61276605320061B353 /* Day11 */, ); name = Products; sourceTree = ""; @@ -199,6 +221,23 @@ productReference = 263BA5B8275EA5A600839C92 /* Day6 */; productType = "com.apple.product-type.tool"; }; + 263CCF60276605320061B353 /* Day11 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 263CCF67276605320061B353 /* Build configuration list for PBXNativeTarget "Day11" */; + buildPhases = ( + 263CCF5D276605320061B353 /* Sources */, + 263CCF5E276605320061B353 /* Frameworks */, + 263CCF5F276605320061B353 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day11; + productName = Day11; + productReference = 263CCF61276605320061B353 /* Day11 */; + productType = "com.apple.product-type.tool"; + }; 269BE5B32762958800871C85 /* Day8Part2 */ = { isa = PBXNativeTarget; buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; @@ -255,6 +294,9 @@ CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; }; + 263CCF60276605320061B353 = { + CreatedOnToolsVersion = 13.1; + }; 269BE5B32762958800871C85 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -282,6 +324,7 @@ 269BE5B32762958800871C85 /* Day8Part2 */, 269BE5C02762A03F00871C85 /* Day9 */, 261F31762763DAB60018D49C /* Day10 */, + 263CCF60276605320061B353 /* Day11 */, ); }; /* End PBXProject section */ @@ -314,6 +357,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263CCF5D276605320061B353 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 263CCF6A276606010061B353 /* day11.swift in Sources */, + 263CCF68276605F40061B353 /* common.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B02762958800871C85 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -555,6 +607,28 @@ }; name = Release; }; + 263CCF65276605320061B353 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 263CCF66276605320061B353 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 269BE5B82762958800871C85 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -651,6 +725,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 263CCF67276605320061B353 /* Build configuration list for PBXNativeTarget "Day11" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 263CCF65276605320061B353 /* Debug */, + 263CCF66276605320061B353 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day11.swift b/day11.swift index 2d16909..2d3656d 100644 --- a/day11.swift +++ b/day11.swift @@ -1,5 +1,5 @@ - -struct Day11 { +@main +struct Day11: Puzzle { mutating func run() { var count = 0 var currentStep = 0 From 76784119c0ff184c277c6685b3b249b6d003cca6 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 12 Dec 2021 12:11:27 +0100 Subject: [PATCH 09/32] Day 12 Part 1 --- AoC21.xcodeproj/project.pbxproj | 83 +++++++++++++++++++++++++++ day12.swift | 99 +++++++++++++++++++++++++++++++++ 2 files changed, 182 insertions(+) create mode 100644 day12.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 3b59b84..02aec33 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -13,6 +13,8 @@ 263BA5C0275EA5BD00839C92 /* day6.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263BA5BF275EA5BD00839C92 /* day6.swift */; }; 263CCF68276605F40061B353 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 263CCF6A276606010061B353 /* day11.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263CCF69276606010061B353 /* day11.swift */; }; + 263CCF782766066D0061B353 /* day12.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263CCF76276606670061B353 /* day12.swift */; }; + 263CCF79276606730061B353 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; 269BE5CA2762A05300871C85 /* day9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5C82762A04F00871C85 /* day9.swift */; }; 269BE5CC2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; @@ -58,6 +60,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 263CCF6D2766065B0061B353 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 269BE5B22762958800871C85 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -87,6 +98,8 @@ 263BA5BF275EA5BD00839C92 /* day6.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day6.swift; sourceTree = ""; }; 263CCF61276605320061B353 /* Day11 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day11; sourceTree = BUILT_PRODUCTS_DIR; }; 263CCF69276606010061B353 /* day11.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day11.swift; sourceTree = ""; }; + 263CCF6F2766065B0061B353 /* Day12 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day12; sourceTree = BUILT_PRODUCTS_DIR; }; + 263CCF76276606670061B353 /* day12.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day12.swift; sourceTree = ""; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5BB2762959B00871C85 /* day8part2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day8part2.swift; sourceTree = ""; }; 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -123,6 +136,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263CCF6C2766065B0061B353 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B12762958800871C85 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -143,6 +163,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 263CCF76276606670061B353 /* day12.swift */, 263CCF69276606010061B353 /* day11.swift */, 261F317E2763DACC0018D49C /* day10.swift */, 269BE5CB2762A08800871C85 /* common.swift */, @@ -163,6 +184,7 @@ 269BE5C12762A03F00871C85 /* Day9 */, 261F31772763DAB60018D49C /* Day10 */, 263CCF61276605320061B353 /* Day11 */, + 263CCF6F2766065B0061B353 /* Day12 */, ); name = Products; sourceTree = ""; @@ -238,6 +260,23 @@ productReference = 263CCF61276605320061B353 /* Day11 */; productType = "com.apple.product-type.tool"; }; + 263CCF6E2766065B0061B353 /* Day12 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 263CCF732766065B0061B353 /* Build configuration list for PBXNativeTarget "Day12" */; + buildPhases = ( + 263CCF6B2766065B0061B353 /* Sources */, + 263CCF6C2766065B0061B353 /* Frameworks */, + 263CCF6D2766065B0061B353 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day12; + productName = Day12; + productReference = 263CCF6F2766065B0061B353 /* Day12 */; + productType = "com.apple.product-type.tool"; + }; 269BE5B32762958800871C85 /* Day8Part2 */ = { isa = PBXNativeTarget; buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; @@ -297,6 +336,9 @@ 263CCF60276605320061B353 = { CreatedOnToolsVersion = 13.1; }; + 263CCF6E2766065B0061B353 = { + CreatedOnToolsVersion = 13.1; + }; 269BE5B32762958800871C85 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -325,6 +367,7 @@ 269BE5C02762A03F00871C85 /* Day9 */, 261F31762763DAB60018D49C /* Day10 */, 263CCF60276605320061B353 /* Day11 */, + 263CCF6E2766065B0061B353 /* Day12 */, ); }; /* End PBXProject section */ @@ -366,6 +409,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263CCF6B2766065B0061B353 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 263CCF79276606730061B353 /* common.swift in Sources */, + 263CCF782766066D0061B353 /* day12.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B02762958800871C85 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -629,6 +681,28 @@ }; name = Release; }; + 263CCF742766065B0061B353 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 263CCF752766065B0061B353 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 269BE5B82762958800871C85 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -734,6 +808,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 263CCF732766065B0061B353 /* Build configuration list for PBXNativeTarget "Day12" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 263CCF742766065B0061B353 /* Debug */, + 263CCF752766065B0061B353 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day12.swift b/day12.swift new file mode 100644 index 0000000..eef9531 --- /dev/null +++ b/day12.swift @@ -0,0 +1,99 @@ +@main +struct Day12: Puzzle { + func run() { + let rooms = input.reduce(into: Set()) { partialResult, pair in + partialResult.insert(pair.0) + partialResult.insert(pair.1) + }.sorted() + + let small = Set(rooms.enumerated().compactMap { $0.element.first!.isLowercase ? $0.offset : nil }) + + let matrix = buildMatrix(rooms, small: small) + + let startIndex = rooms.firstIndex(of: "start")! + let endIndex = rooms.firstIndex(of: "end")! + + let paths = matrix.findPaths(from: startIndex, to: endIndex, continuing: Path()) + + print("total paths", paths.count) + } + + struct Path { + var rooms: [Int] = [] + } + + struct Matrix { + var data: [Bool] + let size: Int + let small: Set + + func neighbors(of index: Int) -> [Int] { + let slice = data[index * size ..< (index + 1) * size] + return slice.enumerated().compactMap { $0.element ? $0.offset : nil } + } + + func findPaths(from: Int, to: Int, continuing: Path) -> [Path] { + guard from != to else { + return [continuing] + } + + var current = continuing + current.rooms.append(from) + var result: [Path] = [] + + for next in neighbors(of: from) { + if small.contains(next) && current.rooms.contains(next) { + continue + } + + let nextPaths = findPaths(from: next, to: to, continuing: current) + result.append(contentsOf: nextPaths) + } + + return result + } + } + + func buildMatrix(_ rooms: [String], small: Set) -> Matrix { + let count = rooms.count + var matrix = [Bool](repeating: false, count: count * count) + + for (from, to) in input { + let fromIndex = rooms.firstIndex(of: from)! + let toIndex = rooms.firstIndex(of: to)! + + matrix[fromIndex + count * toIndex] = true + matrix[toIndex + count * fromIndex] = true + } + + return Matrix(data: matrix, size: count, small: small) + } + + + let input: [(String, String)] = [ + ("rf", "RL"), + ("rf", "wz"), + ("wz", "RL"), + ("AV", "mh"), + ("end", "wz"), + ("end", "dm"), + ("wz", "gy"), + ("wz", "dm"), + ("cg", "AV"), + ("rf", "AV"), + ("rf", "gy"), + ("end", "mh"), + ("cg", "gy"), + ("cg", "RL"), + ("gy", "RL"), + ("VI", "gy"), + ("AV", "gy"), + ("dm", "rf"), + ("start", "cg"), + ("start", "RL"), + ("rf", "mh"), + ("AV", "start"), + ("qk", "mh"), + ("wz", "mh"), + ] +} From 0e3b825b3b7be1f4899aff174923787e51fab96d Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 12 Dec 2021 12:14:49 +0100 Subject: [PATCH 10/32] appending method --- day12.swift | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/day12.swift b/day12.swift index eef9531..766f205 100644 --- a/day12.swift +++ b/day12.swift @@ -20,6 +20,10 @@ struct Day12: Puzzle { struct Path { var rooms: [Int] = [] + + func appending(_ room: Int) -> Path { + Path(rooms: rooms + [room]) + } } struct Matrix { @@ -37,8 +41,7 @@ struct Day12: Puzzle { return [continuing] } - var current = continuing - current.rooms.append(from) + let current = continuing.appending(from) var result: [Path] = [] for next in neighbors(of: from) { From 9f919904abffff3aad86fd7310348fd6db22b422 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 12 Dec 2021 12:20:35 +0100 Subject: [PATCH 11/32] Refactor: Let path decide whether a room can be visited --- day12.swift | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/day12.swift b/day12.swift index 766f205..99461aa 100644 --- a/day12.swift +++ b/day12.swift @@ -21,8 +21,12 @@ struct Day12: Puzzle { struct Path { var rooms: [Int] = [] - func appending(_ room: Int) -> Path { - Path(rooms: rooms + [room]) + func appending(_ room: Int, small: Bool) -> Path? { + guard !small || !rooms.contains(room) else { + return nil + } + + return Path(rooms: rooms + [room]) } } @@ -41,19 +45,13 @@ struct Day12: Puzzle { return [continuing] } - let current = continuing.appending(from) - var result: [Path] = [] - - for next in neighbors(of: from) { - if small.contains(next) && current.rooms.contains(next) { - continue - } - - let nextPaths = findPaths(from: next, to: to, continuing: current) - result.append(contentsOf: nextPaths) + guard let current = continuing.appending(from, small: small.contains(from)) else { + return [] } - return result + return neighbors(of: from).reduce(into: []) { partialResult, next in + partialResult += findPaths(from: next, to: to, continuing: current) + } } } From 5c44a65f67da56922196151e5509ca436a42309b Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 12 Dec 2021 12:33:20 +0100 Subject: [PATCH 12/32] Day 12 Part 2 --- day12.swift | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/day12.swift b/day12.swift index 99461aa..aa645f6 100644 --- a/day12.swift +++ b/day12.swift @@ -1,3 +1,7 @@ +protocol PathProtocol { + func appending(_ room: Int, small: Bool) -> Self? +} + @main struct Day12: Puzzle { func run() { @@ -14,11 +18,13 @@ struct Day12: Puzzle { let endIndex = rooms.firstIndex(of: "end")! let paths = matrix.findPaths(from: startIndex, to: endIndex, continuing: Path()) - print("total paths", paths.count) + + let paths2 = matrix.findPaths(from: startIndex, to: endIndex, continuing: PathParth2(start: startIndex)) + print("part 2:", paths2.count) } - struct Path { + struct Path: PathProtocol { var rooms: [Int] = [] func appending(_ room: Int, small: Bool) -> Path? { @@ -30,6 +36,25 @@ struct Day12: Puzzle { } } + struct PathParth2: PathProtocol { + let start: Int + var rooms: [Int] = [] + var repeatedSmall: Bool = false + + func appending(_ room: Int, small: Bool) -> PathParth2? { + var rs = repeatedSmall + if small && rooms.contains(room) { + if room == start || repeatedSmall { + return nil + } + + rs = true + } + + return PathParth2(start: start, rooms: rooms + [room], repeatedSmall: rs) + } + } + struct Matrix { var data: [Bool] let size: Int @@ -40,13 +65,13 @@ struct Day12: Puzzle { return slice.enumerated().compactMap { $0.element ? $0.offset : nil } } - func findPaths(from: Int, to: Int, continuing: Path) -> [Path] { + func findPaths(from: Int, to: Int, continuing: Path) -> [Path] { guard from != to else { return [continuing] } guard let current = continuing.appending(from, small: small.contains(from)) else { - return [] + return [] } return neighbors(of: from).reduce(into: []) { partialResult, next in From e5f2ac72bd1933e4aa693099858e02d54892f192 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 13 Dec 2021 20:46:05 +0100 Subject: [PATCH 13/32] Day 13 --- AoC21.xcodeproj/project.pbxproj | 97 ++++ day13.swift | 931 ++++++++++++++++++++++++++++++++ 2 files changed, 1028 insertions(+) create mode 100644 day13.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 02aec33..9d36372 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -15,6 +15,8 @@ 263CCF6A276606010061B353 /* day11.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263CCF69276606010061B353 /* day11.swift */; }; 263CCF782766066D0061B353 /* day12.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263CCF76276606670061B353 /* day12.swift */; }; 263CCF79276606730061B353 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 265112972767D16D009B7607 /* day13.swift in Sources */ = {isa = PBXBuildFile; fileRef = 265112962767D16D009B7607 /* day13.swift */; }; + 265112982767D171009B7607 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; 269BE5CA2762A05300871C85 /* day9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5C82762A04F00871C85 /* day9.swift */; }; 269BE5CC2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; @@ -69,6 +71,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 2651128D2767D15A009B7607 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 269BE5B22762958800871C85 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -100,6 +111,8 @@ 263CCF69276606010061B353 /* day11.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day11.swift; sourceTree = ""; }; 263CCF6F2766065B0061B353 /* Day12 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day12; sourceTree = BUILT_PRODUCTS_DIR; }; 263CCF76276606670061B353 /* day12.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day12.swift; sourceTree = ""; }; + 2651128F2767D15A009B7607 /* Day13 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day13; sourceTree = BUILT_PRODUCTS_DIR; }; + 265112962767D16D009B7607 /* day13.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day13.swift; sourceTree = ""; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5BB2762959B00871C85 /* day8part2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day8part2.swift; sourceTree = ""; }; 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -143,6 +156,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2651128C2767D15A009B7607 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B12762958800871C85 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -163,6 +183,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 265112962767D16D009B7607 /* day13.swift */, 263CCF76276606670061B353 /* day12.swift */, 263CCF69276606010061B353 /* day11.swift */, 261F317E2763DACC0018D49C /* day10.swift */, @@ -185,6 +206,7 @@ 261F31772763DAB60018D49C /* Day10 */, 263CCF61276605320061B353 /* Day11 */, 263CCF6F2766065B0061B353 /* Day12 */, + 2651128F2767D15A009B7607 /* Day13 */, ); name = Products; sourceTree = ""; @@ -277,6 +299,23 @@ productReference = 263CCF6F2766065B0061B353 /* Day12 */; productType = "com.apple.product-type.tool"; }; + 2651128E2767D15A009B7607 /* Day13 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */; + buildPhases = ( + 2651128B2767D15A009B7607 /* Sources */, + 2651128C2767D15A009B7607 /* Frameworks */, + 2651128D2767D15A009B7607 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day13; + productName = Day13; + productReference = 2651128F2767D15A009B7607 /* Day13 */; + productType = "com.apple.product-type.tool"; + }; 269BE5B32762958800871C85 /* Day8Part2 */ = { isa = PBXNativeTarget; buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; @@ -339,6 +378,10 @@ 263CCF6E2766065B0061B353 = { CreatedOnToolsVersion = 13.1; }; + 2651128E2767D15A009B7607 = { + CreatedOnToolsVersion = 13.1; + LastSwiftMigration = 1310; + }; 269BE5B32762958800871C85 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -368,6 +411,7 @@ 261F31762763DAB60018D49C /* Day10 */, 263CCF60276605320061B353 /* Day11 */, 263CCF6E2766065B0061B353 /* Day12 */, + 2651128E2767D15A009B7607 /* Day13 */, ); }; /* End PBXProject section */ @@ -418,6 +462,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2651128B2767D15A009B7607 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 265112982767D171009B7607 /* common.swift in Sources */, + 265112972767D16D009B7607 /* day13.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B02762958800871C85 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -703,6 +756,41 @@ }; name = Release; }; + 265112932767D15A009B7607 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 265112942767D15A009B7607 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 269BE5B82762958800871C85 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -817,6 +905,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 265112932767D15A009B7607 /* Debug */, + 265112942767D15A009B7607 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day13.swift b/day13.swift new file mode 100644 index 0000000..4c4869f --- /dev/null +++ b/day13.swift @@ -0,0 +1,931 @@ +@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..= 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, max: Int) { + let mid = dots.partition { dot in dot[keyPath: keyPath] > limit } + for index in mid.. Date: Tue, 14 Dec 2021 22:33:06 +0100 Subject: [PATCH 14/32] Day 14 --- day14.swift | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 day14.swift diff --git a/day14.swift b/day14.swift new file mode 100644 index 0000000..692fa14 --- /dev/null +++ b/day14.swift @@ -0,0 +1,151 @@ +@main +struct Day14 { + mutating func run() { + let part1 = calculate(start, depth: 10) + print("Part 1:", part1.values.max()! - part1.values.min()!) + + let part2 = calculate(start, depth: 40) + print("Part 2:", part2.values.max()! - part2.values.min()!) + } + + mutating func calculate(_ string: String, depth: Int) -> [Character: Int] { + var result: [Character: Int] = [:] + for (a, b) in zip(string, string.dropFirst()) { + result.merge(calculate(a, b, depth: depth), uniquingKeysWith: +) + } + result[string.last!, default: 0] += 1 + return result + } + + struct CacheKey: Hashable { + var string: String + var depth: Int + } + var cache: [CacheKey: [Character: Int]] = [:] + + mutating func calculate(_ first: Character, _ second: Character, depth: Int) -> [Character: Int] { + guard depth > 0 else { + return [first: 1] + } + + let pair = "\(first)\(second)" + let cacheKey = CacheKey(string: pair, depth: depth) + + if let result = cache[cacheKey] { + return result + } + + let mid = mapping[pair]! + + var result = calculate(first, mid, depth: depth - 1) + result.merge(calculate(mid, second, depth: depth - 1), uniquingKeysWith: +) + + cache[cacheKey] = result + + return result + } + + let start = "KBKPHKHHNBCVCHPSPNHF" + let mapping: [String: Character] = [ + "OP": "H", + "CF": "C", + "BB": "V", + "KH": "O", + "CV": "S", + "FV": "O", + "FS": "K", + "KO": "C", + "PP": "S", + "SH": "K", + "FH": "O", + "NF": "H", + "PN": "P", + "BO": "H", + "OK": "K", + "PO": "P", + "SF": "K", + "BF": "P", + "HH": "S", + "KP": "H", + "HB": "N", + "NP": "V", + "KK": "P", + "PF": "P", + "BK": "V", + "OF": "H", + "FO": "S", + "VC": "P", + "FK": "B", + "NK": "S", + "CB": "B", + "PV": "C", + "CO": "N", + "BN": "C", + "HV": "H", + "OC": "N", + "NB": "O", + "CS": "S", + "HK": "C", + "VS": "F", + "BH": "C", + "PC": "S", + "KC": "O", + "VO": "P", + "FB": "K", + "BV": "V", + "VN": "N", + "ON": "F", + "VH": "H", + "CN": "O", + "HO": "O", + "SV": "O", + "SS": "H", + "KF": "N", + "SP": "C", + "NS": "V", + "SO": "F", + "BC": "P", + "HC": "C", + "FP": "H", + "OH": "S", + "OB": "S", + "HF": "V", + "SC": "B", + "SN": "N", + "VK": "C", + "NC": "V", + "VV": "S", + "SK": "K", + "PK": "K", + "PS": "N", + "KB": "S", + "KS": "C", + "NN": "C", + "OO": "C", + "BS": "B", + "NV": "H", + "FF": "P", + "FC": "N", + "OS": "H", + "KN": "N", + "VP": "B", + "PH": "N", + "NH": "S", + "OV": "O", + "FN": "V", + "CP": "B", + "NO": "V", + "CK": "C", + "VF": "B", + "HS": "B", + "KV": "K", + "VB": "H", + "SB": "S", + "BP": "S", + "CC": "F", + "HP": "B", + "PB": "P", + "HN": "P", + "CH": "O", + ] +} From 50dd18ea43faeb9bc66617143aae419823f3fd17 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 15 Dec 2021 19:35:04 +0100 Subject: [PATCH 15/32] Add day 14 to Xcode --- AoC21.xcodeproj/project.pbxproj | 97 +++++++++++++++++++++++++++++++++ day14.swift | 2 +- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 9d36372..1ca7e06 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 2615545A276A6C2C00374D18 /* day14.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26155459276A6C2C00374D18 /* day14.swift */; }; + 2615545B276A6C3200374D18 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 261F317F2763DACC0018D49C /* day10.swift in Sources */ = {isa = PBXBuildFile; fileRef = 261F317E2763DACC0018D49C /* day10.swift */; }; 261F31802763DACE0018D49C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 263BA5B3275E97AA00839C92 /* day5part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263BA5B2275E97AA00839C92 /* day5part2.swift */; }; @@ -26,6 +28,15 @@ /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ + 26155450276A6C1C00374D18 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 261F31752763DAB60018D49C /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -101,6 +112,8 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 26155452276A6C1C00374D18 /* Day14 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day14; sourceTree = BUILT_PRODUCTS_DIR; }; + 26155459276A6C2C00374D18 /* day14.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day14.swift; sourceTree = ""; }; 261F31772763DAB60018D49C /* Day10 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day10; sourceTree = BUILT_PRODUCTS_DIR; }; 261F317E2763DACC0018D49C /* day10.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day10.swift; sourceTree = ""; }; 263BA5AB275E978400839C92 /* Day5Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day5Part2; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -121,6 +134,13 @@ /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ + 2615544F276A6C1C00374D18 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 261F31742763DAB60018D49C /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -183,6 +203,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 26155459276A6C2C00374D18 /* day14.swift */, 265112962767D16D009B7607 /* day13.swift */, 263CCF76276606670061B353 /* day12.swift */, 263CCF69276606010061B353 /* day11.swift */, @@ -207,6 +228,7 @@ 263CCF61276605320061B353 /* Day11 */, 263CCF6F2766065B0061B353 /* Day12 */, 2651128F2767D15A009B7607 /* Day13 */, + 26155452276A6C1C00374D18 /* Day14 */, ); name = Products; sourceTree = ""; @@ -214,6 +236,23 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ + 26155451276A6C1C00374D18 /* Day14 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 26155458276A6C1C00374D18 /* Build configuration list for PBXNativeTarget "Day14" */; + buildPhases = ( + 2615544E276A6C1C00374D18 /* Sources */, + 2615544F276A6C1C00374D18 /* Frameworks */, + 26155450276A6C1C00374D18 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day14; + productName = Day14; + productReference = 26155452276A6C1C00374D18 /* Day14 */; + productType = "com.apple.product-type.tool"; + }; 261F31762763DAB60018D49C /* Day10 */ = { isa = PBXNativeTarget; buildConfigurationList = 261F317D2763DAB60018D49C /* Build configuration list for PBXNativeTarget "Day10" */; @@ -360,6 +399,10 @@ LastSwiftUpdateCheck = 1310; LastUpgradeCheck = 1310; TargetAttributes = { + 26155451276A6C1C00374D18 = { + CreatedOnToolsVersion = 13.1; + LastSwiftMigration = 1310; + }; 261F31762763DAB60018D49C = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -412,11 +455,21 @@ 263CCF60276605320061B353 /* Day11 */, 263CCF6E2766065B0061B353 /* Day12 */, 2651128E2767D15A009B7607 /* Day13 */, + 26155451276A6C1C00374D18 /* Day14 */, ); }; /* End PBXProject section */ /* Begin PBXSourcesBuildPhase section */ + 2615544E276A6C1C00374D18 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 2615545B276A6C3200374D18 /* common.swift in Sources */, + 2615545A276A6C2C00374D18 /* day14.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 261F31732763DAB60018D49C /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -492,6 +545,41 @@ /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ + 26155456276A6C1C00374D18 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 26155457276A6C1C00374D18 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 261F317B2763DAB60018D49C /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -851,6 +939,15 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ + 26155458276A6C1C00374D18 /* Build configuration list for PBXNativeTarget "Day14" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 26155456276A6C1C00374D18 /* Debug */, + 26155457276A6C1C00374D18 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 261F317D2763DAB60018D49C /* Build configuration list for PBXNativeTarget "Day10" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day14.swift b/day14.swift index 692fa14..3ad017e 100644 --- a/day14.swift +++ b/day14.swift @@ -1,5 +1,5 @@ @main -struct Day14 { +struct Day14: Puzzle { mutating func run() { let part1 = calculate(start, depth: 10) print("Part 1:", part1.values.max()! - part1.values.min()!) From e34db0fa0206bc196f7f07ea7cb31c6efdbc48bc Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 15 Dec 2021 19:35:16 +0100 Subject: [PATCH 16/32] Print duration --- common.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common.swift b/common.swift index 1df8bf4..3716c97 100644 --- a/common.swift +++ b/common.swift @@ -1,3 +1,5 @@ +import Foundation + protocol Puzzle { mutating func run() init() @@ -5,8 +7,15 @@ protocol Puzzle { extension Puzzle { static func main() { + let start = Date() var instance = Self() instance.run() + let duration = Date().timeIntervalSince(start) + if duration > 1 { + print(String(format: "Took %.2f s", duration)) + } else { + print(String(format: "Took %.2f ms", 1000 * duration)) + } } } From 630829ea2fe15ac0406d012b13447e76e52ea229 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 15 Dec 2021 20:57:10 +0100 Subject: [PATCH 17/32] Day 15 --- AoC21.xcodeproj/project.pbxproj | 97 ++++++++++++++++++ day15.swift | 171 ++++++++++++++++++++++++++++++++ 2 files changed, 268 insertions(+) create mode 100644 day15.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 1ca7e06..366484e 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -9,6 +9,8 @@ /* Begin PBXBuildFile section */ 2615545A276A6C2C00374D18 /* day14.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26155459276A6C2C00374D18 /* day14.swift */; }; 2615545B276A6C3200374D18 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 26155468276A6D0A00374D18 /* day15.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26155467276A6D0A00374D18 /* day15.swift */; }; + 26155469276A6D2400374D18 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 261F317F2763DACC0018D49C /* day10.swift in Sources */ = {isa = PBXBuildFile; fileRef = 261F317E2763DACC0018D49C /* day10.swift */; }; 261F31802763DACE0018D49C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 263BA5B3275E97AA00839C92 /* day5part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263BA5B2275E97AA00839C92 /* day5part2.swift */; }; @@ -37,6 +39,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 2615545E276A6CF700374D18 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 261F31752763DAB60018D49C /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -114,6 +125,8 @@ /* Begin PBXFileReference section */ 26155452276A6C1C00374D18 /* Day14 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day14; sourceTree = BUILT_PRODUCTS_DIR; }; 26155459276A6C2C00374D18 /* day14.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day14.swift; sourceTree = ""; }; + 26155460276A6CF700374D18 /* Day15 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day15; sourceTree = BUILT_PRODUCTS_DIR; }; + 26155467276A6D0A00374D18 /* day15.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day15.swift; sourceTree = ""; }; 261F31772763DAB60018D49C /* Day10 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day10; sourceTree = BUILT_PRODUCTS_DIR; }; 261F317E2763DACC0018D49C /* day10.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day10.swift; sourceTree = ""; }; 263BA5AB275E978400839C92 /* Day5Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day5Part2; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -141,6 +154,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2615545D276A6CF700374D18 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 261F31742763DAB60018D49C /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -203,6 +223,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 26155467276A6D0A00374D18 /* day15.swift */, 26155459276A6C2C00374D18 /* day14.swift */, 265112962767D16D009B7607 /* day13.swift */, 263CCF76276606670061B353 /* day12.swift */, @@ -229,6 +250,7 @@ 263CCF6F2766065B0061B353 /* Day12 */, 2651128F2767D15A009B7607 /* Day13 */, 26155452276A6C1C00374D18 /* Day14 */, + 26155460276A6CF700374D18 /* Day15 */, ); name = Products; sourceTree = ""; @@ -253,6 +275,23 @@ productReference = 26155452276A6C1C00374D18 /* Day14 */; productType = "com.apple.product-type.tool"; }; + 2615545F276A6CF700374D18 /* Day15 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 26155464276A6CF700374D18 /* Build configuration list for PBXNativeTarget "Day15" */; + buildPhases = ( + 2615545C276A6CF700374D18 /* Sources */, + 2615545D276A6CF700374D18 /* Frameworks */, + 2615545E276A6CF700374D18 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day15; + productName = Day15; + productReference = 26155460276A6CF700374D18 /* Day15 */; + productType = "com.apple.product-type.tool"; + }; 261F31762763DAB60018D49C /* Day10 */ = { isa = PBXNativeTarget; buildConfigurationList = 261F317D2763DAB60018D49C /* Build configuration list for PBXNativeTarget "Day10" */; @@ -403,6 +442,10 @@ CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; }; + 2615545F276A6CF700374D18 = { + CreatedOnToolsVersion = 13.1; + LastSwiftMigration = 1310; + }; 261F31762763DAB60018D49C = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -456,6 +499,7 @@ 263CCF6E2766065B0061B353 /* Day12 */, 2651128E2767D15A009B7607 /* Day13 */, 26155451276A6C1C00374D18 /* Day14 */, + 2615545F276A6CF700374D18 /* Day15 */, ); }; /* End PBXProject section */ @@ -470,6 +514,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2615545C276A6CF700374D18 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 26155469276A6D2400374D18 /* common.swift in Sources */, + 26155468276A6D0A00374D18 /* day15.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 261F31732763DAB60018D49C /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -580,6 +633,41 @@ }; name = Release; }; + 26155465276A6CF700374D18 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 26155466276A6CF700374D18 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 261F317B2763DAB60018D49C /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -948,6 +1036,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 26155464276A6CF700374D18 /* Build configuration list for PBXNativeTarget "Day15" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 26155465276A6CF700374D18 /* Debug */, + 26155466276A6CF700374D18 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 261F317D2763DAB60018D49C /* Build configuration list for PBXNativeTarget "Day10" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day15.swift b/day15.swift new file mode 100644 index 0000000..16f4b5b --- /dev/null +++ b/day15.swift @@ -0,0 +1,171 @@ +@main +struct Day15: Puzzle { + mutating func run() { + let part1 = find(target: Point(x: width - 1, y: height - 1)) + print("Part 1:", part1) + + let part2 = find(target: Point(x: 5 * width - 1, y: 5 * height - 1)) + print("Part 2:", part2) + } + + struct Point: Hashable { + var x: Int + var y: Int + + var neighbors: [Point] { + [ + Point(x: x - 1, y: y), + Point(x: x + 1, y: y), + Point(x: x, y: y - 1), + Point(x: x, y: y + 1) + ] + } + } + + func find(target: Point) -> Int { + var current = Point(x: 0, y: 0) + var totalCost = 0 + + var possible: [Point: Int] = [:] + var visited: Set = [] + + func isInside(_ point: Point) -> Bool { + 0...target.x ~= point.x && 0...target.y ~= point.y + } + + while current != target { + visited.insert(current) + + for n in current.neighbors where isInside(n) && !visited.contains(n) { + possible[n] = min(possible[n, default: .max], totalCost + self[n]) + } + + (current, totalCost) = possible.min { $0.value < $1.value }! + + possible.removeValue(forKey: current) + } + + return totalCost + } + + subscript(point: Point) -> Int { + self[point.x , point.y] + } + + subscript(x: Int, y: Int) -> Int { + let repX = x / width + let xPos = x % width + + let repY = y / height + let yPos = y % height + + return (input[xPos + width * yPos] + repX + repY - 1) % 9 + 1 + } + + let width = 100 + let height = 100 + + let input: [Int] = """ +8576219475874583191916312133474175459337114195988185136398151631965391991813219974121211251194786128 +1713881999231519357799114192443351147195293575386923868711388519361669464234397975938889146414199688 +2151922113728581598379349983928489926261239862882888968998791929828316718921297117449631496888879522 +3291722311299652548445221254456535112944431135116631433192483199481594768169444158222431199929579291 +1472287315918113113959972189351931626141472991992791413146153989481318998519171231313898962992319251 +8978798318873934543151823824117132228417869392811216547172919119998739911121199246294815175868992773 +4711149989992717728465321161611967173674239298593935117599173279267114155265393397571372124856718732 +9843245319692998612781837729915931371619886371128818266322121623171343132131239399743799827972585645 +7183211712173272827112268971459424836293613294388761928991542137194981939169315879277494581991465822 +2385721996578834579224651431411717198341139751429156637229888849181154922897746418293184413519931124 +6188313732929496115412842928273812153715747411177189187726751991191841181119822534511125656211991265 +8797112815141319881991983914861912838966446818811271274141461487947171138945321912192198815813211761 +6131514331472951494911549373681638637191295997625615885219668259269297171939941498581919718223529141 +3916711951831246219991217199412116783411218814949692281987659123812288937983969116847986196936523779 +9111121227192813382983442234171148191131821497435387936912532695293912972491131977272231919798477344 +9994893855122115213919995636978443773161196511775674783979472217917498285423121574533491643891265199 +9245818921321877317663254739619499915821162412924816263648913499811482474392667993739139899758919393 +1717618849193152316325119128859116191132672215933282127611134982961918179152189411131259943819997123 +3697848795188715264992962617223177221118919863975198881197837936258983111819488622362584888223331999 +1798259211962116675922944295117391732295963342131192112612386194749389991199821195162975124619149837 +5365696872959269494682128272373619348283964191994187749824841195582221478325111187843285531119934786 +6717218734155417511564162235845524121493736962442884479191718924594119911521121422231982184671122116 +9346412964321187889663345611132183519588187791111227269112129947122999176114184938494824342939375892 +2912811929919891428891223818529115949522464779949526791515797886471246181118183121786524127979998512 +7353285281129811915971956929718433284441492484117162562212973157265812816731843791838571691421923924 +3239161911151141369392413114665193927469988999219478935992541193184945926828911597158489999651151172 +9229716159183212219791136121569512978927999177196111291668152292896495877722611129462617229299962112 +5994461796358342348216915883991574569784887978731368749888449599246152414391635925763156831839199496 +1569319989929452917219726368583887924179117119284971126181346919789949393972171262535283921926293357 +9983673949614595396426594299567925779229751982433631919159114841538978735893759492592481573495963491 +3725632421623191323789991288299296415942162988289929992238877153268488127718372211951117951486111926 +1219319492614741819142363452974731825929485665978219725131218962626179997611254524524915768686381164 +8248145978417221154822663831966124539168641311913211295941167919583189792718599334616827916567313217 +3483959252172951953519211189255336234459522397526829492685113849198611624111948187714151963198671919 +7812289559812455122297839958719917624841136314322613942214613249662544617928521344873779598816941946 +6868958791119815122313931178313637489923479492171741443993619161318781194421239654468391996636923611 +6999166111129212624151555925991311538952878946191234511973499736237742491811348911419449161895978178 +3532973784281182181552481928144111246711742443831184236962981395142863562121961323169398316921741321 +8433727191521455193223914171391447439164791659118411391327413285243717794299999523811137619197999791 +8169125649963232439519311131691539694589231314268683691811431259295132344195953951287639889737849189 +8928161919934761971442719391119881113318323987227229831917897151155121657624142152884624176461913498 +2949199492196424164751892146395174669111921799261527568751283712133148212821261936297739912939743599 +9931616797885267969938591912672996183187926259991298429278229232257219428413174972999193796827459799 +3138768154311742899419449741617489465993174645228814938145273266941566223933119891379181296548999222 +1338961668848636157444719898575174699419997972125194827176897565699991611654214368232592793825617931 +9283643143692917314535119897889711191946688911436965194232811624712861576291987716317193114445363435 +8718611398299869991788113111214414269554542114329621286592313532782819131919527228417764279755899812 +8128249329314753847725634378737239939769913811444529224619715471961999278915341955188871714271145449 +1286217894699991715153139268991918988338684391252271254127193342854156438873836258484975151167996316 +4191439845728418348368191516746499654147257851256861534634988281199187541199892317145428248531129171 +9799283477747387253566811199414256363339122372629545249542373591951156699415998714124115397811359516 +3191985767689943186838293175311429527499612824265518522881922917119891424717751985621941995373481629 +8127933395192162886259956635985821651844893796541627799276258692298972881471991789861232424926758792 +4349683335349673317521761437148933192989961489478395417258212949865876242843242464123471244631449998 +3983861771861891251448475182984833811718959521263444111998834975935221115813839514917176912994677265 +3361812177831144971531441184671112122322312961859915722393944269211333951428175372918136112814926411 +2492891241191991149319731182431211211971267682931551534199328118919751416881458658314218331565282229 +1316191719349114251772629411233483317516143684488291792691839198723174287211241498955137331159443961 +1295632882814319179937625629218199381191127966181781921829819969815912382191294796979515315471941392 +1952318366571121641614137937116326339638859628226299126699118638171813164397332391891669798271559135 +1123676641998146639499323488991286491444174358479157916696196841912233773269986812299996975117612192 +2496461124219121332118311532239527696198787778122345883331631119147159212262659717289674149798169111 +3994915157217763242581922474512992479146377117411981999549977495462427199524334285127723311599337186 +2856249932899117429471617195415229923432185189218399415357391954728651191591964713418532917111296399 +1916415314958527255299763241981929186681257596912948977397875248189748655173819381269954513499896991 +4214987991254389723346911149661118597277889213928551947599177915888233175182921923128853579525754718 +3582279578128192231339943295188563998645998149526284997789731787116991199391984141983177825883111821 +3318115949152512995142691231324793411691198458654473675135993122216324137859959272731599996923465431 +9431374329994955614816239978928993493129129641248882311462522524447519388869238199852941551532223632 +6749132793518479891929891686579791123712167784913331559922969149125915827657111724348929978159964476 +6899888192198113996189996152913216258469182711411338723293823541574149295918323829819122941288693461 +9833728958423343229397292861582211971713299293611778961979819175799893793971822162383179128129741328 +9719459171294372288418126215914532995125999274359633129923191213332337513812761149562146497198614329 +6323381739518574154814713691189651618965915153972197836392492518147816118266818195856772641337997448 +9515391698994467556612815425651153225477392335219196697477512338938131933398177519117437353271396315 +2881769116189531115832167881372917543267529333184621316918481786111252281738298936313393416198972192 +1262821848371618529985323295192217356152412696271318793198457225899782896767339942896611231999996177 +7611115824284211421951282951346181221891123993118231772195952872865982323819415318945742389969468591 +6289176957971112617212793915412958239574931721216722823933461763146195116737161463663388274118352919 +2739192179312181318764617662499189345995579913463399176128133623751919135855775349195561978131664671 +7419179135228196296753972713643556671138381494838116814212892453118963456121883515171981991493112197 +2351962484381996196119791327953881225881124837211622273581789216339733276411173141146816794919111532 +4828494193941149581788515219884312818182198261739598611663311138255885557141492174934279519487959315 +6266441934234532987821537914696156313934795144412166199339199381197916324959558778257894958593689511 +1218836684761811498991162549547375891999161535133511817388897491266957819219117988952234747197245136 +7714718988198712181711441996373181351671962319416917816121119483959397254815953421521642116185224622 +6315878819598412161782318179586237893968466116199183185764132999388411414821117761121199725212571499 +9298381112525419214597534611761811137687414291394795245955115116482261788724982532573616634235949295 +2513837618149681781571514782119218925111312739891194994857915371836413112952729282168681511999132471 +9859244419394114469927618849921646911349163815889492121537711577378978633345419421881312649299234749 +6241913851991287776811271361777919519213726771122769111499925516752149976397933753482495291399144132 +7549785323789821947513966787125532113931855716249321712383292746672763573114656989299712174939322181 +9188852123922512181114117536955617335173328988385462159798591117758397298294919117614163738414981949 +2974419341361292817129453536824618155299993142149899966226312193848695911943416589913541194129913422 +1672761999113289962322634642122163368424955339864962951462929691975798622972131599198673671183449929 +3462285147627397867977869242451116812198183328383813959284449969199819896174618466572298882951459875 +6153638634925613615423256118159531429294172496996993184762972841759153392753653191942715379879852345 +6516291128536155113712481272992636373774429965994966353494115221979925592128929821611928492143929534 +3993993153713717844719492314391992638719111242291783837279215399219822611221331985991484863975594721 +5995891457939115884519762228128552168915218299831775259316392719113962218429351553267511523198149462 +""".compactMap { $0.wholeNumberValue } + +} From c5129af5a161ba4775f183b2bff23ebaf006a892 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 16 Dec 2021 19:06:25 +0100 Subject: [PATCH 18/32] Day 16 --- AoC21.xcodeproj/project.pbxproj | 87 ++++++++++++++++++++- day16.swift | 131 ++++++++++++++++++++++++++++++++ 2 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 day16.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 366484e..05ac05b 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -27,6 +27,8 @@ 269BE5CD2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5CE2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5CF2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 26EA5DFE276BA670003E0305 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 26EA5E00276BA680003E0305 /* day16.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26EA5DFF276BA680003E0305 /* day16.swift */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -120,6 +122,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 26EA5DF5276BA668003E0305 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ @@ -144,6 +155,8 @@ 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5C82762A04F00871C85 /* day9.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day9.swift; sourceTree = ""; }; 269BE5CB2762A08800871C85 /* common.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = common.swift; sourceTree = ""; }; + 26EA5DF7276BA668003E0305 /* Day16 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day16; sourceTree = BUILT_PRODUCTS_DIR; }; + 26EA5DFF276BA680003E0305 /* day16.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day16.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -217,12 +230,20 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26EA5DF4276BA668003E0305 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 26EA5DFF276BA680003E0305 /* day16.swift */, 26155467276A6D0A00374D18 /* day15.swift */, 26155459276A6C2C00374D18 /* day14.swift */, 265112962767D16D009B7607 /* day13.swift */, @@ -251,6 +272,7 @@ 2651128F2767D15A009B7607 /* Day13 */, 26155452276A6C1C00374D18 /* Day14 */, 26155460276A6CF700374D18 /* Day15 */, + 26EA5DF7276BA668003E0305 /* Day16 */, ); name = Products; sourceTree = ""; @@ -428,6 +450,23 @@ productReference = 269BE5C12762A03F00871C85 /* Day9 */; productType = "com.apple.product-type.tool"; }; + 26EA5DF6276BA668003E0305 /* Day16 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 26EA5DFD276BA668003E0305 /* Build configuration list for PBXNativeTarget "Day16" */; + buildPhases = ( + 26EA5DF3276BA668003E0305 /* Sources */, + 26EA5DF4276BA668003E0305 /* Frameworks */, + 26EA5DF5276BA668003E0305 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day16; + productName = Day16; + productReference = 26EA5DF7276BA668003E0305 /* Day16 */; + productType = "com.apple.product-type.tool"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -435,7 +474,7 @@ isa = PBXProject; attributes = { BuildIndependentTargetsInParallel = 1; - LastSwiftUpdateCheck = 1310; + LastSwiftUpdateCheck = 1320; LastUpgradeCheck = 1310; TargetAttributes = { 26155451276A6C1C00374D18 = { @@ -475,6 +514,9 @@ 269BE5C02762A03F00871C85 = { CreatedOnToolsVersion = 13.1; }; + 26EA5DF6276BA668003E0305 = { + CreatedOnToolsVersion = 13.2; + }; }; }; buildConfigurationList = 263BA598275E974800839C92 /* Build configuration list for PBXProject "AoC21" */; @@ -500,6 +542,7 @@ 2651128E2767D15A009B7607 /* Day13 */, 26155451276A6C1C00374D18 /* Day14 */, 2615545F276A6CF700374D18 /* Day15 */, + 26EA5DF6276BA668003E0305 /* Day16 */, ); }; /* End PBXProject section */ @@ -595,6 +638,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26EA5DF3276BA668003E0305 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 26EA5DFE276BA670003E0305 /* common.swift in Sources */, + 26EA5E00276BA680003E0305 /* day16.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ @@ -1024,6 +1076,30 @@ }; name = Release; }; + 26EA5DFB276BA668003E0305 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 26EA5DFC276BA668003E0305 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -1126,6 +1202,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 26EA5DFD276BA668003E0305 /* Build configuration list for PBXNativeTarget "Day16" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 26EA5DFB276BA668003E0305 /* Debug */, + 26EA5DFC276BA668003E0305 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ }; rootObject = 263BA595275E974800839C92 /* Project object */; diff --git a/day16.swift b/day16.swift new file mode 100644 index 0000000..7bf4ac2 --- /dev/null +++ b/day16.swift @@ -0,0 +1,131 @@ +@main +struct Day16: Puzzle { + mutating func run() { + var start = Cursor.start + + let part2 = packet(at: &start) + print("Part 1:", versionSum) + print("Part 2:", part2) + } + + var versionSum = 0 + + mutating func packet(at cursor: inout Cursor) -> UInt64 { + let version = getBits(at: &cursor, count: 3) + let type = getBits(at: &cursor, count: 3) + + versionSum += Int(version) + + if type == 4 { + return literal(at: &cursor) + } + + let subPackets = operatorPacket(at: &cursor) + + switch type { + case 0: return subPackets.reduce(0, +) + case 1: return subPackets.reduce(1, *) + case 2: return subPackets.min()! + case 3: return subPackets.max()! + case 5: return subPackets[0] > subPackets[1] ? 1 : 0 + case 6: return subPackets[0] < subPackets[1] ? 1 : 0 + case 7: return subPackets[0] == subPackets[1] ? 1 : 0 + default: fatalError("Unknown packet type \(type)") + } + + } + + func literal(at cursor: inout Cursor) -> UInt64 { + var result: UInt64 = 0 + while getBit(at: &cursor) { + result = result << 4 | getBits(at: &cursor, count: 4) + } + result = result << 4 | getBits(at: &cursor, count: 4) + return result + } + + mutating func operatorPacket(at cursor: inout Cursor) -> [UInt64] { + let type = getBit(at: &cursor) + var result: [UInt64] = [] + if type { + let count = getBits(at: &cursor, count: 11) + for _ in 0.. Cursor { + var result = self + result.skip(bits) + return result + } + + var bitPos: Int { + offset * 4 + bit + } + + static func < (lhs: Cursor, rhs: Cursor) -> Bool { + lhs.bitPos < rhs.bitPos + } + } + + func getBit(at cursor: inout Cursor) -> Bool { + getBits(at: &cursor, count: 1) == 1 + } + + func getBitRange(_ x: Int, start: Int, count: Int) -> UInt64 { + let mask = UInt64((1 << count) - 1) + let shift = 4 - count - start + precondition(shift >= 0) + + return UInt64(x) >> shift & mask + } + + func getBits(at cursor: inout Cursor, count: Int) -> UInt64 { + var remaining = count + var result: UInt64 = 0 + + while remaining > 0 { + let take = min(cursor.remainingBits, remaining) + let bits = getBitRange(input[cursor.offset], start: cursor.bit, count: take) + + result = result << take | bits + remaining -= take + + cursor.bit += take + if cursor.bit == 4 { + cursor.bit = 0 + cursor.offset += 1 + } + } + + return result + } + + let input = """ +E20D79005573F71DA0054E48527EF97D3004653BB1FC006867A8B1371AC49C801039171941340066E6B99A6A58B8110088BA008CE6F7893D4E6F7893DCDCFDB9D6CBC4026FE8026200DC7D84B1C00010A89507E3CCEE37B592014D3C01491B6697A83CB4F59E5E7FFA5CC66D4BC6F05D3004E6BB742B004E7E6B3375A46CF91D8C027911797589E17920F4009BE72DA8D2E4523DCEE86A8018C4AD3C7F2D2D02C5B9FF53366E3004658DB0012A963891D168801D08480485B005C0010A883116308002171AA24C679E0394EB898023331E60AB401294D98CA6CD8C01D9B349E0A99363003E655D40289CBDBB2F55D25E53ECAF14D9ABBB4CC726F038C011B0044401987D0BE0C00021B04E2546499DE824C015B004A7755B570013F2DD8627C65C02186F2996E9CCD04E5718C5CBCC016B004A4F61B27B0D9B8633F9344D57B0C1D3805537ADFA21F231C6EC9F3D3089FF7CD25E5941200C96801F191C77091238EE13A704A7CCC802B3B00567F192296259ABD9C400282915B9F6E98879823046C0010C626C966A19351EE27DE86C8E6968F2BE3D2008EE540FC01196989CD9410055725480D60025737BA1547D700727B9A89B444971830070401F8D70BA3B8803F16A3FC2D00043621C3B8A733C8BD880212BCDEE9D34929164D5CB08032594E5E1D25C0055E5B771E966783240220CD19E802E200F4588450BC401A8FB14E0A1805B36F3243B2833247536B70BDC00A60348880C7730039400B402A91009F650028C00E2020918077610021C00C1002D80512601188803B4000C148025010036727EE5AD6B445CC011E00B825E14F4BBF5F97853D2EFD6256F8FFE9F3B001420C01A88915E259002191EE2F4392004323E44A8B4C0069CEF34D304C001AB94379D149BD904507004A6D466B618402477802E200D47383719C0010F8A507A294CC9C90024A967C9995EE2933BA840 +""".compactMap { $0.hexDigitValue } +} From 92e0895ed252868c1421ffc93968a77a3eb6ab08 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 16 Dec 2021 19:07:09 +0100 Subject: [PATCH 19/32] Xcode 11.4 format --- AoC21.xcodeproj/project.pbxproj | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 05ac05b..614d3a5 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 55; + objectVersion = 53; objects = { /* Begin PBXBuildFile section */ @@ -476,6 +476,7 @@ BuildIndependentTargetsInParallel = 1; LastSwiftUpdateCheck = 1320; LastUpgradeCheck = 1310; + ORGANIZATIONNAME = 5sw; TargetAttributes = { 26155451276A6C1C00374D18 = { CreatedOnToolsVersion = 13.1; @@ -520,7 +521,7 @@ }; }; buildConfigurationList = 263BA598275E974800839C92 /* Build configuration list for PBXProject "AoC21" */; - compatibilityVersion = "Xcode 13.0"; + compatibilityVersion = "Xcode 11.4"; developmentRegion = en; hasScannedForEncodings = 0; knownRegions = ( From 3ba710552d43c47b5ed09f484ec76d1bb60d3f18 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 18 Dec 2021 13:33:50 +0100 Subject: [PATCH 20/32] Day 17 --- AoC21.xcodeproj/project.pbxproj | 99 +++++++++++++++++++++++++++++++++ day17.swift | 71 +++++++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 day17.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 614d3a5..2ac86f0 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -19,6 +19,8 @@ 263CCF6A276606010061B353 /* day11.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263CCF69276606010061B353 /* day11.swift */; }; 263CCF782766066D0061B353 /* day12.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263CCF76276606670061B353 /* day12.swift */; }; 263CCF79276606730061B353 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 263D3E33276DDA6600B0AC5B /* day17.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263D3E32276DDA6600B0AC5B /* day17.swift */; }; + 263D3E34276DDA7000B0AC5B /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 265112972767D16D009B7607 /* day13.swift in Sources */ = {isa = PBXBuildFile; fileRef = 265112962767D16D009B7607 /* day13.swift */; }; 265112982767D171009B7607 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; @@ -95,6 +97,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 263D3E29276DDA5400B0AC5B /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 2651128D2767D15A009B7607 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -148,6 +159,8 @@ 263CCF69276606010061B353 /* day11.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day11.swift; sourceTree = ""; }; 263CCF6F2766065B0061B353 /* Day12 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day12; sourceTree = BUILT_PRODUCTS_DIR; }; 263CCF76276606670061B353 /* day12.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day12.swift; sourceTree = ""; }; + 263D3E2B276DDA5400B0AC5B /* Day17 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day17; sourceTree = BUILT_PRODUCTS_DIR; }; + 263D3E32276DDA6600B0AC5B /* day17.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day17.swift; sourceTree = ""; }; 2651128F2767D15A009B7607 /* Day13 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day13; sourceTree = BUILT_PRODUCTS_DIR; }; 265112962767D16D009B7607 /* day13.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day13.swift; sourceTree = ""; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -209,6 +222,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263D3E28276DDA5400B0AC5B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2651128C2767D15A009B7607 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -243,6 +263,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 263D3E32276DDA6600B0AC5B /* day17.swift */, 26EA5DFF276BA680003E0305 /* day16.swift */, 26155467276A6D0A00374D18 /* day15.swift */, 26155459276A6C2C00374D18 /* day14.swift */, @@ -273,6 +294,7 @@ 26155452276A6C1C00374D18 /* Day14 */, 26155460276A6CF700374D18 /* Day15 */, 26EA5DF7276BA668003E0305 /* Day16 */, + 263D3E2B276DDA5400B0AC5B /* Day17 */, ); name = Products; sourceTree = ""; @@ -399,6 +421,23 @@ productReference = 263CCF6F2766065B0061B353 /* Day12 */; productType = "com.apple.product-type.tool"; }; + 263D3E2A276DDA5400B0AC5B /* Day17 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 263D3E31276DDA5400B0AC5B /* Build configuration list for PBXNativeTarget "Day17" */; + buildPhases = ( + 263D3E27276DDA5400B0AC5B /* Sources */, + 263D3E28276DDA5400B0AC5B /* Frameworks */, + 263D3E29276DDA5400B0AC5B /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day17; + productName = Day17; + productReference = 263D3E2B276DDA5400B0AC5B /* Day17 */; + productType = "com.apple.product-type.tool"; + }; 2651128E2767D15A009B7607 /* Day13 */ = { isa = PBXNativeTarget; buildConfigurationList = 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */; @@ -504,6 +543,10 @@ 263CCF6E2766065B0061B353 = { CreatedOnToolsVersion = 13.1; }; + 263D3E2A276DDA5400B0AC5B = { + CreatedOnToolsVersion = 13.2; + LastSwiftMigration = 1320; + }; 2651128E2767D15A009B7607 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -544,6 +587,7 @@ 26155451276A6C1C00374D18 /* Day14 */, 2615545F276A6CF700374D18 /* Day15 */, 26EA5DF6276BA668003E0305 /* Day16 */, + 263D3E2A276DDA5400B0AC5B /* Day17 */, ); }; /* End PBXProject section */ @@ -612,6 +656,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263D3E27276DDA5400B0AC5B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 263D3E34276DDA7000B0AC5B /* common.swift in Sources */, + 263D3E33276DDA6600B0AC5B /* day17.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2651128B2767D15A009B7607 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -985,6 +1038,43 @@ }; name = Release; }; + 263D3E2F276DDA5400B0AC5B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 263D3E30276DDA5400B0AC5B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 265112932767D15A009B7607 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1176,6 +1266,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 263D3E31276DDA5400B0AC5B /* Build configuration list for PBXNativeTarget "Day17" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 263D3E2F276DDA5400B0AC5B /* Debug */, + 263D3E30276DDA5400B0AC5B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day17.swift b/day17.swift new file mode 100644 index 0000000..9d17df7 --- /dev/null +++ b/day17.swift @@ -0,0 +1,71 @@ +@main +struct Day17: Puzzle { + func run() { + var maxHeight: Int = 0 + var count = 0 + + for speedX in 1..<300 { + for speedY in -600..<300 { + if case .hit(let height) = run(vx: speedX, vy: speedY) { + maxHeight = max(height, maxHeight) + count += 1 + } + } + } + + print("Part 1:", maxHeight) + print("Part 2:", count) + } + + enum Result { + case tooFar + case tooShort + case hit(height: Int) + } + + struct Vector: Hashable { + var x: Int + var y: Int + } + + func run(vx: Int, vy: Int) -> Result { + var state = State(vx: vx, vy: vy) + while state.x <= xMax && state.y >= yMin { + state.step() + if xMin...xMax ~= state.x && yMin...yMax ~= state.y { + return .hit(height: state.maxHeight) + } + } + + if state.x > xMax { + return .tooFar + } + + return .tooShort + } + + struct State { + var vx: Int + var vy: Int + var x: Int = 0 + var y: Int = 0 + var maxHeight = 0 + + mutating func step() { + if y > maxHeight { + maxHeight = y + } + + x += vx + y += vy + vx -= vx.signum() + vy -= 1 + } + } + + // target area: x=138..184, y=-125..-71 + let xMin = 138 + let xMax = 184 + let yMin = -125 + let yMax = -71 +} From aa37e6ae99b2c87eaff2d3c57d41d925c67cefcb Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 18 Dec 2021 14:59:37 +0100 Subject: [PATCH 21/32] Day 18 --- AoC21.xcodeproj/project.pbxproj | 85 +++++++++ day18.swift | 318 ++++++++++++++++++++++++++++++++ 2 files changed, 403 insertions(+) create mode 100644 day18.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 2ac86f0..0b352ff 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -21,6 +21,8 @@ 263CCF79276606730061B353 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 263D3E33276DDA6600B0AC5B /* day17.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263D3E32276DDA6600B0AC5B /* day17.swift */; }; 263D3E34276DDA7000B0AC5B /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 263D3E40276E10A500B0AC5B /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 263D3E42276E10AE00B0AC5B /* day18.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263D3E41276E10AE00B0AC5B /* day18.swift */; }; 265112972767D16D009B7607 /* day13.swift in Sources */ = {isa = PBXBuildFile; fileRef = 265112962767D16D009B7607 /* day13.swift */; }; 265112982767D171009B7607 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; @@ -106,6 +108,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 263D3E37276E109D00B0AC5B /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 2651128D2767D15A009B7607 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -161,6 +172,8 @@ 263CCF76276606670061B353 /* day12.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day12.swift; sourceTree = ""; }; 263D3E2B276DDA5400B0AC5B /* Day17 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day17; sourceTree = BUILT_PRODUCTS_DIR; }; 263D3E32276DDA6600B0AC5B /* day17.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day17.swift; sourceTree = ""; }; + 263D3E39276E109D00B0AC5B /* Day18 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day18; sourceTree = BUILT_PRODUCTS_DIR; }; + 263D3E41276E10AE00B0AC5B /* day18.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day18.swift; sourceTree = ""; }; 2651128F2767D15A009B7607 /* Day13 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day13; sourceTree = BUILT_PRODUCTS_DIR; }; 265112962767D16D009B7607 /* day13.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day13.swift; sourceTree = ""; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -229,6 +242,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263D3E36276E109D00B0AC5B /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2651128C2767D15A009B7607 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -263,6 +283,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 263D3E41276E10AE00B0AC5B /* day18.swift */, 263D3E32276DDA6600B0AC5B /* day17.swift */, 26EA5DFF276BA680003E0305 /* day16.swift */, 26155467276A6D0A00374D18 /* day15.swift */, @@ -295,6 +316,7 @@ 26155460276A6CF700374D18 /* Day15 */, 26EA5DF7276BA668003E0305 /* Day16 */, 263D3E2B276DDA5400B0AC5B /* Day17 */, + 263D3E39276E109D00B0AC5B /* Day18 */, ); name = Products; sourceTree = ""; @@ -438,6 +460,23 @@ productReference = 263D3E2B276DDA5400B0AC5B /* Day17 */; productType = "com.apple.product-type.tool"; }; + 263D3E38276E109D00B0AC5B /* Day18 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 263D3E3D276E109D00B0AC5B /* Build configuration list for PBXNativeTarget "Day18" */; + buildPhases = ( + 263D3E35276E109D00B0AC5B /* Sources */, + 263D3E36276E109D00B0AC5B /* Frameworks */, + 263D3E37276E109D00B0AC5B /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day18; + productName = Day18; + productReference = 263D3E39276E109D00B0AC5B /* Day18 */; + productType = "com.apple.product-type.tool"; + }; 2651128E2767D15A009B7607 /* Day13 */ = { isa = PBXNativeTarget; buildConfigurationList = 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */; @@ -547,6 +586,9 @@ CreatedOnToolsVersion = 13.2; LastSwiftMigration = 1320; }; + 263D3E38276E109D00B0AC5B = { + CreatedOnToolsVersion = 13.2; + }; 2651128E2767D15A009B7607 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -588,6 +630,7 @@ 2615545F276A6CF700374D18 /* Day15 */, 26EA5DF6276BA668003E0305 /* Day16 */, 263D3E2A276DDA5400B0AC5B /* Day17 */, + 263D3E38276E109D00B0AC5B /* Day18 */, ); }; /* End PBXProject section */ @@ -665,6 +708,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 263D3E35276E109D00B0AC5B /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 263D3E40276E10A500B0AC5B /* common.swift in Sources */, + 263D3E42276E10AE00B0AC5B /* day18.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2651128B2767D15A009B7607 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -1075,6 +1127,30 @@ }; name = Release; }; + 263D3E3E276E109D00B0AC5B /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 263D3E3F276E109D00B0AC5B /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 265112932767D15A009B7607 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1275,6 +1351,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 263D3E3D276E109D00B0AC5B /* Build configuration list for PBXNativeTarget "Day18" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 263D3E3E276E109D00B0AC5B /* Debug */, + 263D3E3F276E109D00B0AC5B /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 265112952767D15A009B7607 /* Build configuration list for PBXNativeTarget "Day13" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day18.swift b/day18.swift new file mode 100644 index 0000000..dd2f68e --- /dev/null +++ b/day18.swift @@ -0,0 +1,318 @@ +import Foundation + +@main +struct Day18: Puzzle { + func run() { + let pairs = Scanner(string: input).all() + let first = pairs[0] + let result = pairs.dropFirst().reduce(first, +) + print(result.toString()) + print("Part 1:", result.magnitude()) + + var largest = 0 + for a in pairs { + for b in pairs where a !== b { + largest = max(largest, (a + b).magnitude()) + largest = max(largest, (b + a).magnitude()) + } + } + print("Part 2:", largest) + } +} + +extension Scanner { + func pair() -> Pair? { + guard scanString("[") != nil else { return nil } + guard let leftPart = part() else { return nil } + guard scanString(",") != nil else { return nil } + guard let rightPart = part() else { return nil } + guard scanString("]") != nil else { return nil } + return Pair(leftPart, rightPart) + } + + func part() -> Number? { + if let digits = scanInt() { + return Regular(value: digits) + } + + return pair() + } + + func all() -> [Pair] { + var result: [Pair] = [] + while let pair = self.pair() { + result.append(pair) + } + assert(isAtEnd) + return result + } +} + +struct ExplodeContext { + var lastRegular: Regular? = nil + var addToNextNumber: Int? = nil + var didExplode = false +} + +enum Action { + case goOn + case stop + case replace(Number) +} + +protocol Number: AnyObject { + func toString() -> String + + func visitExplode(depth: Int, context: inout ExplodeContext) -> Action + func visitSplit(didSplit: inout Bool) -> Action + + func magnitude() -> Int + + func copy() -> Number +} + +extension Number where Self == Pair { + static func parse(_ string: String) -> Self { + Scanner(string: string).pair()! + } +} + +class Regular: Number { + var value: Int + + init(value: Int) { + self.value = value + } + + func toString() -> String { + "\(value)" + } + + func visitExplode(depth: Int, context: inout ExplodeContext) -> Action { + if let add = context.addToNextNumber { + value += add + return .stop + } + + context.lastRegular = self + return .goOn + } + + func visitSplit(didSplit: inout Bool) -> Action { + if value >= 10 { + let first = value / 2 + didSplit = true + return .replace(Pair(Regular(value: first), Regular(value: value - first))) + } + + return .goOn + } + + func magnitude() -> Int { + value + } + + func copy() -> Number { Regular(value: value) } + +} + +class Pair: Number { + var left: Number + var right: Number + + init(_ left: Number, _ right: Number) { + self.left = left + self.right = right + } + + func toString() -> String { + "[\(left.toString()), \(right.toString())]" + } + + func splitFirst() -> Bool { + var didSplit = false + _ = visitSplit(didSplit: &didSplit) + return didSplit + } + + func explodeFirst() -> Bool { + var context = ExplodeContext() + _ = visitExplode(depth: 0, context: &context) + return context.didExplode + } + + func reduce() -> Pair { + while true { + if explodeFirst() { continue } + if splitFirst() { continue } + break + } + + return self + } + + func visitExplode(depth: Int, context: inout ExplodeContext) -> Action { + if !context.didExplode && depth == 4 { + context.lastRegular?.value += (left as! Regular).value + context.addToNextNumber = (right as! Regular).value + context.didExplode = true + return .replace(Regular(value: 0)) + } + + + switch left.visitExplode(depth: depth + 1, context: &context) { + case .goOn: + break + case .stop: + return .stop + case .replace(let number): + left = number + break + } + + let result = right.visitExplode(depth: depth + 1, context: &context) + switch result { + case .goOn: + return .goOn + case .stop: + return .stop + case .replace(let number): + right = number + return .goOn + } + + } + + func visitSplit(didSplit: inout Bool) -> Action { + switch left.visitSplit(didSplit: &didSplit) { + case .goOn: break + case .stop: return .stop + case .replace(let new): + left = new + return .stop + } + + switch right.visitSplit(didSplit: &didSplit) { + case .goOn: return .goOn + case .stop: return .stop + case .replace(let new): + right = new + return .stop + } + } + + func magnitude() -> Int { + 3 * left.magnitude() + 2 * right.magnitude() + } + + func copy() -> Number { + Pair(left.copy(), right.copy()) + } +} + +func +(lhs: Number, rhs: Number) -> Pair { + Pair(lhs.copy(), rhs.copy()).reduce() +} + +let input = """ +[4,[3,[9,[9,0]]]] +[[[7,6],[2,[2,5]]],[5,[[7,3],8]]] +[4,[4,6]] +[[0,[5,6]],[[[1,3],[2,7]],[[0,6],4]]] +[6,[[3,[6,0]],3]] +[[7,[9,[8,5]]],[6,7]] +[[[[2,6],1],2],[3,[8,4]]] +[4,[[[5,4],[2,7]],[[8,0],[2,3]]]] +[[[[4,3],2],[[3,6],[2,5]]],[[[3,7],8],0]] +[[[8,[0,7]],1],[[9,[3,9]],9]] +[[[[3,0],[1,3]],[[0,9],8]],[[[7,2],9],[[1,4],[3,5]]]] +[[[[9,6],[4,4]],[1,3]],[[4,3],[[6,4],[8,4]]]] +[[[1,2],[[7,6],[2,3]]],[[4,6],[4,2]]] +[[[4,8],[[5,8],1]],[2,3]] +[[[5,2],[3,[5,7]]],[[2,9],5]] +[[[6,[3,2]],[2,6]],[[8,[4,2]],[[5,2],7]]] +[[[[2,6],[0,1]],[7,[3,6]]],[[1,6],[[7,9],0]]] +[[[0,3],[8,1]],[[[9,0],3],[0,2]]] +[[8,[[7,1],[4,7]]],[[0,[1,3]],[8,2]]] +[[[[2,3],4],[[0,8],[9,0]]],[1,[[5,3],4]]] +[[[[7,2],2],[[1,3],[8,3]]],[4,[[7,9],[0,6]]]] +[[[[2,2],[3,4]],[[1,5],[4,3]]],[6,[[7,2],1]]] +[1,[[[5,7],0],[9,[8,8]]]] +[[[[9,2],[0,9]],[4,[7,8]]],[[4,8],[[1,8],[4,9]]]] +[[[[4,7],2],2],4] +[1,[[2,[4,2]],1]] +[[[[7,2],[3,8]],[0,[1,3]]],[[[4,4],[2,4]],[8,2]]] +[[[[1,0],[0,5]],2],[[9,[5,0]],[[1,6],5]]] +[4,[[[8,1],[1,4]],[7,[1,3]]]] +[[[6,[0,4]],[[4,6],[2,4]]],[9,[1,5]]] +[[[[3,6],[3,3]],1],[0,[[8,8],2]]] +[[7,[5,[2,6]]],[[[7,9],6],[0,[3,6]]]] +[[[[6,7],4],[[2,9],2]],3] +[[[7,[1,7]],[5,4]],[[[1,1],[0,1]],5]] +[[6,[[1,0],6]],[0,[6,[0,5]]]] +[[[[2,4],[4,6]],9],[4,[[8,0],7]]] +[[[[9,9],[5,7]],[9,[8,6]]],[[3,[2,3]],0]] +[[0,[1,[5,3]]],[3,[8,[3,4]]]] +[[[[4,3],8],[2,9]],[[1,[6,5]],[[5,7],2]]] +[[[0,[7,4]],[9,[9,6]]],[[8,[5,5]],[[6,4],1]]] +[[[[7,3],[7,9]],[8,[6,2]]],[[8,[4,5]],[[6,4],[6,7]]]] +[[7,[[9,0],[9,0]]],[[[0,8],2],[8,[8,3]]]] +[4,[7,[5,6]]] +[7,[[[3,8],8],3]] +[[[4,[6,6]],0],[9,0]] +[[[[7,4],8],8],[[0,1],[[0,0],[2,4]]]] +[7,[1,[[9,4],[3,6]]]] +[[[[2,8],9],[[8,6],[2,2]]],[[[5,1],9],[2,[0,7]]]] +[8,7] +[[[[0,8],4],[[9,9],[9,9]]],[[[4,3],[1,0]],[6,8]]] +[[[[8,3],[8,9]],1],[[4,[1,0]],[[4,0],[2,3]]]] +[[[[4,7],[1,3]],[6,9]],[[1,0],[[1,8],5]]] +[[2,[4,[6,5]]],[3,[[9,9],5]]] +[[[[7,6],4],9],[8,[4,5]]] +[[[0,[6,6]],[7,[8,9]]],[[[0,0],[3,4]],[4,[1,8]]]] +[[[9,[7,0]],[5,8]],[6,[[5,0],[0,6]]]] +[[[[4,0],[1,9]],[7,[3,6]]],[[2,[8,6]],[[2,8],[8,2]]]] +[[[9,6],8],[[[5,5],[4,8]],0]] +[[[[1,7],1],2],[[[6,8],3],[[3,3],5]]] +[3,[5,[[3,8],6]]] +[3,[[[9,6],[5,8]],[9,2]]] +[[6,1],[6,4]] +[[2,6],[[[1,2],2],8]] +[[[[1,7],[3,6]],[2,[0,2]]],[[3,0],9]] +[1,[[0,[4,9]],5]] +[[[[5,5],[5,2]],[0,[6,4]]],8] +[0,[7,[[6,9],[6,0]]]] +[[[[2,2],[4,7]],[[7,4],6]],[[0,[1,7]],[[3,2],6]]] +[[9,8],0] +[[[[5,4],[4,8]],2],[3,[8,9]]] +[[[[7,0],8],5],[2,6]] +[[[5,[0,8]],5],[[[5,0],[1,8]],[[0,2],7]]] +[[[[9,4],8],[[6,5],4]],[[5,[8,9]],[4,[0,4]]]] +[[[[3,6],7],[[9,3],7]],[7,[[8,3],9]]] +[[[[0,7],5],[[5,7],2]],[[2,[9,5]],[[7,7],[5,0]]]] +[[[[7,5],2],[8,6]],[[2,[6,2]],[5,[3,1]]]] +[[9,[9,1]],6] +[[[0,7],[[5,9],2]],3] +[[[9,3],[8,8]],[0,[4,5]]] +[[[[6,2],5],[4,[3,1]]],[9,[2,8]]] +[[[1,[9,4]],[[0,0],2]],[[1,[2,1]],[[7,8],[3,2]]]] +[[[[0,6],[8,9]],[[4,7],[5,6]]],[[[1,4],[8,7]],[4,6]]] +[[[[6,4],[1,5]],[0,8]],[[[9,7],[1,2]],[9,4]]] +[[[[4,5],[0,7]],[9,[1,8]]],[[[5,0],6],7]] +[[[0,[6,9]],[5,[5,6]]],7] +[[4,5],[[7,[6,5]],1]] +[[[7,9],[6,7]],[4,1]] +[[[[9,6],1],[[3,1],[9,7]]],[1,[7,1]]] +[[[0,[2,0]],5],[[8,[7,6]],[[7,3],4]]] +[[[6,[1,7]],[9,[2,7]]],3] +[[[6,[8,2]],5],[4,[[1,3],[5,1]]]] +[[[4,[3,3]],[4,[2,4]]],[5,4]] +[[[1,6],[4,[4,0]]],[[8,[2,2]],[[8,1],[4,7]]]] +[[2,0],[[2,1],[[4,8],[2,7]]]] +[9,[[8,4],0]] +[[1,6],[[5,[1,3]],[9,[0,9]]]] +[[[0,[3,5]],3],[[2,[8,0]],[[2,0],[4,3]]]] +[[[1,[1,9]],[9,[7,9]]],[[2,2],[[6,7],[0,7]]]] +[[[4,6],[[6,2],[0,9]]],[[1,0],[1,[6,7]]]] +[9,[[[0,1],4],[[9,3],3]]] +""" From 879040b18ab9ff8c9b2a85ae3baac39d718e546d Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sun, 19 Dec 2021 15:56:05 +0100 Subject: [PATCH 22/32] Day 19 --- AoC21.xcodeproj/project.pbxproj | 99 +++ day19.swift | 1104 +++++++++++++++++++++++++++++++ 2 files changed, 1203 insertions(+) create mode 100644 day19.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 0b352ff..93eab85 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -25,6 +25,8 @@ 263D3E42276E10AE00B0AC5B /* day18.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263D3E41276E10AE00B0AC5B /* day18.swift */; }; 265112972767D16D009B7607 /* day13.swift in Sources */ = {isa = PBXBuildFile; fileRef = 265112962767D16D009B7607 /* day13.swift */; }; 265112982767D171009B7607 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 2680ECD2276F23FE00CAB23C /* day19.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2680ECD1276F23FE00CAB23C /* day19.swift */; }; + 2680ECD3276F240200CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; 269BE5CA2762A05300871C85 /* day9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5C82762A04F00871C85 /* day9.swift */; }; 269BE5CC2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; @@ -126,6 +128,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 2680ECC8276F23E800CAB23C /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 269BE5B22762958800871C85 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -176,6 +187,8 @@ 263D3E41276E10AE00B0AC5B /* day18.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day18.swift; sourceTree = ""; }; 2651128F2767D15A009B7607 /* Day13 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day13; sourceTree = BUILT_PRODUCTS_DIR; }; 265112962767D16D009B7607 /* day13.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day13.swift; sourceTree = ""; }; + 2680ECCA276F23E800CAB23C /* Day19 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day19; sourceTree = BUILT_PRODUCTS_DIR; }; + 2680ECD1276F23FE00CAB23C /* day19.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day19.swift; sourceTree = ""; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5BB2762959B00871C85 /* day8part2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day8part2.swift; sourceTree = ""; }; 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -256,6 +269,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECC7276F23E800CAB23C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B12762958800871C85 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -283,6 +303,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 2680ECD1276F23FE00CAB23C /* day19.swift */, 263D3E41276E10AE00B0AC5B /* day18.swift */, 263D3E32276DDA6600B0AC5B /* day17.swift */, 26EA5DFF276BA680003E0305 /* day16.swift */, @@ -317,6 +338,7 @@ 26EA5DF7276BA668003E0305 /* Day16 */, 263D3E2B276DDA5400B0AC5B /* Day17 */, 263D3E39276E109D00B0AC5B /* Day18 */, + 2680ECCA276F23E800CAB23C /* Day19 */, ); name = Products; sourceTree = ""; @@ -494,6 +516,23 @@ productReference = 2651128F2767D15A009B7607 /* Day13 */; productType = "com.apple.product-type.tool"; }; + 2680ECC9276F23E800CAB23C /* Day19 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 2680ECD0276F23E800CAB23C /* Build configuration list for PBXNativeTarget "Day19" */; + buildPhases = ( + 2680ECC6276F23E800CAB23C /* Sources */, + 2680ECC7276F23E800CAB23C /* Frameworks */, + 2680ECC8276F23E800CAB23C /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day19; + productName = Day19; + productReference = 2680ECCA276F23E800CAB23C /* Day19 */; + productType = "com.apple.product-type.tool"; + }; 269BE5B32762958800871C85 /* Day8Part2 */ = { isa = PBXNativeTarget; buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; @@ -593,6 +632,10 @@ CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; }; + 2680ECC9276F23E800CAB23C = { + CreatedOnToolsVersion = 13.2.1; + LastSwiftMigration = 1320; + }; 269BE5B32762958800871C85 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -631,6 +674,7 @@ 26EA5DF6276BA668003E0305 /* Day16 */, 263D3E2A276DDA5400B0AC5B /* Day17 */, 263D3E38276E109D00B0AC5B /* Day18 */, + 2680ECC9276F23E800CAB23C /* Day19 */, ); }; /* End PBXProject section */ @@ -726,6 +770,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECC6276F23E800CAB23C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 2680ECD3276F240200CAB23C /* common.swift in Sources */, + 2680ECD2276F23FE00CAB23C /* day19.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B02762958800871C85 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -1186,6 +1239,43 @@ }; name = Release; }; + 2680ECCE276F23E800CAB23C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 2680ECCF276F23E800CAB23C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 269BE5B82762958800871C85 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1369,6 +1459,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 2680ECD0276F23E800CAB23C /* Build configuration list for PBXNativeTarget "Day19" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2680ECCE276F23E800CAB23C /* Debug */, + 2680ECCF276F23E800CAB23C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day19.swift b/day19.swift new file mode 100644 index 0000000..7cd61c4 --- /dev/null +++ b/day19.swift @@ -0,0 +1,1104 @@ +@main +struct Day19: Puzzle { + func run() { + var locations = Array(repeating: [Vector(0, 0, 0)], count: input.count) + + while input.count > 1 { + for i in input.indices { + for j in input.index(after: i).. (Matrix, Vector)? { + let referenceSet = Set(reference) + + for matrix in directions { + let points = input.map { matrix * $0 } + + let possibleShifts = findPossibleShifts(points, reference) + for shift in possibleShifts { + let shifted = points.lazy.map { $0 - shift }.filter { referenceSet.contains($0) }.count + if shifted >= 12 { + return (matrix, shift) + } + } + } + + + return nil +} + +func findPossibleShifts(_ a: [Vector], _ b: [Vector]) -> Set { + var result: Set = [] + for pointA in a { + for pointB in b { + result.insert(pointA - pointB) + } + } + return result +} + + + +struct Vector: Hashable { + var x, y, z: Int + + init(_ x: Int, _ y: Int, _ z: Int) { + self.x = x + self.y = y + self.z = z + } + + static func +(lhs: Vector, rhs: Vector) -> Vector { + Vector(lhs.x + rhs.x, lhs.y + rhs.y, lhs.z + rhs.z) + } + + static func - (lhs: Vector, rhs: Vector) -> Vector { + .init(lhs.x - rhs.x, lhs.y - rhs.y, lhs.z - rhs.z) + } + +} + +func manhattenDistance(_ a: Vector, _ b: Vector) -> Int { + abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z) +} + +struct Matrix { + var a, b, c: Int + var d, e, f: Int + var g, h, i: Int + + init(_ a: Int, _ b: Int, _ c: Int, _ d: Int, _ e: Int, _ f: Int, _ g: Int, _ h: Int, _ i: Int) { + (self.a, self.b, self.c) = (a, b, c) + (self.d, self.e, self.f) = (d, e, f) + (self.g, self.h, self.i) = (g, h, i) + } + + init(rows a: Vector, _ b: Vector, _ c: Vector) { + self.init( + a.x, a.y, a.z, + b.x, b.y, b.z, + c.x, c.y, c.z + ) + } +} + +func *(lhs: Matrix, rhs: Vector) -> Vector { + Vector( + lhs.a * rhs.x + lhs.b * rhs.y + lhs.c * rhs.z, + lhs.d * rhs.x + lhs.e * rhs.y + lhs.f * rhs.z, + lhs.g * rhs.x + lhs.h * rhs.y + lhs.i * rhs.z + ) +} + +func cross(_ a: Vector, _ b: Vector) -> Vector { + Vector( + a.y * b.z - a.z * b.y, + a.z * b.x - a.x * b.z, + a.x * b.y - a.y * b.x + ) +} + +func dot(_ a: Vector, _ b: Vector) -> Int { + a.x * b.x + a.y * b.y + a.z * b.z +} + +func calcDirections() -> [Matrix] { + let axes: [Vector] = [ + .init(1, 0, 0), + .init(0, 1, 0), + .init(0, 0, 1), + .init(-1, 0, 0), + .init(0, -1, 0), + .init(0, 0, -1), + ] + + var result: [Matrix] = [] + + for fi in axes.indices { + let forward = axes[fi] + for fj in axes.indices where fi != fj { + let up = axes[fj] + if dot(forward, up) != 0 { + continue + } + + let right = cross(up, forward) + result.append(.init(rows: forward, right, up)) + } + } + + return result +} +let directions = calcDirections() + +var input: [[Vector]] = [ + [ + Vector(-779,774,841), + Vector(-623,687,-622), + Vector(-569,692,-472), + Vector(-815,791,890), + Vector(545,-807,-699), + Vector(655,564,594), + Vector(-781,645,919), + Vector(561,908,-538), + Vector(-453,-675,694), + Vector(-885,-565,-404), + Vector(596,-595,-679), + Vector(464,-620,445), + Vector(481,-658,-759), + Vector(-574,-702,800), + Vector(-145,151,93), + Vector(-814,-575,-354), + Vector(-611,653,-643), + Vector(532,950,-506), + Vector(-475,-638,816), + Vector(403,-510,477), + Vector(736,559,518), + Vector(-17,7,140), + Vector(-861,-741,-321), + Vector(513,-554,586), + Vector(518,512,519), + Vector(549,924,-562), + ], + [ + Vector(689,772,-758), + Vector(537,371,685), + Vector(-672,-658,689), + Vector(123,51,-21), + Vector(-401,435,-501), + Vector(12,-98,95), + Vector(735,588,-720), + Vector(844,-573,-785), + Vector(-423,-889,-362), + Vector(-816,-649,595), + Vector(655,605,-727), + Vector(785,-703,-813), + Vector(536,-624,720), + Vector(-360,-902,-383), + Vector(-432,400,599), + Vector(-689,-669,583), + Vector(625,506,710), + Vector(455,445,698), + Vector(650,-556,751), + Vector(747,-531,-750), + Vector(-346,-875,-402), + Vector(-417,420,420), + Vector(-501,413,-386), + Vector(490,-584,661), + Vector(-499,374,505), + Vector(-449,476,-453), + ], + [ + Vector(663,667,-407), + Vector(-595,-818,-576), + Vector(-309,605,562), + Vector(-598,-610,-587), + Vector(-517,-586,691), + Vector(46,-151,22), + Vector(-881,281,-669), + Vector(732,-947,-380), + Vector(587,624,746), + Vector(-55,-3,100), + Vector(-458,-497,709), + Vector(593,687,-248), + Vector(572,498,738), + Vector(694,-939,-344), + Vector(401,-634,682), + Vector(-847,239,-653), + Vector(397,-527,600), + Vector(-610,-743,-467), + Vector(-474,659,598), + Vector(-414,-596,646), + Vector(657,542,-317), + Vector(-397,630,676), + Vector(378,-659,657), + Vector(-848,236,-666), + Vector(514,509,638), + Vector(753,-870,-512), + ], + [ + Vector(438,673,-607), + Vector(-400,-430,341), + Vector(-795,-514,-738), + Vector(-705,-416,-761), + Vector(760,-806,792), + Vector(-469,803,349), + Vector(670,-798,675), + Vector(363,648,-428), + Vector(-349,-447,506), + Vector(-425,412,-482), + Vector(520,455,660), + Vector(452,-616,-878), + Vector(553,372,771), + Vector(24,68,-59), + Vector(654,413,728), + Vector(-464,-413,481), + Vector(-404,598,-552), + Vector(-459,781,522), + Vector(538,-597,-851), + Vector(-346,488,-476), + Vector(420,-481,-842), + Vector(-470,896,540), + Vector(651,-822,721), + Vector(421,522,-506), + Vector(-690,-511,-769), + ], + [ + Vector(844,592,402), + Vector(-636,679,516), + Vector(-720,-400,911), + Vector(-509,-714,-788), + Vector(-826,755,-326), + Vector(39,-121,70), + Vector(661,741,-539), + Vector(727,636,527), + Vector(869,-741,-241), + Vector(742,-762,619), + Vector(-703,-565,962), + Vector(725,-852,686), + Vector(-639,710,621), + Vector(829,579,592), + Vector(654,730,-279), + Vector(-737,-540,847), + Vector(-734,702,606), + Vector(-487,-705,-778), + Vector(799,-689,-246), + Vector(-754,837,-380), + Vector(854,-715,-315), + Vector(754,-648,701), + Vector(-615,725,-335), + Vector(-628,-713,-672), + Vector(736,727,-443), + ], + [ + Vector(-405,590,-366), + Vector(-409,651,-375), + Vector(439,489,371), + Vector(-542,-507,536), + Vector(-367,404,-381), + Vector(299,-367,848), + Vector(-670,-452,-700), + Vector(-49,132,11), + Vector(340,-226,895), + Vector(478,-800,-337), + Vector(613,500,-723), + Vector(279,-348,939), + Vector(-558,-502,654), + Vector(390,411,415), + Vector(366,500,429), + Vector(400,-731,-350), + Vector(-612,-578,-728), + Vector(629,662,-720), + Vector(-522,-490,-688), + Vector(-453,406,393), + Vector(-679,460,405), + Vector(-652,411,356), + Vector(-532,-505,547), + Vector(707,591,-772), + Vector(635,-703,-379), + ], + [ + Vector(539,-841,504), + Vector(743,731,-445), + Vector(-822,-423,673), + Vector(-770,-492,627), + Vector(-762,805,536), + Vector(622,783,749), + Vector(-757,320,-629), + Vector(710,684,-611), + Vector(393,-892,456), + Vector(-769,705,691), + Vector(365,-675,-379), + Vector(-831,401,-736), + Vector(561,779,740), + Vector(-815,623,585), + Vector(385,-726,-591), + Vector(305,-611,-528), + Vector(-26,-6,-67), + Vector(-690,441,-682), + Vector(-830,-855,-756), + Vector(-707,-427,667), + Vector(808,821,734), + Vector(660,755,-621), + Vector(444,-961,435), + Vector(-831,-858,-732), + Vector(-873,-889,-537), + Vector(-116,-144,21), + ], + [ + Vector(620,-673,-810), + Vector(601,-454,-802), + Vector(-496,543,-614), + Vector(-596,554,-672), + Vector(963,-487,692), + Vector(-701,-396,401), + Vector(808,816,748), + Vector(638,-546,-875), + Vector(887,-534,765), + Vector(-604,428,-687), + Vector(916,-611,754), + Vector(-456,393,374), + Vector(-554,-588,-462), + Vector(549,385,-763), + Vector(-420,447,209), + Vector(-784,-504,377), + Vector(811,771,781), + Vector(-510,-687,-472), + Vector(-725,-524,511), + Vector(459,510,-757), + Vector(795,813,747), + Vector(71,-145,-43), + Vector(390,365,-693), + Vector(-534,422,297), + Vector(-565,-706,-544), + Vector(122,13,-148), + ], + [ + Vector(300,838,-511), + Vector(723,-429,-827), + Vector(-489,-531,-733), + Vector(-862,690,478), + Vector(272,779,-641), + Vector(365,729,650), + Vector(-749,-606,618), + Vector(-412,-399,-712), + Vector(-420,-363,-674), + Vector(-763,627,434), + Vector(584,-407,497), + Vector(-725,663,-381), + Vector(702,-366,-840), + Vector(358,649,667), + Vector(-832,581,-377), + Vector(683,-352,-813), + Vector(-773,672,537), + Vector(699,-507,475), + Vector(-616,-486,630), + Vector(-700,638,-358), + Vector(327,683,834), + Vector(632,-455,538), + Vector(335,741,-469), + Vector(-541,-595,615), + Vector(-76,130,123), + ], + [ + Vector(643,-906,-639), + Vector(554,-930,-576), + Vector(-438,-780,848), + Vector(885,648,-659), + Vector(-617,748,624), + Vector(708,-887,-479), + Vector(562,675,352), + Vector(513,-416,680), + Vector(-551,607,575), + Vector(867,701,-509), + Vector(567,599,433), + Vector(-593,592,-482), + Vector(533,-475,546), + Vector(-618,-692,-723), + Vector(-629,538,-371), + Vector(928,733,-581), + Vector(116,-32,-131), + Vector(405,-499,656), + Vector(515,674,570), + Vector(-629,-736,-551), + Vector(71,-144,33), + Vector(-384,-744,683), + Vector(-655,512,-481), + Vector(-658,-823,-634), + Vector(-413,-666,760), + Vector(-507,716,685), + ], + [ + Vector(392,400,680), + Vector(715,-577,488), + Vector(872,575,-780), + Vector(-527,-916,-745), + Vector(-756,759,-452), + Vector(-341,-954,-730), + Vector(360,461,570), + Vector(553,-799,-423), + Vector(-489,-888,625), + Vector(731,-478,552), + Vector(-844,478,532), + Vector(609,-456,515), + Vector(777,572,-702), + Vector(-439,-817,615), + Vector(641,-709,-383), + Vector(501,510,636), + Vector(-705,828,-431), + Vector(-764,593,595), + Vector(-610,708,-500), + Vector(-406,-864,449), + Vector(-77,-136,-15), + Vector(515,-651,-411), + Vector(-588,-912,-736), + Vector(1,17,72), + Vector(844,455,-646), + Vector(-784,542,383), + ], + [ + Vector(-705,-729,760), + Vector(-796,-680,666), + Vector(34,174,59), + Vector(681,672,720), + Vector(-634,-677,682), + Vector(685,-381,-430), + Vector(736,900,-429), + Vector(-892,-338,-314), + Vector(-28,77,-93), + Vector(-796,472,419), + Vector(731,-487,-512), + Vector(-885,399,-599), + Vector(746,727,-451), + Vector(-885,458,443), + Vector(678,-536,-330), + Vector(652,-373,448), + Vector(666,-453,483), + Vector(-706,-323,-352), + Vector(-807,414,492), + Vector(734,-275,479), + Vector(602,750,578), + Vector(-890,436,-400), + Vector(591,761,630), + Vector(-645,-332,-356), + Vector(-836,448,-626), + Vector(735,709,-336), + ], + [ + Vector(560,-631,663), + Vector(3,-21,-120), + Vector(425,-576,-600), + Vector(455,612,-831), + Vector(-472,492,-534), + Vector(615,888,330), + Vector(-382,383,326), + Vector(414,511,-837), + Vector(76,90,15), + Vector(-423,476,-725), + Vector(-568,-599,563), + Vector(751,919,441), + Vector(480,-626,-579), + Vector(-464,570,-714), + Vector(425,438,-790), + Vector(-744,-590,-577), + Vector(461,-722,-487), + Vector(-459,-674,578), + Vector(-688,-533,-461), + Vector(-366,425,292), + Vector(-369,380,325), + Vector(-448,-466,575), + Vector(597,-635,584), + Vector(647,-481,631), + Vector(753,946,365), + Vector(-709,-755,-482), + ], + [ + Vector(-793,-712,526), + Vector(43,-66,-77), + Vector(-592,814,658), + Vector(608,-557,-325), + Vector(-817,-758,545), + Vector(-447,441,-677), + Vector(-476,805,776), + Vector(377,-808,643), + Vector(685,332,-669), + Vector(-306,539,-675), + Vector(-533,-777,-578), + Vector(527,469,775), + Vector(576,-489,-523), + Vector(666,440,-717), + Vector(418,-765,542), + Vector(-678,-863,-623), + Vector(623,-570,-556), + Vector(-625,-783,-492), + Vector(460,-697,676), + Vector(653,391,833), + Vector(-611,841,744), + Vector(774,405,-693), + Vector(-801,-829,436), + Vector(678,570,758), + Vector(-11,91,43), + Vector(-418,455,-584), + ], + [ + Vector(805,-443,-751), + Vector(494,718,482), + Vector(2,-16,82), + Vector(104,91,-72), + Vector(-597,622,-615), + Vector(-753,481,430), + Vector(-531,502,409), + Vector(-672,731,-630), + Vector(-588,446,525), + Vector(571,-495,655), + Vector(-464,742,-627), + Vector(385,808,-378), + Vector(751,-407,-916), + Vector(382,821,-377), + Vector(-351,-554,-767), + Vector(-338,-407,491), + Vector(560,-451,483), + Vector(-450,-491,-811), + Vector(478,767,673), + Vector(531,-364,566), + Vector(-440,-536,-708), + Vector(-297,-435,349), + Vector(776,-521,-828), + Vector(572,726,662), + Vector(427,824,-344), + Vector(-266,-423,363), + ], + [ + Vector(-459,-573,-393), + Vector(-541,-681,657), + Vector(-691,578,-927), + Vector(846,-686,740), + Vector(854,-755,-493), + Vector(-670,527,-858), + Vector(-18,154,-79), + Vector(540,944,-704), + Vector(-392,942,855), + Vector(-622,673,-807), + Vector(-435,-635,739), + Vector(-453,-688,-389), + Vector(673,-696,718), + Vector(689,-806,-510), + Vector(549,934,-726), + Vector(345,935,570), + Vector(541,828,-585), + Vector(122,-41,-59), + Vector(-499,-636,-458), + Vector(382,940,769), + Vector(-377,756,817), + Vector(714,-691,-476), + Vector(-590,-628,839), + Vector(655,-685,833), + Vector(-386,695,813), + Vector(394,886,543), + ], + [ + Vector(729,789,-842), + Vector(-819,-334,502), + Vector(-741,-438,568), + Vector(399,-785,-551), + Vector(682,756,-676), + Vector(753,706,-666), + Vector(-9,-118,-9), + Vector(318,-711,-658), + Vector(-857,-784,-531), + Vector(439,-644,-605), + Vector(-891,-827,-750), + Vector(479,467,552), + Vector(659,-682,340), + Vector(-106,61,-102), + Vector(-681,548,823), + Vector(-664,604,849), + Vector(-860,-785,-555), + Vector(-613,351,-759), + Vector(-677,411,-908), + Vector(550,-780,387), + Vector(-728,431,-696), + Vector(-844,-469,484), + Vector(437,638,591), + Vector(-693,572,807), + Vector(491,488,586), + Vector(669,-789,409), + ], + [ + Vector(-418,-800,568), + Vector(690,-779,507), + Vector(441,-370,-505), + Vector(-573,-900,575), + Vector(-327,-603,-487), + Vector(552,-453,-472), + Vector(863,-801,517), + Vector(-436,328,515), + Vector(420,808,-712), + Vector(731,415,512), + Vector(-524,-718,522), + Vector(491,824,-603), + Vector(-18,24,64), + Vector(109,-51,-38), + Vector(-303,-536,-502), + Vector(-677,620,-677), + Vector(-482,397,567), + Vector(537,800,-622), + Vector(-442,332,630), + Vector(706,449,397), + Vector(484,-411,-606), + Vector(672,-894,518), + Vector(-576,655,-745), + Vector(871,432,448), + Vector(-517,524,-688), + Vector(-258,-444,-547), + ], + [ + Vector(-674,750,495), + Vector(-412,-547,817), + Vector(610,-432,859), + Vector(-410,-437,-273), + Vector(648,612,-461), + Vector(-691,786,446), + Vector(738,446,520), + Vector(-464,-684,742), + Vector(637,554,-561), + Vector(-494,402,-503), + Vector(33,115,-7), + Vector(775,424,642), + Vector(-612,484,-492), + Vector(541,-269,-637), + Vector(-545,752,417), + Vector(720,-493,891), + Vector(-430,-571,-332), + Vector(487,-434,-601), + Vector(-31,4,175), + Vector(802,545,536), + Vector(654,-542,750), + Vector(-302,-461,-356), + Vector(516,-357,-523), + Vector(522,627,-453), + Vector(-459,-765,818), + Vector(-593,412,-547), + ], + [ + Vector(-62,159,-61), + Vector(396,-693,-479), + Vector(-641,729,543), + Vector(-709,-486,661), + Vector(-552,-810,-705), + Vector(-796,708,-772), + Vector(-656,620,468), + Vector(522,-264,566), + Vector(-653,609,676), + Vector(-662,-436,507), + Vector(360,-565,-518), + Vector(-774,808,-657), + Vector(7,-11,2), + Vector(419,-516,-543), + Vector(409,-300,420), + Vector(352,889,-418), + Vector(383,849,609), + Vector(341,867,-488), + Vector(406,888,-551), + Vector(-733,760,-705), + Vector(501,-319,516), + Vector(-741,-399,511), + Vector(404,870,387), + Vector(499,905,552), + Vector(-725,-728,-734), + Vector(-597,-770,-717), + ], + [ + Vector(-649,836,908), + Vector(463,700,656), + Vector(552,809,589), + Vector(524,739,-436), + Vector(758,-723,501), + Vector(528,-639,-338), + Vector(484,-540,-348), + Vector(-914,776,-711), + Vector(-716,790,773), + Vector(-942,-616,878), + Vector(4,117,4), + Vector(-627,-773,-678), + Vector(-917,764,-831), + Vector(376,757,614), + Vector(-810,864,802), + Vector(699,-703,635), + Vector(-646,-554,-686), + Vector(539,-521,-420), + Vector(579,871,-390), + Vector(675,-831,525), + Vector(-184,-41,64), + Vector(-950,617,-725), + Vector(-664,-681,-751), + Vector(-989,-740,816), + Vector(-913,-693,889), + Vector(610,802,-431), + ], + [ + Vector(-328,515,-481), + Vector(-417,574,-499), + Vector(-621,584,416), + Vector(-465,-344,-508), + Vector(595,590,900), + Vector(793,-530,-444), + Vector(-358,-340,-590), + Vector(-386,-522,745), + Vector(154,-26,152), + Vector(-358,-580,619), + Vector(705,529,868), + Vector(50,99,-7), + Vector(799,454,-514), + Vector(540,-327,651), + Vector(577,-349,529), + Vector(-755,704,388), + Vector(-427,471,-544), + Vector(661,639,759), + Vector(774,366,-628), + Vector(-490,-321,-533), + Vector(513,-448,545), + Vector(-677,701,483), + Vector(-398,-670,707), + Vector(667,-515,-452), + Vector(678,-457,-552), + Vector(828,531,-592), + ], + [ + Vector(-354,917,781), + Vector(-281,841,734), + Vector(639,-856,-861), + Vector(562,853,614), + Vector(600,-872,-861), + Vector(650,-340,634), + Vector(-669,-830,764), + Vector(615,792,515), + Vector(-514,-535,-659), + Vector(-645,-747,872), + Vector(566,-839,-856), + Vector(-581,-624,-768), + Vector(399,493,-762), + Vector(-262,577,-795), + Vector(535,464,-683), + Vector(391,829,533), + Vector(-535,-771,744), + Vector(-368,738,-823), + Vector(-397,644,-749), + Vector(74,32,12), + Vector(587,-316,723), + Vector(490,-359,569), + Vector(-292,870,657), + Vector(549,579,-709), + Vector(-556,-448,-716), + ], + [ + Vector(-854,-686,494), + Vector(471,874,648), + Vector(488,-734,-887), + Vector(764,427,-727), + Vector(-759,681,-745), + Vector(-725,-714,449), + Vector(-605,728,-695), + Vector(795,538,-591), + Vector(458,-463,533), + Vector(405,770,623), + Vector(387,-740,-928), + Vector(-843,-741,525), + Vector(477,-316,412), + Vector(-885,549,507), + Vector(-750,-482,-862), + Vector(822,462,-589), + Vector(-158,-21,43), + Vector(461,812,673), + Vector(-755,760,-782), + Vector(-166,125,-115), + Vector(448,-380,515), + Vector(-700,-503,-917), + Vector(-884,648,444), + Vector(-824,602,404), + Vector(-846,-390,-881), + Vector(314,-693,-917), + ], + [ + Vector(-886,423,811), + Vector(785,453,423), + Vector(-396,553,-508), + Vector(-81,38,127), + Vector(-415,618,-348), + Vector(-522,-528,979), + Vector(706,-897,440), + Vector(-479,-748,-503), + Vector(652,-801,-610), + Vector(-610,-708,-592), + Vector(603,-785,-419), + Vector(611,-707,-599), + Vector(693,-894,441), + Vector(861,406,496), + Vector(736,672,-473), + Vector(-636,-503,891), + Vector(-372,464,-445), + Vector(814,530,434), + Vector(-29,-119,-2), + Vector(-558,-474,814), + Vector(-803,315,787), + Vector(773,654,-249), + Vector(-566,-719,-420), + Vector(709,-852,407), + Vector(797,573,-420), + Vector(-827,443,677), + ], + [ + Vector(-729,-745,477), + Vector(-809,409,-655), + Vector(628,-585,534), + Vector(405,464,-835), + Vector(309,581,335), + Vector(-697,-756,-375), + Vector(-459,599,568), + Vector(-390,539,409), + Vector(-626,-825,-487), + Vector(-823,-783,491), + Vector(340,-640,-575), + Vector(-14,-66,28), + Vector(-435,584,501), + Vector(334,-554,-667), + Vector(420,614,418), + Vector(352,480,376), + Vector(-157,-24,-97), + Vector(560,-387,566), + Vector(254,-685,-720), + Vector(359,483,-859), + Vector(-649,398,-559), + Vector(-616,-862,-402), + Vector(547,-618,581), + Vector(-708,341,-690), + Vector(288,374,-843), + Vector(-751,-815,603), + ], + [ + Vector(409,289,-495), + Vector(-847,-575,496), + Vector(413,467,-563), + Vector(508,-736,-627), + Vector(-930,-464,529), + Vector(530,380,608), + Vector(-764,280,526), + Vector(628,-869,503), + Vector(-42,-169,-23), + Vector(650,-658,-548), + Vector(503,274,552), + Vector(624,-954,561), + Vector(-499,-886,-493), + Vector(-406,-695,-494), + Vector(-856,235,571), + Vector(560,-747,-638), + Vector(-400,550,-551), + Vector(-889,-507,630), + Vector(-500,395,-544), + Vector(-521,-713,-483), + Vector(-582,501,-498), + Vector(-697,304,511), + Vector(440,253,530), + Vector(-170,-6,-82), + Vector(378,387,-577), + Vector(649,-801,614), + ], + [ + Vector(367,-847,-431), + Vector(805,450,827), + Vector(-651,-490,-341), + Vector(574,-485,737), + Vector(86,-58,110), + Vector(706,386,811), + Vector(-461,301,-317), + Vector(-745,-441,-432), + Vector(419,347,-310), + Vector(827,413,705), + Vector(-667,-493,-503), + Vector(329,-832,-245), + Vector(466,513,-343), + Vector(-529,329,473), + Vector(-599,353,-300), + Vector(-438,-863,890), + Vector(-541,433,385), + Vector(-516,-876,959), + Vector(371,-838,-492), + Vector(-673,-834,862), + Vector(632,-513,611), + Vector(-573,307,-497), + Vector(484,-525,649), + Vector(395,523,-393), + Vector(-513,468,378), + ], + [ + Vector(-568,386,784), + Vector(20,-72,121), + Vector(-121,24,13), + Vector(544,777,598), + Vector(-450,-510,942), + Vector(778,-914,-767), + Vector(830,-551,427), + Vector(-470,476,807), + Vector(-501,832,-460), + Vector(-378,-456,860), + Vector(358,756,528), + Vector(-717,-908,-600), + Vector(552,-860,-755), + Vector(790,-530,473), + Vector(-589,-465,830), + Vector(579,-909,-804), + Vector(-484,781,-295), + Vector(750,-556,440), + Vector(434,835,522), + Vector(-576,-802,-608), + Vector(727,722,-476), + Vector(-565,491,815), + Vector(-467,717,-435), + Vector(749,712,-303), + Vector(842,711,-494), + Vector(-695,-918,-609), + ], + [ + Vector(417,-415,713), + Vector(-449,647,870), + Vector(27,91,28), + Vector(402,-451,-539), + Vector(476,382,916), + Vector(-393,704,754), + Vector(548,-455,624), + Vector(-428,869,849), + Vector(857,539,-539), + Vector(-777,-601,-329), + Vector(-126,-2,126), + Vector(-665,-378,797), + Vector(827,434,-697), + Vector(-541,876,-635), + Vector(319,-462,-354), + Vector(504,-444,796), + Vector(-462,887,-670), + Vector(-714,-362,606), + Vector(-673,-456,617), + Vector(460,454,788), + Vector(488,413,930), + Vector(-888,-669,-268), + Vector(842,448,-606), + Vector(-852,-520,-276), + Vector(400,-532,-343), + Vector(-583,781,-617), + ], + [ + Vector(397,-399,-587), + Vector(-692,640,606), + Vector(-549,-426,-591), + Vector(517,-468,-682), + Vector(503,792,687), + Vector(-665,724,474), + Vector(683,415,-508), + Vector(469,573,730), + Vector(-665,-505,-567), + Vector(-470,730,-541), + Vector(632,627,-528), + Vector(747,564,-448), + Vector(466,-420,-536), + Vector(72,63,-118), + Vector(935,-637,679), + Vector(-375,-466,480), + Vector(-385,-494,361), + Vector(931,-631,564), + Vector(866,-591,627), + Vector(500,768,688), + Vector(-305,727,-516), + Vector(-647,841,608), + Vector(-776,-459,-594), + Vector(-413,713,-575), + Vector(-379,-503,254), + ], + [ + Vector(-626,-772,665), + Vector(-298,775,-361), + Vector(795,558,497), + Vector(-650,-716,745), + Vector(470,819,-606), + Vector(-465,-720,-367), + Vector(783,706,535), + Vector(476,861,-359), + Vector(426,-668,854), + Vector(16,67,24), + Vector(-559,670,547), + Vector(59,-95,-131), + Vector(754,-852,-774), + Vector(775,-921,-723), + Vector(560,-548,826), + Vector(-456,-698,-357), + Vector(823,616,623), + Vector(-452,696,550), + Vector(706,-801,-628), + Vector(-457,-579,-473), + Vector(-310,549,-415), + Vector(-441,686,552), + Vector(504,-665,759), + Vector(461,823,-545), + Vector(-755,-747,659), + Vector(-261,576,-366), + ], + [ + Vector(908,721,721), + Vector(86,-57,-97), + Vector(-713,-299,-592), + Vector(-758,-783,430), + Vector(-779,-597,500), + Vector(818,456,-829), + Vector(-623,521,428), + Vector(-837,-707,424), + Vector(907,-562,-902), + Vector(549,-311,329), + Vector(528,-351,375), + Vector(860,594,720), + Vector(-655,405,401), + Vector(-714,-392,-401), + Vector(829,529,-887), + Vector(912,-363,-865), + Vector(554,-391,342), + Vector(695,675,687), + Vector(-724,-414,-574), + Vector(-659,757,-629), + Vector(-69,-11,3), + Vector(-569,667,-540), + Vector(-586,448,388), + Vector(-580,615,-551), + Vector(907,480,-843), + Vector(858,-589,-853), + ], + [ + Vector(-779,-614,345), + Vector(695,317,397), + Vector(-753,576,801), + Vector(505,341,-394), + Vector(411,432,-345), + Vector(-667,-708,-706), + Vector(615,-385,529), + Vector(643,-378,564), + Vector(557,300,449), + Vector(-879,-666,355), + Vector(483,-862,-738), + Vector(-112,-148,91), + Vector(489,503,-397), + Vector(718,-890,-767), + Vector(-711,-622,-690), + Vector(-721,-601,-807), + Vector(-870,-758,402), + Vector(33,-26,-17), + Vector(-604,557,-459), + Vector(640,-959,-780), + Vector(-592,444,-618), + Vector(627,-473,425), + Vector(-758,670,920), + Vector(-605,562,-647), + Vector(583,497,403), + Vector(-870,560,878), + ] +] From 42f299d11b6f2721775cf770a0a267b4918749f0 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Mon, 20 Dec 2021 13:01:42 +0100 Subject: [PATCH 23/32] Day 20 --- AoC21.xcodeproj/project.pbxproj | 99 ++++++++++++++++ day20.swift | 192 ++++++++++++++++++++++++++++++++ 2 files changed, 291 insertions(+) create mode 100644 day20.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 93eab85..ca5d4b9 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -27,6 +27,8 @@ 265112982767D171009B7607 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 2680ECD2276F23FE00CAB23C /* day19.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2680ECD1276F23FE00CAB23C /* day19.swift */; }; 2680ECD3276F240200CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 2680ECE02770A2FC00CAB23C /* day20.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2680ECDF2770A2FC00CAB23C /* day20.swift */; }; + 2680ECE12770A30300CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; 269BE5CA2762A05300871C85 /* day9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5C82762A04F00871C85 /* day9.swift */; }; 269BE5CC2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; @@ -137,6 +139,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 2680ECD62770A2E900CAB23C /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 269BE5B22762958800871C85 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -189,6 +200,8 @@ 265112962767D16D009B7607 /* day13.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day13.swift; sourceTree = ""; }; 2680ECCA276F23E800CAB23C /* Day19 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day19; sourceTree = BUILT_PRODUCTS_DIR; }; 2680ECD1276F23FE00CAB23C /* day19.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day19.swift; sourceTree = ""; }; + 2680ECD82770A2E900CAB23C /* Day20 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day20; sourceTree = BUILT_PRODUCTS_DIR; }; + 2680ECDF2770A2FC00CAB23C /* day20.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day20.swift; sourceTree = ""; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5BB2762959B00871C85 /* day8part2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day8part2.swift; sourceTree = ""; }; 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -276,6 +289,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECD52770A2E900CAB23C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B12762958800871C85 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -303,6 +323,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 2680ECDF2770A2FC00CAB23C /* day20.swift */, 2680ECD1276F23FE00CAB23C /* day19.swift */, 263D3E41276E10AE00B0AC5B /* day18.swift */, 263D3E32276DDA6600B0AC5B /* day17.swift */, @@ -339,6 +360,7 @@ 263D3E2B276DDA5400B0AC5B /* Day17 */, 263D3E39276E109D00B0AC5B /* Day18 */, 2680ECCA276F23E800CAB23C /* Day19 */, + 2680ECD82770A2E900CAB23C /* Day20 */, ); name = Products; sourceTree = ""; @@ -533,6 +555,23 @@ productReference = 2680ECCA276F23E800CAB23C /* Day19 */; productType = "com.apple.product-type.tool"; }; + 2680ECD72770A2E900CAB23C /* Day20 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 2680ECDE2770A2E900CAB23C /* Build configuration list for PBXNativeTarget "Day20" */; + buildPhases = ( + 2680ECD42770A2E900CAB23C /* Sources */, + 2680ECD52770A2E900CAB23C /* Frameworks */, + 2680ECD62770A2E900CAB23C /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day20; + productName = Day20; + productReference = 2680ECD82770A2E900CAB23C /* Day20 */; + productType = "com.apple.product-type.tool"; + }; 269BE5B32762958800871C85 /* Day8Part2 */ = { isa = PBXNativeTarget; buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; @@ -636,6 +675,10 @@ CreatedOnToolsVersion = 13.2.1; LastSwiftMigration = 1320; }; + 2680ECD72770A2E900CAB23C = { + CreatedOnToolsVersion = 13.2.1; + LastSwiftMigration = 1320; + }; 269BE5B32762958800871C85 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -675,6 +718,7 @@ 263D3E2A276DDA5400B0AC5B /* Day17 */, 263D3E38276E109D00B0AC5B /* Day18 */, 2680ECC9276F23E800CAB23C /* Day19 */, + 2680ECD72770A2E900CAB23C /* Day20 */, ); }; /* End PBXProject section */ @@ -779,6 +823,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECD42770A2E900CAB23C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 2680ECE12770A30300CAB23C /* common.swift in Sources */, + 2680ECE02770A2FC00CAB23C /* day20.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B02762958800871C85 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -1276,6 +1329,43 @@ }; name = Release; }; + 2680ECDC2770A2E900CAB23C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 2680ECDD2770A2E900CAB23C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 269BE5B82762958800871C85 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1468,6 +1558,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 2680ECDE2770A2E900CAB23C /* Build configuration list for PBXNativeTarget "Day20" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2680ECDC2770A2E900CAB23C /* Debug */, + 2680ECDD2770A2E900CAB23C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day20.swift b/day20.swift new file mode 100644 index 0000000..e948e75 --- /dev/null +++ b/day20.swift @@ -0,0 +1,192 @@ + +@main +struct Day20: Puzzle { + func run() { + var image = Image(input) + for i in 0..<50 { + image = image.run(map: map) + if i == 1 { + print("Part 1:", image.countLitPixels()) + } + } + print("Part 2:", image.countLitPixels()) + } +} + +struct Image { + var width: Int + var height: Int + var pixels: [Bool] + var border: Bool + + init(_ string: String) { + pixels = [] + width = 0 + height = 1 + border = false + for char in string { + if char.isNewline { + width = 0 + height += 1 + continue + } + + pixels.append(char == "#") + width += 1 + } + } + + init(width: Int, height: Int, border: Bool) { + self.width = width + self.height = height + self.border = border + self.pixels = Array(repeating: false, count: width * height) + } + + subscript(x: Int, y: Int) -> Bool { + get { + guard 0.. Int { + var index = 0 + for i in -1...1 { + for j in -1...1 { + index = index << 1 | (self[x + j, y + i] ? 1 : 0) + } + } + return index + } + + func run(map: [Bool]) -> Image { + var outputImage = Image(width: width + 2, height: height + 2, border: map[borderIndex]) + for y in -1...height { + for x in -1...width { + outputImage[x + 1, y + 1] = map[lookupIndex(x: x, y: y)] + } + } + return outputImage + } + + func countLitPixels() -> Int { + pixels.lazy.filter { $0 }.count + } +} + +let map = "#####.#.###.###.#.#.####.#####.####.#..####.##.####...##......#.##.##.#.#..#.##.###.#.#.#.##.#..#.####.#.#.##.#..........###.##...#.#...#.#.###..#...##.####.##...###.....##..##..##.#......#.#.##.#.#.##.#.......###.##.#.###.##..##.......##....#.##....#..###.######.##...##.##...#.#.##...##.##.#....##.####..#..#..##.##.#.#..#....#######.###.##...#.####..#.#.#.##...##..##.#.#.#.##.#.......###..######..###..##......###..###.#.#.#.......#.....##.#.##..#.##.#.##.####..#.##....##.#.#..#.####.##.#.#.##...#..##.####." + .map { $0 == "#" } + +let input = """ +.###..###..#.####.#....#..##.###.........#.#.#..#.#...##...#.#........##..#..#.##....#.##.#...###.## +.##......#.#..###.######.#.#.######...#..##...#......###..###..####..####..##..#.##.#.#.#....##..##. +.##.##..###..#...###....#...##..#..######...#.#.##.#..####.#..#..###.######.#.#..#..##...##.#.#.#.#. +..#..##..####...###.#...###.#.####...#####...#####.#.#########.##..####...#....##....##..#.#..#...## +.#.#..#.###....###.#.#..###.#..#####.#.###...####.##.###.#..####.###...#...#.##.#..#..#...##....#### +######....#..##.#..#.##.#.#.##.#.#####.#..###.#####..#######..##..#..#........##.##..#...#..##..#.## +.#.##########.##...#.###..##...#....#.##..##.###..#####...#..##.#.....##..####.#.#.##..#...#..#.#.#. +.##..#.#..#..##.###..#.#.##..#.#...########.##..#....##...#..#.####.#.#..####..#.#..##.#.......##..# +##..##...###.##..#.#.#.###.#..####.##.....#.###.##.##......#.......#.#####.#..##.#.##.#..##.###.##.# +..#..###.....##..####.......####.###.#..##.##.#######.####..#.######.###.#.#..###..###.##.###.##..#. +.##.#..#..#.####.#.#.#..#....#...##.....##..##..##.##.#..##...#..#...##.###.##.##........#.#..#..### +....#.######.#..#......####.##....##.#..#..##...#...#...#.##...##..###.......##.....#..#..#..#####.. +###..##.....##.##.###.##..#.#.###..####.#.#....#.##.#.....#######.##.....#..#####.##..#..#####.##.## +#.#.#..#.#.#..........##.##......#.###...#.###.##.######..#...#...#.......###...#..#.##...#.#.##..## +##...##.###.#.##..##....#.#.#..##.###.##.#...#.#..##..#..##...#..##...##..#.####..#..##.#.#...##.... +...#..###.####.#.####.#.#.#....#.##.##..#####..#####.#.####.##..##.###....###..#....#..##.#.#..#..#. +..#.#.###...###...#####.####..#.#...###.###..#.##....#.#.#.....#..#...#..#.##..####......##.#.###... +#.##.....##...#.#...#####.#.##.##..##.#.##....#.##.....#.###..#..##....###.....#####..####..#..##... +#...##....##..#..####...#...###...##.##..##..#....###.#....#########.##..##.#.#.######.###..####.##. +##.#.##.#.#..#......##.#..#...##..#.#..##.###..##.......##..#.#...#.#..#.#.#....##..######.##...#... +.#..##..#....##..#.##..##...#..##...#....#.#.##..#...##.##.#..#.#.#.##.#..#.#.......#.###..######..# +#..##.#.##.#..##...####.##.#...####...##.###..#..#.#.#..###..##.#....#####.###..#...#..#..####...##. +#.####....######.....#.##..####....###.##....###.####...##.#....####.#..##..#..##..#.##.####.#.....# +...#.#.###...####..#..#.#..#.##...###.#.#.##.#...#.#.#.##.##...#.#####.#....#..##.#...#.##....##.... +#..##..#.##..#.#.#..#..###.##.....####..##..##..#######...##.#.....##..#...########..#.#.#.....##... +.#...##...##..##.#..##......#.##......#.###.##.##...##....####.##.#.....#.#.#.#.##.#.##.#..#..##..#. +.#..#..###.#.#.##..#..#...#..##..#.##...##..#...######..##.#.######........###.#..##..##..######.##. +#####..##.####.....###.###.########.###...#########..#.#.###.#..#####..#..###.#####.#.....#..##..##. +........#....###..####.#.###...#.###.........#.......#..#.#..###.##.#...#.###.##..#...##.##.##.#.### +.##....##.#..##.##...####..########.#####..#.#.#...#.##..#.##..######.###.#..##.#..#...###..#.##.### +#..#.##.##..#.#.#...##....#.#..#..###...###..#..#..##.#.....#..#..#.#.####...#.####.#.#.#.#.#.....#. +###..##.##..##.###.#.#....#..........##.#....#.#.##......#.##.#...#####.##....#.#.#....#.###.#.####. +###.....##..#....#..##.##....#..##.##.#...#..#.#####.####.#..##...#...##.###..##.####..##...##.####. +#.#..#.#.#.#..#..#.###..###.##.###.#....#..###.#.###.....##..#.#.##.#....#.#.#.#.#...#.##.######.##. +##....###...#...#.######.#.#.#....#..#.###.#.##..##...#.....##.#...##.#.#####.##.#.##########.####.. +...#.#.#..###.#....##.#....##.#..##...#.##....#...#####.#.....##..#.##.##.....#.##.#.#..##.##.#..#.# +###.##.########.##..##.#...###.##.##.#.#...##..###...##.##.##.#..#...#.#.##.....##.....#..##.#.#.### +#.###.#......####..###.....#.##.###..###....###.....#..#.#.##.##....#.#....#.######.#...##...######. +#.#####.###..#.#..........#.######.#####.####.##.##....###..#.###.#.......#....##.#.######..#.###.#. +#.#.##..#...##...#..#.#...#.##..#.####.#.......####.##..#.##..##.#....#.#.....#..#..#...#..#.##.#.#. +####.#.##..##....####.#.##.#.##..#.#.#.#####.####.#.##..##.#.####.#..##..#.###.#...#.##...#.#..###.. +#.###...#.#...##.####...###..#..##.#.##.#.#.######...##.##.#.####..#.###..#.##...##..##.#.#.#.####.# +....#.##..##..####......###.#.....#....######.#...##..#..##.###...##.#####..#.##...#...##.#.#####.## +..##...#..##.......#.#.###.#...#####........###...#....#...##.##...###....##.##.#..####....##..##.#. +.#..###.##......##..#...##......##.#.##..##..###.#.#.#.##...#...#.#.....#.#.#.......#..###..####.### +...#..#..##...#..##.##.#####.#.##.#.#...##.####.###...###.#.##.##.##..#.##..#..##.#.###....###..#### +#......#...#.#.#.#.#..#...#....###.##.##...######....#.###.##..##.##..#.#..#.####.#.####.......##.#. +#.###.#..####....##.##.###.##.#.#######..##.##.#.#..#...#.#..####.##..#.##.#....#....#.##.#.#.##...# +##...###..##.#.##...##..####.#.#.#####.#.#.##..........#....#...#..##.###....###.#.#.###.###....#.#. +###.#.##..#....#.#.#.##.######..####.#...##.##...###..###..##.........##.##.#.###...##.#####...###.# +..###..#.##..######.#....#...##.#.....#..######.....#.####.#..###..#..##.##...##..##....##..###..##. +#.#.#############.#..##.#.#..#####.#.#.....#....####.#....#..##..###.#...#.##...###.##...##...#..##. +##..##........##.#.##.###..#.#.##..#...#.##.###.#.###########..#.#.#.##..#....#....#.#..#...##...#.. +#.#.#...##...#.#.##.#..##.#.####.##.#....##..#..#.....#...######.#...##...####.#....#.........#.#... +##..#.#.##...#....#.##..##.##.###.#.....#.########.##.....#....######..##......#.#...##..##.#.####.. +.##.....###..######..#...#......#..#....##.##...#.##...#.#.###...##...#.#.#..#.#..#.#..#.##..###.### +#..#....#...###.#..#########..##.#.......#.###..###...#....#..##.########.#....#.#..###.##.#......#. +###..#..##....####...###.....#....###...#.#..#...#..#.#####...#.####.......#.##.#.....##.##...##.### +..###..#.###..#.##.......###..##...##....#.#.##.#.....###..###.#..####.#####.#..#.#...###.#.####.#.. +#.##.#..##.####.#####.#.#.#...######.#.#.#....#.#.#.#...#.##.......###...#.#######...##..###.#.###.# +..##.#....#####.#..##.#..#.#.#.#.#####.#.###...#..##.#.#.####.#.#....##..###.#.##....##..#####.###.. +.....#####.#..#.#.....##.#..###.#.##..#..#..#...#.#....#.##.#.#..#...####...#.#..#.##.##.##.#.#...#. +##.##.######....#....#..##.##.##..###..##.#..####.#.##.#...##.#.##.###.######..#####.....#.##.##.### +.#.##.#...#..#######.####.....###.#.#.#...###....#.#.#...#..#.#.....###...###.#...#.#.....##..#...## +.#####..##.####.##.###.##.##..##..####...#.#.###....#.#.....#...#####...###.##.####....#.###.##.#### +.#...#....###.########.##..##.##.##...###..#...#..###..#..#.#.###.#...#.#..##..###.#.....#...####.#. +#.#.#..#.##..###.#.###.##.##...##.#.##.#.#.##......#.....###.#.####..##.#.##..#...###...#..##..#...# +.##....######.#.##....#.#.#...#....###...##.##.#.#..##.#....#.##.###.#.####..##...###.#..#.....#.### +...#.####......#.##...#...####......#..#######.#.#.##..##....####.##....##.##..#.##..###...####.#.#. +####..####..##..#...#.#.....#..#..#.......#.##.#.....#.#.##.#..#####...##.#....##........#.........# +.#.#.#......##..#.###.##....#.#..######.##.#.#..#...#...#....###.....###.###....##.#.##..#.#..##.#.# +.##..###....#.#.#.#..#...##.#.##..#.#.##..#.#####.#...#..#...#####.###..##..#.....#.##..##.####.#.#. +..#..##.####....##.##.#...##.##.#..##.###.#..#.###...#...##.#.##.##.#.#..##.##.##..#.....#..####.##. +.#..#..#.#.#.###...###.##.##..##..##....##...#####.#.#...###.##.#.#....##.#..#...#.#..##...#.####... +#####......##....#.##..##.##..#..#.##..#.##.#.######..##..##...#..#.#..#.###..####.#.###..#.#..###.# +.####..#....#.##....#.##.#...#.###..###...##.##.##.#..#..##..#.....#.#########..#...#..####.##....## +####.###..##.#.#.##.#.####.###.###.#...#####.....#.#....#..####.#.#...####.##....#..##.#....#.#.###. +##.##...##...#.#####..#...#.#.##.#.##.#....#..#..####.#.#####.#...#...#..##.##.###..###.#....####..# +.##.####..###..#.####.#.....###....#...#..#...###.....#.####.#....##.####..#.###..#.###...#....##... +#....#...###.#...#.##.#.....#.##.###.....#.##...#...##.##...###.##...##..#####...#....#....#..###.#. +#.#...##.#.#..##..##..###..##...####.#.#..##..##.##..##.#.###.##...###.#.##.##.##..##..###..###..#.# +.##..###...#.####..#####.#.###....###.#..##..####.##..#.##.....###...#...#..##.###.#.#######.....#.. +.####....##..##......##.#......#.##..##.##.###..###.......###....####.#.##..#.####..##.#.#.#.##.#..# +##...#####.#.....##..###...###.#...##..#####.#..###...#####..###......#...#.###.##..##...#.###...### +..###.#...###..#.##.##.##.#.##.....##.###..##..##.##..###....###..#.......###.##...##....#..##..##.# +#..#...##.###.#...#.##...#.##.###.##...##.#...##.#.#.#..#....###.#.......#.##.#..###.#.##.##..####.# +##...####.......#..##.#.#.#..#.##....#.##.##.#.####..##....###.####.#.###..#.##..##......#.#.#..#### +###...#..##.#.......##..#.#...##...#...##..##.#.#..#.#.#.####.##.##.###..#.##.####..####....####.#.. +#..###.##.....###..##.##.###.###.#.#....##.#######....#####.##.#....###.##..#...#..#.##.#.#.#.#..#.. +.#.....#..###..##.#..##.##.###.##..#....###...##........#....####..#.##########...#....###.#...##.## +#.....#.#.#####.#.######.#....##..#...#.##.#..##..#####...######.#.####...#.####.###..#.##....##...# +.#.#...#.#..#.##.#..####..#..#.....##.#....#.#######.#.#..#.#.#.#.###...##..####..##.##.##..#.###... +.#..#.##.#..##.#.##....##..###..#........#.##.#.##.#..##.#.###.#.#..#...#..####.##...#.....#.###...# +..#....##..#..#.##.##.#..###......#.#..#.####..##.#.######..#######..##.##.#..##....#.##.....#.###.. +.#....#..#....#...#.###...#.#...##..#..##.######..#..###.##..###.#.##...#.#..#.###.#..###.##...#..#. +##...##.####.##..####.#.#....#....#.###.##.###.##.##..#.#.#.....##...#.#..#####.##..#.#.....#.#.#### +.....#.#.##..######....#...#.##..#...#..#...#.##..##..##.....#...##..#.#.####......#.#.##..#.#.#..#. +#.##...##.##..###..#....#...####..##....#.#....#..#.#.....######.....##....#..###.......##......#.#. +#.##.....##.#.#...#.#######.###..#..#...##...#.#.####.#########...#..#....#.....##.#.#.###.##..#.#.. +.#.......#.##.....#####.#..#...###.##...#.#..#.###.#.#.#.#########.##.###...####...##.##...###.#.#.# +""" From 65b3920d515d5f45e0ddc2725fa35d5d5f5c6533 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 21 Dec 2021 10:53:36 +0100 Subject: [PATCH 24/32] Day 21 Part 1 --- AoC21.xcodeproj/project.pbxproj | 85 +++++++++++++++++++++++++++++++++ day21.swift | 55 +++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 day21.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index ca5d4b9..069ed41 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -29,6 +29,8 @@ 2680ECD3276F240200CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 2680ECE02770A2FC00CAB23C /* day20.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2680ECDF2770A2FC00CAB23C /* day20.swift */; }; 2680ECE12770A30300CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 2680ECEE2771D84100CAB23C /* day21.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2680ECE22771D82400CAB23C /* day21.swift */; }; + 2680ECEF2771D84100CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; 269BE5CA2762A05300871C85 /* day9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5C82762A04F00871C85 /* day9.swift */; }; 269BE5CC2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; @@ -148,6 +150,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 2680ECE52771D82F00CAB23C /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 269BE5B22762958800871C85 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -202,6 +213,8 @@ 2680ECD1276F23FE00CAB23C /* day19.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day19.swift; sourceTree = ""; }; 2680ECD82770A2E900CAB23C /* Day20 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day20; sourceTree = BUILT_PRODUCTS_DIR; }; 2680ECDF2770A2FC00CAB23C /* day20.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day20.swift; sourceTree = ""; }; + 2680ECE22771D82400CAB23C /* day21.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day21.swift; sourceTree = ""; }; + 2680ECE72771D82F00CAB23C /* Day21 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day21; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5BB2762959B00871C85 /* day8part2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day8part2.swift; sourceTree = ""; }; 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -296,6 +309,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECE42771D82F00CAB23C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B12762958800871C85 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -323,6 +343,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 2680ECE22771D82400CAB23C /* day21.swift */, 2680ECDF2770A2FC00CAB23C /* day20.swift */, 2680ECD1276F23FE00CAB23C /* day19.swift */, 263D3E41276E10AE00B0AC5B /* day18.swift */, @@ -361,6 +382,7 @@ 263D3E39276E109D00B0AC5B /* Day18 */, 2680ECCA276F23E800CAB23C /* Day19 */, 2680ECD82770A2E900CAB23C /* Day20 */, + 2680ECE72771D82F00CAB23C /* Day21 */, ); name = Products; sourceTree = ""; @@ -572,6 +594,23 @@ productReference = 2680ECD82770A2E900CAB23C /* Day20 */; productType = "com.apple.product-type.tool"; }; + 2680ECE62771D82F00CAB23C /* Day21 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 2680ECEB2771D82F00CAB23C /* Build configuration list for PBXNativeTarget "Day21" */; + buildPhases = ( + 2680ECE32771D82F00CAB23C /* Sources */, + 2680ECE42771D82F00CAB23C /* Frameworks */, + 2680ECE52771D82F00CAB23C /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day21; + productName = Day21; + productReference = 2680ECE72771D82F00CAB23C /* Day21 */; + productType = "com.apple.product-type.tool"; + }; 269BE5B32762958800871C85 /* Day8Part2 */ = { isa = PBXNativeTarget; buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; @@ -679,6 +718,9 @@ CreatedOnToolsVersion = 13.2.1; LastSwiftMigration = 1320; }; + 2680ECE62771D82F00CAB23C = { + CreatedOnToolsVersion = 13.2.1; + }; 269BE5B32762958800871C85 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -719,6 +761,7 @@ 263D3E38276E109D00B0AC5B /* Day18 */, 2680ECC9276F23E800CAB23C /* Day19 */, 2680ECD72770A2E900CAB23C /* Day20 */, + 2680ECE62771D82F00CAB23C /* Day21 */, ); }; /* End PBXProject section */ @@ -832,6 +875,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECE32771D82F00CAB23C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 2680ECEE2771D84100CAB23C /* day21.swift in Sources */, + 2680ECEF2771D84100CAB23C /* common.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B02762958800871C85 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -1366,6 +1418,30 @@ }; name = Release; }; + 2680ECEC2771D82F00CAB23C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 2680ECED2771D82F00CAB23C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 269BE5B82762958800871C85 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1567,6 +1643,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 2680ECEB2771D82F00CAB23C /* Build configuration list for PBXNativeTarget "Day21" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2680ECEC2771D82F00CAB23C /* Debug */, + 2680ECED2771D82F00CAB23C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day21.swift b/day21.swift new file mode 100644 index 0000000..78d6bb5 --- /dev/null +++ b/day21.swift @@ -0,0 +1,55 @@ +@main +struct Day21: Puzzle { + func run() { + part1() + } + + func part1() { + var die = Die() + var player1 = Player(position: 6) + var player2 = Player(position: 7) + + while true { + if player1.move(die.roll3()) { + print("Part 1: Player 1 won. ", die.rolls * player2.score) + break + } + + if player2.move(die.roll3()) { + print("Part 1: Player 2 won. ", die.rolls * player1.score) + break + } + } + } + + struct Player { + var position: Int + var score: Int = 0 + + mutating func move(_ roll: Int) -> Bool { + position = 1 + (position + roll - 1) % 10 + score += position + return score >= 1000 + } + } + + struct Die { + + var rolls = 0 + var nextRoll = 1 + + mutating func roll() -> Int { + defer { + rolls += 1 + nextRoll += 1 + } + + return nextRoll + } + + mutating func roll3() -> Int { + roll() + roll() + roll() + } + } + +} From c957038fd78df0f1e8d2d08ff0943c3e67e1adf2 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Tue, 21 Dec 2021 11:25:58 +0100 Subject: [PATCH 25/32] Day 21 Part 2 --- day21.swift | 84 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 19 deletions(-) diff --git a/day21.swift b/day21.swift index 78d6bb5..90e9c82 100644 --- a/day21.swift +++ b/day21.swift @@ -2,54 +2,100 @@ struct Day21: Puzzle { func run() { part1() + part2() } + let player1 = Player(position: 6) + let player2 = Player(position: 7) func part1() { var die = Die() - var player1 = Player(position: 6) - var player2 = Player(position: 7) + var player1 = player1 + var player2 = player2 while true { - if player1.move(die.roll3()) { + if player1.move(die.roll3()) >= 1000 { print("Part 1: Player 1 won. ", die.rolls * player2.score) break } - if player2.move(die.roll3()) { + if player2.move(die.roll3()) >= 1000 { print("Part 1: Player 2 won. ", die.rolls * player1.score) break } } } - struct Player { + func part2() { + var universes: [Universe: Int] = [Universe(player1: player1, player2: player2): 1] + var player1Wins = 0 + var player2Wins = 0 + while !universes.isEmpty { + var newUniverses: [Universe: Int] = [:] + for (universe, universeCount) in universes { + for (offset, player1RollCount) in rollCounts.enumerated() { + var u = universe + if u.player1.move(offset + minRoll) >= quantumMax { + player1Wins += universeCount * player1RollCount + continue + } + + for (offset, player2RollCount) in rollCounts.enumerated() { + var u2 = u + if u2.player2.move(offset + minRoll) >= quantumMax { + player2Wins += universeCount * player1RollCount * player2RollCount + continue + } + + newUniverses[u2, default: 0] += universeCount * player1RollCount * player2RollCount + } + } + } + + universes = newUniverses + } + + print("Part 2: ", max(player1Wins, player2Wins)) + } + + let quantumMax = 21 + + let minRoll = 3 + // 3 4 5 6 7 8 9 + let rollCounts = [1, 3, 6, 7, 6, 3, 1] + + struct Universe: Hashable { + var player1: Player + var player2: Player + } + + struct Player: Hashable { var position: Int var score: Int = 0 - mutating func move(_ roll: Int) -> Bool { + mutating func move(_ roll: Int) -> Int { position = 1 + (position + roll - 1) % 10 score += position - return score >= 1000 + return score } } struct Die { - var rolls = 0 - var nextRoll = 1 + var rolls = 0 + var nextRoll = 1 - mutating func roll() -> Int { - defer { - rolls += 1 - nextRoll += 1 + mutating func roll() -> Int { + defer { + rolls += 1 + nextRoll += 1 + } + + return nextRoll } - return nextRoll - } - - mutating func roll3() -> Int { - roll() + roll() + roll() - } + mutating func roll3() -> Int { + roll() + roll() + roll() + } } } From 67d4d86e8149c46de63877b17fc965544bfd57e6 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Wed, 22 Dec 2021 11:19:25 +0100 Subject: [PATCH 26/32] Day 22 Part 1 --- AoC21.xcodeproj/project.pbxproj | 99 +++++++ day22.swift | 511 ++++++++++++++++++++++++++++++++ 2 files changed, 610 insertions(+) create mode 100644 day22.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 069ed41..6151db3 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -31,6 +31,8 @@ 2680ECE12770A30300CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 2680ECEE2771D84100CAB23C /* day21.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2680ECE22771D82400CAB23C /* day21.swift */; }; 2680ECEF2771D84100CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 2680ECFC27732A9400CAB23C /* day22.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2680ECFB27732A9400CAB23C /* day22.swift */; }; + 2680ECFD27732AA000CAB23C /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 269BE5BC2762959C00871C85 /* day8part2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5BB2762959B00871C85 /* day8part2.swift */; }; 269BE5CA2762A05300871C85 /* day9.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5C82762A04F00871C85 /* day9.swift */; }; 269BE5CC2762A08800871C85 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; @@ -159,6 +161,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 2680ECF227732A8300CAB23C /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 269BE5B22762958800871C85 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -215,6 +226,8 @@ 2680ECDF2770A2FC00CAB23C /* day20.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day20.swift; sourceTree = ""; }; 2680ECE22771D82400CAB23C /* day21.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day21.swift; sourceTree = ""; }; 2680ECE72771D82F00CAB23C /* Day21 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day21; sourceTree = BUILT_PRODUCTS_DIR; }; + 2680ECF427732A8300CAB23C /* Day22 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day22; sourceTree = BUILT_PRODUCTS_DIR; }; + 2680ECFB27732A9400CAB23C /* day22.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day22.swift; sourceTree = ""; }; 269BE5B42762958800871C85 /* Day8Part2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day8Part2; sourceTree = BUILT_PRODUCTS_DIR; }; 269BE5BB2762959B00871C85 /* day8part2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day8part2.swift; sourceTree = ""; }; 269BE5C12762A03F00871C85 /* Day9 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day9; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -316,6 +329,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECF127732A8300CAB23C /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B12762958800871C85 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -343,6 +363,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 2680ECFB27732A9400CAB23C /* day22.swift */, 2680ECE22771D82400CAB23C /* day21.swift */, 2680ECDF2770A2FC00CAB23C /* day20.swift */, 2680ECD1276F23FE00CAB23C /* day19.swift */, @@ -383,6 +404,7 @@ 2680ECCA276F23E800CAB23C /* Day19 */, 2680ECD82770A2E900CAB23C /* Day20 */, 2680ECE72771D82F00CAB23C /* Day21 */, + 2680ECF427732A8300CAB23C /* Day22 */, ); name = Products; sourceTree = ""; @@ -611,6 +633,23 @@ productReference = 2680ECE72771D82F00CAB23C /* Day21 */; productType = "com.apple.product-type.tool"; }; + 2680ECF327732A8300CAB23C /* Day22 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 2680ECFA27732A8400CAB23C /* Build configuration list for PBXNativeTarget "Day22" */; + buildPhases = ( + 2680ECF027732A8300CAB23C /* Sources */, + 2680ECF127732A8300CAB23C /* Frameworks */, + 2680ECF227732A8300CAB23C /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day22; + productName = Day22; + productReference = 2680ECF427732A8300CAB23C /* Day22 */; + productType = "com.apple.product-type.tool"; + }; 269BE5B32762958800871C85 /* Day8Part2 */ = { isa = PBXNativeTarget; buildConfigurationList = 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */; @@ -721,6 +760,10 @@ 2680ECE62771D82F00CAB23C = { CreatedOnToolsVersion = 13.2.1; }; + 2680ECF327732A8300CAB23C = { + CreatedOnToolsVersion = 13.2.1; + LastSwiftMigration = 1320; + }; 269BE5B32762958800871C85 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -762,6 +805,7 @@ 2680ECC9276F23E800CAB23C /* Day19 */, 2680ECD72770A2E900CAB23C /* Day20 */, 2680ECE62771D82F00CAB23C /* Day21 */, + 2680ECF327732A8300CAB23C /* Day22 */, ); }; /* End PBXProject section */ @@ -884,6 +928,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 2680ECF027732A8300CAB23C /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 2680ECFD27732AA000CAB23C /* common.swift in Sources */, + 2680ECFC27732A9400CAB23C /* day22.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 269BE5B02762958800871C85 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -1442,6 +1495,43 @@ }; name = Release; }; + 2680ECF827732A8400CAB23C /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 2680ECF927732A8400CAB23C /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 269BE5B82762958800871C85 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1652,6 +1742,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 2680ECFA27732A8400CAB23C /* Build configuration list for PBXNativeTarget "Day22" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 2680ECF827732A8400CAB23C /* Debug */, + 2680ECF927732A8400CAB23C /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 269BE5BA2762958800871C85 /* Build configuration list for PBXNativeTarget "Day8Part2" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day22.swift b/day22.swift new file mode 100644 index 0000000..9a67df3 --- /dev/null +++ b/day22.swift @@ -0,0 +1,511 @@ +import Foundation + +@main +struct Day22: Puzzle { + func run() { + var cube: [Point: Bool] = [:] + let activeRange = -50...50 + + let scanner = Scanner(string: input) + while !scanner.isAtEnd { + let (command, xrange, yrange, zrange) = scanner.line() + if let xrange = xrange.intersection(with: activeRange) { + for x in xrange.clamped(to: activeRange) { + if let yrange = yrange.intersection(with: activeRange) { + for y in yrange.clamped(to: activeRange) { + if let zrange = zrange.intersection(with: activeRange) { + for z in zrange.clamped(to: activeRange) { + cube[Point(x: x, y: y, z: z)] = command + } + } + } + } + } + } + } + + + var sum = 0 + for x in activeRange { + for y in activeRange { + for z in activeRange { + sum += cube[Point(x: x, y: y, z: z), default: false] ? 1 : 0 + } + } + } + print("Part 1:", sum) + } +} + +extension ClosedRange { + func intersection(with other: Self) -> Self? { + guard upperBound >= other.lowerBound && lowerBound <= other.upperBound else { return nil } + return clamped(to: other) + } +} + +struct Point: Hashable { + var x: Int + var y: Int + var z: Int +} + +extension Scanner { + func onOff() -> Bool { + if scanString("on") != nil { + return true + } + + if (scanString("off") != nil) { + return false + } + + fatalError("Invalid command") + } + + func range() -> ClosedRange { + guard let min = scanInt(), + scanString("..") != nil, + let max = scanInt() + else { + fatalError("Invalid range") + } + + return min...max + } + + func line() -> (Bool, ClosedRange, ClosedRange, ClosedRange) { + let command = onOff() + guard scanString("x=") != nil else { fatalError() } + let xRange = range() + guard scanString(",y=") != nil else { fatalError() } + let yRange = range() + guard scanString(",z=") != nil else { fatalError() } + let zRange = range() + return (command, xRange, yRange, zRange) + } +} + +let input = """ +on x=-28..25,y=-34..15,z=-36..13 +on x=-9..36,y=-2..43,z=-47..5 +on x=-27..26,y=-7..41,z=-8..46 +on x=-28..26,y=-11..39,z=-32..17 +on x=-13..41,y=-30..24,z=-47..1 +on x=-4..45,y=-48..-3,z=-39..13 +on x=-49..-3,y=-29..20,z=-39..7 +on x=-29..20,y=2..46,z=-31..15 +on x=-10..34,y=-12..41,z=-15..32 +on x=-12..41,y=-6..42,z=-22..28 +off x=-8..9,y=-31..-21,z=-30..-11 +on x=1..47,y=-30..16,z=-9..38 +off x=-34..-16,y=-48..-31,z=9..25 +on x=-29..19,y=-38..15,z=-34..20 +off x=-3..12,y=25..34,z=-43..-32 +on x=-36..17,y=-23..31,z=-29..16 +off x=14..27,y=-30..-21,z=3..21 +on x=-31..14,y=-36..8,z=-36..12 +off x=24..34,y=-27..-10,z=-34..-17 +on x=-33..14,y=-45..6,z=-42..2 +on x=-84084..-49906,y=-34022..-13687,z=-26508..-7161 +on x=-5505..17661,y=15376..39557,z=64634..85669 +on x=-77470..-55853,y=-25906..-5142,z=-42096..-20083 +on x=-44899..-13829,y=5050..26536,z=67119..86305 +on x=-63460..-40980,y=33003..59169,z=25378..44440 +on x=-17311..-1574,y=-86884..-62730,z=-54963..-35830 +on x=26480..34369,y=39725..55309,z=-77095..-43655 +on x=22634..49093,y=-13808..7138,z=-79760..-60071 +on x=2496..13198,y=-73951..-53407,z=-43731..-30331 +on x=-27451..-20414,y=1868..32906,z=-75318..-62730 +on x=31848..53483,y=-73320..-63684,z=-24860..-14722 +on x=6461..28672,y=1908..29124,z=-84376..-68877 +on x=16931..39395,y=37598..63107,z=47860..60054 +on x=14945..40546,y=-7317..549,z=-93498..-67762 +on x=-85540..-58126,y=7461..23740,z=-29051..-17311 +on x=38640..49114,y=55796..68805,z=-35888..-8915 +on x=-25483..8215,y=32431..39809,z=70198..82364 +on x=-3703..17675,y=61466..90313,z=16909..48922 +on x=44604..75529,y=-22304..-6299,z=45999..60729 +on x=-70454..-38359,y=25776..59602,z=-45661..-30347 +on x=-20074..9993,y=56063..71942,z=-49242..-17209 +on x=35569..59289,y=-52746..-21591,z=33105..49948 +on x=-69553..-46700,y=-21120..10014,z=-78684..-51923 +on x=-64276..-54684,y=41723..63992,z=-20013..-7784 +on x=-1824..9573,y=66316..76607,z=23895..46349 +on x=-54006..-31537,y=9127..34043,z=57224..87032 +on x=61803..80774,y=-50282..-13511,z=-35593..-9531 +on x=-65653..-36057,y=-71428..-40824,z=-50428..-20504 +on x=-35635..-12972,y=-6991..2475,z=-88189..-59265 +on x=67324..77277,y=12215..17359,z=9446..16623 +on x=46320..63691,y=23806..30377,z=-62984..-38536 +on x=-6894..13135,y=-44916..-38815,z=49846..79849 +on x=-67348..-54769,y=-57422..-29061,z=25855..36474 +on x=-28019..-23220,y=-60400..-37046,z=49526..71048 +on x=-42960..-30831,y=-78337..-64643,z=-22038..12986 +on x=-43601..-27531,y=-35599..-20965,z=52822..83732 +on x=-85892..-68621,y=-10994..9023,z=-20838..-9147 +on x=59413..97068,y=-22230..-1408,z=-3604..19485 +on x=62228..85826,y=-31716..-2979,z=12114..36300 +on x=46514..66369,y=-35480..1374,z=36011..51592 +on x=-48685..-36357,y=55919..77922,z=-20577..-12176 +on x=63584..82792,y=11027..31562,z=-43607..-35346 +on x=-13917..12041,y=-73682..-62007,z=35642..62665 +on x=45316..56476,y=13007..26619,z=45798..63220 +on x=-58080..-47733,y=8079..17846,z=53185..62392 +on x=2479..30325,y=-52517..-31422,z=-70357..-52270 +on x=-39911..-29141,y=52574..71764,z=-33493..-5376 +on x=75460..84989,y=-7735..-6452,z=-5685..823 +on x=45533..71393,y=-47931..-13874,z=30994..40795 +on x=-14338..7548,y=-96290..-65900,z=-14911..-8244 +on x=39472..46973,y=-45007..-16123,z=-68972..-43340 +on x=15323..43991,y=-79867..-60758,z=-46..13763 +on x=-34857..-7822,y=70341..75898,z=22751..44742 +on x=60051..71299,y=6656..26788,z=-46635..-29393 +on x=46242..67391,y=-60111..-42428,z=-63406..-43389 +on x=-35622..-13029,y=61821..88175,z=-19300..-3468 +on x=45267..78654,y=940..23235,z=34829..52055 +on x=-64544..-32718,y=-52424..-32259,z=-51391..-42170 +on x=-45310..-38884,y=60663..76511,z=-20350..6437 +on x=-19546..3011,y=-85555..-56269,z=-48635..-31142 +on x=76489..97638,y=-35407..-9542,z=-22900..679 +on x=-34856..-12798,y=57873..72105,z=28432..41250 +on x=-12695..5075,y=-51326..-34170,z=60410..74327 +on x=48950..55957,y=21833..41299,z=33762..48570 +on x=-72062..-57619,y=-30380..-10079,z=-68295..-30427 +on x=9722..29351,y=-48714..-33393,z=-78170..-44048 +on x=8231..26025,y=-77925..-54037,z=-47724..-25194 +on x=-95495..-73464,y=-7621..14339,z=-29593..-13088 +on x=55959..79890,y=-5967..31737,z=-67844..-36315 +on x=53044..74610,y=-30723..-8664,z=-43868..-14796 +on x=-4307..16454,y=35270..42969,z=-81647..-68472 +on x=45874..60812,y=-69404..-54747,z=-8298..-3138 +on x=-41808..-29758,y=-75803..-46116,z=23276..33715 +on x=-65837..-40686,y=-69179..-53790,z=-47995..-22105 +on x=-9912..23894,y=-1700..23494,z=-97303..-62588 +on x=19429..37260,y=-67376..-50156,z=-53512..-16652 +on x=-57067..-52828,y=-12667..-9591,z=-67443..-58160 +on x=-3523..27218,y=74994..90403,z=-3493..14395 +on x=14169..48854,y=-1568..28900,z=-71852..-52553 +on x=32821..57220,y=47717..78952,z=-34811..-13134 +on x=-79129..-71287,y=-18716..3046,z=21374..41841 +on x=-11439..14964,y=-16250..3021,z=65364..90013 +on x=-38129..-32162,y=58740..80207,z=-9827..9520 +on x=40614..58324,y=-11736..-1046,z=47782..68564 +on x=68136..85433,y=19038..53903,z=-31789..4892 +on x=6721..20772,y=74233..93387,z=-17766..5311 +on x=-38118..-12460,y=-57425..-34797,z=55638..66558 +on x=2737..32272,y=-76998..-55030,z=43688..49155 +on x=12198..38223,y=-73791..-64926,z=31631..39745 +on x=-20261..-2856,y=-74905..-54211,z=7861..34791 +on x=19178..35422,y=-11638..11563,z=58261..77953 +on x=-31001..1243,y=11174..27421,z=-83906..-70511 +on x=26738..51818,y=41573..66947,z=5480..38191 +on x=-4120..27825,y=-85989..-63889,z=-33571..-17260 +on x=65955..73858,y=-30937..-20357,z=19699..51919 +on x=59986..81449,y=-17094..-6765,z=-21325..4146 +on x=-15287..13119,y=-67414..-61189,z=-46979..-43603 +on x=-82390..-66004,y=-10271..3338,z=-38978..-23333 +on x=48845..69167,y=43156..62169,z=6515..37326 +on x=-29512..-18622,y=66618..85799,z=16557..37597 +on x=-13680..11486,y=-73990..-52854,z=34292..59824 +on x=16528..41696,y=-88669..-71438,z=6756..28376 +on x=47386..68246,y=26567..41173,z=32881..45573 +on x=41552..60418,y=-13177..5608,z=40188..72493 +on x=-17271..1089,y=66757..90442,z=-8173..4840 +on x=-83957..-59723,y=-5356..22092,z=-46860..-26958 +on x=-66297..-47544,y=-37434..-22806,z=-60731..-47364 +on x=-14961..-8051,y=-82471..-63555,z=-17655..1207 +on x=4207..34538,y=61925..92475,z=-30435..-15910 +on x=-17617..-10570,y=-95348..-63368,z=-16210..14719 +on x=38496..55233,y=-64106..-40918,z=16863..31642 +on x=50719..79640,y=-61152..-34221,z=-10774..9297 +on x=-42969..-7619,y=-26388..-18715,z=70740..87672 +on x=-47929..-35133,y=49347..82369,z=-14714..10853 +on x=-82604..-63475,y=-34831..-12713,z=-11929..4941 +on x=29358..63182,y=8015..29986,z=54315..82349 +on x=-20131..-1839,y=60333..80914,z=7967..29376 +on x=9353..36272,y=59895..89044,z=-25822..3230 +on x=43168..59888,y=-66721..-49667,z=32648..46482 +on x=10941..29136,y=-72555..-53370,z=-54573..-35565 +on x=11496..31590,y=-44959..-13029,z=53082..87310 +on x=-43159..-20524,y=-3743..13835,z=-87866..-52777 +on x=26627..54751,y=-43550..-24270,z=49732..64107 +on x=28255..59585,y=43842..68816,z=-44115..-40179 +on x=-66412..-55612,y=12166..35074,z=39006..51003 +on x=6312..30405,y=57804..74608,z=39542..48278 +on x=-14943..3493,y=10401..33502,z=-94020..-67669 +on x=35476..48357,y=-61493..-38961,z=-71382..-32979 +on x=20435..28523,y=7481..28051,z=-78353..-58295 +on x=-83068..-71048,y=-23721..6279,z=-22062..-4203 +on x=-51208..-17644,y=46982..85355,z=-40990..-15261 +on x=547..20809,y=60044..87576,z=22999..55796 +on x=-17902..-14415,y=-31625..-509,z=-87372..-60462 +on x=566..30656,y=65157..93943,z=-10094..25356 +on x=-57372..-44522,y=-43215..-4269,z=47571..76802 +on x=51928..61917,y=27192..61997,z=16462..35892 +on x=-91859..-77183,y=-25309..-6900,z=-6636..4658 +on x=35451..49841,y=42840..59899,z=27902..37733 +on x=46697..63066,y=-61951..-35956,z=-43402..-31431 +on x=56820..85532,y=22368..37611,z=8181..29028 +on x=3479..28543,y=-92950..-65707,z=-20147..3586 +on x=-30715..-8102,y=-74389..-38791,z=41838..65462 +on x=53923..84954,y=21345..48479,z=-31117..-22707 +on x=30692..68472,y=51381..74010,z=-43163..-26289 +on x=3963..13629,y=-82301..-68766,z=-32189..-28695 +on x=-26230..-18986,y=48059..75879,z=33800..57890 +on x=-11868..364,y=-17805..-6376,z=-87840..-69361 +on x=-62123..-34039,y=53323..65684,z=-18759..11519 +on x=-49686..-22329,y=44482..62763,z=-50165..-42433 +on x=8295..16668,y=-21473..2954,z=72427..92773 +on x=61061..82812,y=-34561..-15760,z=12195..29626 +on x=18871..46026,y=45645..74462,z=34738..47404 +on x=45250..66555,y=-51615..-23083,z=11910..48756 +on x=-74771..-44970,y=-21127..-11037,z=-64988..-33929 +on x=-28575..-24424,y=71792..78110,z=-18221..-8555 +on x=-25338..-6923,y=-8631..16382,z=-96932..-68836 +on x=-15816..8291,y=23502..46422,z=61737..75046 +on x=24012..60280,y=-83721..-54294,z=7578..28977 +on x=11501..29155,y=3402..11782,z=75736..82902 +on x=-57546..-26246,y=36304..67304,z=-45286..-27281 +on x=-6325..5065,y=22635..45274,z=-81700..-68260 +on x=-66720..-54631,y=-61236..-46412,z=-5425..22854 +on x=-72710..-48798,y=50508..64307,z=1333..13462 +on x=13865..37998,y=51466..65722,z=36006..59077 +on x=-28281..-7465,y=-68705..-40266,z=50853..66666 +on x=-38630..-17127,y=-7930..4812,z=-74660..-58504 +on x=20647..33497,y=-62711..-45250,z=-65188..-41498 +on x=-81459..-63994,y=11465..40585,z=18212..44406 +on x=-52840..-33789,y=-40941..-18153,z=40378..62402 +on x=-53664..-41652,y=19752..32228,z=50381..70997 +on x=-1080..23636,y=-33517..-25727,z=-86099..-57148 +on x=-23319..-3146,y=24049..42456,z=-71419..-57820 +on x=9982..44125,y=-22469..-5211,z=-89475..-57073 +on x=-59300..-41829,y=25692..39878,z=-65256..-42734 +on x=-47735..-38054,y=57249..66648,z=-43826..-25726 +on x=-39553..-32542,y=5165..28643,z=55369..70627 +on x=-12872..3087,y=-36519..-10753,z=60621..76591 +on x=59192..83731,y=18036..35836,z=20877..51212 +on x=26020..48490,y=12554..14377,z=-87022..-63383 +on x=-11854..-4515,y=-12639..-5946,z=-85165..-60021 +on x=-52581..-30843,y=59055..82392,z=-11397..22455 +on x=-36423..-16444,y=29020..41390,z=-71107..-58690 +on x=55463..71190,y=-39991..-27121,z=29306..33897 +on x=-41896..-15502,y=-51414..-28802,z=-67096..-46901 +on x=21576..51776,y=-40007..-10272,z=-84715..-46191 +on x=8292..28045,y=6514..32122,z=66114..83717 +on x=-66406..-58191,y=-43227..-14898,z=-59859..-26427 +on x=65446..83471,y=-7820..13657,z=-50600..-32289 +on x=35134..36286,y=-59572..-40161,z=-60756..-35239 +on x=9254..29476,y=67387..82064,z=-6059..20274 +on x=-53418..-42461,y=-25344..-8754,z=-68658..-59076 +on x=-20702..-12302,y=61483..82828,z=-4045..12127 +on x=-15861..5533,y=65152..81160,z=13062..33238 +on x=-67682..-56916,y=-59758..-23693,z=-36108..-26134 +on x=-19546..10161,y=-85094..-56733,z=-41987..-28045 +on x=48091..80455,y=-55246..-43231,z=-2817..22958 +on x=-77749..-58276,y=50507..63707,z=-10508..8335 +on x=-64911..-37001,y=-62267..-44370,z=-47966..-28755 +on x=62326..86674,y=-51021..-17862,z=-32602..-1819 +on x=-70525..-48780,y=-47797..-19221,z=25331..55925 +on x=-50427..-25959,y=-48504..-28133,z=49547..73361 +on x=-73330..-63515,y=-35500..-23379,z=-5114..19001 +off x=51657..61655,y=-4634..27427,z=-69455..-41980 +off x=-84315..-77210,y=19373..36993,z=-21053..15930 +on x=-22479..10246,y=-36815..-4914,z=-83609..-58867 +off x=-55431..-27453,y=-5767..21070,z=60471..70160 +off x=-5688..3205,y=873..13973,z=77366..82873 +off x=-53425..-21862,y=41423..59744,z=-55084..-43328 +on x=-22427..5852,y=-74250..-39007,z=37690..67662 +on x=-63041..-49752,y=-75175..-41598,z=-28260..4287 +off x=15413..50919,y=-63278..-30199,z=54233..71948 +off x=37405..60088,y=-30130..-3727,z=50275..74270 +on x=44953..76055,y=45013..54715,z=3402..24097 +on x=4477..16728,y=585..26623,z=-96312..-73210 +off x=-60707..-37806,y=33556..55805,z=-55830..-34506 +on x=-43041..-34231,y=-75795..-62433,z=-39802..-6990 +on x=7577..34465,y=63182..75089,z=-51613..-28445 +on x=-40656..-32795,y=39054..59670,z=39143..61093 +on x=61949..77774,y=-8028..6257,z=-25989..-7928 +off x=62480..84973,y=-25581..6076,z=-51280..-37961 +on x=-72929..-40057,y=-35351..-8508,z=-65932..-29811 +off x=-65518..-39994,y=-279..16674,z=-73725..-40504 +off x=-37297..-22465,y=-75675..-45041,z=39399..60052 +off x=-52661..-19343,y=49842..69777,z=-49307..-28892 +on x=16218..47208,y=-36176..-16797,z=62683..80721 +off x=-46068..-21667,y=60526..77312,z=4157..8754 +off x=34070..60691,y=-41133..-30172,z=-53958..-38014 +off x=64392..81661,y=-45176..-23170,z=-14523..15457 +off x=-15744..-7180,y=58905..69594,z=43007..61745 +off x=-52963..-30458,y=-10496..7448,z=-83916..-66777 +off x=69857..81390,y=-25485..-7871,z=5030..29910 +on x=-60959..-46201,y=50105..71526,z=-45080..-16114 +on x=18978..27486,y=55295..76687,z=-58694..-25206 +off x=-6820..20202,y=61572..82949,z=17286..19675 +on x=1728..22714,y=-70670..-51775,z=39263..62822 +off x=-45895..-27411,y=-82575..-60562,z=-35050..-16558 +off x=34247..55770,y=-32892..-572,z=49237..79617 +off x=6279..24334,y=-76907..-57315,z=-58904..-36830 +off x=-29510..-16989,y=69568..77772,z=14319..29972 +on x=60510..76515,y=-9611..13786,z=-33980..-20539 +on x=5371..24846,y=-53948..-32082,z=-72848..-59739 +on x=-16322..-10658,y=-42921..-29325,z=-82769..-55636 +on x=-33676..-21096,y=37528..59995,z=-58072..-30498 +off x=-47215..-31769,y=55087..91254,z=-14424..-5524 +off x=51272..71648,y=-9020..25748,z=48474..57714 +on x=-14910..-9150,y=-31157..-7755,z=-86976..-75518 +off x=-55245..-27776,y=-49766..-31808,z=42101..67152 +on x=-69367..-63985,y=-2671..13295,z=-53128..-27235 +on x=58929..86759,y=24302..53114,z=-4060..16704 +off x=-47623..-41234,y=52703..55259,z=-54922..-38332 +off x=234..33436,y=-38688..-18973,z=-78624..-64983 +on x=-77931..-67269,y=-25142..-18496,z=2965..13641 +off x=-35975..-19833,y=3648..26277,z=-89061..-67038 +off x=47790..55618,y=38688..74556,z=10070..21478 +on x=40666..45048,y=-82962..-64543,z=-4571..9994 +on x=11044..15406,y=66273..95175,z=-5970..10319 +off x=27658..45091,y=-13546..1126,z=47771..81258 +off x=-51366..-27007,y=-44699..-24577,z=46267..69042 +on x=-7039..14125,y=-67429..-36525,z=55266..77835 +off x=-14528..14651,y=60280..82464,z=-5661..19789 +off x=6049..22392,y=57922..78173,z=-68277..-33899 +on x=5350..16965,y=-67738..-44461,z=50282..75195 +on x=-29214..-16694,y=-8319..-575,z=66463..75789 +off x=-15036..-8425,y=70237..95064,z=-4849..24023 +off x=-53599..-37851,y=41971..53928,z=41571..46610 +on x=-13677..2807,y=78384..91484,z=-25356..-8582 +off x=24528..48582,y=62896..85790,z=12460..18522 +on x=-48176..-33364,y=14289..15341,z=54092..87843 +on x=-46960..-30174,y=-12120..6661,z=61935..70809 +on x=3481..27256,y=12288..22963,z=-97219..-62916 +off x=-28747..5507,y=17194..46753,z=-91768..-56220 +off x=42783..58738,y=51060..58581,z=820..24693 +on x=-49353..-37799,y=6360..36216,z=48187..77532 +on x=52517..57152,y=-57632..-40065,z=-52048..-22663 +off x=45577..82271,y=-53151..-34086,z=7606..27599 +on x=-49439..-39837,y=30695..51461,z=-60976..-40779 +off x=-66635..-46630,y=49057..63361,z=3526..12518 +on x=54010..66216,y=36321..55042,z=-6269..24374 +off x=4695..7233,y=455..14476,z=62747..89572 +off x=-85493..-66739,y=2471..22091,z=-36223..-29382 +off x=7884..18171,y=-68024..-45024,z=55300..75806 +off x=43549..76092,y=49654..69738,z=1376..15517 +off x=12346..17191,y=-4032..16643,z=72903..90015 +on x=60703..73244,y=-20825..-3421,z=-57128..-36177 +off x=-9866..20026,y=16321..41773,z=60404..85148 +on x=29431..48382,y=48462..74606,z=41279..44993 +on x=62612..79405,y=-5487..10920,z=-40427..-31710 +off x=11859..30527,y=-34097..-19197,z=-73407..-51576 +off x=63862..89056,y=21916..38067,z=-5939..30841 +on x=40214..68256,y=25471..56425,z=-62984..-30420 +on x=-25366..-18100,y=66147..95119,z=3805..18767 +off x=68425..79895,y=-47516..-25384,z=15293..41106 +on x=30487..56428,y=-1794..5876,z=51906..74514 +off x=46653..51836,y=-76040..-44634,z=18181..37159 +off x=-41608..-12051,y=72020..86112,z=-8983..14501 +on x=-88247..-62536,y=-8129..11276,z=-24055..-4427 +off x=3323..10767,y=59354..66158,z=-59632..-39108 +on x=-76419..-67309,y=3713..37240,z=-37530..-11311 +off x=-2144..12919,y=-54075..-34178,z=57045..63905 +off x=62020..79582,y=-31594..-23121,z=-26597..-24909 +off x=-17401..-9352,y=73039..84740,z=-44057..-13270 +off x=-24093..-6768,y=57829..80320,z=-61101..-38285 +on x=-11495..16269,y=-13453..-6215,z=63349..88399 +on x=36287..58675,y=59742..70305,z=-17524..18377 +off x=56801..73533,y=19040..42381,z=-1979..-474 +on x=-58575..-33767,y=-25457..8768,z=-73498..-64326 +off x=-12572..14666,y=-1123..34793,z=-83475..-75872 +on x=-3033..9242,y=17802..28168,z=74794..85676 +on x=2218..27568,y=67949..82818,z=-25004..-15400 +off x=-6346..-4816,y=56884..81616,z=-27429..-3943 +on x=854..25707,y=22958..43224,z=-82067..-52397 +on x=32311..41886,y=-68875..-42153,z=-52635..-38254 +off x=26308..38287,y=-83310..-60192,z=28347..48640 +on x=-32295..-7848,y=-87573..-75370,z=-12081..5422 +on x=-42105..-23185,y=-29547..-19413,z=-72177..-51007 +on x=-14692..-9809,y=73589..83475,z=-36237..-2728 +off x=-73543..-55115,y=23711..51688,z=-3991..1464 +off x=-14534..22568,y=-54527..-44508,z=57714..69383 +on x=-63081..-39714,y=-45491..-26566,z=-49886..-38562 +off x=-93619..-68748,y=-16696..-4661,z=-30558..-4126 +on x=-66806..-44552,y=37360..55716,z=35175..52862 +on x=-6316..26070,y=-54927..-28748,z=-69954..-47453 +off x=-78871..-50085,y=17954..32161,z=-36569..-23985 +on x=6369..25338,y=67499..88203,z=-26047..-4496 +on x=24755..54569,y=-67118..-37387,z=32897..50992 +off x=46528..63982,y=-54598..-26438,z=37906..65831 +on x=4634..24098,y=-45650..-35611,z=-67709..-58572 +off x=44078..48115,y=-75447..-53188,z=-5442..33129 +off x=-62868..-56634,y=46581..58996,z=-29362..-5667 +on x=-6024..8438,y=65634..74871,z=-38003..-30673 +off x=-59956..-46026,y=41808..66700,z=10165..36114 +off x=-32873..-20372,y=-88729..-60899,z=-19758..7186 +on x=-67393..-60159,y=-56680..-32136,z=-8305..17511 +off x=-20681..3599,y=-92468..-57916,z=12916..28612 +on x=-7301..9140,y=37170..58369,z=-72713..-47101 +on x=66247..85599,y=3262..12242,z=-7260..4927 +off x=-40965..-24082,y=-82146..-62002,z=20400..46309 +off x=-70415..-51837,y=-56997..-42331,z=-875..19799 +on x=57902..75722,y=35482..60227,z=-26389..-9051 +off x=-22870..7672,y=-4882..25620,z=-87984..-60436 +on x=3155..35881,y=-80545..-67121,z=-6933..22549 +off x=-65134..-44685,y=-66218..-45701,z=21040..41162 +on x=36126..69247,y=23453..60238,z=38126..59678 +on x=55937..67003,y=6113..21587,z=45836..60761 +off x=18451..41277,y=-70926..-44655,z=-60498..-33470 +on x=-59038..-40083,y=-53819..-22588,z=-55419..-43900 +on x=-22584..6019,y=59421..80916,z=29210..42933 +on x=-33931..-15311,y=-76316..-51022,z=21445..40518 +on x=42213..72279,y=-36048..-15004,z=26488..50017 +on x=-28842..-8697,y=-68609..-44348,z=40673..64865 +off x=-7167..8781,y=-3621..17677,z=-93905..-64435 +off x=-82972..-60284,y=6678..34439,z=-3504..-1846 +off x=-67834..-53928,y=-58517..-42067,z=-42608..-8421 +off x=-4855..22956,y=9296..23886,z=-94874..-60207 +off x=-79569..-70626,y=2629..27696,z=-22941..3340 +on x=-10965..20710,y=51680..63775,z=45787..69225 +off x=46280..63357,y=42031..64449,z=3459..25386 +on x=-254..28129,y=-81371..-67737,z=21692..52613 +on x=-2383..5437,y=66740..96837,z=10033..17517 +off x=17147..30452,y=-91140..-71945,z=1097..25760 +on x=-47454..-28074,y=-77661..-55440,z=-24637..-384 +off x=-57862..-35813,y=19453..29451,z=39191..70727 +off x=-57539..-22945,y=22003..33680,z=-67047..-49723 +off x=-3162..20231,y=-54634..-42988,z=53582..62417 +on x=-81550..-64507,y=-27996..-14746,z=-10531..-7267 +off x=20586..40845,y=-81557..-67548,z=6923..34475 +off x=22518..23900,y=-66351..-34420,z=51370..64347 +on x=-49052..-35860,y=-19130..11213,z=61999..78547 +off x=-34188..-18864,y=-81419..-70168,z=-37648..-12239 +off x=77362..83490,y=2745..18791,z=8832..18419 +on x=-32402..-14262,y=68320..76432,z=-35620..-15790 +off x=67237..90757,y=4695..12771,z=-19624..10840 +off x=27845..36686,y=-50404..-33761,z=-80878..-55141 +off x=36370..69461,y=33759..65210,z=-54985..-28791 +on x=-59135..-46273,y=7474..26326,z=-65975..-58673 +on x=26877..52927,y=-79947..-54801,z=-2779..22702 +off x=-71675..-51921,y=-59152..-34586,z=-8374..19519 +on x=6403..17478,y=-75737..-58369,z=-71127..-34808 +on x=13332..40836,y=-40399..-31096,z=-85343..-51700 +off x=-67594..-49756,y=11777..23306,z=-64433..-55048 +off x=21971..44163,y=52916..80818,z=-13270..-4413 +on x=66677..74076,y=22958..42365,z=5671..22730 +on x=-17496..17047,y=2018..35721,z=62177..87439 +off x=-46335..-30771,y=12773..50699,z=45373..69810 +on x=-82592..-68036,y=-13665..-9385,z=185..18296 +off x=31849..55495,y=-68614..-40470,z=37173..62475 +on x=-57887..-33776,y=-71721..-40636,z=-45..25174 +off x=35969..44108,y=-5435..14452,z=-88200..-55792 +on x=60659..86542,y=7942..20443,z=4839..32918 +off x=50661..57689,y=-33927..-25875,z=46713..54231 +on x=-30700..-7902,y=71035..78599,z=-30964..-7588 +off x=769..24000,y=-68842..-41325,z=-63861..-53498 +on x=-83517..-62573,y=-23803..5911,z=20605..50776 +off x=-77447..-65015,y=-7232..20379,z=36322..47544 +off x=54759..77852,y=-9847..18404,z=-54569..-25017 +on x=8814..38891,y=44583..61983,z=27053..54606 +off x=-32755..-26721,y=-27446..-17253,z=-80334..-52377 +on x=23465..37441,y=-37240..-20297,z=-68128..-49025 +on x=-5136..18927,y=17373..30697,z=66878..78408 +on x=-29189..-6136,y=-76807..-52880,z=26943..52440 +""" + From ba6cee935ad813644547453474832e5bcfa122ad Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Thu, 23 Dec 2021 20:20:12 +0100 Subject: [PATCH 27/32] Day 23 --- AoC21.xcodeproj/project.pbxproj | 85 ++++++++++ day23.swift | 284 ++++++++++++++++++++++++++++++++ 2 files changed, 369 insertions(+) create mode 100644 day23.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 6151db3..d15ff6e 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 26132D372774C886004F0228 /* day23.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26132D2B2774C871004F0228 /* day23.swift */; }; + 26132D382774C886004F0228 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 2615545A276A6C2C00374D18 /* day14.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26155459276A6C2C00374D18 /* day14.swift */; }; 2615545B276A6C3200374D18 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 26155468276A6D0A00374D18 /* day15.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26155467276A6D0A00374D18 /* day15.swift */; }; @@ -44,6 +46,15 @@ /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ + 26132D2E2774C87D004F0228 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 26155450276A6C1C00374D18 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -200,6 +211,8 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + 26132D2B2774C871004F0228 /* day23.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day23.swift; sourceTree = ""; }; + 26132D302774C87D004F0228 /* Day23 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day23; sourceTree = BUILT_PRODUCTS_DIR; }; 26155452276A6C1C00374D18 /* Day14 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day14; sourceTree = BUILT_PRODUCTS_DIR; }; 26155459276A6C2C00374D18 /* day14.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day14.swift; sourceTree = ""; }; 26155460276A6CF700374D18 /* Day15 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day15; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -238,6 +251,13 @@ /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ + 26132D2D2774C87D004F0228 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2615544F276A6C1C00374D18 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -363,6 +383,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 26132D2B2774C871004F0228 /* day23.swift */, 2680ECFB27732A9400CAB23C /* day22.swift */, 2680ECE22771D82400CAB23C /* day21.swift */, 2680ECDF2770A2FC00CAB23C /* day20.swift */, @@ -405,6 +426,7 @@ 2680ECD82770A2E900CAB23C /* Day20 */, 2680ECE72771D82F00CAB23C /* Day21 */, 2680ECF427732A8300CAB23C /* Day22 */, + 26132D302774C87D004F0228 /* Day23 */, ); name = Products; sourceTree = ""; @@ -412,6 +434,23 @@ /* End PBXGroup section */ /* Begin PBXNativeTarget section */ + 26132D2F2774C87D004F0228 /* Day23 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 26132D342774C87D004F0228 /* Build configuration list for PBXNativeTarget "Day23" */; + buildPhases = ( + 26132D2C2774C87D004F0228 /* Sources */, + 26132D2D2774C87D004F0228 /* Frameworks */, + 26132D2E2774C87D004F0228 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day23; + productName = Day23; + productReference = 26132D302774C87D004F0228 /* Day23 */; + productType = "com.apple.product-type.tool"; + }; 26155451276A6C1C00374D18 /* Day14 */ = { isa = PBXNativeTarget; buildConfigurationList = 26155458276A6C1C00374D18 /* Build configuration list for PBXNativeTarget "Day14" */; @@ -712,6 +751,9 @@ LastUpgradeCheck = 1310; ORGANIZATIONNAME = 5sw; TargetAttributes = { + 26132D2F2774C87D004F0228 = { + CreatedOnToolsVersion = 13.2.1; + }; 26155451276A6C1C00374D18 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -806,11 +848,21 @@ 2680ECD72770A2E900CAB23C /* Day20 */, 2680ECE62771D82F00CAB23C /* Day21 */, 2680ECF327732A8300CAB23C /* Day22 */, + 26132D2F2774C87D004F0228 /* Day23 */, ); }; /* End PBXProject section */ /* Begin PBXSourcesBuildPhase section */ + 26132D2C2774C87D004F0228 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 26132D372774C886004F0228 /* day23.swift in Sources */, + 26132D382774C886004F0228 /* common.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2615544E276A6C1C00374D18 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -967,6 +1019,30 @@ /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ + 26132D352774C87D004F0228 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 26132D362774C87D004F0228 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 26155456276A6C1C00374D18 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1616,6 +1692,15 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ + 26132D342774C87D004F0228 /* Build configuration list for PBXNativeTarget "Day23" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 26132D352774C87D004F0228 /* Debug */, + 26132D362774C87D004F0228 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 26155458276A6C1C00374D18 /* Build configuration list for PBXNativeTarget "Day14" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day23.swift b/day23.swift new file mode 100644 index 0000000..a8be573 --- /dev/null +++ b/day23.swift @@ -0,0 +1,284 @@ + +@main +struct Day23: Puzzle { + func run() { + part1() + part2() + } + + func part1() { + var board = Board(height: 3) + board.fillHomeRow(for: .a, pieces: [.c, .c]) + board.fillHomeRow(for: .b, pieces: [.b, .d]) + board.fillHomeRow(for: .c, pieces: [.a, .a]) + board.fillHomeRow(for: .d, pieces: [.d, .b]) + board.show() + print("Part 1:", find(start: board)) + } + + func part2() { + var board = Board(height: 5) + board.fillHomeRow(for: .a, pieces: [.c, .d, .d, .c]) + board.fillHomeRow(for: .b, pieces: [.b, .c, .b, .d]) + board.fillHomeRow(for: .c, pieces: [.a, .b, .a, .a]) + board.fillHomeRow(for: .d, pieces: [.d, .a, .c, .b]) + board.show() + print("Part 2:", find(start: board)) + } + + func find(start: Board) -> Int { + let target = Board.makeGoal(height: start.height) + var current = start + var totalCost = 0 + + var possible: [Board: Int] = [:] + var visited: Set = [] + + + while current != target { + visited.insert(current) + + for (next, cost) in current.possibleMoves() where !visited.contains(next) { + possible[next] = min(possible[next, default: .max], totalCost + cost) + } + + (current, totalCost) = possible.min { $0.value < $1.value }! + + possible.removeValue(forKey: current) + } + + return totalCost + } + +} + +struct Board: Hashable { + enum Piece: CaseIterable, Hashable, CustomStringConvertible { + case a, b, c, d + + var homeColumn: Int { + switch self { + case .a: + return 2 + case .b: + return 4 + case .c: + return 6 + case .d: + return 8 + } + } + + var cost: Int { + switch self { + case .a: + return 1 + case .b: + return 10 + case .c: + return 100 + case .d: + return 1000 + } + } + + var sign: Character { + switch self { + case .a: + return "A" + case .b: + return "B" + case .c: + return "C" + case .d: + return "D" + } + } + + var description: String { String(sign) } + } + enum Cell: Hashable { + case piece(Piece) + case empty + case outside + } + + var width = 11 + var height = 3 + + var board: [Cell] + + init(height: Int) { + self.height = height + board = Array(repeating: .outside, count: width * height) + for x in 0.. Self { + var board = Self(height: height) + for piece in Piece.allCases { + for y in 1.. [(Piece, Int, Int)] { + var result: [(Piece, Int, Int)] = [] + for x in 0.. [(Board, Int)] { + let pieces = movablePieces() + .filter { !isHome(piece: $0.0, x: $0.1, y: $0.2) } + + var result: [(Board, Int)] = [] + for (piece, x, y) in pieces { + if let newY = freeHomeRowPosition(piece: piece), freeCorridor(from: x, to: piece.homeColumn) { + result.append(move(piece: piece, from: (x, y), to: (piece.homeColumn, newY))) + } + + for newX in openCorridorPositions(x: x) { + result.append(move(piece: piece, from: (x, y), to: (newX, 0))) + } + } + + return result + } + + func freeCorridor(from x0: Int, to x1: Int) -> Bool { + let minX: Int + let maxX: Int + + if x0 < x1 { (minX, maxX) = (x0 + 1, x1) } + else { (minX, maxX) = (x1, x0 - 1) } + + for x in minX...maxX { + if self[x, 0] != .empty { + return false + } + } + + return true + } + + func move(piece: Piece, from: (Int, Int), to: (Int, Int)) -> (Board, Int) { + let (x, y) = from + let (newX, newY) = to + + precondition(self[newX,newY] == .empty) + + let cost = (y + distance((x, 0), (newX, newY))) * piece.cost + var board = self + board[x, y] = .empty + board[newX, newY] = .piece(piece) + + return (board, cost) + } + + func freeHomeRowPosition(piece: Piece) -> Int? { + let x = piece.homeColumn + var depth = height - 1 + + loop: while depth > 0 { + switch self[x, depth] { + case .piece(piece): break + case .piece: return nil + case .empty: break loop + case .outside: preconditionFailure("Invalid board") + } + depth -= 1 + } + + return depth + } + + static let homeColumns = Set(Piece.allCases.map(\.homeColumn)) + + func openCorridorPositions(x: Int) -> [Int] { + var xmin = x + var xmax = x + + while xmin > 0 && self[xmin, 0] == .empty { + xmin -= 1 + } + + while xmax < width && self[xmax, 0] == .empty { + xmax += 1 + } + + let result = (xmin.. Int { + let (x0, y0) = a + let (x1, y1) = b + + return abs(x1 - x0) + abs(y1 - y0) + } + + func isHome(piece: Piece, x: Int, y: Int) -> Bool { + guard x == piece.homeColumn && y > 0 else { return false } + for otherY in (y + 1).. Cell { + get { + board[x + width * y] + } + set { + board[x + width * y] = newValue + } + } +} From 2a68c5ff26249a7f59fc3d1893e9fec9d3bae5a1 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 25 Dec 2021 16:10:40 +0100 Subject: [PATCH 28/32] Day 24 (Slooow) --- AoC21.xcodeproj/project.pbxproj | 99 +++++++ day24.swift | 453 ++++++++++++++++++++++++++++++++ 2 files changed, 552 insertions(+) create mode 100644 day24.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index d15ff6e..81f2578 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -9,6 +9,8 @@ /* Begin PBXBuildFile section */ 26132D372774C886004F0228 /* day23.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26132D2B2774C871004F0228 /* day23.swift */; }; 26132D382774C886004F0228 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 26132D482775D6A5004F0228 /* day24.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26132D472775D6A5004F0228 /* day24.swift */; }; + 26132D492775D6AC004F0228 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 2615545A276A6C2C00374D18 /* day14.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26155459276A6C2C00374D18 /* day14.swift */; }; 2615545B276A6C3200374D18 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 26155468276A6D0A00374D18 /* day15.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26155467276A6D0A00374D18 /* day15.swift */; }; @@ -55,6 +57,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 26132D3E2775D68D004F0228 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 26155450276A6C1C00374D18 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -213,6 +224,8 @@ /* Begin PBXFileReference section */ 26132D2B2774C871004F0228 /* day23.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day23.swift; sourceTree = ""; }; 26132D302774C87D004F0228 /* Day23 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day23; sourceTree = BUILT_PRODUCTS_DIR; }; + 26132D402775D68D004F0228 /* Day24 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day24; sourceTree = BUILT_PRODUCTS_DIR; }; + 26132D472775D6A5004F0228 /* day24.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day24.swift; sourceTree = ""; }; 26155452276A6C1C00374D18 /* Day14 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day14; sourceTree = BUILT_PRODUCTS_DIR; }; 26155459276A6C2C00374D18 /* day14.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day14.swift; sourceTree = ""; }; 26155460276A6CF700374D18 /* Day15 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day15; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -258,6 +271,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26132D3D2775D68D004F0228 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2615544F276A6C1C00374D18 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -383,6 +403,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 26132D472775D6A5004F0228 /* day24.swift */, 26132D2B2774C871004F0228 /* day23.swift */, 2680ECFB27732A9400CAB23C /* day22.swift */, 2680ECE22771D82400CAB23C /* day21.swift */, @@ -427,6 +448,7 @@ 2680ECE72771D82F00CAB23C /* Day21 */, 2680ECF427732A8300CAB23C /* Day22 */, 26132D302774C87D004F0228 /* Day23 */, + 26132D402775D68D004F0228 /* Day24 */, ); name = Products; sourceTree = ""; @@ -451,6 +473,23 @@ productReference = 26132D302774C87D004F0228 /* Day23 */; productType = "com.apple.product-type.tool"; }; + 26132D3F2775D68D004F0228 /* Day24 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 26132D462775D68D004F0228 /* Build configuration list for PBXNativeTarget "Day24" */; + buildPhases = ( + 26132D3C2775D68D004F0228 /* Sources */, + 26132D3D2775D68D004F0228 /* Frameworks */, + 26132D3E2775D68D004F0228 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day24; + productName = Day24; + productReference = 26132D402775D68D004F0228 /* Day24 */; + productType = "com.apple.product-type.tool"; + }; 26155451276A6C1C00374D18 /* Day14 */ = { isa = PBXNativeTarget; buildConfigurationList = 26155458276A6C1C00374D18 /* Build configuration list for PBXNativeTarget "Day14" */; @@ -754,6 +793,10 @@ 26132D2F2774C87D004F0228 = { CreatedOnToolsVersion = 13.2.1; }; + 26132D3F2775D68D004F0228 = { + CreatedOnToolsVersion = 13.2.1; + LastSwiftMigration = 1320; + }; 26155451276A6C1C00374D18 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -849,6 +892,7 @@ 2680ECE62771D82F00CAB23C /* Day21 */, 2680ECF327732A8300CAB23C /* Day22 */, 26132D2F2774C87D004F0228 /* Day23 */, + 26132D3F2775D68D004F0228 /* Day24 */, ); }; /* End PBXProject section */ @@ -863,6 +907,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26132D3C2775D68D004F0228 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 26132D492775D6AC004F0228 /* common.swift in Sources */, + 26132D482775D6A5004F0228 /* day24.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2615544E276A6C1C00374D18 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -1043,6 +1096,43 @@ }; name = Release; }; + 26132D442775D68D004F0228 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 26132D452775D68D004F0228 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/../Frameworks", + ); + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 26155456276A6C1C00374D18 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1701,6 +1791,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 26132D462775D68D004F0228 /* Build configuration list for PBXNativeTarget "Day24" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 26132D442775D68D004F0228 /* Debug */, + 26132D452775D68D004F0228 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 26155458276A6C1C00374D18 /* Build configuration list for PBXNativeTarget "Day14" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day24.swift b/day24.swift new file mode 100644 index 0000000..c94013c --- /dev/null +++ b/day24.swift @@ -0,0 +1,453 @@ +import Foundation + +@main +struct Day24: Puzzle { + + func run() { + let program = readInput() + + var alus: [(Alu, min: Int, max: Int)] = [(Alu(), 0, 0)] + + for instruction in program { + if case .inp(let register) = instruction { + buildNextAlus(&alus, register: register) + } else { + for index in alus.indices { + alus[index].0.run(instruction) + } + } + } + + let serialNumbers = alus + .lazy + .filter { $0.0[Alu.resultRegister] == 0 } + + print("Part 1:", serialNumbers.map(\.max).max()!) + print("Part 2:", serialNumbers.map(\.min).min()!) + } + + func buildNextAlus(_ alus: inout [(Alu, min: Int, max: Int)], register: RegisterId) { + var table: [Alu: Int] = [:] + + var newAlus: [(Alu, Int, Int)] = [] + + for digit: Alu.Register in 1...9 { + for (alu, oldMin, oldMax) in alus { + var alu = alu + alu[register] = digit + let newMin = oldMin * 10 + Int(digit) + let newMax = oldMax * 10 + Int(digit) + + if let index = table[alu] { + newAlus[index].1 = min(newAlus[index].1, newMin) + newAlus[index].2 = max(newAlus[index].2, newMax) + } else { + table[alu] = newAlus.count + newAlus.append((alu, newMin, newMax)) + } + } + } + + print("Alu count", newAlus.count) + alus = newAlus + } + + func readInput() -> [Instruction] { + let scanner = Scanner(string: input) + var program: [Instruction] = [] + while !scanner.isAtEnd { + program.append(scanner.instruction()) + } + return program + } + +} + + +struct Alu: Hashable { + static let resultRegister: RegisterId = 3 + typealias Register = Int + var w: Register = 0 + var x: Register = 0 + var y: Register = 0 + var z: Register = 0 + + func getValue(_ operand: Operand) -> Register { + switch operand { + case .register(let registerId): + return self[registerId] + case .number(let int): + return Register(int) + } + } + + subscript(register: RegisterId) -> Register { + _read { + switch register { + case 0: yield w + case 1: yield x + case 2: yield y + case 3: yield z + default: fatalError() + } + } + + _modify { + switch register { + case 0: yield &w + case 1: yield &x + case 2: yield &y + case 3: yield &z + default: fatalError() + } + } + + } + + mutating func run(_ instruction: Instruction) { + switch instruction { + case .inp: + break + + case .add(let registerId, let operand): + self[registerId] += getValue(operand) + + case .mul(let registerId, let operand): + self[registerId] *= getValue(operand) + + case .div(let registerId, let operand): + self[registerId] /= getValue(operand) + + case .mod(let registerId, let operand): + self[registerId] %= getValue(operand) + + case .eql(let registerId, let operand): + self[registerId] = (self[registerId] == getValue(operand)) ? 1 : 0 + } + } + +} + +typealias RegisterId = Int + +enum Operand { + case register(RegisterId) + case number(Int) +} + +enum Instruction { + case inp(RegisterId) + case add(RegisterId, Operand) + case mul(RegisterId, Operand) + case div(RegisterId, Operand) + case mod(RegisterId, Operand) + case eql(RegisterId, Operand) +} + +extension Scanner { + func register() -> RegisterId? { + + if scanString("w") != nil { + return 0 + } + + if scanString("x") != nil { + return 1 + } + + if scanString("y") != nil { + return 2 + } + + if scanString("z") != nil { + return 3 + } + + return nil + } + + func operand() -> Operand { + if let register = register() { + return .register(register) + } + + if let int = scanInt() { + return .number(int) + } + + fatalError() + } + + func instruction() -> Instruction { + guard let command = scanUpToCharacters(from: .whitespaces), + let target = register() + else { + fatalError() + } + + switch command { + case "inp": return .inp(target) + case "add": return .add(target, operand()) + case "mul": return .mul(target, operand()) + case "div": return .div(target, operand()) + case "mod": return .mod(target, operand()) + case "eql": return .eql(target, operand()) + default: fatalError() + } + } +} + +let input = """ +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 14 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 8 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 15 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 11 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 13 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 2 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -10 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 11 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 14 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 1 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -3 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 5 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -14 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 10 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 12 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 6 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 14 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 1 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 1 +add x 12 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 11 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -6 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 9 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -6 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 14 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -2 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 11 +mul y x +add z y +inp w +mul x 0 +add x z +mod x 26 +div z 26 +add x -9 +eql x w +eql x 0 +mul y 0 +add y 25 +mul y x +add y 1 +mul z y +mul y 0 +add y w +add y 2 +mul y x +add z y +""" From 1923e8ec0bc8d4caafc57f9878e8005a1513b410 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 25 Dec 2021 16:32:24 +0100 Subject: [PATCH 29/32] Day 25 --- AoC21.xcodeproj/project.pbxproj | 85 +++++++++++++ day25.swift | 203 ++++++++++++++++++++++++++++++++ 2 files changed, 288 insertions(+) create mode 100644 day25.swift diff --git a/AoC21.xcodeproj/project.pbxproj b/AoC21.xcodeproj/project.pbxproj index 81f2578..22470f1 100644 --- a/AoC21.xcodeproj/project.pbxproj +++ b/AoC21.xcodeproj/project.pbxproj @@ -11,6 +11,8 @@ 26132D382774C886004F0228 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 26132D482775D6A5004F0228 /* day24.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26132D472775D6A5004F0228 /* day24.swift */; }; 26132D492775D6AC004F0228 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; + 26132D5627776C18004F0228 /* day25.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26132D4A27776BFF004F0228 /* day25.swift */; }; + 26132D5727776C18004F0228 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 2615545A276A6C2C00374D18 /* day14.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26155459276A6C2C00374D18 /* day14.swift */; }; 2615545B276A6C3200374D18 /* common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 269BE5CB2762A08800871C85 /* common.swift */; }; 26155468276A6D0A00374D18 /* day15.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26155467276A6D0A00374D18 /* day15.swift */; }; @@ -66,6 +68,15 @@ ); runOnlyForDeploymentPostprocessing = 1; }; + 26132D4D27776C09004F0228 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = /usr/share/man/man1/; + dstSubfolderSpec = 0; + files = ( + ); + runOnlyForDeploymentPostprocessing = 1; + }; 26155450276A6C1C00374D18 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -226,6 +237,8 @@ 26132D302774C87D004F0228 /* Day23 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day23; sourceTree = BUILT_PRODUCTS_DIR; }; 26132D402775D68D004F0228 /* Day24 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day24; sourceTree = BUILT_PRODUCTS_DIR; }; 26132D472775D6A5004F0228 /* day24.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day24.swift; sourceTree = ""; }; + 26132D4A27776BFF004F0228 /* day25.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = day25.swift; sourceTree = ""; }; + 26132D4F27776C09004F0228 /* Day25 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day25; sourceTree = BUILT_PRODUCTS_DIR; }; 26155452276A6C1C00374D18 /* Day14 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day14; sourceTree = BUILT_PRODUCTS_DIR; }; 26155459276A6C2C00374D18 /* day14.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = day14.swift; sourceTree = ""; }; 26155460276A6CF700374D18 /* Day15 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Day15; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -278,6 +291,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26132D4C27776C09004F0228 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2615544F276A6C1C00374D18 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -403,6 +423,7 @@ 263BA594275E974800839C92 = { isa = PBXGroup; children = ( + 26132D4A27776BFF004F0228 /* day25.swift */, 26132D472775D6A5004F0228 /* day24.swift */, 26132D2B2774C871004F0228 /* day23.swift */, 2680ECFB27732A9400CAB23C /* day22.swift */, @@ -449,6 +470,7 @@ 2680ECF427732A8300CAB23C /* Day22 */, 26132D302774C87D004F0228 /* Day23 */, 26132D402775D68D004F0228 /* Day24 */, + 26132D4F27776C09004F0228 /* Day25 */, ); name = Products; sourceTree = ""; @@ -490,6 +512,23 @@ productReference = 26132D402775D68D004F0228 /* Day24 */; productType = "com.apple.product-type.tool"; }; + 26132D4E27776C09004F0228 /* Day25 */ = { + isa = PBXNativeTarget; + buildConfigurationList = 26132D5327776C09004F0228 /* Build configuration list for PBXNativeTarget "Day25" */; + buildPhases = ( + 26132D4B27776C09004F0228 /* Sources */, + 26132D4C27776C09004F0228 /* Frameworks */, + 26132D4D27776C09004F0228 /* CopyFiles */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Day25; + productName = Day25; + productReference = 26132D4F27776C09004F0228 /* Day25 */; + productType = "com.apple.product-type.tool"; + }; 26155451276A6C1C00374D18 /* Day14 */ = { isa = PBXNativeTarget; buildConfigurationList = 26155458276A6C1C00374D18 /* Build configuration list for PBXNativeTarget "Day14" */; @@ -797,6 +836,9 @@ CreatedOnToolsVersion = 13.2.1; LastSwiftMigration = 1320; }; + 26132D4E27776C09004F0228 = { + CreatedOnToolsVersion = 13.2.1; + }; 26155451276A6C1C00374D18 = { CreatedOnToolsVersion = 13.1; LastSwiftMigration = 1310; @@ -893,6 +935,7 @@ 2680ECF327732A8300CAB23C /* Day22 */, 26132D2F2774C87D004F0228 /* Day23 */, 26132D3F2775D68D004F0228 /* Day24 */, + 26132D4E27776C09004F0228 /* Day25 */, ); }; /* End PBXProject section */ @@ -916,6 +959,15 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 26132D4B27776C09004F0228 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 26132D5627776C18004F0228 /* day25.swift in Sources */, + 26132D5727776C18004F0228 /* common.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; 2615544E276A6C1C00374D18 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -1133,6 +1185,30 @@ }; name = Release; }; + 26132D5427776C09004F0228 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + 26132D5527776C09004F0228 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + DEVELOPMENT_TEAM = 722B335UM5; + ENABLE_HARDENED_RUNTIME = YES; + MACOSX_DEPLOYMENT_TARGET = 12.1; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; 26155456276A6C1C00374D18 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -1800,6 +1876,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 26132D5327776C09004F0228 /* Build configuration list for PBXNativeTarget "Day25" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 26132D5427776C09004F0228 /* Debug */, + 26132D5527776C09004F0228 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; 26155458276A6C1C00374D18 /* Build configuration list for PBXNativeTarget "Day14" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/day25.swift b/day25.swift new file mode 100644 index 0000000..0a8724f --- /dev/null +++ b/day25.swift @@ -0,0 +1,203 @@ +@main struct Day25: Puzzle { + func run() { + var current = input + var stepCount = 0 + while true { + let (next1, count1) = step(.right, input: current) + let (next2, count2) = step(.down, input: next1) + + stepCount += 1 + current = next2 + + if count1 + count2 == 0 { + break + } + } + print("Part 1: ", stepCount) + } +} + +func step(_ space: Space, input: [Space]) -> ([Space], Int) { + var result: [Space] = input + + var count = 0 + for y in 0.. Element { + get { + self[x + width * y] + } + + set { + self[x + width * y] = newValue + } + } +} + +enum Space: Character { + case right = ">" + case down = "v" + case empty = "." + + var dx: Int { self == .right ? 1 : 0 } + var dy: Int { self == .down ? 1 : 0 } +} + +let width = 139 +let height = 137 + +let input = """ +>>vv..>v..>vv.....>vv...>.>...>>......v>v>.v.v..>v..v>.>v.....v..>vvvv.>.>vv>v..v>.v>..v>>>>.>vv.>>vv>.>>v..>.v.v>v>v.>vv.>.......vv.>.>>v. +v>.>v>.v....>>v.vvv>.>>..>....>.v>.v>vv.v..v.v..v>.>.v>.>..>>v.vvvvv..>>..v.v..>>v>vv>.v..vvv.>v..v>>>v.v..>>...>vv..>>v.v>>.>v>.>.v....>>. +v.>vv....v>..v>>..v.>>.....vv..>.vvv>.....v.v>vvv>..v>>..v...>...>...vv.v..>.vvv>>.v>vv..v.>>>....vv.>.>..v..vv.>>v>.>>.>>.v>>>>.>v.v.>v..v +.v.>>..v...v>v.>>>>v....>.....>...v.v..>.v...>.v.....>>.v>v...>.>.>..>v>>>>.v.>..>......>.v>>.>v..>.>vv.>.v>...v.>v.>>.>...>>>..>>>.vv>..v. +.....v.>.v>........>>.>.>vvv.vv>>.>vvv.v.>vvv.....v..>v..>vvv.>.v>.>..v..vv.>...v>>>>vv>.v..v.>..>..vvvv.>.>vvv....v>..>...v...v.v>v.>..v.v +>..>..>..v.>>..v>..vv....>vv.vv>..>v........>..vv>.>.v.>...v.>v...vvv>.....v..vv...vv>.......v>vv.vv.>.vv..v>..v.v.>..>>>.v..vv>...v>vv..>v +.v.>..vv>.>>v>...>.>.>..>.vvv..v>v.v>>.v...v.vv..v..vvv..vv.v.vv...>v..v....>>>>..vvv>v..>.vv.v.>...>vv>......v...>.v...v....v....>v>.>>... +>v.v.v>>.v.>..>>v>>>v.>v.....v>...>>..v.v>.v..>>..>.vvv...>...v>>v.v....v>.vv>.>.v>.>>>.v>>.v..vvv>.vv......v.v...v...>.vvvvv>.>..>>...vv>. +vv>..v.>>v.>.>v.>..v.v>....vv.v..>>.>>v>>..>>>>v>..v>.>.>.>......vv>vv>>>v....>.>v..>.>>vv...>>v.....>>>....>.v.v>v>..v>v.>vv.v>..>.>.>v>.. +v.v.v>v.v..vv.v..>.>v.v.v..>.>.>.>...v.v.>>v>vv.>>....>>..vv...v.v.>..v>.>v.v..v.>.>.>..>...>.vvv...v.v>....vv.>..v>.vv.....>..v>>>.>v.>..v +v...>.>.>v>.v....vv.v>...>v>vv.>..v...>>>..>.>........>..v.>>.v.....>.vvv...>v.>>.vv>v>>>>...>.vv.>...>.>..v..>>>...v.vv.>>v....>.vvv>>v.>. +.>...vv.v........v.v......v>vvv.vv..>...vvv..vv.>.v.v...>vv.>>...>.v..>v>vv>.>v...v.v..vvv.v>>...>.>.vv.>v>v>.v.....>.v.v.>.>v>....>v..>v.> +..v.vvvv.v....>.vvvv.....v.v...>>v>.>.>.vv>>vv.v.v.v.>.vv>vv.>.>v.vvv.>v..v>v.>...>.>>....>.v>...vv...>v>vv...vv>>vvvv..v.v.v>>.>v>..>v>>v. +..>...v.v.....v.v.v>..>..v.>>>..v>v....>v>v..v>.v....>vv>..>.>.....v..>>v..>>v>...>v>v..>.v.>>....>vv.>...vv.........>v>.v..>>v.v.v>>v.v>>> +vv>.v.v>vv.........v..>..>>>>v...>..v...v..v>..vvvv....v.v...v>..>>v.>.v.>v.v.v>..>....>...>v>>vv...>...>>>..vvvv..>.>>v.v>...v..>.v..v.v.. +.>.>vvv..v.>.v>.........v...>..v.>....v.v>.v.v.v..>.>.v..v...v.......v...vv.v.v..v..v>..v..>>>.v.v>...>..>.>>>v>>..>>..>..v...>v....>.....> +v.v.vv.vv>..>>v.>>.>.>>v..>v.vv...>vv....>.>....v...v...>v...>..v>.v>v...>..vv>>..vvv>....>vv>vv>.....>v>>v.>>v..>.v...v..v>>..>.v..v..v... +vv...>v.>...vv>.>.v>..v.....v.>...>.>v.>vvv>>>.>>.........>.>..>...v>..vv..v.v.v.>vv.v>>...>>.v..v>v......>v>v.v...vv....>>>..>>v>.vv...... +v>.v..v..>.v>>....>.>.>>v>.v.>v.v>.v.>v.v>..v>.>.v>v......>>.>......v..v.v.>.v>>v...>v>>..v....>.v..vv>...>.v..>....v.>.>......v.>.>v>v>..> +>>v>.>>..vv>.>>v.v.>v.v...vv.>>.>>..v...v>v....vv...>>>.v>..vvvv.vv>v>>...v....vv.>.>.v....v>...>vv...v.>.v.vvv.v>.vvv>>vvv>.......>.>.vv.v +...vv>v.v>..>.>.v...vvvv..vvv..>.v>..>.>v.>>>v....>..v>.vvv.>..v.>.>.v....>.......vvvv>.v>.>v.>>>.>vv..>.vv.>>...v..>>v.>>.>v..>vv.>v.>.>>. +>.>>v>>>.....>vvv>.vv.>v..>.>..v>>...>>v..>....>>>v..>.>v..>>v>v.>.>v.vv.v>vv..v>v>>>v.>...>...>>>.v>.v>>>v..vv>v>>>>>v>>.........>v.v>.>>> +.......vv....>v..>..v>v.>.v.v.v>>...v.v.v...v>v.>...v...v.v...>>>.v...>v.v>>.v.>....>>>..>.>v...v....>..>.v..>.v>.>..>>>.v>>.>v.>v>.>.v..>. +..v.v...vvv..>>>vv....>>..>.v>..>>...v.>>v..v...v..v.>>.>...v>..>.........v.>..>.vv.>.v..>.>v.>v>.v.v.vv.>...>v..>.v.v.v...v..>.vv...vv>.v> +v....>v.v>...>...>>.......v....vv>>....>....v...v>>.>v.>....>.>.vv>v.>>..>..>...v>v.v.>>>..>.>>..v>.>...>.v..........>..>>>.>v.v.>.v>.>>... +>>.>.....v...>.>>>vvv>.>>>.v>.v>v..>v..vv>..v>vv.>.v.>v.....>>>v.v.>v.>..>>v..>>..v.v..v.v.>>.v.>..v..>.....v...v>..>>>.v..v....v.....v.>.. +....vv..vv.>..vv..>.>..v..v>.v.>v.>>.v...>.v..vv.>v.>>v...>...>..>v>.>...>vv.>.vv..v..>v>v>.v.>..>.>v>....v.>.v>..v...v>..v>>.>..vv.v>v.>.> +v.....>>.>v>v..v..>>v>v>>vvv.v..>....>.>>..vv.....>>..>.....vv>.......v>.>.v..>..v>.v.>.>.>...>.v..v.>>..v.v.vvvv>.v..vv>.>v..>v.v>>.vv...> +v>.v.>>...v.>..v.v>v.vv>..>...>v>>v.v>.v.>..v.>....>.>>v>.>>>vv.>..vvvv..>..>>>...>.....v>..v>>..v.v>..>>v....>.v.v..vv...>....>vv.>.vvvv>. +vv>v>>..>v>>.vvv..>v>vv..>>.vv.v>vv>.v..vv.v...>>....>.v.v>>.>>.>v...v.>...v>>>.v.vv>>v...v.>>..v..vv..v.....>>>.>>v.v>..>.vv>>..>.>.....>. +....vvv>..vv....v...v.v>v>>vvv>.v..vv.>>....>.>.v...>..>....v>...>..>v>vv..>..>>..v..>vv....vvv.>vvvv..v....>....>vv.vvvv.v..>vv.v.>v>.vvv> +>>>>v>v.....vv..>v.v>>.v.v.>..vv.v>>...>v.v..v.v...v>>.>v.v...>..>.>>..>.>v.>>vv.vv.v....v>.v..v....v.v..v.>>....v.>>>>.......>v....>vv.>v. +....v.v.>..v..>vv..v.>>>>.....vvv...v>>.v>..v.>.v>v..v.>v...v.>>>>.>..>..vv.v>.v>..>.>v.....v..>>..>.>v.v..v>v.>>.....>v.v>v.v.vvvv.>.v>>>> +>.>>.vv....>>.>..>........>.>.v...v.>.v......>.>..v.>..v..vv>.vv...vv...v.>v.>....>>......>>>.>vv>.v.>..>vvv...vv.....v>vvv.v...>.vvv.v.>>. +....v>...v...v.v...v...>...>.>v>v.>>...>...v>vv..vv.>.vv>...>v>..>>..>.>v..v>..>.>....v>..v...vv>.vv.>>.v>v.vvvv>.....v.v..>.v..vv>v>>v.>>v +v>..>>v>v.......v.......>v>.>.>.>.v.>>.v.>v>.vv.v.v.v>.v>.v...v.>>..v.....vv.vv...v.>.....>...>..>>>v..v.v>.v>v>v.>..v>v.>.>..v>...>..>v..v +.>..>>vv.>vv.v>v..v>>>....>>v.v..vv>>...>v....v>vvv>.....>.>.>..>v.>...vvv....v...vv..v.>...>...v.vvv..>>..>v.vv>.vvv>.>.>.v.>..>v.>vv..vv> +....>vv.>>>.v..>>v.>vvv...v..>v..vv...vvv.v.vv.v.....>.>vv..v>..>....vvv>..>>.>.....>>>..>v....>..v.>v.v>.>>.v...vv>.v>.v>.v>v....v...vv..v +>.v>....>>.>>...vv..v.>..>.v..>.v>..>.v.v.>v....v>>.v...>>v..>.>>vv..>v>>>.>..v>.>..>.vv.v....>.>.v...>.v.vv>v>v.vv..>.vv.v..vv...>.v..vv>> +..v.vvvv.>>>.......>>>...........v.>.>>>v>v.>....>vv.>.v>..v..>>.>.v.vv>...v...vvvv.>v>vv...>>>.v.v....v.>.>..v.>>>>..>..v..>>..>>.>>>.>>.. +v>v>v....vv..v>v.>v..v.v..v..>.v.>>..>>v>....>...v.....>..>.vv.v.vv....>....vvv>.v.v.....>>v>...v.v>.>>>.vvv>>v.>>>.>v...v>.v..>......>>vvv +.>.v.>..>>..>..v..v>v>...vvv>v>>v>.>.>..v>>...>v....v.v...v>vv>>v.v.v.>v>>..>...vvv.....v>>....v...v.v>.v.>...v>.vv...>v......v>>.v>vvv.vvv +.v>.vv.>>..v.>.>...v>.v.>>.vv..>>>....>.>.>.vv..vv.....>v...v.>.>.....vv.v...>.v>v.vv>.>>v..v>>vvv>vv>>..v.v>>..>.>>>>v>...>v.v>.>v...vv... +>>vv>...v.v>..v..>vv..v>...>......>..v.>.v.v>v........v.>vv>v..v>.v.>.v.vv..v....v.>...v>.v>v.v>...>..v....>....>>>v>>vv...vv>vv.>..>.>.... +.>.>>.>>.>.v>>...>.....>v..>.>vv.>.>v...vv..vvv....v.vv>v...v>.vv>.......v>.v.>....v>...vvv.>.v...v..>v.>>..v...v.>..v..>.>v...v>v>v>..>v>. +..v..v..v.vv>.v..v.....v.>...v>..>..v.>>>v>>v..>.>...>.v.>...>vv>.v..v...>v>v...>v>v.v>v>>v.>v..>..>vv..v>v.v.v.vvvv.>v>>>.>v>v.v...>v..>>> +..vvv>>..>v..v>>...v.>..v>>>>.....v.>>.........>....>.>..vv...v>..v.>......>.v>.v>..>..>>.vv.>>.>..v.>v...v>.>>>.v>.>.>.>v.>.....>>>vvvv>.v +v..v...vv...>>>>....v..>..v>..>.............>v..v.........vv.>.v>vv...v.v.>>v...>...>..vv....v...>..>..>...>v.....v.>v.>..>>.vv.......>>... +...>...>vv>>>.v>......v.>>>>>..>.>>..v>v>.>..v>>>v.>>......v.vv..vv.>>vv>v.v.>>>v>>.vvvv.>v...v..vvv>vv.....v...v>..vv...>...>>>..v.......v +v>.>>.>v.v>>..v>v....v>>>...v.v>..v..v>>.v..v.v.vvv.v>..>..v.vv...v.v....>..v.>vv>v>..>..>vvvv.>.v.......>v>>...>.>..v>v>.>>>v..>v>..>..... +..>.>.>>...>.v.>v..v..>...>..>v.>v.>..vv..v..>>..v.>>...>v...vv>>>>.>.v..v...>.v>.>v>...v.v.v>.vv......v..>v.....>.>.>.>.>>vv>>...>>.v.v.>. +v.>>>...vv>.>v>.>>v......>>>v.v..>.>.>v>>v>.v.vv.v>>.....>.>.vv.>>v>v......v>>v.v.>>..vv..v..>.v..>.>.>>>.vvv.>v..>..v...>.v.vv>>..v...v..> +vv.>.......v.v.....>.>.v.>>>vv..>v.v..>..v.v>vv.v>>..vv..vvv...>..>>.>.v..>.v>.>>v>.vv>.>v>vv.....v....>v>v>.v>.v..vv..v>.vv..v>...vvv..>.> +..v......v..v.>........v...>..v>v>.v....>..v>.>>v>.>>....>>>>.>.>v>..>.v>vv>>.v....>..>.>v>..v....v>>.>.vvvv.vv....>v.v>..vv.......>>.>.>.> +.>....v..>..vv....>.>....>vv>......v.>.vv...v>>....>>.v>.v>.>..>.>>..v..>....v.>>vvv..v.>>>>>.>>v..vv>v.v...vv.v.v>...>...v>.>vv.vv...v.>v. +>v>.>.>v>v>v......>.>>v....v.>.v.....v..>.v>v..>.v..>>.v>>v>.....>>>.>v....>vv.v.v>v..vv..v>vvvvv...>>>...>..>.v...>.>>..v>v..>vv.>..>.v... +v..>v.>..v..v>....v.>.>v..>....v>v.>.....>>vv.>>v...>v>.>v..vv.>>...v.>>..>v.>.>>.>>>>....v.>.vv>>v.....>.v....>.>..v>v..>.....>...>>.v.... +>.v>.vv..>..v.>.....vv.vv.v>>...v>>v..>.v..v..>>v.>.vv...>.v.>..>>v..v..v....v>.>..>.v>v.>......vv..>...>.v>v.....>.>>>.v.v....>>>.>>.vv>>> +>..v>>...>.v..v.>..>v...v...v>..>.>v.v>.>.vvv.v..>.>..>>.>.>v.>vv....v>>.v>>.v>.v.v>v.v>..>.vv...vv.>.v......>>>v.>..>v..v....>v..>>...>.v> +...v>.>vv>..v>v>.vv>.>.>..v......>>v.>.>>.vv>v>.v.v.v.>>..>..>>.v.>.v.>.....v..>..v.>>..v>v>>.>.>.vv.v>vvvvvv.>v>.>>v..v.>.v.>.>>..v.>..vvv +v>..v.>v.......>.v.v>v>v.v.....v>.v>...>v>.>.>.v.vv..>v.>>..v>...v>.>.v>>.vv.v...v....vv.>..>.v>>...v..>>>.v>v....>v.>..>.v...v...>.>..v>.. +v>vvvv>vvv.>>...>>.>.v..v....v.>v......>.v.>>.vv....>v>>>>..v......>vv>...>>>.v.v.v>>v..vvv>....>..>v.>v...v.v..>..vv..>>.v..v..>.>.v.>v.>. +......v.>....vv>>>.vv..>.>v>.v....>v..>..>.v.....vv>v....v>.v.vvvv.>.v...vv>.v.vv..>v.v.>>vvv...>vv.v>.v>.v..>.>.>.>>>..v.v>v>vv.vv>....... +>...>v..v..>vv>.v....>.v>.v.v.>..>v>.>..v.>.>v.>.>.v..v.v.>v>>v.>v.>....vv.v..v.>..>>...>...v>>..>....v.v>>v.v>v>.>.v..v.>..v.>>>...>...... +....v>>v...v..>.v>.>.v..>>.>.>v..v>.>....v.v>..v>>vv>..vvvv>>...v....vv..>..v.vvv.>.vvvv>....v.>.>v...v...>....v..>.v>vv.v.vvv..v.v>.vvv>.. +...v.vv.>v>>v>>.v>..v>.vvv>v>>...v>v>v>.v.>.v..v>.vv>v.v....>.>>vvv..vvv>.v.vv..>..>...>v.>.v.v....v....v>>...v..>.vv.vv.vv.>.>.vv..v.>.>vv +>....vvv>>..v.>.v.v>v.>.v>>>..>.>.vvv.>.>>vv>v.v.>..>...v.v>..v..v.v>v>........v.vv...v>v.>>..>.>...>v..vv>>.>>.>....>vvv>..>.>>....>..>v.. +v>>.>>v.>>vvv...v.>.....>.vv...v>>v..v..v.v>>.>..>>v.>>.>.>vvvv..vvvv.>v....>>....vv..v.v>v.>.>..v.>v...vv.>>.>>v.>...vv...v...v.v.>.vv.>.. +v..>>>v.>>...v>.>v>..v.v>.>..>>..vvv.v....v>v..>...v>>>.>.vv>v>v.v....vv.vv>>.........>........v.vv>.>v.vv.v.v..>vvvvvv...vv..>>>......v>.. +v>.....vvv>.vv>vv.>vv>v...>>v..>..>vvv..vvv....vvvvvvv.>v.>>..>v>>>>...>..v>.v..........>v>...>v>>..v..>>v....vvv..>......vv..v.>>v........ +v.....v>....>>v>.vv.>>v.vvv.v>>v>.>..v..>.v>.vv....>>>v>.>vvv>.v.>..>.>>.v.vv..>>.vv.>>.>.>>.>.vv>>..v.v.>.>........>vv>>v.>..>vv.>...vv.>v +>v..>v.....v.....v..v>.v>v.>..>v.v>>>..v.v.>.>.v....>...v.>>v>v>..>..v>>.v......v>.v.v>.v..>v>..>.....vv.>>..>v.vv.vv.....>>.v..>.....v..>. +.>.>>vv>...>>vv.>..>>.v.>v.>vv>.v>>...>..>....v>.v.v>vvv>.v.>>>..v.>.v...v..>......v...>.>>>v..>v.v.>..>.>>..vv.>v....vvv>.>.>>>>..>.vv...v +>......>...v>..>.v.v>.vvv.v>v>..>...>vv..vv>v>>>...vv.>.vv.v>vv..v.v>v.v>>>.>v.>>.>>.v..>.>.>.>..>.vv..vv.v.v.....>..>v..v.vvvv>v......>.>v +..v.v>v.>v.>v>vv>>.>..>..>v>>.vv...>....>v.v.v.vv.....>.>vv....>..v>.>.>.>.>..>>v>v...>....v.vv.>>.v>.>..>>.>v..vvv>....v.>.....v>.>...v..v +v...>v....v....>..v....>vv.>.v.v.vv.......vv.v...>vv>>..>...>.....v.v>.v.v>>.....>>.v.....v.>.vv.v>.>.v..vv...v....>>.v.vvv.v....>.....>>.> +...v.v>.>..>.>.>...vv....>....>v....>.>>vv..>v>v..>...>..v..v.>>>v>>vv...v..>>.>>..v.v.>>.>.v.>...>....v.>.>v.>...>>v....>>.>v.v......>>..v +..vv.v.....v>...>..v...vv.........>.....v..vv>....>vvvv..>v>v.>>>>v.v>..v.v.>..>...>v...>.>...>.>v>>...vv.v..v.>>.....>....v>v>v..>..vv.... +..>v>>.>..v.....>v.v.v.v>.>.>v.v.>>>>vv>......>.v.v.>>.>.v>>..vv..v.>...>..v>v.>v>>vvv..v..vv.>.>...>>....v>v..v.......>v.vvvvv...>.>>..>.. +v.v..>>v..>.>v..>..v.>...>v.....>vvv..>.>v.v....>.v.v>..>.v>.>.>vvv..>..v...>...v>v.....>v..v.v.>vv..>>...>.v>..v>>.v..v.v>..v>..vvv>..>.v. +.v.vvvvv.>.>>>>vvv>>v>>>....>vv..>..v>..v>>v...>..>v.vvv>....>.v.....>....>.v>.>vv.v>vv..>..>...vv>.v.>>..v.v.v>.>v>.>v>..v..>.>v...v>.v..> +.v.v>..>....>.v>vv..>.>>.....>.>..>v>..vvv.v.v>..vv.>.>>.>..v.>>.>............v.vvv>.v>>>..v..>...>.>v>>...v..v.v...>>v>.>..>>v.v>v..>.>v>. +v>>..v.>v.v.>.>>v......>v...>>...>>>..v...v...>.v...v>>v>>>>vv.>>..>...v....>>...>vv.>v...v.....vv.v.v..v.>...v.v.>....>.>..>v...>.>>.....v +>.v.>.v...v.....>v.vv....>v.>.v.v.v....>.........>v..>....v.vv.v>.v.>...vvv.v.>>>vv.....>.....vv.>>>.>>.v>..>..>.>v.....vvv>v>....v.>..>.>v +v>v.v.>...>.vv..>>.>>.>..v.>.>v.>.v..v>v.>.v.>..vv..>>v..>..v>.v.......v..v..v.>vv.>>v...>....v>..vv.>>..vv.v....v>...v.v....v>.>..v..vv.v. +..>.v.v.>v...>>v>.>.>.vv..>..>v.....v.>>vv..v.>.>..>>..>.vv..v.v..vv...>>.vv>v...>vv>>>v....>vv.v.....>>.vvv.v>>>v....vv.v..v..>.>.v..>..>> +v>..v>.>..>..>....>>v...v..v>..>..>v..>.v>...v...>.>.>.>...>...v.v>..v.>v>.>vv..>..vvv..v>vvv.>..v.v.v.>v..>>>>....v>>>>>v.v..v>>...v..v..v +.>....v>.>...v.v>.....>v....>......>>..v..v..>>vv.vv.v.v.v......>..>.vvv.>>..>v.v.>.>.....v>.>.>>>.>.vv.....v>v.>>v....vv.>.v.v.v....v...>. +v>v.....>.vvv.>....>.>>vv.>>vv>>>.v>>vv>..>..vv...>>>vv>>>..v..vv.>...vvvv>....>......v.v..v.v>..vvv..>.v>.v..>.v...v.vvvv>...v.v>.>>....>. +>>.>v..v.>.v.>...>>.>..>vv.....>v...v...>..>>..>v>..vv>..v.vv>.v.....v....>v..>v.v.>....>>vv>...>v>>..vvv>.......vv..>.v>v.vv>vvvv...>v..>> +..vv..>..vv.v>..v>>>>v....>>......>..v.v...v.v>vvv.>>>>vv..>v..>>..>.>.>v.v.>.>vvv>.v.v..vv...v.....v..vv.v....v.>.v.v.v>>v....v.....v.v>vv +..v.v.>>....v...v>.>.....v.v>....v...v..v..v>.>vv.>v.v>v.v..>>>.v>.>>v>..v>vv.>vv......>v.v.........v..v>v>.vvv.>v>v..>v.v..v.vv.vv>.v>>.>. +v...v.>v>.>..>v>..>>v.>.v>.v>.>.v....vv>..>..vv.>..>v..>v.vv.>.>..>>v..v>.>.....>....>>..vv.>>..>.>....>vv.v..v>.vv.vvv...>.>>......>>..v.. +.v..>>...>>..>...>v.>....>>..>.v..>>..>.>..>v>.v>.v.v..>v.>>v>...v.>.v..>..>....>...>.>.>.v....>vv>>>.>v>.vv...>.v.v>v..>>vv..>.>v.v.v.>.v> +v..v...v..vv.>.>.vv...vv.v..>>vv.>..v.v>.>v..>>.>..>>...v..v.>>>.v>>...v.>v.v>>v..>..v>>>v.v..>..v.>vvv..v>>...v..>...>...>>..v..vvv>.>.... +...vv.v.>.....>>>vv.....v.>..v..>v..>>v>..>.>.v>.v..>>v.vv>>v...vv..>vv.>.v..v>v....>>....v.>>v...>>.v.v>>v..v.v..v>>>>vv>>..>...>v.....v.> +>>.v..v.>.vv..v.>.v>.>.v>.v>>.>>v.>>..>..>.vvvv>>.v.>>..vv...>vv>.v.>...v>...>.v..>v>.v>v..>>>>.....v.>..v.vv.v.>....v.>.vvv.v>v>..>.>>v>.v +....>.v>>..v.vv...>.v...v>....>v>..v>.>>...>>..vvv.>.vv>>>>v.vv>..>..>vv.v>>..>.>>>>..vv>>.>.....>.v.v>...>.>>>..vv..v...>.>..>>v..v.....>> +>>>.v.v.>>.v..>v.v......>.v>..v>.>>..v>v...v...v>v>..v.>.v...>>v>.>..v...v>..>.>vv.>v.vvv>>.v>..........v.........>.v..>.>...v.....>..>..v. +>.>>>>vv.>>..>.v>...vv..vv>..v>.>>.vv>.>..v...v>...>>.....>v.vv...>>.v>>>.v...>.>v>v.vv.....>>.v>.v...v>>.....>..>>v..v>.v....>v...v.....>v +.v..>.v.v..>.v..v..v.>..v.v>v.v>>vv>vv...>..v.v.....>>..v.v>.v>>.>vv....>>>.>>vv>......v>>..v..>.>v>.>>>....v>vv..>.....>>vv.v..>..v...vv.v +..v.>.>.>v>.v>.>...>..vv..vvv....v.vv..v....>.>.v.v...>....>v>v....v.v..v>vv...v>>v.v....v.v.>..vv..vv.v>>>..v....>>.v>.>.>v...v.v>>>>.v... +v.v..>>.>vv.v>......>v>.>..v>.v.>...>......vv..vvv.v..>...v..v>.>.v>v...v..>....>.>.v.v..vv.>v.>v>.vv>v...v>.vv..v..v...>>.v.v....>>..>>vv. +.vv...vvvv.>.v...v.>.v.>v......>vvv>>v..v>vv.>>.v>v.vv..>.>..>......v>...v.>>>>>.vvv.>>v...>>>v..v.v>>v>...>.....>vv..v.>v>......>>>v...vv> +....>>...v>...>v>>>>v>>>.....>....>.>.>..>>..vv...vv...v.>vv.>>>...>>.>..v>>vv.v.>..vv..>.>v..v.>.v>..>.vv.>v>v>.>...v..v.v...v>.>v...v>>>. +.v.v.v.v>.......v.vv......v..v.vv>>>...>..v.>.>..>......>v..>v>v>vv.>.v.v.>.v.>..v>.v.>>>v..>>..>.v..>...vv>.>>v>.....v..v.>.>.....vvv>..>. +v.....vv>vv>.>vvv..>>...>>v.>v.vv...>....vv.v>>>vv>.>>v.v>>vv.vvv>.>>..>vv>.v...v...v...>...>.>>vv>>v...vvvvvv>..v..>.v..>>>..>.vvv...>vv>v +>..>>>.vvv..vv>>...v.v>...>...vv...>>v.v.>v....v>.....>v.v.>...>v>vv>>.>>.>.>..>v>..v.>>v.v.v.>...>>vv.>>.>.>..v>.v.v....vv..v>.>...v>v.>.> +>v>...v..vv...>.>.v.>>...v>>>.>v.v.v.v.>...vvv.>.v....v....>....v>v>.>vv.>v...v>...v.......>.v.......>v..v...v>.>..v>..>..v>.v..>vv.>v>vv.. +vv.>>..v>>vv.v..v.vv>>>>>v>>......v..v...v>vv>>v.v..>.v>vv>v..v......>>>>v>v..>v>.v..v.>..v....vv..v...v>>>..>....>...v...>>>.vv>..>v....>> +..>.v.v.>>.vv...>v>>>v>>.v..>>.>>>.....>>.vv>v>>>>...v...v...v.>>..>>..>>v.>>>>v...>.>v..>.>.>..>.v.>..>v>v.v.vvv>....vvv.v..v.v...v.v>v... +>v.>>>.v.>v.v.>.>v>.v>.>>>vvv.>.v>..>vvv..v..>.>.vv>v.>..vv..v...v.>>v.v>..>.>vv.v.vvv.v...vv>v.vv..>v.v>..vv...>>.......v....>>...>vv..vv> +vv>.>v>.v.vv..>.>.>..>>vv>>v>......v>.v.v>.v.v.....>.>>..>v>v.>>.>..>v.v>v.v>...vv>v>...>>vv..>.>.>>.>>.v.v...vv....>v.v..v>v.>>..v>>..v.>. +.>v>.v.>>...v....>...v..>v>v.>...v.>.....v.>.>.>v......v>>.>v.>.v..>.>>...v..vv..>v..v.>..vv>>..>.>.v..vv.>.>.....>....>.vv.v...v.>v.>v..v> +>v>.>vv..>.>>v>..>v>v.>...>.v.vv>>.>.vvv..>...>....v>.>vv>>..>>>.v>>v>.v..>..v..v>...v.>v...>>..v>>..>.v.>>..v..vv..vv.....v.v..v..vvv....v +vv....>vvv.>.>>>....>.>v>.>vv..v>.>..>.>v>.vv.v.vv.>v>.>>.>>vv>>.....v....>>>vv>>....>vv.v..>.v.vv>..>v....>.>>>>..>.>..vvvv.v.......v...v. +>>..>.>.>vvvv>.>v>>v.v.>>>.v>..>.>.....v>.....vv.>...>>v.>v.>>...>>.>.vvvv>v.....v>v.v>>..v..>..>>.v.>.>...v>...vv>.>v>..vvv......v.>v....> +.vvv...vv.>.>.v..vv>>.>...v>.vv....vv.v..v>........>v.>>>...>.>.....v..>.v.>v.v..>..v..v>.>..>>>vv>..>.v...v>...vvv.v.v.>.v.vv..>>>v..v.>v. +>v.v>>>....vv>v..v..vv>.v.>.....vv.>..v..v>vvv.>>v.v.>.>.>v.>..v>v.v>.v.v...v..v.>.v.>v.>v>.v>v.........v..>v.>v>v.v.v.>..>.>....vvvvvv>.>. +...vv.>v.vv.>v>..v..v.>v>v..>.....>.>>.v.>>.>..>>.v.vv..>v.v>v..>v...v.vv.>.>.v>vv...vv.vv.v.vvv...>v>vvv......v...>>>>.>.v>..vv..>.v>>>..> +>.v...v....v.vv.....>>v..>....vv..vv....>..>>..>.>vvvvv>v..>.>>>>>..>v..v.>>>.v>....v..v>...>...vvvvv...>>.......>>vvvv.>v..v...>..>>>..... +.v.>v..>vv...v>......>vv..v.....v....>.vv.>vvvv.>v>v.>.>v..v......v.>>.>....v.>>v>>v.>v>>v....>.v>.v>...v>.v>v>.>..v>...>>>v>..>>>>>...v.v> +.>>.v>.>.>>>..v.v.v.v>>v..v>.>>v..v>v>>.>>......>v>..>.>vvv.>...>>...>.>>.>>..>>v.....v..>......>...v>vv..v>..v>.vvv.vvv....>.>..vv>>..v>v. +v.>.....v>>vv.v.v>>vvvv.>>vvv...>v>.v.v...>..>>v.>v>.>v>>>....>v..v..v>..>..vv>>.v.v.vv.v..v....v>.v.v.vv...v...>.>.>v....v.>>v..vv.>>.>... +>v>vv.>v...>.v..v.vvv>.>.>>vv>..>>>...>v.>v..vv>vv..v.>>v.>v>v.>..>...v..v>>>vv..>>v...>.v.>.v.>.v>.v.>.>v...v>>>v..vv..v>.v>.vvvv>v.>.v.>. +.>..v..>.vv.>vv.>v.>......>..v....v>.v>vv>...>vv....v...>v>.v..vv>>.......v>v.>v>>....v.>....vv.vv..>.>>v.v...>.>v.>.v.vv.>.>v...v..>>.v... +>.....vv...>v.>v>.>>.vv>>>..vvv.>vvv>..>v..v..v>v>>vvv>v>>..>>v..>..v.v.>v..>...v>.>v....v.v>>..v....v.vvvv...>....v.>..v.>v....v.>.>vv...> +>v..>>.v>>.vv>..>v.>..>>v>v..>..v...>>v.v.>v............>vv.v.vv..>>vv...>..>>.>...v.v>>..>>v..v>.vv..v.v>v..>.vv>>.>>>v>.v>>.>v.vv>..v.v.> +.v..vv....v..v.v.v.........v>>.>v...>v..v>.v.>..vv>...v...v.vv.v>v..>..vv.vv..>vv.v.>.v>vv.>..........>>vv...>v>.....vv..v.>.>v.v>...>....v +.>>.>...v.>v....>.....v>.>v.>.....v.>>..v...>.>v.>>.>>>.vv..>>.>v..v>.>>...>..>.vv.v....>.v..>vv.v...>>.v>.vvv.>.>v.vv..v.>.>>>>.>>...v>v.. +...vv>>>v..>..v>vv.>.>>v..v>vvv>..vv.>v>.v..vvvvv.....vv>>.v>v..>..>>.v.v...v...v...>v.v..vv.v.>v>.>v..>v.>.vvv..>.>.v..>.vv..>v>...>>v.>.. +v.vv.>>>..v>.vv>...v>....>v>..>v.>.....>vv..>>......>>.>.vv.v..>>..>.>.v.....vv..v.v.....>.v>v.>.>>.>.v>>>.v.>..v..vv..>.v...>..v.>.>>..v.v +..vv...v.>v>.........v>>v.....v..vv.>vv.>.>.>...>>.......>...vv.>vv>.....v.>>.>......>....>>.vv..>....vv.v.vv..v..v....>...v.......>v>..>.> +.>.v..v.....>.>>.v.>>...v.>.v>.v>..v....>>v>>v>.>..>.v.vv.v.>..v>>...vv.v.>>>...v.>>.v>vv.v.vv...>v.>>....v.v....vv>.v.v...>...>...>v.>v... +.v.vv>>v.v.vv>..>.>.>.v.v.>.....vv>>v>vvv.....>>>v.>.v..>>..>.vv>v..v.>vv.>v.>..v.>vv>v>...v>v>>.v.>.....>..v>.>..v>v..........vv......>.vv +.vv..>>>>..v.>.v.>..>.v>.....v.>>v.>.......vv>>v.v>..>>.>v>>>.....>..v......>..v>.vv.>.v.>v>.>.v....>v.>v.v.v..v.vv...v.vv>.v>.....>..v>..v +.v..v>v>>....v....vv>...vv..v>...v..>...>...>..>v..>>>.vv.v....v...>v.>.......v.v.>v....v..v>...........v..>v>>.>.>>v....v.>.v...v.>.v>..v. +""".compactMap { Space(rawValue: $0) } + From 6427bb0aac9a9c0dee481778187d97d54dc3eaea Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 25 Dec 2021 17:47:53 +0100 Subject: [PATCH 30/32] Day 22 part 2 --- day22.swift | 98 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 31 deletions(-) diff --git a/day22.swift b/day22.swift index 9a67df3..a27b374 100644 --- a/day22.swift +++ b/day22.swift @@ -3,44 +3,57 @@ import Foundation @main struct Day22: Puzzle { func run() { - var cube: [Point: Bool] = [:] - let activeRange = -50...50 - let scanner = Scanner(string: input) + var cubes: [Cube] = [] + while !scanner.isAtEnd { - let (command, xrange, yrange, zrange) = scanner.line() - if let xrange = xrange.intersection(with: activeRange) { - for x in xrange.clamped(to: activeRange) { - if let yrange = yrange.intersection(with: activeRange) { - for y in yrange.clamped(to: activeRange) { - if let zrange = zrange.intersection(with: activeRange) { - for z in zrange.clamped(to: activeRange) { - cube[Point(x: x, y: y, z: z)] = command - } - } - } + cubes.append(scanner.cube()) + } + + var xStepsSet: Set = [] + var yStepsSet: Set = [] + var zStepsSet: Set = [] + + for cube in cubes { + xStepsSet.insert(cube.start.x) + xStepsSet.insert(cube.end.x + 1) + yStepsSet.insert(cube.start.y) + yStepsSet.insert(cube.end.y + 1) + zStepsSet.insert(cube.start.z) + zStepsSet.insert(cube.end.z + 1) + } + + var xSteps = xStepsSet.sorted() + var ySteps = yStepsSet.sorted() + var zSteps = zStepsSet.sorted() + + let firstX = xSteps.removeFirst() + let firstY = ySteps.removeFirst() + let firstZ = zSteps.removeFirst() + + var part2 = 0 + + var x0 = firstX + for x in xSteps { + let possibleX = cubes.filter { $0.xRange.overlaps(x0.. Self? { - guard upperBound >= other.lowerBound && lowerBound <= other.upperBound else { return nil } - return clamped(to: other) + print("Part 2:", part2) } } @@ -50,6 +63,20 @@ struct Point: Hashable { var z: Int } +struct Cube { + var start: Point + var end: Point + var state: Bool + + var xRange: ClosedRange { start.x...end.x } + var yRange: ClosedRange { start.y...end.y } + var zRange: ClosedRange { start.z...end.z } + + func contains(_ point: Point) -> Bool { + xRange.contains(point.x) && yRange.contains(point.y) && zRange.contains(point.z) + } +} + extension Scanner { func onOff() -> Bool { if scanString("on") != nil { @@ -84,6 +111,15 @@ extension Scanner { let zRange = range() return (command, xRange, yRange, zRange) } + + func cube() -> Cube { + let (state, xrange, yrange, zrange) = line() + return Cube( + start: Point(x: xrange.lowerBound, y: yrange.lowerBound, z: zrange.lowerBound), + end: Point(x: xrange.upperBound, y: yrange.upperBound, z: zrange.upperBound), + state: state + ) + } } let input = """ From 2543975e2d3d6d5d5f5173beed5b7972d5e40ba7 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 25 Dec 2021 17:59:43 +0100 Subject: [PATCH 31/32] Reimplement day 22 part 1 together with part 2. --- day22.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/day22.swift b/day22.swift index a27b374..2bc5051 100644 --- a/day22.swift +++ b/day22.swift @@ -31,19 +31,35 @@ struct Day22: Puzzle { let firstY = ySteps.removeFirst() let firstZ = zSteps.removeFirst() + var part1 = 0 var part2 = 0 var x0 = firstX for x in xSteps { let possibleX = cubes.filter { $0.xRange.overlaps(x0.. Date: Wed, 23 Nov 2022 16:48:18 +0100 Subject: [PATCH 32/32] Move to subdirectory --- {AoC21.xcodeproj => 2021/AoC21.xcodeproj}/project.pbxproj | 0 .../AoC21.xcodeproj}/project.xcworkspace/contents.xcworkspacedata | 0 .../project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist | 0 common.swift => 2021/common.swift | 0 day1.hs => 2021/day1.hs | 0 day10.swift => 2021/day10.swift | 0 day11.swift => 2021/day11.swift | 0 day12.swift => 2021/day12.swift | 0 day13.swift => 2021/day13.swift | 0 day14.swift => 2021/day14.swift | 0 day15.swift => 2021/day15.swift | 0 day16.swift => 2021/day16.swift | 0 day17.swift => 2021/day17.swift | 0 day18.swift => 2021/day18.swift | 0 day19.swift => 2021/day19.swift | 0 day2.hs => 2021/day2.hs | 0 day20.swift => 2021/day20.swift | 0 day21.swift => 2021/day21.swift | 0 day22.swift => 2021/day22.swift | 0 day23.swift => 2021/day23.swift | 0 day24.swift => 2021/day24.swift | 0 day25.swift => 2021/day25.swift | 0 day3.hs => 2021/day3.hs | 0 day4.hs => 2021/day4.hs | 0 day5.hs => 2021/day5.hs | 0 day5part2.swift => 2021/day5part2.swift | 0 day6.swift => 2021/day6.swift | 0 day8part2.swift => 2021/day8part2.swift | 0 day9.swift => 2021/day9.swift | 0 29 files changed, 0 insertions(+), 0 deletions(-) rename {AoC21.xcodeproj => 2021/AoC21.xcodeproj}/project.pbxproj (100%) rename {AoC21.xcodeproj => 2021/AoC21.xcodeproj}/project.xcworkspace/contents.xcworkspacedata (100%) rename {AoC21.xcodeproj => 2021/AoC21.xcodeproj}/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist (100%) rename common.swift => 2021/common.swift (100%) rename day1.hs => 2021/day1.hs (100%) rename day10.swift => 2021/day10.swift (100%) rename day11.swift => 2021/day11.swift (100%) rename day12.swift => 2021/day12.swift (100%) rename day13.swift => 2021/day13.swift (100%) rename day14.swift => 2021/day14.swift (100%) rename day15.swift => 2021/day15.swift (100%) rename day16.swift => 2021/day16.swift (100%) rename day17.swift => 2021/day17.swift (100%) rename day18.swift => 2021/day18.swift (100%) rename day19.swift => 2021/day19.swift (100%) rename day2.hs => 2021/day2.hs (100%) rename day20.swift => 2021/day20.swift (100%) rename day21.swift => 2021/day21.swift (100%) rename day22.swift => 2021/day22.swift (100%) rename day23.swift => 2021/day23.swift (100%) rename day24.swift => 2021/day24.swift (100%) rename day25.swift => 2021/day25.swift (100%) rename day3.hs => 2021/day3.hs (100%) rename day4.hs => 2021/day4.hs (100%) rename day5.hs => 2021/day5.hs (100%) rename day5part2.swift => 2021/day5part2.swift (100%) rename day6.swift => 2021/day6.swift (100%) rename day8part2.swift => 2021/day8part2.swift (100%) rename day9.swift => 2021/day9.swift (100%) diff --git a/AoC21.xcodeproj/project.pbxproj b/2021/AoC21.xcodeproj/project.pbxproj similarity index 100% rename from AoC21.xcodeproj/project.pbxproj rename to 2021/AoC21.xcodeproj/project.pbxproj diff --git a/AoC21.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/2021/AoC21.xcodeproj/project.xcworkspace/contents.xcworkspacedata similarity index 100% rename from AoC21.xcodeproj/project.xcworkspace/contents.xcworkspacedata rename to 2021/AoC21.xcodeproj/project.xcworkspace/contents.xcworkspacedata diff --git a/AoC21.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/2021/AoC21.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist similarity index 100% rename from AoC21.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist rename to 2021/AoC21.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/common.swift b/2021/common.swift similarity index 100% rename from common.swift rename to 2021/common.swift diff --git a/day1.hs b/2021/day1.hs similarity index 100% rename from day1.hs rename to 2021/day1.hs diff --git a/day10.swift b/2021/day10.swift similarity index 100% rename from day10.swift rename to 2021/day10.swift diff --git a/day11.swift b/2021/day11.swift similarity index 100% rename from day11.swift rename to 2021/day11.swift diff --git a/day12.swift b/2021/day12.swift similarity index 100% rename from day12.swift rename to 2021/day12.swift diff --git a/day13.swift b/2021/day13.swift similarity index 100% rename from day13.swift rename to 2021/day13.swift diff --git a/day14.swift b/2021/day14.swift similarity index 100% rename from day14.swift rename to 2021/day14.swift diff --git a/day15.swift b/2021/day15.swift similarity index 100% rename from day15.swift rename to 2021/day15.swift diff --git a/day16.swift b/2021/day16.swift similarity index 100% rename from day16.swift rename to 2021/day16.swift diff --git a/day17.swift b/2021/day17.swift similarity index 100% rename from day17.swift rename to 2021/day17.swift diff --git a/day18.swift b/2021/day18.swift similarity index 100% rename from day18.swift rename to 2021/day18.swift diff --git a/day19.swift b/2021/day19.swift similarity index 100% rename from day19.swift rename to 2021/day19.swift diff --git a/day2.hs b/2021/day2.hs similarity index 100% rename from day2.hs rename to 2021/day2.hs diff --git a/day20.swift b/2021/day20.swift similarity index 100% rename from day20.swift rename to 2021/day20.swift diff --git a/day21.swift b/2021/day21.swift similarity index 100% rename from day21.swift rename to 2021/day21.swift diff --git a/day22.swift b/2021/day22.swift similarity index 100% rename from day22.swift rename to 2021/day22.swift diff --git a/day23.swift b/2021/day23.swift similarity index 100% rename from day23.swift rename to 2021/day23.swift diff --git a/day24.swift b/2021/day24.swift similarity index 100% rename from day24.swift rename to 2021/day24.swift diff --git a/day25.swift b/2021/day25.swift similarity index 100% rename from day25.swift rename to 2021/day25.swift diff --git a/day3.hs b/2021/day3.hs similarity index 100% rename from day3.hs rename to 2021/day3.hs diff --git a/day4.hs b/2021/day4.hs similarity index 100% rename from day4.hs rename to 2021/day4.hs diff --git a/day5.hs b/2021/day5.hs similarity index 100% rename from day5.hs rename to 2021/day5.hs diff --git a/day5part2.swift b/2021/day5part2.swift similarity index 100% rename from day5part2.swift rename to 2021/day5part2.swift diff --git a/day6.swift b/2021/day6.swift similarity index 100% rename from day6.swift rename to 2021/day6.swift diff --git a/day8part2.swift b/2021/day8part2.swift similarity index 100% rename from day8part2.swift rename to 2021/day8part2.swift diff --git a/day9.swift b/2021/day9.swift similarity index 100% rename from day9.swift rename to 2021/day9.swift