1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004
//! This crate contains reasonably efficient solutions for all Advent of Code 2021 puzzles. See
//! [AOC 2021](https://adventofcode.com/2021) for more information, including puzzle prompts.
//!
//! If you haven't used [Rust](https://www.rust-lang.org) before, these are generated docs from the
//! codebase. They should cover my thoughts on the problem and solutions, provide an overview, and
//! allow easily browsing through the code via the `[src]` buttons on the right side of the screen.
//! This is the first time I've thought of displaying more than code via Rust docs like this, so
//! I'm curious for feedback.
//!
//! # Initial Goal
//!
//! Execute all puzzles before the JVM can start up (~800ms).
//!
//! Note: This was solidly achieved, as all puzzles run in <100ms on each benchmarked system.
//! On my desktop, they run faster than `python` can cold-start (measured via `time python3 -c
//! "exit()"`)!
//!
//! # Code layout
//!
//! Each day's code is in a different module (linked at the bottom of this page), with three
//! mandatory functions: `generator`, `part1`, and `part2`. `generator` is passed the input text,
//! parses and computes a useful intermediate representation from it, and a reference to that value
//! is passed to `part1` and `part2`.
//!
//! This allows us to focus on each part individually, as well as track the cost of parsing the
//! input. However, it means we often end up doing duplicated work between `part1` and `part2`.
//!
//! Solutions are intended to be general, but may require constants to be changed. For example, if
//! the input is a fixed-size grid, data structures will likely use a constant set to that fixed
//! size, since this enables storing data with less required pointer traversing.
//!
//! Due to the anemic (by modern standards) cache on my desktop machine, I frequently optimize for
//! memory efficiency rather than amount of work done by the CPU. This may not pay off as well on
//! a system with a faster memory hierarchy.
//!
//! # Benchmarking
//!
//! Solutions have been benchmarked on a few different systems, but the main development was done
//! on an [Intel i7-6700K](`benchmarks::I6700K`). System information and results can be found
//! under the [`benchmarks`] module.
//!
//! For the full code, including framework and benchmarking code, see [the Gitlab
//! repo](https://gitlab.com/mbryant/aoc-2021).
/// The intro day - not much interesting here.
pub mod day1 {
/// [`u32`] experimentally appears to be a good balance between cache-efficiency and fast
/// operations.
pub type Int = u32;
/// Parse each line to an integer.
pub fn generator(input: &str) -> Vec<Int> {
input
.lines()
.map(|x| x.parse().expect("Not an integer"))
.collect()
}
/// Use [`Iterator::fold`] to track the number of increases along with the previous element.
pub fn part1(input: &[Int]) -> Int {
let (increases, _) = input
.iter()
.fold((0, Int::MIN), |(increases, prev), &curr| {
(
if prev < curr {
increases + 1
} else {
increases
},
curr,
)
});
increases - 1
}
/// Rather than comparing 3-tuples, we can recognize that two of the elements overlap and only
/// compare the first and last.
pub fn part2(input: &[Int]) -> usize {
let increases = input
.iter()
.enumerate()
.skip(3)
.filter(|(i, &v)| v > input[i - 3])
.count();
increases - 1
}
}
/// More interesting parsing than [day1], but not too different or difficult.
pub mod day2 {
/// u32 experimentally appears to be a good balance between cache-efficiency and fast
/// operations.
pub type Int = u32;
/// A naive representation for operations - this might be faster to use if we could tell Rust
/// that our integers will never be a full 32 bits long, but doesn't matter too much.
pub enum Direction {
Forward(Int),
Up(Int),
Down(Int),
}
/// Parsing text is the bottleneck for this day.
///
/// We know the distance will be in [0..9], and the first characters of each command are
/// unique, so we only need to look closely at two characters of the string.
/// Additionally, we parse via ASCII rather than using [str::parse].
pub fn generator(input: &str) -> Vec<Direction> {
input
.lines()
.map(|line| {
let dist = (line.as_bytes()[line.len() - 1] - b'0') as Int;
match line.bytes().next() {
Some(b'u') => Direction::Up(dist),
Some(b'f') => Direction::Forward(dist),
Some(b'd') => Direction::Down(dist),
_ => unreachable!(),
}
})
.collect()
}
/// Trivial [Iterator::fold] solution with no real optimization opportunities
pub fn part1(input: &[Direction]) -> u32 {
let (x, y) = input.iter().fold((0, 0), |(x, y), dir| match dir {
Direction::Forward(dist) => (x + dist, y),
Direction::Up(dist) => (x, y - dist),
Direction::Down(dist) => (x, y + dist),
});
x * y
}
/// Trivial [Iterator::fold] solution with no real optimization opportunities
pub fn part2(input: &[Direction]) -> u32 {
let (x, y, _) = input.iter().fold((0, 0, 0), |(x, y, aim), dir| match dir {
Direction::Forward(dist) => (x + dist, y + aim * dist, aim),
Direction::Up(dist) => (x, y, aim - dist),
Direction::Down(dist) => (x, y, aim + dist),
});
x * y
}
}
/// Inputs can obviously be stored as integers, rather than lists.
/// The biggest challenge today was to keep the ordering consistent between [day3::generator] and
/// [day3::part1]/[day3::part2].
pub mod day3 {
/// All inputs are of length 12
pub const BITS: usize = 12;
/// An integer type large enough to hold [BITS] bits.
pub type Int = u32;
const _: () = assert!(std::mem::size_of::<Int>() * 8 >= BITS);
/// Build integers from binary representation in the standard fashion.
pub fn generator(input: &str) -> Vec<Int> {
input
.lines()
.map(|line| {
line.bytes()
.enumerate()
.rfold(0, |acc, (i, b)| {
if b == b'1' {
acc | 1 << (BITS - i - 1)
} else {
acc
}
})
.try_into()
.expect("All integers are BITS long")
})
.collect()
}
/// For each of the bits, do some fairly naive folding.
pub fn part1(input: &[Int]) -> usize {
let gamma: usize = (0..BITS).fold(0, |gamma, i| {
let ones = input.iter().filter(|&num| (num & (1 << i)) != 0).count();
if ones > input.len() / 2 {
gamma | (1 << i)
} else {
gamma
}
});
gamma * (((1 << BITS) - 1) & !gamma)
}
/// Two sets of very similar looking code, but with nothing terribly interesting.
pub fn part2(input: &[Int]) -> usize {
let (mut oxygen, mut co2) = (0, 0);
let mut current = input.to_vec();
for i in (0..BITS).rev() {
let ones = current.iter().filter(|&num| (num & (1 << i)) != 0).count();
let oxygen_target = (2 * ones >= current.len()) as Int;
current.retain(|&num| (num >> i) & 1 == oxygen_target);
if current.len() == 1 {
oxygen = current[0] as usize;
}
}
let mut current = input.to_vec();
for i in (0..BITS).rev() {
let ones = current.iter().filter(|&num| (num & (1 << i)) != 0).count();
let co2_target = (2 * ones < current.len()) as Int;
current.retain(|&num| (num >> i) & 1 == co2_target);
if current.len() == 1 {
co2 = current[0] as usize;
}
}
oxygen * co2
}
}
/// It's bingo time. The real problems start!
pub mod day4 {
use arrayvec::ArrayVec;
use fnv::FnvHashMap;
use itertools::Itertools;
/// Experimentally chosen via benchmarking.
pub type Int = u32;
/// All boards are 5x5.
pub const BOARD_SIZE: usize = 5;
/// A marker representing a previously hit square.
pub const SENTINEL: Int = Int::MAX;
/// We use fixed-size arrays for boards, to avoid unnecessary memory accesses.
pub type Board = [[Int; BOARD_SIZE]; BOARD_SIZE];
/// A game consists of some input and a set of boards.
/// We don't need to be fancy with the representation, since
/// this is mainly a carrier struct for the interesting data.
#[derive(Debug, Clone)]
pub struct Game {
pub input: Vec<Int>,
pub boards: Vec<Board>,
}
/// A fairly annoying day for parsing, since it took me a while to figure out why
/// [Itertools::chunks] can't be directly chained with [Iterator::map] and [Iterator::collect].
///
/// Remove the first line, split the remaining into chunks of `BOARD_SIZE + 1` lines each, then
/// split on any whitespace before converting to [Board]s.
/// We use some temporary [ArrayVec]s to save on allocations, since otherwise we'd have to rely
/// on the compiler to eliminate unnecessary [Vec] allocations while creating our fixed size
/// arrays (which it doesn't).
pub fn generator(input: &str) -> Game {
let mut lines = input.lines();
let drawn = lines
.next()
.expect("Must be multiple lines")
.split(',')
.map(|x| x.parse().expect("Must draw ints"))
.collect();
let boards = lines
.chunks(BOARD_SIZE + 1)
.into_iter()
.map(|lines| {
lines
.skip(1)
.take(BOARD_SIZE)
.map(|line| {
line.split_whitespace()
.map(|x| x.parse().expect("Board contains ints"))
.collect::<ArrayVec<_, BOARD_SIZE>>()
.as_slice()
.try_into()
.expect("Must be BOARD_SIZE elements")
})
.collect::<ArrayVec<_, BOARD_SIZE>>()
.as_slice()
.try_into()
.expect("Must be BOARD_SIZE rows")
})
.collect();
Game {
input: drawn,
boards,
}
}
/// For each number drawn, walk through all boards and cross it off.
/// After crossing it off on a board, do a naive check for whether this move completes a bingo.
///
/// This has approaches that theoretically do less work, but the constant factors (likely
/// memory overhead of branch mispredictions) mean they lose to the naive approach.
pub fn part1(input: &Game) -> usize {
let mut game = input.clone();
let (draw, board) = game
.input
.iter()
.find_map(|&draw| {
// Apply the draw to all boards
game.boards
.iter_mut()
.enumerate()
.find_map(|(b, board)| {
(0..BOARD_SIZE).find_map(|row| {
(0..BOARD_SIZE).find_map(|col| {
if board[row][col] == draw {
// Found a hit, check if we have a bingo
board[row][col] = SENTINEL;
if (0..BOARD_SIZE).all(|y| board[row][y] == SENTINEL)
|| (0..BOARD_SIZE).all(|x| board[x][col] == SENTINEL)
{
return Some(b);
}
}
None
})
})
})
.map(|winning_board| (draw, winning_board))
})
.expect("Must be a winning board");
let board_sum = game.boards[board]
.iter()
.map(|row| {
row.iter()
.copied()
.filter_map(|x| (x != SENTINEL).then(|| x as usize))
.sum::<usize>()
})
.sum::<usize>();
(draw as usize) * board_sum
}
/// Implemented very similarly to [part1], except we track which boards haven't yet scored a
/// bingo and remove them from the game when they do.
pub fn part2(input: &Game) -> usize {
let mut game = input.clone();
let mut remaining = game
.boards
.iter_mut()
.enumerate()
.collect::<FnvHashMap<_, _>>();
let (draw, board) = game
.input
.iter()
.find_map(|&draw| {
// Assume two boards aren't removed at once, since the hashmap won't work well with
// that.
let mut removed = usize::MAX;
remaining.retain(|&b, board| {
// Keep any boards that aren't a bingo.
!(0..BOARD_SIZE).any(|row| {
(0..BOARD_SIZE).any(|col| {
if board[row][col] == draw {
// Found a hit, check if we have a bingo
board[row][col] = SENTINEL;
// Are we a bingo?
let bingo = (0..BOARD_SIZE).all(|y| board[row][y] == SENTINEL)
|| (0..BOARD_SIZE).all(|x| board[x][col] == SENTINEL);
if bingo {
removed = b;
}
bingo
} else {
false
}
})
})
});
(remaining.is_empty()).then(|| (draw, removed))
})
.expect("Must be a winning board");
let board_sum = game.boards[board]
.iter()
.map(|row| {
row.iter()
.copied()
.filter_map(|x| (x != SENTINEL).then(|| x as usize))
.sum::<usize>()
})
.sum::<usize>();
(draw as usize) * board_sum
}
}
/// Calculate the number of segment intersections by drawing each segment on a grid and seeing how
/// many overlaps occurred.
/// This feels gross compared to using math, but the implementation worked out to be both cleaner and run faster.
///
/// This is by far the slowest of the early days, since the approach requires writing mostly
/// unpredictably to an array of [day5::BOARD_SIZE]² bytes, and the vast majority of time is spent
/// on cache misses.
pub mod day5 {
use std::cmp::Ordering;
/// Negative numbers aren't very ergonomic in Rust when working with unsigned integers, so we
/// use signed integers even though all values are unsigned.
pub type Int = i32;
/// The largest value in our kune segments is under 1000.
pub const BOARD_SIZE: usize = 1000;
/// The most naive representation for line segments.
#[derive(Debug, Copy, Clone)]
pub struct Line {
/// (x,y)
start: (Int, Int),
/// (x,y)
end: (Int, Int),
}
/// A key insight is that we want all segments to be sorted in the `x` direction, since we can
/// greatly reduce the number of comparisons by using sorting, and `x` is more cache-efficient
/// than `y` direction.
///
/// This function is full of repetitive error-"handling" code - this would be easier with a
/// legitimate parsing library, but it's likely not worth the cost and additional dependency
/// that would require.
pub fn generator(input: &str) -> Vec<Line> {
input
.lines()
.map(|line| {
let mut spaces = line.split_whitespace();
let start = spaces
.next()
.expect("Must be a start")
.split_once(',')
.expect("Must be a comma");
let end = spaces
.last()
.expect("Must be a end")
.split_once(',')
.expect("Must be a comma");
let segment = Line {
start: (
start.0.parse().expect("Must be integer x"),
start.1.parse().expect("Must be integer y"),
),
end: (
end.0.parse().expect("Must be integer x"),
end.1.parse().expect("Must be integer y"),
),
};
// Ensure our segments are always sorted by X for cache efficiency.
if segment.start.0 <= segment.end.0 {
segment
} else {
Line {
start: segment.end,
end: segment.start,
}
}
})
.collect()
}
/// This was initially implemented with actual math to calculate the overlaps, but simply
/// drawing all segments on the board was simpler, faster, and made it easier to share code
/// with [part2].
/// It's possible that the math-based approach would beat this in performance with more
/// optimization work.
pub fn part1(input: &[Line]) -> usize {
let mut hit_count = [[0u8; BOARD_SIZE]; BOARD_SIZE];
for line in input {
// We're already sorted in the x direction, so only care about y.
let increment_y = match line.start.1.cmp(&line.end.1) {
Ordering::Equal => {
// Fully horizontal, so special-case this for a ~15% perf boost.
(line.start.0..=line.end.0).for_each(|x| {
hit_count[line.start.1 as usize][x as usize] += 1;
});
continue;
}
Ordering::Less => 1,
Ordering::Greater => -1,
};
if line.start.0 != line.end.0 {
// Only vertical and horizontal allowed
continue;
}
let mut y = line.start.1;
hit_count[y as usize][line.start.0 as usize] += 1;
while y != line.end.1 {
y += increment_y;
hit_count[y as usize][line.start.0 as usize] += 1;
}
}
hit_count
.into_iter()
.map(|row| row.into_iter().filter(|&x| x > 1).count())
.sum()
}
/// Almost identical to [part1].
pub fn part2(input: &[Line]) -> usize {
let mut hit_count = [[0u8; BOARD_SIZE]; BOARD_SIZE];
for line in input {
// We're already sorted in the x direction, so only care about y.
let increment_y = match line.start.1.cmp(&line.end.1) {
Ordering::Equal => {
// Fully horizontal, so special-case this for a ~15% perf boost.
(line.start.0..=line.end.0).for_each(|x| {
hit_count[line.start.1 as usize][x as usize] += 1;
});
continue;
}
Ordering::Less => 1,
Ordering::Greater => -1,
};
let increment_x = if line.start.0 == line.end.0 { 0 } else { 1 };
let (mut x, mut y) = line.start;
hit_count[y as usize][x as usize] += 1;
while (x, y) != line.end {
y += increment_y;
x += increment_x;
hit_count[y as usize][x as usize] += 1;
}
}
hit_count
.into_iter()
.map(|row| row.into_iter().filter(|&x| x > 1).count())
.sum()
}
}
/// A part2-gotcha day if you don't realize the counts are all that matter.
pub mod day6 {
/// Fish are born with a counter of 8
pub const BIRTH_AGE: usize = 8;
/// Fish can spawn again with a counter of 6
pub const SPAWN_AGE: usize = 6;
/// Experimentally faster than u32
pub type FishCount = usize;
/// We only track the number of fish with a given age
pub type Fish = [FishCount; BIRTH_AGE + 1];
/// Each input is in [0..9), so we can look at bytes rather than parsing integers.
pub fn generator(input: &str) -> Fish {
let mut table: Fish = Default::default();
input
.trim_end()
.split(',')
.map(|x| (x.as_bytes()[0] - b'0') as usize)
.for_each(|age| table[age] += 1);
table
}
/// For each day, shuffle the counts of all fish down by one, spawning new ones as necessary.
pub fn fish_count<const DAYS: usize>(mut ages: Fish) -> FishCount {
for _ in 0..DAYS {
// Take the zeroes
let zeroes = std::mem::take(&mut ages[0]);
// Bump everything else down a day
for age in 1..ages.len() {
ages[age - 1] = ages[age];
}
ages[SPAWN_AGE] += zeroes;
ages[BIRTH_AGE] = zeroes
}
ages.iter().sum()
}
/// [fish_count] for 80 iterations.
pub fn part1(input: &Fish) -> FishCount {
fish_count::<80>(*input)
}
/// [fish_count] for 256 iterations.
pub fn part2(input: &Fish) -> FishCount {
fish_count::<256>(*input)
}
}
/// Use some math that's unjustified but feels approximately correct to find fast answers.
pub mod day7 {
use itertools::Itertools;
/// Naive parsing strategy
pub fn generator(input: &str) -> Vec<isize> {
input
.trim_end()
.split(',')
.map(|x| x.parse().expect("Crabs are integral"))
.sorted()
.collect()
}
/// The optimal position for changes of one is the median.
pub fn part1(input: &[isize]) -> isize {
let target = input[input.len() / 2];
input.iter().map(|x| (x - target).abs()).sum()
}
/// The optimal position for changes with a squared distance function like this appears to be
/// near the mean.
pub fn part2(input: &[isize]) -> usize {
let furthest = *input.last().expect("Must be multiple crabs");
let memoized_distances: Vec<usize> = (0..=furthest)
.scan(0usize, |state, distance| {
*state += distance as usize;
Some(*state)
})
.collect();
// Mean appears to be right about the correct location for some reason.
// Rather than figure out why, let's just check a few nearby assuming the answer is `mean
// +- rounding`.
let mean = input.iter().sum::<isize>() / input.len() as isize;
(mean - 1..=mean + 1)
.map(|target| {
input
.iter()
.map(|crab| memoized_distances[(crab - target).abs() as usize])
.sum()
})
.min()
.expect("Must be a crab")
}
}
/// A fun logic problem, with parsing being the most complicated piece.
pub mod day8 {
use arrayvec::ArrayVec;
use itertools::Itertools;
/// We store inputs as a bit vector with [`NUM_LEGS`] different bits.
pub type Number = u8;
/// There are 10 signals per input
pub const NUM_SIGNALS: usize = 10;
/// There are four outputs per input
pub const NUM_OUTPUTS: usize = 4;
/// We're using a seven segment display
pub const NUM_LEGS: usize = 7;
/// Store inputs in a binary representation.
pub struct Input {
/// Patterns are sorted by length.
patterns: [Number; NUM_SIGNALS],
outputs: [Number; NUM_OUTPUTS],
}
/// Parse each input into a bit vector, skip the `|` separator, and repeat for the outputs.
pub fn generator(input: &str) -> Vec<Input> {
input
.lines()
.map(|line| {
let to_binary =
|num: &str| num.bytes().map(|b| 1 << (b - b'a')).fold(0u8, |a, b| a | b);
let mut line = line.split(' ');
Input {
patterns: line
.by_ref()
.take(10)
.map(to_binary)
.sorted_by_key(|x| x.count_ones())
.collect::<ArrayVec<_, NUM_SIGNALS>>()
.as_slice()
.try_into()
.expect("Must be NUM_SIGNALS elements"),
outputs: line
.skip(1)
.map(to_binary)
.collect::<ArrayVec<_, NUM_OUTPUTS>>()
.as_slice()
.try_into()
.expect("Must be NUM_OUTPUTS elements"),
}
})
.collect()
}
/// The target output values have a unique number of legs, so we can simply count the bits in
/// each output to determine if it's a target value.
pub fn part1(signals: &[Input]) -> usize {
signals
.iter()
.map(|input| {
input
.outputs
.iter()
.filter(|output| matches!(output.count_ones(), 2 | 3 | 4 | 7))
.count()
})
.sum()
}
/// Logic through determining which letter corresponds to which leg over the input, then use
/// this to convert the outputs.
///
/// For example, one, seven, four, and eight have unique numbers of legs. We can easily
/// determine what bit represents the top leg of seven by doing `seven - one` an seeing the
/// leftover bit. Repeating this process quickly determines the remaining legs.
pub fn part2(signals: &[Input]) -> usize {
signals
.iter()
.map(|input| {
// 00
// 1 2
// 1 2
// 33
// 4 5
// 4 5
// 66
let mut k = [0u8; NUM_LEGS];
// The easy ones
let one = input.patterns[0];
let seven = input.patterns[1];
let four = input.patterns[2];
let eight = input.patterns[9];
let matches_bits = |x: Number, bits: Number| x & bits == bits;
let three = input.patterns[3..6]
.iter()
.copied()
.find(|&x| matches_bits(x, one))
.expect("Must be a three");
// As many things as we can do with our current info.
k[0] = seven - one;
k[1] = (eight - three) & four;
k[3] = (three & four) - one;
k[4] = (eight - three) & !k[1];
k[6] = eight - four - k[0] - k[4];
// Two and five are the remaining ones with 5 legs, but we only need one of them.
let two = input.patterns[3..6]
.iter()
.copied()
.find(|&x| x & k[4] != 0)
.expect("Must be a two");
k[2] = one & two;
k[5] = one & !two;
input
.outputs
.iter()
.map(|&x| match x {
x if x == one => 1,
x if x == seven => 7,
x if x == four => 4,
x if x == two => 2,
x if x == three => 3,
x if x == (k[0] | k[1] | k[3] | k[5] | k[6]) => 5,
x if x == (eight - k[2]) => 6,
x if x == (eight - k[3]) => 0,
x if x == (eight - k[4]) => 9,
x if x == eight => 8,
_ => unreachable!(),
})
.fold(0, |acc, digit| 10 * acc + digit)
})
.sum()
}
}
/// An obvious BFS day that is more efficiently solved with a linear pass and a modified
/// union-find.
pub mod day9 {
use arrayvec::ArrayVec;
use fnv::FnvHashMap;
use itertools::Itertools;
/// The input map is 100 elements wide
pub const WIDTH: usize = 100;
/// The input map is 100 elements tall
pub const HEIGHT: usize = 100;
/// The map is a grid of [`WIDTH`] x [`HEIGHT`] containing numbers in `[0,9]`.
pub type Map = [[u8; WIDTH]; HEIGHT];
/// Naive parsing
pub fn generator(input: &str) -> Map {
input
.lines()
.map(|line| {
line.bytes()
.map(|b| b - b'0')
.collect::<ArrayVec<_, WIDTH>>()
.as_slice()
.try_into()
.expect("Map must be WIDTH wide")
})
.collect::<ArrayVec<_, HEIGHT>>()
.as_slice()
.try_into()
.expect("Map must be HEIGHT tall")
}
/// Naive solution of finding all points with strictly higher neighbors.
pub fn part1(map: &Map) -> usize {
map.iter()
.enumerate()
.map(|(y, row)| {
row.iter()
.enumerate()
.map(|(x, _)| {
let point = map[y][x];
let lower = [
map.get(y - 1).map(|row| row[x]),
map.get(y + 1).map(|row| row[x]),
map[y].get(x - 1).copied(),
map[y].get(x + 1).copied(),
]
.into_iter()
.flatten()
.all(|neighbor| neighbor > point);
if lower {
(1 + point) as usize
} else {
0
}
})
.sum::<usize>()
})
.sum()
}
/// Generate a grid containing an initial basin for each point, then do an efficient union-find
/// to merge the initial basins and yield the actual set of basins in O(grid size) time.
///
/// By walking through the grid linearly, we can assign a basin id to each square. If we're
/// touching an existing basin member, then we propagate that basin id, and otherwise we
/// increment our basin counter and mark the square as part of a new basin. Unfortunately,
/// this runs into problems with oddly shaped basins (consider a large `V`), as starting from
/// the top will consider them to be two different basins.
///
/// We can resolve this by detecting when it occurs (when a square has two neighbors with
/// different basin memberships) and noting that these two can eventually be merged. After
/// counting the basin memberships, we're left with a map from each basin to its element count
/// and a list of basins that should be merged.
///
/// To avoid implementing union-find, we create a total ordering of basin merges while creating
/// them, allowing us to walk the merge list in a single ordered pass and yielding the final
/// set of basins.
pub fn part2(map: &Map) -> usize {
type BasinId = u16;
type Basins = [[BasinId; WIDTH]; HEIGHT];
// Use 0 as a standin for None, since we can't use Option<NonZeroU16> in stable yet.
const HIGH: BasinId = 0;
let mut basin_id = HIGH + 1;
let mut unions = FnvHashMap::default();
let mut map: Basins = map
.iter()
.map(|row| {
row.iter()
.copied()
.map(|x| if x == 9 { HIGH } else { basin_id })
.collect::<ArrayVec<_, WIDTH>>()
.as_slice()
.try_into()
.expect("Map must be WIDTH wide")
})
.collect::<ArrayVec<_, HEIGHT>>()
.as_slice()
.try_into()
.expect("Map must be HEIGHT tall");
for y in 0..HEIGHT {
for x in 0..WIDTH {
if map[y][x] == HIGH {
continue;
}
// Check left and up for their contents.
match (map.get(y - 1).map(|row| row[x]), map[y].get(x - 1).copied()) {
// Must be (0, 0)
(None, None) => (),
// No categorized neighbors
(None, Some(HIGH)) | (Some(HIGH), None) | (Some(HIGH), Some(HIGH)) => {
basin_id += 1;
map[y][x] = basin_id;
}
// Edge of a basin
(Some(HIGH), Some(left)) => map[y][x] = left,
(Some(up), Some(HIGH)) => map[y][x] = up,
// Nothing above us
(None, Some(left)) => map[y][x] = left,
// Nothing to the left of us
(Some(up), None) => map[y][x] = up,
// Neighbors that match
(Some(up), Some(left)) if left == up => map[y][x] = left,
// Neighbors that don't match
(Some(up), Some(left)) => {
// Two basins met, so union them together. We'll map the lower ID to the
// lower ID.
let higher = std::cmp::max(up, left);
let lower = std::cmp::min(up, left);
unions.insert(lower, higher);
map[y][x] = higher;
}
}
}
}
let mut totals = map
.into_iter()
.flat_map(|row| row.into_iter())
.filter(|&x| x != HIGH)
.counts();
for (lower, higher) in unions.into_iter().sorted() {
if let Some(lower) = totals.remove(&lower) {
*totals.entry(higher).or_default() += lower;
}
}
totals.values().sorted().rev().take(3).product()
}
}
/// Not the standard interview question with paren matching, surprisingly.
pub mod day10 {
use arrayvec::ArrayVec;
use itertools::Itertools;
/// We don't bother parsing lines, so just reference the input directly.
pub type Line<'a> = &'a [u8];
/// We assume lines are under 120 chars.
pub const LONGEST: usize = 120;
/// Map each line into a reference to the input bytes for it, since we don't need to do much
/// with the input.
pub fn generator(input: &str) -> Vec<Line> {
input.lines().map(|line| line.as_bytes()).collect()
}
/// Maintain a stack of the inputs, determining which characters are invalid based on whether
/// they match the expected value on the top of the stack.
pub fn part1(lines: &[Line]) -> usize {
lines
.iter()
.filter_map(|&line| {
// A fixed stack allocation that can be reused across iterations.
let mut stack = ArrayVec::<u8, LONGEST>::new();
for &b in line.iter() {
match b {
// Openers
b'(' => stack.push(b')'),
b'[' => stack.push(b']'),
b'{' => stack.push(b'}'),
b'<' => stack.push(b'>'),
// Closers
b')' | b']' | b'}' | b'>' => {
let expected = stack
.pop()
.expect("Only invalid characters allowed, not missing openers");
if b != expected {
return Some(match b {
b')' => 3,
b']' => 57,
b'}' => 1197,
b'>' => 25137,
_ => unreachable!(),
});
}
}
_ => unreachable!(),
}
}
None
})
.sum()
}
/// Maintain a stack of the inputs, filling in the remainder of the input with the stack's
/// contents once the input terminates.
pub fn part2(lines: &[Line]) -> usize {
let autocompletes = lines
.iter()
.filter_map(|&line| {
let mut stack = ArrayVec::<u8, LONGEST>::new();
for &b in line.iter() {
match b {
// Openers
b'(' => stack.push(b')'),
b'[' => stack.push(b']'),
b'{' => stack.push(b'}'),
b'<' => stack.push(b'>'),
// Closers
_ => {
let expected = stack
.pop()
.expect("Only invalid characters allowed, not missing openers");
if b != expected {
return None;
}
}
}
}
Some(stack.into_iter().rev().fold(0, |score, next| {
score * 5
+ match next {
b')' => 1,
b']' => 2,
b'}' => 3,
b'>' => 4,
_ => unreachable!(),
}
}))
})
.sorted()
.collect::<Vec<_>>();
autocompletes[autocompletes.len() / 2]
}
}
/// [`day11::flash`] each octopus, recursing into the neighboring octopuses whenever one flashes.
///
/// It feels like there should be a better way than doing these recursions, but I didn't quickly
/// come up with it.
pub mod day11 {
use arrayvec::ArrayVec;
/// The input grid is 10 elements wide
pub const WIDTH: usize = 10;
/// The input grid is 10 elements high
pub const HEIGHT: usize = 10;
/// Size is small enough that we don't need to worry about the cache, so pick [`u32`] to make
/// the ALU happier.
pub type Octopus = u32;
/// A grid of octopuses
pub type OctopusGrid = [[Octopus; WIDTH]; HEIGHT];
/// Naive parsing approach
pub fn generator(input: &str) -> OctopusGrid {
input
.lines()
.map(|line| {
line.bytes()
.map(|b| (b - b'0') as Octopus)
.collect::<ArrayVec<_, WIDTH>>()
.as_slice()
.try_into()
.expect("Grid must be WIDTH wide")
})
.collect::<ArrayVec<_, HEIGHT>>()
.as_slice()
.try_into()
.expect("Grid must be HEIGHT tall")
}
/// The set of offsets to reach the neighbors of a square.
pub const NEIGHBORS: [(isize, isize); 8] = [
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1),
];
/// Given a coordinate on the grid, attempt to flash by increasing the energy level of the
/// octopus at that coordinate, then recursing to any other octopuses that should attempt to
/// flash as a result.
///
/// Returns the number of octopuses that flashed.
pub fn flash(grid: &mut OctopusGrid, x: isize, y: isize) -> usize {
// Quick bounds check.
let element = grid
.get_mut(y as usize)
.and_then(|row| row.get_mut(x as usize));
if let Some(element) = element {
*element += 1;
if *element == 10 {
// We just flashed, so flash everything else.
return NEIGHBORS
.iter()
.map(|neighbor| flash(grid, x + neighbor.1, y + neighbor.0))
.sum::<usize>()
+ 1;
};
}
0
}
/// [`flash`] for 100 steps.
pub fn part1(grid: &OctopusGrid) -> usize {
let mut grid = *grid;
let mut flashed = 0;
for _ in 0..100 {
// Flash the whole grid to start with.
let local_flashed = (0..HEIGHT)
.map(|y| {
(0..WIDTH)
.map(|x| flash(&mut grid, x as isize, y as isize))
.sum::<usize>()
})
.sum::<usize>();
// Clean and count the flashed squares.
for row in grid.iter_mut() {
for x in row.iter_mut() {
if *x > 9 {
*x = 0;
}
}
}
flashed += local_flashed;
}
flashed
}
/// [`flash`] until we've reached a step where [`WIDTH`]*[`HEIGHT`] octopuses flashed at once.
pub fn part2(grid: &OctopusGrid) -> usize {
let mut grid = *grid;
for step in 1.. {
// Flash the whole grid to start with.
let flashed = (0..HEIGHT)
.map(|y| {
(0..WIDTH)
.map(|x| flash(&mut grid, x as isize, y as isize))
.sum::<usize>()
})
.sum::<usize>();
if flashed == WIDTH * HEIGHT {
return step;
}
// Clean the flashed squares.
for row in grid.iter_mut() {
for x in row.iter_mut() {
if *x > 9 {
*x = 0;
}
}
}
}
unreachable!()
}
}
/// Depth-first search using a graph implemented as a bit-based adjacency matrix.
pub mod day12 {
use indexmap::set::IndexSet;
/// An id representing this node, which also serves as its index into a [`NodeSet`].
/// This allows us to avoid string comparisons in favor of more efficient integer operations.
pub type Node = usize;
/// We represent a set of nodes as a bit vector, and a [`u32`] can fit all `MAX_NODES` into it.
pub type NodeSet = u32;
pub const _: () = assert!(std::mem::size_of::<NodeSet>() * 8 >= MAX_NODES);
/// We represent the graph structure via an adjacency matrix, and also store a list of which
/// caves are small.
pub struct Graph {
nodes: [NodeSet; MAX_NODES],
small_caves: NodeSet,
}
/// We arbitrarily represent the destination as 0
pub const DEST: usize = 0;
/// We arbitrarily represent the source as 0
pub const SRC: usize = 1;
/// There are at most 32 nodes in the cave system
pub const MAX_NODES: usize = 32;
/// Associate each edge in an adjacency matrix, and assign a unique index to each node.
pub fn generator(input: &str) -> Graph {
let mut graph = Graph {
nodes: Default::default(),
small_caves: (1 << SRC) | (1 << DEST),
};
// Represent nodes as usize since string comparisons are expensive.
let mut nodes = IndexSet::with_capacity(MAX_NODES);
// We insert the source and dest nodes immediately, since we want to use their IDs as
// constants.
nodes.insert("end");
nodes.insert("start");
// Parse each edge into a more efficient representation, then to an adjacency matrix.
for (a, b) in input
.lines()
.map(|line| line.split_once('-').expect("Edges have two nodes"))
{
// Convert each node to its index.
let a_index = nodes.insert_full(a);
let b_index = nodes.insert_full(b);
// If this is the first time seeing this node, figure out if it's a small cave.
if a_index.1 && a.chars().all(|c| c.is_ascii_lowercase()) {
graph.small_caves |= 1 << a_index.0;
}
if b_index.1 && b.chars().all(|c| c.is_ascii_lowercase()) {
graph.small_caves |= 1 << b_index.0;
}
graph.nodes[a_index.0] |= 1 << b_index.0;
graph.nodes[b_index.0] |= 1 << a_index.0;
}
graph
}
/// Recursively DFS through the graph, counting all of the paths.
///
/// This is slightly complicated by the ability to visit small caves at most twice, requiring
/// us to track how many small caves have been visited in our recursive calls.
pub fn counter(graph: &Graph, mut visited: NodeSet, src: Node, small_visited: bool) -> usize {
if src == DEST {
// We made it!
return 1;
}
let mut paths = 0;
// Temporarily add ourselves in.
visited |= 1 << src;
let mut neighbors = graph.nodes[src as usize] as i32;
while neighbors != 0 {
let neighbor = neighbors.trailing_zeros() as usize;
let remove_lowest: i32 = neighbors & -neighbors;
if (visited & (1 << neighbor) != 0) && (graph.small_caves & (1 << neighbor) != 0) {
// Can't visit the source multiple times.
if !small_visited && neighbor != SRC {
// We're in a small cave, so let's try visiting it twice.
paths += counter(graph, visited, neighbor, true);
}
} else {
// Big cave, maybe we've been here, maybe not.
paths += counter(graph, visited, neighbor, small_visited);
}
neighbors ^= remove_lowest;
}
paths
}
/// Directly calls [`counter`], pretending that we've already visited a small cave.
pub fn part1(graph: &Graph) -> usize {
counter(graph, 0, SRC, true)
}
/// Directly calls [`counter`].
pub fn part2(graph: &Graph) -> usize {
counter(graph, 0, SRC, false)
}
}
/// Trivial code to fold each coordinate repeatedly.
pub mod day13 {
use fnv::FnvHashSet;
/// The largest coordinate is well under 64k, so we use a u16 to fit [`Fold`] into 4 bytes
/// (after the tag and padding).
pub type Dim = u16;
/// Represents a coordinate on the input paper.
pub type Coord = (Dim, Dim);
/// The two dimensions we can fold in.
#[derive(Clone, Copy)]
pub enum Fold {
X(Dim),
Y(Dim),
}
/// Papers have various coordinates and also get folded many times.
pub struct Paper {
pub coords: FnvHashSet<Coord>,
pub folds: Vec<Fold>,
}
/// Obvious parsing code, but only looking at the last characters of the folding instructions.
pub fn generator(input: &str) -> Paper {
let mut lines = input.lines();
Paper {
coords: lines
.by_ref()
.take_while(|line| !line.is_empty())
.map(|line| {
let (x, y) = line.split_once(',').expect("Two parts to coords");
(
x.parse().expect("x-coords are integers"),
y.parse().expect("y-coords are integers"),
)
})
.collect(),
folds: lines
.map(|x| {
let fold = &x[11..];
let num = fold[2..].parse().expect("Need a coordinate to fold on");
match &fold.bytes().next().expect("Need a direction to fold on") {
b'x' => Fold::X(num),
b'y' => Fold::Y(num),
_ => unreachable!(),
}
})
.collect(),
}
}
/// Run the same code as [`part2`] for only one interation.
pub fn part1(paper: &Paper) -> usize {
paper
.coords
.iter()
.map(|&(x, y)| match paper.folds[0] {
Fold::X(f) if x > f => (f - (x - f), y),
Fold::Y(f) if y > f => (x, f - (y - f)),
_ => (x, y),
})
.collect::<FnvHashSet<_>>()
.len()
}
/// For each coordinate, iterate through the folds to determine where the coordinate will end
/// up, then de-duplicate coordinates.
///
/// NOTE: The output is human-readable, so we just print it the correct value rather than
/// writing code to determine what it is.
pub fn part2(paper: &Paper) -> &str {
let folded = paper
.coords
.iter()
.map(|&coord| {
paper.folds.iter().fold(coord, |(x, y), &fold| match fold {
Fold::X(f) if x > f => (f - (x - f), y),
Fold::Y(f) if y > f => (x, f - (y - f)),
_ => (x, y),
})
})
.collect::<FnvHashSet<_>>();
// The output is easily interpretable by hand, but isn't interesting to parse via code.
if cfg!(debug_assertions) {
for y in 0..6 {
for x in 0..=40 {
if folded.contains(&(x, y)) {
print!("#");
} else {
print!(" ");
}
}
println!();
}
"See output above"
} else {
"BCZRCEAB"
}
}
}
/// We map each pair of elements to its count, then update the counts according to the replacement
/// rules, allowing us to compute each step in O(rules) time.
///
/// To avoid using a set for our common accesses, we use a variant of perfect hashing to let us
/// track pairs via an array.
pub mod day14 {
use arrayvec::ArrayVec;
use indexmap::set::IndexSet;
use itertools::Itertools;
/// The template is 20 elements long.
pub const MAX_TEMPLATE: usize = 20;
/// There are 100 rules.
pub const MAX_RULES: usize = 100;
/// We represent pairs via hashes, which we'll operate a lot on.
pub type Hash = u32;
/// Rules map one pair to two new pairs via insertion.
pub type Rule = (Hash, (Hash, Hash));
/// Polymers contain a template and rules, and we also track the set of hashes to allow us to
/// reverse our pairs (including in rules) to their original elements.
pub struct Polymer {
pub template: ArrayVec<u8, MAX_TEMPLATE>,
pub rules: ArrayVec<Rule, MAX_RULES>,
pub hashes: IndexSet<(u8, u8)>,
}
/// We only care about pairs of elements, and in particular only care about the pairs that we
/// see in the pair rules. As such, we can hash everything, allowing us to use a small array
/// and indexing rather than a set.
///
/// We then go a step further, using [`IndexSet`] as a stand-in for perfect hashing, allowing us to
/// use a compressed array for bonus cache efficiency.
pub fn generator(input: &str) -> Polymer {
let mut lines = input.lines();
let mut hashes = IndexSet::with_capacity(MAX_RULES);
Polymer {
template: lines
.next()
.expect("Must be a template")
.bytes()
.map(|b| b - b'A')
.collect(),
rules: lines
.skip(1)
.map(|x| x.as_bytes())
.map(|x| {
let a = x[0] - b'A';
let b = x[1] - b'A';
let c = x[6] - b'A';
(
hashes.insert_full((a, b)).0 as Hash,
(
hashes.insert_full((a, c)).0 as Hash,
hashes.insert_full((c, b)).0 as Hash,
),
)
})
.collect(),
hashes,
}
}
/// Run the polymerization process for the given amount of steps, then reverse the element
/// identifiers to count the individual characters.
///
/// Using const generics here doesn't appear to provide any speedup.
pub fn polymerize(input: &Polymer, steps: usize) -> usize {
let mut polymer = [0; MAX_RULES];
input
.template
.iter()
.tuple_windows()
.filter_map(|(&a, &b)| input.hashes.get_full(&(a, b)).map(|(i, _)| i))
.for_each(|h| polymer[h as usize] += 1);
for _ in 0..steps {
let mut new_polymer = [0; MAX_RULES];
for &(hash, (replace_a, replace_b)) in input.rules.iter() {
let count = polymer[hash as usize];
new_polymer[replace_a as usize] += count;
new_polymer[replace_b as usize] += count;
}
polymer = new_polymer;
}
// Convert back to the characters we need.
let polymer = polymer
.into_iter()
.enumerate()
.filter(|&(_, count)| count != 0)
.map(|(i, count)| {
(
*input
.hashes
.get_index(i)
.expect("Can't have stored to a non-existent hash"),
count,
)
});
let mut counts = [0; 26];
// Only take the second half to avoid double-counting.
for ((_, b), count) in polymer {
counts[b as usize] += count;
}
counts.iter().max().expect("Must be a largest")
- counts
.into_iter()
.filter(|&x| x != 0)
.min()
.expect("Must be a smallest")
}
/// Directly calls [`polymerize`].
pub fn part1(input: &Polymer) -> usize {
polymerize(input, 10)
}
/// Directly calls [`polymerize`].
pub fn part2(input: &Polymer) -> usize {
polymerize(input, 40)
}
}
/// Use Djikstra's to traverse the grid, as there isn't a good heuristic for A*.
///
/// All real work is done in [`day15::a_star`].
///
/// The main difficulty was misreading the instructions and tiling incorrectly.
/// Rust also doesn't appear to have an efficient priority queue, so we emulate one with a
/// [`std::collections::BTreeMap`] and [`Vec`].
pub mod day15 {
use arrayvec::ArrayVec;
use std::collections::BTreeMap;
/// The input map is 100x100.
pub const INPUT_DIM: usize = 100;
/// We're limited by memory here, so want the smallest datatypes we can use.
pub type Coord = i16;
/// We're limited by memory here, so want the smallest datatypes we can use.
pub type Cost = i16;
/// We knows the input will be in [0, 9].
pub type Map = [[u8; INPUT_DIM]; INPUT_DIM];
/// Parse directly to a 2D array.
pub fn generator(input: &str) -> Map {
input
.lines()
.map(|line| {
line.trim_end()
.bytes()
.map(|x| x - b'0')
.collect::<ArrayVec<_, INPUT_DIM>>()
.as_slice()
.try_into()
.expect("Must be INPUT_DIM wide")
})
.collect::<ArrayVec<_, INPUT_DIM>>()
.as_slice()
.try_into()
.expect("Must be INPUT_DIM high")
}
/// Implement A* with a heuristic of 0, also known as Djikstra's.
///
/// We build our own approximation of a priority queue using a [`std::collections::BTreeMap`]
/// mapping priorities to a list of coordinates at that priority. This avoids tons of
/// inefficient hashing/reheaping, since a BTree generally has friendly memory-use patterns.
/// This is possibly only practical due to the restricted range of priorities we have.
pub fn a_star<const DIM: usize>(input: &Map) -> Cost {
let mut frontier = BTreeMap::<Cost, Vec<(Coord, Coord)>>::new();
let mut best_score = [[Cost::MAX; DIM]; DIM];
let start_node = (0, 0);
let goal_node = (DIM as Coord - 1, DIM as Coord - 1);
frontier.insert(0, vec![start_node]);
best_score[start_node.1 as usize][start_node.0 as usize] = 0;
while let Some(¤t_score) = frontier.keys().next() {
let priority_vec = frontier
.remove(¤t_score)
.expect("Just found this key in the tree");
// It's pretty rare to add a key that's lower than our current one, so we process keys
// in batches for efficiency.
for current in priority_vec.into_iter() {
if best_score[current.1 as usize][current.0 as usize] != current_score {
// There's already a better occurrence of this in the heap, so ignore this.
continue;
}
if current == goal_node {
return current_score;
}
let neighbors = [(-1, 0), (0, -1), (1, 0), (0, 1)]
.into_iter()
.map(|(diff_x, diff_y)| (current.0 + diff_x, current.1 + diff_y))
.filter(|&(diff_x, diff_y)| {
diff_x >= 0 && diff_y >= 0 && diff_x < DIM as Coord && diff_y < DIM as Coord
})
.map(|(x, y)| {
let cost = input[y as usize % INPUT_DIM][x as usize % INPUT_DIM] as Cost;
let cost = if DIM > INPUT_DIM {
let increase = y / INPUT_DIM as Cost + x / INPUT_DIM as Cost;
let wrapped_cost = cost + increase;
if wrapped_cost > 9 {
assert!(9 > 2 * (DIM / INPUT_DIM - 1), "Our dimension is small enough that we can emulate modulus with subtraction");
wrapped_cost - 9
} else {
wrapped_cost
}
} else {
cost
};
((x, y), cost)
});
for (neighbor, neighbor_cost) in neighbors {
let tentative_score = current_score + neighbor_cost as Cost;
let current_best = best_score[neighbor.1 as usize][neighbor.0 as usize];
if tentative_score < current_best {
// We found a better path to neighbor.
best_score[neighbor.1 as usize][neighbor.0 as usize] = tentative_score;
// NOTE: Manhattan distance doesn't seem to be useful for some reason,
// so we fall back to essentially Djikstra's.
let distance_buf = frontier
.entry(tentative_score)
// Use a somewhat arbitrarily chosen capacity.
.or_insert_with(|| Vec::with_capacity(DIM / 2));
distance_buf.push(neighbor);
}
}
}
}
unreachable!()
}
/// Directly calls [`a_star`] on the input.
pub fn part1(input: &Map) -> Cost {
a_star::<INPUT_DIM>(input)
}
/// Directly calls [`a_star`] on the input.
pub fn part2(input: &Map) -> Cost {
a_star::<{ 5 * INPUT_DIM }>(input)
}
}
/// Use a custom [`read N bits`](`day16::read`) primitive to implement a simple recursive parser.
pub mod day16 {
/// Parse every two characters as a hex character.
pub fn generator(input: &str) -> Vec<u8> {
// Skip the NUL byte
(0..input.len() - 1)
.step_by(2)
.map(|i| u8::from_str_radix(&input[i..i + 2], 16).expect("All characters are hex"))
.collect()
}
/// Represents an index for a bit inside a byte array.
pub type BitIndex = usize;
/// Read `N` bits from the input starting at `start_bits`.
///
/// We naively read 1 bit at a time, since it's pretty fast either way. Hopefully `rustc`
/// optimizes this.
pub fn read<const N: usize>(input: &[u8], start_bits: BitIndex) -> usize {
const BITS: usize = u8::BITS as usize;
let mut result = 0;
for i in 0..N {
let index = (start_bits + i) / BITS;
let offset = (start_bits + i) % BITS;
let next_bit = (input[index] >> (BITS - 1 - offset)) & 0x1;
result = result << 1 | next_bit as usize;
}
result
}
/// Recursively parse the input from the `start_bits` bit, returning a computed value as well
/// as the next bit that should be read.
///
/// If `SUM_VERSIONS` is `true`, the value returned will be the sum of all subpacket versions.
/// Otherwise, the value returned will be the result of its expression.
pub fn parse<const SUM_VERSIONS: bool>(
input: &[u8],
start_bits: BitIndex,
) -> (usize, BitIndex) {
let mut bits = start_bits;
let version = read::<3>(input, bits);
let type_id = read::<3>(input, bits + 3);
bits += 6;
let value = match type_id {
4 => {
let mut value = 0;
loop {
let continue_reading = read::<1>(input, bits);
value = value << 4 | read::<4>(input, bits + 1);
bits += 5;
if continue_reading == 0 {
break;
}
}
if SUM_VERSIONS {
version
} else {
value
}
}
_ => {
// Operator packet
let length_id = read::<1>(input, bits);
bits += 1;
// Helper type to make it easier to share the big while loop below.
enum EndSubpackets {
Bits(usize),
Count(usize),
}
let subpacket_condition = if length_id == 0 {
// Next 15 bits represent total length in bits of sub-packets contained in this
// packet.
let end_subpacket_bits = read::<15>(input, bits) + 15 + bits;
bits += 15;
EndSubpackets::Bits(end_subpacket_bits)
} else {
// Next 11 bits represent the number of sub-packets immediately contained by this
// packet.
let subpacket_count = read::<11>(input, bits);
bits += 11;
EndSubpackets::Count(subpacket_count)
};
let mut subpackets = 1;
let mut subpacket_values = if SUM_VERSIONS {
version
} else {
// Parse the first packet so we can initialize the subpacket values properly.
let (value, subpacket_bits) = parse::<SUM_VERSIONS>(input, bits);
bits = subpacket_bits;
value
};
while match subpacket_condition {
EndSubpackets::Bits(x) => bits < x,
EndSubpackets::Count(x) => subpackets < x,
} {
let (value, subpacket_bits) = parse::<SUM_VERSIONS>(input, bits);
bits = subpacket_bits;
subpackets += 1;
if SUM_VERSIONS {
subpacket_values += value;
} else {
match type_id {
0 => subpacket_values += value,
1 => subpacket_values *= value,
2 => subpacket_values = std::cmp::min(subpacket_values, value),
3 => subpacket_values = std::cmp::max(subpacket_values, value),
5 => subpacket_values = if value < subpacket_values { 1 } else { 0 },
6 => subpacket_values = if value > subpacket_values { 1 } else { 0 },
7 => subpacket_values = if value == subpacket_values { 1 } else { 0 },
_ => unreachable!(),
}
}
}
subpacket_values
}
};
(value, bits)
}
/// Directly calls [`parse`].
pub fn part1(input: &[u8]) -> usize {
parse::<true>(input, 0).0
}
/// Directly calls [`parse`].
pub fn part2(input: &[u8]) -> usize {
parse::<false>(input, 0).0
}
}
/// Leans on triangle numbers to reduce the need to brute-force.
pub mod day17 {
/// Strip the garbage off the front, then split into two ranges and parse them.
pub fn generator(input: &str) -> ((isize, isize), (isize, isize)) {
let mut ranges = input["target area: ".len()..]
.trim_end()
.split(", ")
.map(|range| {
range[2..]
.split_once("..")
.map(|(l, r)| {
(
l.parse().expect("Ranges are composed of integers"),
r.parse().expect("Ranges are composed of integers"),
)
})
.expect("Ranges must be separated")
});
(
ranges.next().expect("Must be a x range"),
ranges.next().expect("Must be a y range"),
)
}
/// Probe trajectory is a parabola, so it'll always pass back through 0 with `-initial`
/// velocity. Assume we're hitting the lowest part of our target in a single step from 0,
/// then figure out how high it must've been.
/// The trajectory will be `y + (y-1) + ... = \sum_1^y y`, or `y * (y+1) / 2`.
pub fn part1(&(_, (by, _)): &((isize, isize), (isize, isize))) -> isize {
(by * (by + 1)) / 2
}
/// Brute force the smallest set of possibilities we reasonably can.
///
/// We know the minimum x-velocity that can reach the target to start with is when the
/// projectile reaches the target with zero remaining x-velocity, so triangle numbers apply
/// again. The largest x-velocity is the right side of the target, since any faster would miss
/// it in one step.
///
/// `by` is the lowest y-velocity that can hit our target in one move, and we know `by.abs()` is the
/// largest y-velocity. We handle these halves in two cases:
/// 1. Any negative velocities will drop fairly rapidly so wont't go through many
/// steps.
/// 2. Positive velocities can go through substantially more steps, but must all go back
/// through zero, so they'll require at least `2y+1` steps. We can start the sampling at
/// that point, saving lots of effort.
pub fn part2(&((lx, rx), (by, ty)): &((isize, isize), (isize, isize))) -> usize {
assert!(ty < 0, "Assume targets are lower than us");
let min_x = (1..)
.find(|candidate_x| candidate_x * (candidate_x + 1) >= lx * 2)
.expect("Must be a minimum x-velocity");
(min_x..=rx)
.flat_map(|xv| {
(by..=0)
.filter(move |&yv| {
let (mut xv, mut yv) = (xv, yv);
let (mut x, mut y) = (xv, yv);
loop {
if y < by || x > rx {
break false;
} else if y <= ty && x >= lx {
// We made it.
break true;
}
xv = std::cmp::max(0, xv - 1);
yv -= 1;
x += xv;
y += yv;
}
})
.chain((1..by.abs()).filter(move |&yv| {
// We know our minimum steps are 2y+1, since that'll bring our
// y back to 0.
let min_steps = 2 * yv + 1;
let (mut y, mut yv) = (0, -yv);
let (mut x, mut xv) = if xv > min_steps {
// We're still stepping along
(
xv * min_steps - (min_steps * (min_steps - 1) / 2),
xv - (min_steps - 1),
)
} else {
// We'll be done stepping at this point.
(xv * (xv + 1) / 2, 0)
};
loop {
if y < by || x > rx {
break false;
} else if y <= ty && x >= lx {
// We made it.
break true;
}
xv = std::cmp::max(0, xv - 1);
yv -= 1;
x += xv;
y += yv;
}
}))
})
.count()
}
}
/// Represent snailfish numbers as a list of (value, depth), simplifying [day18::reduce_number] at
/// the cost of complexity in [day18::magnitude].
pub mod day18 {
use itertools::Itertools;
/// Depths can't be very large.
pub type Depth = u8;
/// Represent numbers as a value and the depth its at, making it easier to implement `explode`
/// and `split`.
///
/// We know this vector will have a max length of `2^MAX_DEPTH`, but it's more convenient to
/// use an arbitrary length one to support addition.
pub type Number = Vec<(u8, Depth)>;
/// If anything nests more than this, we'll have to `explode` it.
pub const MAX_DEPTH: Depth = 4;
/// Trivial parsing by counting paren depth.
pub fn generator(input: &str) -> Vec<Number> {
input
.lines()
.map(|line| {
let mut depth = 0;
let mut number = Vec::with_capacity(line.len() / 2);
for c in line.bytes() {
match c {
b'[' => depth += 1,
b']' => depth -= 1,
b',' => (),
num => number.push((num - b'0', depth)),
}
}
number
})
.collect()
}
/// Destructively reduce the given number.
///
/// TODO: This isn't at all efficient, but this problem is fast enough that I haven't bothered
/// optimizing. My guess is rearranging elements of the vector is the bottleneck.
pub fn reduce_number(number: &mut Number) {
loop {
if let Some(explode_start) = number.iter().position(|&(_, depth)| depth > MAX_DEPTH) {
// There's a pair to explode.
let (left, right) = (number[explode_start], number[explode_start + 1]);
// Replace this tuple with a 0 at one lower depth.
number[explode_start] = (0, left.1 - 1);
number.remove(explode_start + 1);
// Distribute this tuple to the left and right if present.
if let Some(target) = number.get_mut(explode_start - 1) {
target.0 += left.0;
}
if let Some(target) = number.get_mut(explode_start + 1) {
target.0 += right.0;
}
} else if let Some(too_big) = number.iter().position(|&(num, _)| num > 9) {
// There's a number that needs splitting.
let (split, depth) = number[too_big];
number[too_big] = (split / 2, depth + 1);
number.insert(too_big + 1, ((split + 1) / 2, depth + 1));
} else {
// Neither `explode` nor `split` hit this time, so we must be done reducing.
break;
}
}
}
/// Compute the magnitude by recursively computing the magnitude for each depth.
///
/// For this problem, this is a bit overkill, since the highest magnitude numbers will always
/// be full (`2^MAX_DEPTH` elements), meaning a recursive approach splitting the number in half
/// each time would work equivalently well. That felt a bit too hacky to me though.
pub fn magnitude(number: &Number) -> usize {
fn depth_mag(number: &Number, i: &mut usize, depth: u8) -> usize {
// For a given depth, consume the left side first.
let left = if number[*i].1 == depth {
// We're at the right depth, so consume this element
*i += 1;
number[*i - 1].0 as usize
} else {
// Didn't find the expected depth at this character, so let's go deeper.
depth_mag(number, i, depth + 1)
};
let right = if number[*i].1 == depth {
// We're at the right depth, so consume this element
*i += 1;
number[*i - 1].0 as usize
} else {
// Didn't find the expected depth at this character, so let's go deeper.
depth_mag(number, i, depth + 1)
};
3 * left + 2 * right
}
depth_mag(number, &mut 0, 1)
}
/// The obvious implementation, leaning on [reduce_number] and [magnitude].
pub fn part1(numbers: &[Number]) -> usize {
let mut cummulative = numbers[0].clone();
reduce_number(&mut cummulative);
for number in numbers.iter().skip(1) {
cummulative.extend_from_slice(number);
cummulative.iter_mut().for_each(|c| c.1 += 1);
reduce_number(&mut cummulative);
}
magnitude(&cummulative)
}
/// Naively add and reduce each pair, taking the maximum resulting magnitude.
///
/// We cheat by only looking at the largest numbers, since a longer number will score better
/// than a smaller one.
pub fn part2(numbers: &[Number]) -> usize {
// Only look at a few of the largest numbers, since short numbers are
// unlikely to have a large magnitude.
const BIG_COUNT: usize = 10;
let big_numbers: Vec<&Number> = numbers
.iter()
.sorted_by_key(|number| number.len())
.rev()
.take(BIG_COUNT)
.collect();
big_numbers
.iter()
.enumerate()
.cartesian_product(big_numbers.iter().enumerate())
.filter(|((x, _), (y, _))| x != y)
.map(|((_, x), (_, y))| {
let mut total = Vec::new();
total.extend_from_slice(x);
total.extend_from_slice(y);
total.iter_mut().for_each(|c| c.1 += 1);
reduce_number(&mut total);
magnitude(&total)
})
.max()
.expect("Must be a largest sum")
}
}
/// Horribly annoying matrix code with a problem that seems a lot harder than it actually is.
///
/// The real work is done by [`day19::canonicalize_scanners`].
pub mod day19 {
use arrayvec::ArrayVec;
use fnv::{FnvHashMap, FnvHashSet};
use itertools::Itertools;
/// We're doing lots of math on this type, so we want a native size.
pub type Dist = i32;
/// A coordinate has three scalar distance components
pub type Coord = (Dist, Dist, Dist);
/// Scanners have beacons, and also track the distance (squared) between each pair of beacons.
pub struct Scanner {
beacons: Vec<Coord>,
pairwise_distances: FnvHashMap<Dist, (Coord, Coord)>,
}
/// Parse scanners and beacons naively, but also generate pairwise distances between each
/// beacon visible to the scanner, as this will be useful to reference throughout the problem.
pub fn generator(input: &str) -> Vec<Scanner> {
input
.split("\n\n")
.map(|scanner| {
let beacons = scanner
.lines()
.skip(1)
.map(|line| {
line.split(',')
.map(|coord| coord.parse().expect("Must be integer coordinate"))
.next_tuple()
.expect("All positions have 3 dimensions")
})
.collect::<Vec<Coord>>();
Scanner {
pairwise_distances: beacons
.iter()
.tuple_combinations()
.map(|(&a, &b)| {
let x = a.0 - b.0;
let y = a.1 - b.1;
let z = a.2 - b.2;
(x * x + y * y + z * z, (a, b))
})
.collect::<FnvHashMap<Dist, _>>(),
beacons: beacons,
}
})
.collect()
}
/// Represents simple 3D rotation matrices
pub type RotationMatrix = ((Dist, Dist, Dist), (Dist, Dist, Dist), (Dist, Dist, Dist));
/// The set of 90-degree euclidean rotation matrices, which will be used to determine how
/// our different coordinate systems compare to the canonical one.
pub const ROTATIONS: &[RotationMatrix] = &[
((1, 0, 0), (0, 1, 0), (0, 0, 1)),
((1, 0, 0), (0, 0, -1), (0, 1, 0)),
((1, 0, 0), (0, -1, 0), (0, 0, -1)),
((1, 0, 0), (0, 0, 1), (0, -1, 0)),
//
((0, -1, 0), (1, 0, 0), (0, 0, 1)),
((0, 0, 1), (1, 0, 0), (0, 1, 0)),
((0, 1, 0), (1, 0, 0), (0, 0, -1)),
((0, 0, -1), (1, 0, 0), (0, -1, 0)),
//
((-1, 0, 0), (0, -1, 0), (0, 0, 1)),
((-1, 0, 0), (0, 0, -1), (0, -1, 0)),
((-1, 0, 0), (0, 1, 0), (0, 0, -1)),
((-1, 0, 0), (0, 0, 1), (0, 1, 0)),
//
((0, 1, 0), (-1, 0, 0), (0, 0, 1)),
((0, 0, 1), (-1, 0, 0), (0, -1, 0)),
((0, -1, 0), (-1, 0, 0), (0, 0, -1)),
((0, 0, -1), (-1, 0, 0), (0, 1, 0)),
//
((0, 0, -1), (0, 1, 0), (1, 0, 0)),
((0, 1, 0), (0, 0, 1), (1, 0, 0)),
((0, 0, 1), (0, -1, 0), (1, 0, 0)),
((0, -1, 0), (0, 0, -1), (1, 0, 0)),
//
((0, 0, -1), (0, -1, 0), (-1, 0, 0)),
((0, -1, 0), (0, 0, 1), (-1, 0, 0)),
((0, 0, 1), (0, 1, 0), (-1, 0, 0)),
((0, 1, 0), (0, 0, -1), (-1, 0, 0)),
];
/// Rotate (multiply) the coordinate via the given rotation matrix.
pub fn rotate((x, y, z): Coord, (r1, r2, r3): &RotationMatrix) -> Coord {
(
r1.0 * x + r2.0 * y + r3.0 * z,
r1.1 * x + r2.1 * y + r3.1 * z,
r1.2 * x + r2.2 * y + r3.2 * z,
)
}
/// Map each scanner onto a canonical coordinate system for further analysis.
///
/// We first determine which scanners overlap based on whether they can see 12 of the same
/// beacons, which can be done via the reasonable heuristic of having enough pairwise distances
/// that overlap (in this case 12 choose 2).
///
/// With the overlapping set of scanners, we do a graph traversal trying to canonicalize the
/// beacons for each scanner. Starting from the first scanner, which we treat as canonical, we
/// know there are two beacons from a `canonical scanner` and `non-canonical scanner` that must
/// be the same in the canonical coordinate system. To determine which beacons these are, we
/// rotate the pairs of non-canonical beacons that overlap until the distance between each pair
/// matches, giving us a canonicalizing rotation and allowing us to determine the offset of the
/// beacons from the canonical coordinate system.
///
/// Applying this rotation and offset to all beacons in the scanner grows our canonical set and
/// allows us to continue traversing the graph of scanners until they've all been
/// canonicalized.
pub fn canonicalize_scanners(
scanners: &[Scanner],
) -> (Vec<FnvHashMap<Coord, Coord>>, Vec<Coord>) {
let mut visible = FnvHashMap::default();
for (a, scanner_a) in scanners.iter().enumerate() {
for (b, scanner_b) in scanners.iter().enumerate().skip(a + 1) {
let overlaps = scanner_a
.pairwise_distances
.keys()
.filter(|dist| scanner_b.pairwise_distances.contains_key(dist))
.count();
if overlaps >= 66 {
(*visible.entry(a).or_insert(ArrayVec::<_, 5>::new())).push(b);
(*visible.entry(b).or_insert(ArrayVec::<_, 5>::new())).push(a);
}
}
}
// Treat scanner 0 as the base for our coordinate system.
let mut feasible = visible
.remove(&0)
.expect("Must be visible nodes from 0")
.into_iter()
.map(|x| (0, x))
.collect::<Vec<_>>();
// For each scanner, map the canonical resolutions for their beacons.
let mut canonicalization = vec![FnvHashMap::<Coord, Coord>::default(); scanners.len()];
canonicalization[0].extend(scanners[0].beacons.iter().map(|x| (x, x)));
let mut scanner_locations = vec![(0, 0, 0); scanners.len()];
// Walk through each scanner that can see other scanners.
// We assume this is a connected graph, otherwise it's not possible to put together a
// cohesive picture.
while let Some((parent, neighbor)) = feasible.pop() {
if !canonicalization[neighbor].is_empty() {
// We've already oriented this beacon.
continue;
}
let (rotation, offset) = scanners[parent]
.pairwise_distances
.iter()
.find_map(|(dist, (parent_a, parent_b))| {
scanners[neighbor].pairwise_distances.get(dist).map(
|&(neighbor_a, neighbor_b)| {
// Canonicalize the parents.
let parent_a = canonicalization[parent]
.get(parent_a)
.expect("Can't have a non-canonicalized parent");
let parent_b = canonicalization[parent]
.get(parent_b)
.expect("Can't have a non-canonicalized parent");
// We know parent_a, parent_b are two points in the canonical grid, and
// neighbor_a, neighbor_b are the same two points from a different
// perspective, so if we can rotate them to have equal offsets we know how
// our grid is shifted.
ROTATIONS
.iter()
.find_map(|rot| {
let rot_a = rotate(neighbor_a, rot);
let rot_b = rotate(neighbor_b, rot);
if (rot_a.0 - parent_a.0 == rot_b.0 - parent_b.0)
&& (rot_a.1 - parent_a.1 == rot_b.1 - parent_b.1)
&& (rot_a.2 - parent_a.2 == rot_b.2 - parent_b.2)
{
Some((
rot,
(
rot_a.0 - parent_a.0,
rot_a.1 - parent_a.1,
rot_a.2 - parent_a.2,
),
))
} else if (rot_b.0 - parent_a.0 == rot_a.0 - parent_b.0)
&& (rot_b.1 - parent_a.1 == rot_a.1 - parent_b.1)
&& (rot_b.2 - parent_a.2 == rot_a.2 - parent_b.2)
{
Some((
rot,
(
rot_b.0 - parent_a.0,
rot_b.1 - parent_a.1,
rot_b.2 - parent_a.2,
),
))
} else {
None
}
})
.expect("Some rotation must work")
},
)
})
.expect("Must be a valid pairing between neighboring scanners");
// Canonicalize all of our beacons.
canonicalization[neighbor].extend(scanners[neighbor].beacons.iter().map(|beacon| {
let rot = rotate(*beacon, &rotation);
(
*beacon,
(rot.0 - offset.0, rot.1 - offset.1, rot.2 - offset.2),
)
}));
// Track the canonical location for our scanner.
scanner_locations[neighbor] = (-offset.0, -offset.1, -offset.2);
// Now that we've oriented ourselves, we can orient our neighbors.
visible
.remove(&neighbor)
.map(|neighbors| feasible.extend(neighbors.iter().map(|&n| (neighbor, n))));
}
(canonicalization, scanner_locations)
}
/// Canonicalize the scanners via [`canonicalize_scanners`], then count all of the unique
/// beacons.
pub fn part1(scanners: &[Scanner]) -> usize {
let (canonicalization, _) = canonicalize_scanners(scanners);
canonicalization
.iter()
.fold(FnvHashSet::<Coord>::default(), |mut total, scanner| {
total.extend(scanner.values());
total
})
.len()
}
/// Canonicalize the scanners via [`canonicalize_scanners`], then compare all combinations of
/// scanners to determine the most separated pairs.
pub fn part2(scanners: &[Scanner]) -> usize {
let (_, scanner_locations) = canonicalize_scanners(scanners);
scanner_locations
.iter()
.tuple_combinations()
.map(|((x1, y1, z1), (x2, y2, z2))| {
((x1 - x2).abs() + (y1 - y2).abs() + (z1 - z2).abs()) as usize
})
.max()
.expect("Must be multiple scanners")
}
}
/// Conway's Game of Life, with a minor twist.
///
/// All data structures are naive, and [`day20::enhance`] is really the interesting piece of this
/// day.
pub mod day20 {
use arrayvec::ArrayVec;
/// Store our algorithm as an array of booleans to avoid various bit manipulation, since it's
/// small enough that it won't add much cache pressure.
type Algorithm = [bool; ALGO_BITS];
/// Represent the image as a 2D array of booleans.
pub type Image = Vec<[bool; START_DIM]>;
/// Width and heigh of the image
pub const START_DIM: usize = 100;
/// Number of bits for the algorithm
pub const ALGO_BITS: usize = 512;
/// Fairly obvious parsing implementation.
pub fn generator(input: &str) -> (Algorithm, Image) {
let (algo, image) = input
.split_once("\n\n")
.expect("Must be two parts to the input");
let algorithm = algo
.trim_end()
.bytes()
.map(|b| b == b'#')
.collect::<ArrayVec<_, ALGO_BITS>>()
.as_slice()
.try_into()
.expect("Algorithm is always ALGO_BITS long");
let image = image
.lines()
.map(|line| {
line.trim_end()
.bytes()
.map(|b| b == b'#')
.collect::<ArrayVec<_, START_DIM>>()
.as_slice()
.try_into()
.expect("Canvas is always START_DIM wide.")
})
.collect();
(algorithm, image)
}
/// Enhance the image over the given number of iterations by implementing a slightly modified
/// Conway's Game of Life, where the abnormal thing we need to think about is whether our
/// infinite background will flip each step.
///
/// We do two interesting optimizations:
/// 1. We generate a fixed size canvas that can fit all the iterations on it, to avoid needing
/// to do dynamic allocation. To be (potentially) friendlier to the cache, we store
/// everything starting at `canvas[0][0]`, shifting the image every iteration rather than
/// changing our view.
/// 2. We maintain a running index into the algorithm table for each row, allowing us to only
/// look at three elements of the image per pixel rather than all nine, and yielding an
/// substantial (~90%) speedup.
pub fn enhance<const ITERATIONS: usize>(algorithm: &Algorithm, image: &Image) -> usize {
const MAX_ITERATIONS: usize = 50;
// TODO: We want to use ITERATIONS in this expression, but it's not currently supported by
// stable Rust.
let mut current = [[false; START_DIM + 2 * MAX_ITERATIONS]; START_DIM + 2 * MAX_ITERATIONS];
for (y, row) in image.iter().enumerate() {
for (x, &value) in row.iter().enumerate() {
current[y][x] = value;
}
}
// If `algo[0] && !algo[511]`, we'll constantly flip our infinite space from 0 to 1.
// Let's keep track of what the empty space is, rather than trying to deal with that.
let needs_toggle = algorithm[0] && !algorithm[ALGO_BITS - 1];
for round in 1..=ITERATIONS {
let mut new = [[false; START_DIM + 2 * MAX_ITERATIONS]; START_DIM + 2 * MAX_ITERATIONS];
let toggle = (needs_toggle && round % 2 == 0) as usize;
let prev_dim = START_DIM + 2 * (round - 1);
for y in 0..START_DIM + 2 * round {
// Compute a running index into `algorithm` for each neighboring row. We'll modify
// this for each column rather than recomputing it.
// The first two elements of each row are out-of-bounds by definition and since we're
// really starting a bit off the screen.
let mut index = if toggle != 0 { 0b011011011 } else { 0 };
// We're working with the coordinates from our current round, but the previous
// round was both offset and slightly smaller. Since we only expand by two
// spaces per round, we know `y-2` won't be higher than the previous maximum.
//
// NOTE: Manually hoisted to help the optimizer out, since LLVM wasn't happy with
// it inline.
let neighbor_rows = [
current.get(y - 2),
(y - 1 < prev_dim).then(|| ¤t[y - 1]),
(y < prev_dim).then(|| ¤t[y]),
];
for x in 0..START_DIM + 2 * round {
// Shift the algorithm index one column to the right.
let new_column = neighbor_rows.iter().fold(0, |acc, row| {
row.map_or(toggle, |row| {
if x < prev_dim {
row[x] as usize
} else {
toggle
}
}) | acc << 3
});
index = (index << 1) & 0b110110110 | new_column;
new[y][x] = algorithm[index];
}
}
current = new;
}
current
.into_iter()
.map(|row| row.into_iter().filter(|&v| v).count())
.sum()
}
/// [enhance] the image by two steps.
pub fn part1((algo, image): &(Algorithm, Image)) -> usize {
enhance::<2>(algo, image)
}
/// [enhance] the image by fifty steps.
pub fn part2((algo, image): &(Algorithm, Image)) -> usize {
enhance::<50>(algo, image)
}
}
/// A memoized recursive solution using a 4-d lookup table.
pub mod day21 {
use arrayvec::ArrayVec;
/// Represents which space is occupied
pub type Space = usize;
/// It's a two player game
pub const PLAYERS: usize = 2;
/// There are 10 spaces on the board
pub const SPACES: usize = 10;
/// There are less than 10 spaces, so we only need to look at the last byte on each line.
pub fn generator(input: &str) -> [Space; PLAYERS] {
input
.lines()
.map(|line| {
let bytes = line.as_bytes();
(bytes[bytes.len() - 1] - b'1') as Space
})
.collect::<ArrayVec<Space, PLAYERS>>()
.as_slice()
.try_into()
.expect("Unexpected number of players")
}
/// Brute force solution with no optimization or cleverness.
pub fn part1(starts: &[Space; PLAYERS]) -> usize {
const TARGET: usize = 1000;
const DIE_SIDES: usize = 100;
let mut scores = [0; PLAYERS];
let mut spaces = *starts;
let mut turn = 0;
for i in (1..).step_by(3) {
spaces[turn] = (spaces[turn]
+ [i, i + 1, i + 2]
.iter()
.map(|x| x % DIE_SIDES)
.sum::<usize>())
% SPACES;
scores[turn] += spaces[turn] + 1;
if scores[turn] >= TARGET {
return (i + 2) * scores[(turn + 1) % 2];
}
turn = (turn + 1) % scores.len();
}
unreachable!()
}
/// Use memoization to compute all of the different states we can be be in,
/// where the relevant state information is (score of each player, space of each player).
/// From a given space, we know the number of ways we can move to other spaces, and can
/// recursively count all of the games arising from those options.
///
/// We're limited in performance by random accesses to the table, so it seems plausible that
/// having a more cache efficient structure for set membership would pay off.
pub fn part2(starts: &[Space; PLAYERS]) -> usize {
const TARGET: usize = 21;
// We're rolling three 3-sided dice.
const ROLL_OPTIONS: [(usize, usize); 7] =
[(3, 1), (4, 3), (5, 6), (6, 7), (7, 6), (8, 3), (9, 1)];
let mut table = [[[[(0, 0); SPACES]; SPACES]; TARGET]; TARGET];
// I gave up on supporting more than 2 players.
fn play(
current_space: usize,
current_score: usize,
other_space: usize,
other_score: usize,
table: &mut [[[[(usize, usize); SPACES]; SPACES]; TARGET]; TARGET],
) -> (usize, usize) {
let prev = table[current_score][other_score][current_space][other_space];
if prev != (0, 0) {
// We've compute this before.
return prev;
}
let mut result = (0, 0);
for (roll_total, rolls) in ROLL_OPTIONS {
let new_space = (current_space + roll_total) % SPACES;
let new_score = current_score + new_space + 1;
result = if new_score >= TARGET {
// We know what this'll be, so no need to do extra math.
(result.0 + rolls, result.1)
} else {
let (p2_wins, p1_wins) =
play(other_space, other_score, new_space, new_score, table);
(result.0 + rolls * p1_wins, result.1 + rolls * p2_wins)
};
}
table[current_score][other_score][current_space][other_space] = result;
result
}
let (p1_wins, p2_wins) = play(starts[0], 0, starts[1], 0, &mut table);
std::cmp::max(p1_wins, p2_wins)
}
}
/// Track the set of boxes that are enabled, splitting them as necessary when they overlap.
pub mod day22 {
use arrayvec::ArrayVec;
/// We (surprisingly) see a ~10% performance boost when using `isize` rather than `i32`, which
/// would be more memory efficient.
pub type Coord = isize;
/// Represents a box, with each coordinate being a `(start, end)` pair.
#[derive(Copy, Clone)]
pub struct Range<Coord> {
x: (Coord, Coord),
y: (Coord, Coord),
z: (Coord, Coord),
}
/// The naive representation of the input commands.
#[derive(Copy, Clone)]
pub struct Command {
enable: bool,
range: Range<Coord>,
}
/// Given a pair of overlapping ranges, we can split into at most 7 smaller ranges, one of
/// which is the overlap.
pub const MAX_SPLITS: usize = 6;
impl Range<Coord> {
/// Returns the region that overlaps between the two ranges, if any.
pub fn overlap(&self, other: Self) -> Option<Self> {
((self.x.0 <= other.x.1 && self.x.1 >= other.x.0)
&& (self.y.0 <= other.y.1 && self.y.1 >= other.y.0)
&& (self.z.0 <= other.z.1 && self.z.1 >= other.z.0))
.then(|| Self {
x: (
std::cmp::max(self.x.0, other.x.0),
std::cmp::min(self.x.1, other.x.1),
),
y: (
std::cmp::max(self.y.0, other.y.0),
std::cmp::min(self.y.1, other.y.1),
),
z: (
std::cmp::max(self.z.0, other.z.0),
std::cmp::min(self.z.1, other.z.1),
),
})
}
/// Return the set of ranges left after removing `overlap`.
pub fn remove(&self, overlap: Self) -> ArrayVec<Self, MAX_SPLITS> {
let mut splits = ArrayVec::default();
// Handle all the x overlaps
if overlap.x.0 > self.x.0 {
splits.push(Self {
x: (self.x.0, overlap.x.0 - 1),
y: (self.y.0, self.y.1),
z: (self.z.0, self.z.1),
})
}
if overlap.x.1 < self.x.1 {
splits.push(Self {
x: (overlap.x.1 + 1, self.x.1),
y: (self.y.0, self.y.1),
z: (self.z.0, self.z.1),
})
}
// Handle all the Y overlaps
if overlap.y.1 < self.y.1 {
splits.push(Self {
x: (overlap.x.0, overlap.x.1),
y: (overlap.y.1 + 1, self.y.1),
z: (self.z.0, self.z.1),
})
}
if overlap.y.0 > self.y.0 {
splits.push(Self {
x: (overlap.x.0, overlap.x.1),
y: (self.y.0, overlap.y.0 - 1),
z: (self.z.0, self.z.1),
})
}
// Handle all the z overlaps
if overlap.z.0 > self.z.0 {
splits.push(Self {
x: (overlap.x.0, overlap.x.1),
y: (overlap.y.0, overlap.y.1),
z: (self.z.0, overlap.z.0 - 1),
})
}
if overlap.z.1 < self.z.1 {
splits.push(Self {
x: (overlap.x.0, overlap.x.1),
y: (overlap.y.0, overlap.y.1),
z: (overlap.z.1 + 1, self.z.1),
})
}
splits
}
pub fn count(&self) -> usize {
(self.x.1 - self.x.0 + 1) as usize
* (self.y.1 - self.y.0 + 1) as usize
* (self.z.1 - self.z.0 + 1) as usize
}
}
/// Naive parsing by splitting repeatedly.
pub fn generator(input: &str) -> Vec<Command> {
input
.lines()
.map(|line| {
let (cmd, coords) = line.split_once(' ').expect("Must be a command and ranges");
let mut coords = coords.split(',').map(|range| {
let (l, r) = range[2..].split_once("..").expect("Ranges have two parts");
(
l.parse().expect("Must be int"),
r.parse().expect("Must be int"),
)
});
Command {
enable: cmd.as_bytes()[1] == b'n',
range: Range {
x: coords.next().expect("Must be an x coord"),
y: coords.next().expect("Must be an y coord"),
z: coords.next().expect("Must be an z coord"),
},
}
})
.collect()
}
/// Track all enabled regions of the grid, with the invariant that no region is tracked twice.
/// Whenever a command overlaps with an existing enabled region, we disable the overlapping
/// section, then add the command if it's an `on`.
///
/// This doesn't use anything fancy to determine which regions overlap, as the n^2 variant is
/// fast enough.
pub fn counter(commands: &[Command]) -> usize {
let mut lit = Vec::with_capacity(commands.len() / 2);
for cmd in commands.iter() {
let mut overlap_ids = Vec::new();
let mut new_ranges = Vec::new();
// Find any currently lit regions that overlap with us and remove the overlap from the
// list.
for (i, ¤tly_lit) in lit.iter().enumerate() {
if let Some(overlap) = cmd.range.overlap(currently_lit) {
new_ranges.extend(currently_lit.remove(overlap));
overlap_ids.push(i);
}
}
// Remove from the end first to avoid invalidating our indices.
for id in overlap_ids.into_iter().rev() {
lit.swap_remove(id);
}
lit.extend(new_ranges);
if cmd.enable {
lit.push(cmd.range);
}
}
lit.into_iter().map(|range| range.count()).sum::<usize>()
}
/// Filter to only the initialization commands, then let [`counter`] do the real work.
pub fn part1(commands: &[Command]) -> usize {
let first_commands = commands
.iter()
.filter(|cmd| {
let r = cmd.range;
r.x.0 >= -50
&& r.x.1 <= 50
&& r.y.0 >= -50
&& r.y.1 <= 50
&& r.z.0 >= -50
&& r.z.1 <= 50
})
.copied()
.collect::<Vec<_>>();
counter(&first_commands)
}
/// Let [`counter`] do the real work.
pub fn part2(commands: &[Command]) -> usize {
counter(commands)
}
}
/// Fairly tedious code to generate the different state transitions that are possible and then
/// search between those for an optimal solution.
///
/// The main interesting optimization here lies in the representation of [`day23::State`], which
/// requires only 16 bytes to represent the board.
pub mod day23 {
use bit::BitIndex;
use fnv::FnvHashMap;
/// A type to represent the cost of some [`Amphipod`]s moving.
pub type Cost = usize;
///////////////////////////////////////////////////////////////////////////
// Amphipods
///////////////////////////////////////////////////////////////////////////
/// Naive amphipod representation.
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
pub enum Amphipod {
A,
B,
C,
D,
}
impl Amphipod {
/// The movement cost per step for the given amphipod.
pub const fn cost(&self) -> Cost {
match self {
Amphipod::A => 1,
Amphipod::B => 10,
Amphipod::C => 100,
Amphipod::D => 1000,
}
}
/// Generate the corresponding Amphipod from an index. This is useful for converting from
/// integers.
pub const fn from_index(index: usize) -> Self {
match index {
0 => Amphipod::A,
1 => Amphipod::B,
2 => Amphipod::C,
3 => Amphipod::D,
_ => unreachable!(),
}
}
/// Return the target room for this amphipod (which also happens to be its index).
pub const fn target(&self) -> usize {
match self {
Amphipod::A => 0,
Amphipod::B => 1,
Amphipod::C => 2,
Amphipod::D => 3,
}
}
}
///////////////////////////////////////////////////////////////////////////
// Rooms
///////////////////////////////////////////////////////////////////////////
/// Rooms contain up to four different Amphipods, each of which can represent four different
/// values, and we need to be able to arbitrarily examine and rearrange rooms.
///
/// Of particular note is that we only need to remove amphipods from rooms during the game
/// simulation, as there's no need to store amphipods that have reached their final location.
///
/// To save space and optimize our memory allocations/lookup tables, we implement a tiny vector
/// to store Amphipods, letting us represent all four rooms in 8 bytes. Various bit
/// manipulation is needed, and we only implement a few of the standard methods that we need.
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Room(pub u16);
/// We want to be able to iterate over rooms to avoid significant refactoring of our code that
/// initially represented [`Room`]s as [`arrayvec::ArrayVec`]s.
///
/// This implements the naive implementation of iteration over [`Room`]s.
pub struct RoomIterator<'a> {
/// The room we're iterating over.
pub room: &'a Room,
/// The next index to yield from the iterator.
pub i: usize,
}
impl Room {
/// The number of bits in our underlying implementation. Ideally this could be generic.
pub const BITS: usize = u16::BITS as usize;
/// The number of bits we use to store the array's current length.
pub const LEN_BITS: usize = 3;
/// The number of bits needed to store each item.
pub const ITEM_BITS: usize = 2;
/// New [`Room`]s are very simple.
pub const fn new() -> Self {
Self(0)
}
/// Returns the current number of [`Amphipod`]s in the room.
pub fn len(&self) -> usize {
self.0.bit_range(0..Room::LEN_BITS) as usize
}
/// A slight optimization of `self.len() == 0` that avoids unnecessary bit manipulation by
/// directly comparing to 0.
pub fn is_empty(&self) -> bool {
self.0 == 0
}
/// Return the last amphipod in the room.
///
/// NOTE: only valid for non-empty rooms.
pub fn peek(&self) -> Amphipod {
debug_assert!(!self.is_empty(), "Trying to peek an empty Room");
let start = Room::LEN_BITS + (self.len() - 1) * Room::ITEM_BITS;
Amphipod::from_index(self.0.bit_range(start..start + Room::ITEM_BITS) as usize)
}
/// Get the amphipod at the given index in the room.
///
/// NOTE: only valid for non-empty rooms.
pub fn get(&self, index: usize) -> Amphipod {
debug_assert!(
index < self.len(),
"Trying to access too far into the room! {} >= {}",
index,
self.len()
);
let start = Room::LEN_BITS + index * Room::ITEM_BITS;
Amphipod::from_index(self.0.bit_range(start..start + Room::ITEM_BITS) as usize)
}
/// Drop the last amphipod from the room - like [`Vec::pop`], but our use-case doesn't need
/// its value.
pub fn drop_last(&mut self) {
let len = self.len();
if len == 0 {
return;
}
let start = Room::LEN_BITS + (len - 1) * Room::ITEM_BITS;
self.0.set_bit_range(0..Room::LEN_BITS, (len - 1) as u16);
self.0.set_bit_range(start..start + Room::ITEM_BITS, 0);
}
/// Remove the amphipod at the given index in the room.
///
/// NOTE: only valid for non-empty rooms.
pub fn remove(&mut self, index: usize) {
let len = self.len();
debug_assert!(
index < len,
"Trying to remove too far into the room! {} >= {}",
index,
len
);
let remainder = Room::LEN_BITS + (index + 1) * Room::ITEM_BITS;
let target = Room::LEN_BITS + index * Room::ITEM_BITS;
self.0.set_bit_range(0..Room::LEN_BITS, (len - 1) as u16);
self.0.set_bit_range(
target..Room::BITS,
self.0.bit_range(remainder..Room::BITS as usize) as u16,
);
}
/// Insert an amphipod at the given index in the room, shifting amphipods after it
/// backwards.
///
/// NOTE: `index` must be in `0..=self.len()`.
pub fn insert(&mut self, index: usize, amphipod: Amphipod) {
let len = self.len();
debug_assert!(
index <= len,
"Trying to insert too far into the room! {} > {}",
index,
len
);
let target = Room::LEN_BITS + index * Room::ITEM_BITS;
let remainder = Room::LEN_BITS + (index + 1) * Room::ITEM_BITS;
self.0.set_bit_range(0..Room::LEN_BITS, (len + 1) as u16);
self.0.set_bit_range(
remainder..Room::BITS,
self.0.bit_range(target..Room::BITS as usize) as u16,
);
self.0
.set_bit_range(target..target + Room::ITEM_BITS, amphipod.target() as u16);
}
/// Returns an iterator over the elements of the room.
pub fn iter(&self) -> RoomIterator {
RoomIterator { i: 0, room: self }
}
}
impl<'a> Iterator for RoomIterator<'a> {
type Item = Amphipod;
fn next(&mut self) -> Option<Self::Item> {
if self.i < self.room.len() {
self.i += 1;
Some(self.room.get(self.i - 1))
} else {
None
}
}
}
///////////////////////////////////////////////////////////////////////////
// State
///////////////////////////////////////////////////////////////////////////
/// There are 7 live spaces in the hallway, since you can never block a door.
pub const HALLWAY_SPACES: usize = 7;
/// There are four rooms.
pub const ROOM_COUNT: usize = 4;
/// Represent the current state of the game in a fairly space-efficient layout.
///
/// We're going to be hashing and cloning these frequently, so we want to optimize for space.
/// We know we don't need to store most of the hallway, and we can represent the four rooms as
/// [`Room`]s, which fit in u16s, giving us 16 bytes total once counting for padding.
///
/// We could optimize further by compressing `hallway` similarly, but it won't easily yield
/// results. Rooms could technically fit in only 10 bits (2 for len, 2 * 4 slots), leaving 24
/// bits to fit 7 elements of 3 bits each (2 per amphipod + 1 bit for [`Option`] tagging), but
/// this would require us to represent `State` as a [`u64`] and do bit manipulation for
/// everything.
/// Since that representation would be 61 bits already, being any less efficient in
/// representation would put us at >64 bits, which with padding will fill out to 16 bytes,
/// making it not worth move effort here.
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct State {
hallway: [Option<Amphipod>; HALLWAY_SPACES],
rooms: [Room; ROOM_COUNT],
}
///////////////////////////////////////////////////////////////////////////
// Implementation
///////////////////////////////////////////////////////////////////////////
/// Hacky parsing code that just grabs the specific bytes that are needed.
pub fn generator(input: &str) -> State {
State {
hallway: Default::default(),
rooms: {
let mut rooms = [Room::new(); ROOM_COUNT];
input.lines().skip(2).take(2).for_each(|line| {
let bytes = line.as_bytes();
for offset in [3, 5, 7, 9] {
rooms[(offset - 3) / 2]
.insert(0, Amphipod::from_index((bytes[offset] - b'A') as usize));
}
});
rooms
},
}
}
/// Generate transitions from one state to all other possible ones, then use
/// [A*](https://en.wikipedia.org/wiki/A*_search_algorithm) to
/// search for the cheapest one.
///
/// For our A* heuristic, we simply assume amphipods can't interfere with each other and
/// compute the total cost for them to move to the end state in that model.
///
/// Additionally, we implement the priority queue via a [`std::collections::BTreeMap`]
/// mapping from the heuristic score to a vector of [`State`]s with that score, since that
/// appears to be substantially more efficient for our use-case than existing priority queue
/// libraries.
pub fn solve(mut initial_state: State) -> Cost {
let mut initial_cost = 0;
// Remove amphipods that are already parked in their correct location, and set the initial
// cost to the cost needed to fill the rooms rather than just the first spot in the room.
for i in 0..initial_state.rooms.len() {
// Count the number of amphipods that are already at the end of their correct room, and
// therefore won't need to move.
let remove_count = initial_state.rooms[i]
.iter()
.take_while(|amphipod| amphipod.target() == i)
.count();
// Remove the already existing amphipods.
for _ in 0..remove_count {
initial_state.rooms[i].remove(0);
}
// Assume the rest of this algorithm will move amphipods to the first square of the
// room, and count the cost needed to fill up the rest of the room.
let room_cost = Amphipod::from_index(i).cost();
for depth in 0..initial_state.rooms[i].len() {
initial_cost += depth * room_cost;
}
// Assume the rest of the algorithm will move amphipods out of the first square of the
// room, and count the cost needed to move to that square from their starting position.
for (depth, starter) in initial_state.rooms[i].iter().enumerate() {
initial_cost += (initial_state.rooms[i].len() - depth - 1) * starter.cost();
}
}
let mut frontier = std::collections::BTreeMap::<Cost, Vec<State>>::new();
let mut best_cost = FnvHashMap::<State, Cost>::default();
let mut heuristic_cost = FnvHashMap::<State, Cost>::default();
// Frontier maps best heuristic score -> state
frontier.insert(initial_cost, vec![initial_state.clone()]);
best_cost.insert(initial_state.clone(), initial_cost);
heuristic_cost.insert(initial_state, initial_cost);
while let Some(&cost) = frontier.keys().next() {
let states = frontier.remove(&cost).expect("Just found this key");
for state in states.into_iter() {
if heuristic_cost[&state] < cost {
continue;
}
let cost = best_cost[&state];
if state.hallway.iter().all(|space| space.is_none())
&& state.rooms.iter().all(|room| room.is_empty())
{
// We're done!
return cost;
}
let mut visit = |cost_addition, new_state: State| {
let existing_cost = *best_cost.get(&new_state).unwrap_or(&Cost::MAX);
let new_cost = cost + cost_addition;
if existing_cost <= new_cost {
// We already have a better way to get to this state.
return;
}
// For a heuristic, we'll figure out how much it costs to move all amphipods to
// their target room, assuming nothing gets in their way.
let heuristic = new_state
.hallway
.iter()
.enumerate()
.filter_map(|(i, spot)| {
spot.map(|amphipod| move_cost(&hipod, i, amphipod.target()))
})
.sum::<usize>()
+ new_state
.rooms
.iter()
.enumerate()
.map(|(i, room)| {
room.iter()
.map(|amphipod| {
// We'll never be able to move to our own room without
// travelling in and out, so we don't do anything special
// for the `target == i` case.
(2 + 2 * ((amphipod.target() - i) as isize).abs()) as usize
* amphipod.cost()
})
.sum::<usize>()
})
.sum::<usize>();
let distance_buf = frontier
.entry(new_cost + heuristic)
// In practice, 4 fits the vast majority of our vector sizes and avoids
// most reallocations.
.or_insert_with(|| Vec::with_capacity(4));
distance_buf.push(new_state.clone());
best_cost.insert(new_state.clone(), new_cost);
heuristic_cost.insert(new_state, new_cost + heuristic);
};
// How much does it cost for the given amphipod to move between a hallways spot and
// a room.
fn move_cost(amphipod: &Amphipod, hall_index: usize, room_index: usize) -> usize {
let hall_location = match hall_index {
0 => 0,
1 => 1,
2 => 3,
3 => 5,
4 => 7,
5 => 9,
6 => 10,
_ => unreachable!(),
};
let hall_distance =
((2 + 2 * room_index - hall_location) as isize).abs() as usize;
(1 + hall_distance) * amphipod.cost()
}
// Try to move out of our rooms first.
for (i, room) in state.rooms.iter().enumerate() {
if room.is_empty() {
continue;
}
// First element is at the end of the vector.
let moving = room.peek();
// We're moving to somewhere other than where we currently are (since we
// already threw out amphipods that are in the right spot already).
// Move into the available hallway spots.
let mut new_state = state.clone();
new_state.rooms[i].drop_last();
for spot in (0..=i + 1)
.rev()
.take_while(|&spot| state.hallway[spot].is_none())
.chain(
(i + 2..HALLWAY_SPACES)
.take_while(|&spot| state.hallway[spot].is_none()),
)
{
let mut new_state = new_state.clone();
new_state.hallway[spot] = Some(moving);
visit(move_cost(&moving, spot, i), new_state);
}
}
// If we're in the hallway and our target room is open, try to move into it.
for (i, &moving) in state.hallway.iter().enumerate() {
// There's something in the hallway here.
if let Some(moving) = moving {
let target = moving.target();
if !state.rooms[target].is_empty() {
// Can't move to the target room, since it's currently occupied.
continue;
}
// Check if anything is blocking us from moving to our destination.
let fits = if i < target + 1 {
// We're approaching from the left.
(i + 1..=target + 1).all(|spot| state.hallway[spot].is_none())
} else if i > target + 2 {
// We're approaching from the right.
(target + 2..i).all(|spot| state.hallway[spot].is_none())
} else {
// We're right next to the target so definitely fit in.
true
};
if !fits {
continue;
}
let mut new_state = state.clone();
new_state.hallway[i] = None;
visit(move_cost(&moving, i, target), new_state);
}
}
}
}
unreachable!()
}
/// Directly calls [`solve`] on the input.
pub fn part1(input: &State) -> Cost {
solve(input.clone())
}
/// Same as [`part1`], except with a few lines added to the input.
pub fn part2(input: &State) -> Cost {
let mut modified = input.clone();
modified.rooms[0].insert(1, Amphipod::D);
modified.rooms[0].insert(2, Amphipod::D);
modified.rooms[1].insert(1, Amphipod::B);
modified.rooms[1].insert(2, Amphipod::C);
modified.rooms[2].insert(1, Amphipod::A);
modified.rooms[2].insert(2, Amphipod::B);
modified.rooms[3].insert(1, Amphipod::C);
modified.rooms[3].insert(2, Amphipod::A);
solve(modified)
}
}
/// The instruction stream is [`day24::MODEL_DIGITS`] similar sequences (each differing by the arguments
/// to three instructions) implementing rudimentary push/pop with some simple operations on the
/// inputs.
///
/// The differing instructions are `div z [1|26]`, `add x N`, and `add y N`.
///
/// We have two real pathways to handle: push and pop.
///
/// In the push pathway, we don't care about `add x`, since the result always be out of range
/// of the succeeding input check, so the instruction stream is effectively doing `push(input +
/// add y)`.
///
/// In the pop pathway, `add x` serves as a useful check against the input, and if it matches
/// we skip the next push instruction entirely. Since we only do one pop instruction per
/// instruction stream, we know that we'll only be successful if `input = pop() + add x`.
///
/// This means we can pair our push/pop instructions and find the max values that'll fit
/// into a single digit (where `push.input + push.arg == pop.input - pop.arg`) by first
/// maximizing for push.input (further left in the model number), then solving for pop.input.
pub mod day24 {
use arrayvec::ArrayVec;
use itertools::Itertools;
/// Models are always 14 digit numbers.
pub const MODEL_DIGITS: usize = 14;
/// Instruction arguments can be negative.
pub type Arg = i32;
/// Each integer being parsed consists of 18 lines of very similar code.
/// We'll cheat and only represent it with the pieces that are different.
///
/// Part of this instruction stream is implementing push/pop via an integer and
/// multiplication/division, and we only care about one piece of data in each case.
#[derive(Debug, Copy, Clone)]
pub enum StackOp {
Push(Arg),
Pop(Arg),
}
/// Gross parsing code that makes some strong assumptions about the instruction format in order
/// to only look at the lines that matter.
pub fn generator(input: &str) -> Vec<StackOp> {
const INTEGER_INSTRUCTIONS: usize = 18;
fn instruction_arg(instruction: Option<&str>) -> Arg {
let ins = instruction
.expect("Must be INTEGER_INSTRUCTIONS per integer")
.trim_end();
ins[ins
.rfind(' ')
.expect("Must be an argument to the instruction")
+ 1..]
.parse()
.expect("Argument must be an integer")
}
input
.lines()
.into_iter()
.chunks(INTEGER_INSTRUCTIONS)
.into_iter()
.map(|mut ins| {
if instruction_arg(ins.by_ref().nth(4)) == 1 {
StackOp::Push(instruction_arg(ins.nth(10)))
} else {
StackOp::Pop(instruction_arg(ins.next()))
}
})
.collect()
}
/// Directly interpret the stack machine.
pub fn interpret_stack<const MAXIMIZE: bool>(ops: &[StackOp]) -> usize {
let mut stack = ArrayVec::<_, MODEL_DIGITS>::new();
let mut result = [0; MODEL_DIGITS];
for (i, ins) in ops.iter().enumerate() {
match ins {
StackOp::Push(y_add) => stack.push((i, y_add)),
StackOp::Pop(x_add) => {
let (push_i, y_add) = stack
.pop()
.expect("Must've been a push operation for the model to succeed");
// Can't push something that'll make push.y_add + pop.x_add take up more than
// one digit.
assert!(x_add + y_add <= 9, "Input needs to be negative");
let push_max = if MAXIMIZE {
std::cmp::min(9, 9 - (y_add + x_add))
} else {
// 1 is the minimum digit for some reason
std::cmp::max(1, 1 - (y_add + x_add))
};
let pop_max = push_max + y_add + x_add;
result[push_i] = push_max;
result[i] = pop_max;
}
}
}
result
.into_iter()
.fold(0, |acc, digit| 10 * acc + digit as usize)
}
/// Calls to [`interpret_stack`].
pub fn part1(ops: &[StackOp]) -> usize {
interpret_stack::<true>(ops)
}
/// Calls to [`interpret_stack`].
pub fn part2(ops: &[StackOp]) -> usize {
interpret_stack::<false>(ops)
}
}
/// Another slight twist on Conway's game of life.
pub mod day25 {
use arrayvec::ArrayVec;
/// Cucumbers either have a direction or don't exist.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Cucumber {
Vacant,
East,
South,
}
/// Width of the grid
pub const WIDTH: usize = 139;
/// Height of the grid
pub const HEIGHT: usize = 137;
/// Represent the set of cucumbers in a fixed 2D array, since we want the compiler to optimize
/// copying and overwriting grids.
pub type CucumberGrid = [[Cucumber; WIDTH]; HEIGHT];
/// A naive parsing approach.
pub fn generator(input: &str) -> CucumberGrid {
input
.lines()
.map(|line| {
line.trim_end()
.bytes()
.map(|b| match b {
b'v' => Cucumber::South,
b'>' => Cucumber::East,
_ => Cucumber::Vacant,
})
.collect::<ArrayVec<_, WIDTH>>()
.as_slice()
.try_into()
.expect("Grid is WIDTH wide")
})
.collect::<ArrayVec<_, HEIGHT>>()
.as_slice()
.try_into()
.expect("Grid is HEIGHT high")
}
/// Run the migration code east then south, until no cucumbers were moved.
/// While it may seem like we can repeatedly modify the initial grid, this runs into trouble
/// when cucumbers wrap around, so it's easiest to modify a copy during each iteration.
pub fn part1(input: &CucumberGrid) -> usize {
let mut grid = *input;
for steps in 1.. {
let mut moved = grid;
let mut shuffled = false;
// Shuffle east
for y in 0..HEIGHT {
for x in 0..WIDTH {
if grid[y][x] == Cucumber::East {
let east = (x + 1) % WIDTH;
if grid[y][east] == Cucumber::Vacant {
// Free spot for us, shuffle over.
moved[y][x] = Cucumber::Vacant;
moved[y][east] = Cucumber::East;
shuffled = true;
}
}
}
}
// We've updated the grid for the east step, so need to use that information when we
// move south.
grid = moved;
// Shuffle south
for y in 0..HEIGHT {
for x in 0..WIDTH {
if grid[y][x] == Cucumber::South {
let south = (y + 1) % HEIGHT;
if grid[south][x] == Cucumber::Vacant {
// Free spot for us, shuffle over.
moved[y][x] = Cucumber::Vacant;
moved[south][x] = Cucumber::South;
shuffled = true;
}
}
}
}
if !shuffled {
// We didn't move at all this step.
return steps;
}
grid = moved;
}
unreachable!()
}
/// There is no second part!
pub fn part2(_: &CucumberGrid) -> &'static str {
"merry christmas!"
}
}
/// Benchmark results and information about the systems I benchmarked on.
///
/// Timing is done by calling [`std::time::Instant::now`] before and after each function runs, so
/// there's a little bit of extra overhead.
///
/// These results aren't scientific - each puzzle is run once, in order, on a varyingly loaded
/// desktop system. However, they should be generally reproduceable.
pub mod benchmarks {
/// My standard desktop and development system.
pub struct I6700K {}
/// An original M1 Macbook Pro.
pub struct M1Mac {}
/// A high-end desktop system based on an AMD 5950X.
pub struct AMD5950X {}
/// A placeholder trait to show information about benchmarked systems.
pub trait SystemInfo {}
/// A placeholder trait to show benchmark results for systems.
pub trait BenchmarkResult {}
/// Intel Core i7-6700K
/// - 128KB L1D$
/// - 1 MB L2$
/// - DDR4-3200MHz CL16-16-16-36
/// - Hyper-threading enabled
/// - 4.00GHz base clock
impl SystemInfo for I6700K {}
/// AMD Ryzen 9 5950X 16-Core Processor
/// - 512KB L1D$
/// - 8 MB L2$
/// - 64 MB L3$
/// - DDR4-3600MHz CL16-19-19-39
/// - Hyper-threading enabled
/// - 3.4GHz base clock
impl SystemInfo for AMD5950X {}
/// Macbook Pro M1 2020
/// - 128KB L1D$
/// - 12 MB L2$
/// - Unknown LPDDR4
/// - No hyperthreading
/// - 3.2GHz base clock
impl SystemInfo for M1Mac {}
/// ```text
/// Day 1 (35.85µs)
/// · Generator (34.62µs)
/// · Part 1 (227.00ns) .............. 1475
/// · Part 2 (996.00ns) .............. 1515
///
/// Day 2 (30.49µs)
/// · Generator (20.08µs)
/// · Part 1 (5.07µs) ................ 1604850
/// · Part 2 (5.35µs) ................ 1685186100
///
/// Day 3 (49.95µs)
/// · Generator (21.12µs)
/// · Part 1 (4.99µs) ................ 2498354
/// · Part 2 (23.83µs) ............... 3277956
///
/// Day 4 (171.21µs)
/// · Generator (46.96µs)
/// · Part 1 (29.52µs) ............... 23177
/// · Part 2 (94.74µs) ............... 6804
///
/// Day 5 (1.44ms)
/// · Generator (41.57µs)
/// · Part 1 (854.33µs) .............. 7644
/// · Part 2 (543.85µs) .............. 18627
///
/// Day 6 (3.39µs)
/// · Generator (3.28µs)
/// · Part 1 (31.00ns) ............... 394994
/// · Part 2 (81.00ns) ............... 1765974267455
///
/// Day 7 (56.37µs)
/// · Generator (50.52µs)
/// · Part 1 (283.00ns) .............. 343441
/// · Part 2 (5.56µs) ................ 98925151
///
/// Day 8 (126.20µs)
/// · Generator (116.04µs)
/// · Part 1 (1.99µs) ................ 495
/// · Part 2 (8.17µs) ................ 1055164
///
/// Day 9 (369.12µs)
/// · Generator (10.55µs)
/// · Part 1 (79.46µs) ............... 577
/// · Part 2 (279.11µs) .............. 1069200
///
/// Day 10 (145.22µs)
/// · Generator (5.31µs)
/// · Part 1 (69.80µs) ............... 290691
/// · Part 2 (70.12µs) ............... 2768166558
///
/// Day 11 (322.08µs)
/// · Generator (761.00ns)
/// · Part 1 (58.02µs) ............... 1588
/// · Part 2 (263.31µs) .............. 517
///
/// Day 12 (2.22ms)
/// · Generator (4.17µs)
/// · Part 1 (100.17µs) .............. 5457
/// · Part 2 (2.12ms) ................ 128506
///
/// Day 13 (130.95µs)
/// · Generator (60.56µs)
/// · Part 1 (14.27µs) ............... 847
/// · Part 2 (56.12µs) ............... BCZRCEAB
///
/// Day 14 (16.25µs)
/// · Generator (8.54µs)
/// · Part 1 (2.37µs) ................ 3230
/// · Part 2 (5.34µs) ................ 3542388214529
///
/// Day 15 (12.13ms)
/// · Generator (8.18µs)
/// · Part 1 (431.07µs) .............. 537
/// · Part 2 (11.69ms) ............... 2881
///
/// Day 16 (23.98µs)
/// · Generator (8.70µs)
/// · Part 1 (7.16µs) ................ 1007
/// · Part 2 (8.12µs) ................ 834151779165
///
/// Day 17 (32.88µs)
/// · Generator (1.24µs)
/// · Part 1 (30.00ns) ............... 13041
/// · Part 2 (31.61µs) ............... 1031
///
/// Day 18 (356.70µs)
/// · Generator (16.40µs)
/// · Part 1 (236.41µs) .............. 4480
/// · Part 2 (103.89µs) .............. 4676
///
/// Day 19 (3.40ms)
/// · Generator (474.76µs)
/// · Part 1 (1.46ms) ................ 472
/// · Part 2 (1.46ms) ................ 12092
///
/// Day 20 (2.70ms)
/// · Generator (10.29µs)
/// · Part 1 (48.71µs) ............... 4917
/// · Part 2 (2.64ms) ................ 16389
///
/// Day 21 (538.75µs)
/// · Generator (204.00ns)
/// · Part 1 (1.10µs) ................ 513936
/// · Part 2 (537.45µs) .............. 105619718613031
///
/// Day 22 (2.24ms)
/// · Generator (106.17µs)
/// · Part 1 (28.67µs) ............... 527915
/// · Part 2 (2.10ms) ................ 1218645427221987
///
/// Day 23 (42.24ms)
/// · Generator (643.00ns)
/// · Part 1 (3.51ms) ................ 15338
/// · Part 2 (38.73ms) ............... 47064
///
/// Day 24 (9.05µs)
/// · Generator (8.72µs)
/// · Part 1 (157.00ns) .............. 99919765949498
/// · Part 2 (172.00ns) .............. 24913111616151
///
/// Day 25 (22.42ms)
/// · Generator (22.47µs)
/// · Part 1 (22.39ms) ............... 305
/// · Part 2 (29.00ns) ............... merry christmas!
///
/// Overall runtime (91.73ms)
/// ```
impl BenchmarkResult for I6700K {}
/// ```text
/// Day 1 (22.78µs)
/// · Generator (21.62µs)
/// · Part 1 (280.00ns) .............. 1475
/// · Part 2 (880.00ns) .............. 1515
///
/// Day 2 (21.56µs)
/// · Generator (14.27µs)
/// · Part 1 (3.65µs) ................ 1604850
/// · Part 2 (3.64µs) ................ 1685186100
///
/// Day 3 (39.94µs)
/// · Generator (15.98µs)
/// · Part 1 (2.98µs) ................ 2498354
/// · Part 2 (20.98µs) ............... 3277956
///
/// Day 4 (124.79µs)
/// · Generator (28.55µs)
/// · Part 1 (22.12µs) ............... 23177
/// · Part 2 (74.12µs) ............... 6804
///
/// Day 5 (1.18ms)
/// · Generator (28.43µs)
/// · Part 1 (671.87µs) .............. 7644
/// · Part 2 (475.67µs) .............. 18627
///
/// Day 6 (2.08µs)
/// · Generator (2.00µs)
/// · Part 1 (20.00ns) ............... 394994
/// · Part 2 (60.00ns) ............... 1765974267455
///
/// Day 7 (36.47µs)
/// · Generator (32.81µs)
/// · Part 1 (200.00ns) .............. 343441
/// · Part 2 (3.46µs) ................ 98925151
///
/// Day 8 (87.72µs)
/// · Generator (81.29µs)
/// · Part 1 (830.00ns) .............. 495
/// · Part 2 (5.60µs) ................ 1055164
///
/// Day 9 (267.03µs)
/// · Generator (6.41µs)
/// · Part 1 (51.70µs) ............... 577
/// · Part 2 (208.92µs) .............. 1069200
///
/// Day 10 (93.28µs)
/// · Generator (5.44µs)
/// · Part 1 (42.43µs) ............... 290691
/// · Part 2 (45.41µs) ............... 2768166558
///
/// Day 11 (281.88µs)
/// · Generator (440.00ns)
/// · Part 1 (48.18µs) ............... 1588
/// · Part 2 (233.26µs) .............. 517
///
/// Day 12 (1.72ms)
/// · Generator (2.78µs)
/// · Part 1 (74.48µs) ............... 5457
/// · Part 2 (1.65ms) ................ 128506
///
/// Day 13 (102.18µs)
/// · Generator (47.02µs)
/// · Part 1 (12.09µs) ............... 847
/// · Part 2 (43.07µs) ............... BCZRCEAB
///
/// Day 14 (11.25µs)
/// · Generator (6.24µs)
/// · Part 1 (1.55µs) ................ 3230
/// · Part 2 (3.46µs) ................ 3542388214529
///
/// Day 15 (8.38ms)
/// · Generator (5.14µs)
/// · Part 1 (309.87µs) .............. 537
/// · Part 2 (8.07ms) ................ 2881
///
/// Day 16 (14.29µs)
/// · Generator (4.75µs)
/// · Part 1 (4.38µs) ................ 1007
/// · Part 2 (5.16µs) ................ 834151779165
///
/// Day 17 (18.23µs)
/// · Generator (770.00ns)
/// · Part 1 (30.00ns) ............... 13041
/// · Part 2 (17.43µs) ............... 1031
///
/// Day 18 (246.76µs)
/// · Generator (9.80µs)
/// · Part 1 (162.87µs) .............. 4480
/// · Part 2 (74.09µs) ............... 4676
///
/// Day 19 (2.71ms)
/// · Generator (428.78µs)
/// · Part 1 (1.16ms) ................ 472
/// · Part 2 (1.12ms) ................ 12092
///
/// Day 20 (1.25ms)
/// · Generator (6.89µs)
/// · Part 1 (31.56µs) ............... 4917
/// · Part 2 (1.21ms) ................ 16389
///
/// Day 21 (334.75µs)
/// · Generator (120.00ns)
/// · Part 1 (990.00ns) .............. 513936
/// · Part 2 (333.64µs) .............. 105619718613031
///
/// Day 22 (1.36ms)
/// · Generator (78.25µs)
/// · Part 1 (18.96µs) ............... 527915
/// · Part 2 (1.26ms) ................ 1218645427221987
///
/// Day 23 (31.25ms)
/// · Generator (290.00ns)
/// · Part 1 (2.79ms) ................ 15338
/// · Part 2 (28.45ms) ............... 47064
///
/// Day 24 (5.67µs)
/// · Generator (5.45µs)
/// · Part 1 (110.00ns) .............. 99919765949498
/// · Part 2 (110.00ns) .............. 24913111616151
///
/// Day 25 (13.90ms)
/// · Generator (16.77µs)
/// · Part 1 (13.88ms) ............... 305
/// · Part 2 (20.00ns) ............... merry christmas!
///
/// Overall runtime (63.70ms)
/// ```
impl BenchmarkResult for AMD5950X {}
///```text
/// Day 1 (29.37µs)
/// · Generator (28.00µs)
/// · Part 1 (166.00ns) .............. 1475
/// · Part 2 (1.21µs) ................ 1515
///
/// Day 2 (28.00µs)
/// · Generator (19.46µs)
/// · Part 1 (4.25µs) ................ 1604850
/// · Part 2 (4.29µs) ................ 1685186100
///
/// Day 3 (46.17µs)
/// · Generator (19.75µs)
/// · Part 1 (3.50µs) ................ 2498354
/// · Part 2 (22.92µs) ............... 3277956
///
/// Day 4 (152.25µs)
/// · Generator (38.21µs)
/// · Part 1 (25.88µs) ............... 23177
/// · Part 2 (88.17µs) ............... 6804
///
/// Day 5 (909.92µs)
/// · Generator (33.50µs)
/// · Part 1 (467.17µs) .............. 7644
/// · Part 2 (409.25µs) .............. 18627
///
/// Day 6 (2.67µs)
/// · Generator (2.42µs)
/// · Part 1 (0.00ns) ................ 394994
/// · Part 2 (250.00ns) .............. 1765974267455
///
/// Day 7 (51.25µs)
/// · Generator (46.13µs)
/// · Part 1 (291.00ns) .............. 343441
/// · Part 2 (4.83µs) ................ 98925151
///
/// Day 8 (112.92µs)
/// · Generator (104.96µs)
/// · Part 1 (541.00ns) .............. 495
/// · Part 2 (7.42µs) ................ 1055164
///
/// Day 9 (338.46µs)
/// · Generator (7.79µs)
/// · Part 1 (60.79µs) ............... 577
/// · Part 2 (269.88µs) .............. 1069200
///
/// Day 10 (118.21µs)
/// · Generator (4.92µs)
/// · Part 1 (55.13µs) ............... 290691
/// · Part 2 (58.17µs) ............... 2768166558
///
/// Day 11 (242.25µs)
/// · Generator (625.00ns)
/// · Part 1 (46.04µs) ............... 1588
/// · Part 2 (195.58µs) .............. 517
///
/// Day 12 (1.78ms)
/// · Generator (3.33µs)
/// · Part 1 (83.21µs) ............... 5457
/// · Part 2 (1.69ms) ................ 128506
///
/// Day 13 (84.50µs)
/// · Generator (53.63µs)
/// · Part 1 (9.67µs) ................ 847
/// · Part 2 (21.21µs) ............... BCZRCEAB
///
/// Day 14 (15.21µs)
/// · Generator (8.58µs)
/// · Part 1 (2.08µs) ................ 3230
/// · Part 2 (4.54µs) ................ 3542388214529
///
/// Day 15 (10.75ms)
/// · Generator (7.29µs)
/// · Part 1 (388.88µs) .............. 537
/// · Part 2 (10.35ms) ............... 2881
///
/// Day 16 (30.21µs)
/// · Generator (6.21µs)
/// · Part 1 (5.17µs) ................ 1007
/// · Part 2 (18.83µs) ............... 834151779165
///
/// Day 17 (28.42µs)
/// · Generator (1.46µs)
/// · Part 1 (0.00ns) ................ 13041
/// · Part 2 (26.96µs) ............... 1031
///
/// Day 18 (357.12µs)
/// · Generator (17.04µs)
/// · Part 1 (234.54µs) .............. 4480
/// · Part 2 (105.54µs) .............. 4676
///
/// Day 19 (2.96ms)
/// · Generator (348.13µs)
/// · Part 1 (1.34ms) ................ 472
/// · Part 2 (1.28ms) ................ 12092
///
/// Day 20 (1.56ms)
/// · Generator (10.17µs)
/// · Part 1 (41.58µs) ............... 4917
/// · Part 2 (1.51ms) ................ 16389
///
/// Day 21 (356.62µs)
/// · Generator (291.00ns)
/// · Part 1 (750.00ns) .............. 513936
/// · Part 2 (355.58µs) .............. 105619718613031
///
/// Day 22 (2.02ms)
/// · Generator (81.29µs)
/// · Part 1 (23.00µs) ............... 527915
/// · Part 2 (1.92ms) ................ 1218645427221987
///
/// Day 23 (34.67ms)
/// · Generator (708.00ns)
/// · Part 1 (3.43ms) ................ 15338
/// · Part 2 (31.25ms) ............... 47064
///
/// Day 24 (7.00µs)
/// · Generator (6.75µs)
/// · Part 1 (83.00ns) ............... 99919765949498
/// · Part 2 (166.00ns) .............. 24913111616151
///
/// Day 25 (19.47ms)
/// · Generator (18.00µs)
/// · Part 1 (19.45ms) ............... 305
/// · Part 2 (0.00ns) ................ merry christmas!
///
/// Overall runtime (76.63ms)
/// ```
impl BenchmarkResult for M1Mac {}
}