From a454b7e15a5ddeade7e83add996aa2a1516b1dfd Mon Sep 17 00:00:00 2001 From: Thibaud Date: Tue, 3 Dec 2024 15:28:23 +0100 Subject: [PATCH] import 2021 --- 2021/day1/day1.py | 44 + 2021/day1/input.txt | 2000 +++++++++++++++++++++++++++++++++++++++++++ 2021/day2/day2.py | 42 + 2021/day2/input.txt | 1000 ++++++++++++++++++++++ 2021/day3/day3.py | 67 ++ 2021/day3/input.txt | 1000 ++++++++++++++++++++++ 2021/day4/day4.py | 97 +++ 2021/day4/input.txt | 601 +++++++++++++ 2021/day5/day5.py | 41 + 2021/day5/input.txt | 500 +++++++++++ 2021/day6/day6.py | 26 + 2021/day6/day6_2.py | 34 + 2021/day6/input.txt | 1 + 2021/day7/day7.py | 33 + 2021/day7/input.txt | 1 + 2021/day8/day8.py | 46 + 2021/day8/input.txt | 1 + 17 files changed, 5534 insertions(+) create mode 100644 2021/day1/day1.py create mode 100644 2021/day1/input.txt create mode 100644 2021/day2/day2.py create mode 100644 2021/day2/input.txt create mode 100644 2021/day3/day3.py create mode 100644 2021/day3/input.txt create mode 100644 2021/day4/day4.py create mode 100644 2021/day4/input.txt create mode 100644 2021/day5/day5.py create mode 100644 2021/day5/input.txt create mode 100644 2021/day6/day6.py create mode 100644 2021/day6/day6_2.py create mode 100644 2021/day6/input.txt create mode 100644 2021/day7/day7.py create mode 100644 2021/day7/input.txt create mode 100644 2021/day8/day8.py create mode 100644 2021/day8/input.txt diff --git a/2021/day1/day1.py b/2021/day1/day1.py new file mode 100644 index 0000000..adef306 --- /dev/null +++ b/2021/day1/day1.py @@ -0,0 +1,44 @@ +from itertools import islice + + +def window(seq, n=3): + "Returns a sliding window (of width n) over data from the iterable" + " s -> (s0,s1,...s[n-1]), (s1,s2,...,sn), ... " + it = iter(seq) + result = tuple(islice(it, n)) + if len(result) == n: + yield result + for elem in it: + result = result[1:] + (elem,) + yield result + + +def part1(infile): + with open(infile) as f: + lines = f.readlines() + previous = int(lines[0]) + i = 0 + for line in lines[1:]: + if int(line) > previous: + i += 1 + previous = int(line) + print("Part 1 ", i) + + + +def part2(infile): + with open(infile) as f: + lines = f.readlines() + previous = None + i = 0 + for w in window(lines): + measure = sum(int(x) for x in w) + if previous is not None and measure > previous: + i += 1 + previous = measure + print("Part 2 ", i) + + +if __name__ == "__main__": + part1("./input.txt") + part2("./input.txt") diff --git a/2021/day1/input.txt b/2021/day1/input.txt new file mode 100644 index 0000000..84db578 --- /dev/null +++ b/2021/day1/input.txt @@ -0,0 +1,2000 @@ +157 +158 +167 +157 +148 +154 +155 +156 +151 +126 +125 +119 +128 +136 +135 +144 +159 +157 +164 +163 +165 +166 +163 +186 +194 +197 +188 +203 +225 +228 +248 +263 +253 +280 +279 +277 +281 +282 +286 +290 +292 +275 +274 +282 +293 +301 +307 +305 +300 +296 +301 +310 +302 +299 +300 +308 +306 +327 +329 +323 +320 +324 +327 +340 +336 +343 +344 +345 +346 +347 +365 +368 +372 +373 +372 +371 +380 +379 +357 +380 +382 +392 +400 +375 +376 +374 +373 +376 +394 +407 +408 +406 +407 +405 +406 +405 +415 +399 +409 +405 +406 +416 +422 +423 +430 +435 +437 +438 +406 +413 +414 +413 +429 +430 +459 +454 +442 +451 +473 +459 +433 +434 +436 +438 +441 +448 +456 +460 +461 +470 +469 +481 +504 +502 +497 +499 +498 +501 +510 +516 +529 +531 +533 +528 +531 +529 +542 +543 +546 +557 +569 +571 +572 +567 +564 +570 +563 +581 +580 +581 +574 +576 +600 +596 +599 +603 +593 +591 +592 +591 +590 +593 +598 +572 +564 +566 +568 +595 +617 +618 +617 +616 +617 +600 +634 +616 +630 +595 +576 +549 +548 +560 +574 +580 +586 +575 +578 +580 +581 +582 +585 +595 +599 +601 +598 +603 +604 +602 +607 +630 +643 +648 +649 +650 +651 +650 +657 +658 +681 +683 +686 +681 +682 +684 +686 +670 +669 +670 +665 +671 +695 +683 +709 +708 +717 +714 +720 +711 +715 +716 +717 +719 +721 +727 +728 +729 +707 +706 +684 +682 +689 +691 +692 +703 +715 +716 +710 +716 +719 +720 +712 +722 +723 +731 +732 +741 +747 +723 +751 +748 +759 +766 +768 +767 +749 +750 +753 +754 +778 +786 +788 +794 +815 +830 +832 +839 +849 +848 +837 +836 +846 +831 +813 +814 +798 +814 +827 +822 +830 +831 +832 +841 +848 +851 +847 +845 +847 +846 +831 +813 +816 +809 +815 +821 +823 +822 +831 +834 +862 +863 +866 +868 +872 +873 +859 +852 +853 +863 +862 +869 +883 +887 +888 +903 +934 +932 +931 +932 +922 +933 +937 +936 +934 +939 +948 +949 +951 +949 +968 +974 +963 +954 +940 +955 +956 +971 +970 +966 +965 +973 +1000 +998 +1006 +1020 +1035 +1036 +1043 +1052 +1054 +1058 +1056 +1055 +1054 +1055 +1067 +1045 +1051 +1050 +1046 +1045 +1043 +1044 +1041 +1044 +1030 +1034 +1037 +1036 +1030 +1037 +1027 +1026 +1022 +1024 +1014 +1016 +1018 +990 +992 +991 +992 +997 +1023 +1024 +1016 +1018 +1027 +1024 +1026 +1006 +1000 +999 +1004 +1005 +1006 +1021 +1022 +1024 +1023 +1021 +1029 +1047 +1062 +1036 +1039 +1053 +1058 +1062 +1051 +1049 +1060 +1069 +1071 +1072 +1063 +1071 +1076 +1082 +1084 +1071 +1080 +1082 +1083 +1084 +1100 +1096 +1095 +1107 +1122 +1120 +1128 +1127 +1109 +1123 +1122 +1125 +1111 +1112 +1109 +1114 +1103 +1104 +1106 +1107 +1110 +1104 +1083 +1086 +1083 +1060 +1061 +1070 +1051 +1016 +1013 +1012 +1013 +1008 +1021 +1022 +1017 +1018 +1012 +1010 +1014 +1007 +1004 +1015 +1018 +1022 +1016 +1040 +1039 +1055 +1057 +1054 +1067 +1066 +1074 +1075 +1079 +1086 +1087 +1074 +1075 +1076 +1084 +1070 +1076 +1075 +1072 +1081 +1097 +1098 +1095 +1077 +1055 +1056 +1058 +1074 +1075 +1074 +1084 +1085 +1099 +1092 +1093 +1094 +1053 +1049 +1068 +1069 +1067 +1069 +1081 +1092 +1104 +1121 +1124 +1125 +1123 +1106 +1143 +1133 +1134 +1116 +1134 +1136 +1137 +1139 +1138 +1142 +1143 +1155 +1156 +1157 +1146 +1156 +1157 +1180 +1200 +1189 +1188 +1187 +1189 +1198 +1203 +1204 +1198 +1219 +1213 +1215 +1220 +1222 +1223 +1224 +1227 +1236 +1232 +1233 +1236 +1238 +1239 +1244 +1245 +1246 +1211 +1213 +1217 +1222 +1218 +1219 +1227 +1228 +1242 +1244 +1243 +1240 +1245 +1244 +1252 +1244 +1242 +1243 +1242 +1252 +1261 +1269 +1284 +1290 +1291 +1292 +1293 +1290 +1292 +1293 +1294 +1298 +1303 +1304 +1308 +1294 +1291 +1292 +1289 +1274 +1281 +1282 +1304 +1305 +1303 +1304 +1285 +1286 +1281 +1284 +1292 +1285 +1281 +1282 +1279 +1287 +1298 +1319 +1311 +1309 +1342 +1325 +1334 +1331 +1340 +1341 +1342 +1343 +1344 +1345 +1332 +1333 +1338 +1340 +1375 +1331 +1357 +1351 +1366 +1362 +1360 +1336 +1334 +1335 +1336 +1333 +1336 +1330 +1335 +1334 +1328 +1329 +1323 +1337 +1338 +1333 +1322 +1326 +1323 +1324 +1323 +1332 +1336 +1341 +1332 +1367 +1370 +1371 +1372 +1385 +1391 +1414 +1399 +1400 +1398 +1399 +1388 +1386 +1384 +1383 +1412 +1416 +1417 +1416 +1419 +1407 +1414 +1424 +1423 +1417 +1439 +1451 +1450 +1455 +1453 +1456 +1449 +1464 +1467 +1468 +1473 +1470 +1472 +1473 +1474 +1470 +1456 +1472 +1452 +1447 +1469 +1471 +1452 +1453 +1454 +1461 +1462 +1463 +1478 +1482 +1477 +1476 +1482 +1483 +1489 +1490 +1484 +1485 +1484 +1486 +1469 +1476 +1479 +1478 +1480 +1479 +1469 +1467 +1477 +1495 +1494 +1480 +1474 +1475 +1483 +1497 +1499 +1487 +1480 +1481 +1479 +1512 +1514 +1516 +1519 +1521 +1513 +1516 +1521 +1522 +1529 +1521 +1524 +1526 +1525 +1526 +1527 +1526 +1527 +1529 +1535 +1547 +1548 +1552 +1555 +1559 +1561 +1571 +1572 +1571 +1573 +1577 +1580 +1579 +1580 +1581 +1561 +1565 +1567 +1562 +1573 +1584 +1586 +1578 +1581 +1583 +1582 +1573 +1596 +1597 +1598 +1609 +1615 +1620 +1610 +1616 +1607 +1609 +1593 +1598 +1599 +1603 +1604 +1597 +1602 +1615 +1618 +1627 +1625 +1619 +1618 +1614 +1617 +1616 +1610 +1630 +1639 +1642 +1646 +1648 +1645 +1656 +1658 +1652 +1644 +1645 +1657 +1656 +1690 +1691 +1692 +1701 +1717 +1715 +1718 +1712 +1711 +1710 +1709 +1715 +1712 +1723 +1722 +1725 +1720 +1711 +1710 +1713 +1717 +1718 +1730 +1722 +1700 +1715 +1740 +1743 +1742 +1740 +1743 +1749 +1752 +1751 +1754 +1762 +1775 +1776 +1808 +1805 +1806 +1802 +1800 +1802 +1812 +1809 +1810 +1806 +1807 +1826 +1823 +1826 +1828 +1824 +1830 +1839 +1842 +1814 +1815 +1818 +1824 +1825 +1826 +1859 +1834 +1837 +1841 +1849 +1865 +1878 +1875 +1876 +1888 +1889 +1906 +1908 +1911 +1912 +1916 +1920 +1929 +1933 +1923 +1920 +1931 +1926 +1927 +1917 +1914 +1912 +1904 +1910 +1911 +1902 +1899 +1894 +1886 +1889 +1885 +1887 +1898 +1899 +1893 +1881 +1905 +1904 +1930 +1931 +1928 +1926 +1923 +1889 +1849 +1839 +1841 +1842 +1835 +1836 +1808 +1801 +1800 +1793 +1798 +1804 +1823 +1808 +1809 +1820 +1828 +1831 +1819 +1820 +1824 +1817 +1819 +1815 +1810 +1802 +1819 +1835 +1838 +1829 +1824 +1822 +1829 +1837 +1867 +1870 +1869 +1876 +1878 +1873 +1874 +1884 +1882 +1859 +1844 +1840 +1860 +1844 +1852 +1849 +1860 +1861 +1864 +1855 +1854 +1850 +1855 +1859 +1849 +1853 +1871 +1870 +1843 +1839 +1844 +1848 +1854 +1839 +1841 +1862 +1863 +1869 +1870 +1872 +1877 +1879 +1878 +1873 +1877 +1885 +1884 +1883 +1889 +1897 +1896 +1902 +1899 +1900 +1897 +1891 +1895 +1908 +1911 +1917 +1927 +1926 +1929 +1935 +1937 +1935 +1932 +1934 +1936 +1932 +1929 +1932 +1941 +1960 +1953 +1956 +1954 +1966 +1967 +1994 +2002 +1994 +2002 +2016 +2020 +2014 +2001 +1964 +1972 +1973 +1972 +1968 +1969 +1968 +1966 +1953 +1954 +1963 +1962 +1963 +1964 +1960 +1949 +1950 +1954 +1957 +1958 +1960 +1964 +1995 +1997 +1984 +1983 +2005 +2010 +2026 +2034 +2035 +2036 +2034 +2037 +2040 +2044 +2052 +2054 +2081 +2083 +2085 +2086 +2088 +2090 +2089 +2092 +2086 +2088 +2091 +2087 +2090 +2093 +2085 +2086 +2096 +2125 +2132 +2142 +2165 +2169 +2170 +2167 +2168 +2169 +2168 +2158 +2162 +2171 +2169 +2165 +2166 +2164 +2178 +2194 +2190 +2189 +2202 +2205 +2207 +2226 +2227 +2226 +2210 +2209 +2208 +2231 +2232 +2246 +2251 +2250 +2254 +2250 +2249 +2245 +2246 +2247 +2267 +2264 +2259 +2287 +2263 +2274 +2277 +2237 +2242 +2256 +2277 +2278 +2292 +2293 +2306 +2323 +2326 +2300 +2296 +2297 +2318 +2320 +2322 +2326 +2324 +2325 +2331 +2355 +2357 +2380 +2375 +2378 +2391 +2395 +2403 +2404 +2409 +2410 +2405 +2406 +2401 +2404 +2406 +2424 +2418 +2405 +2404 +2401 +2403 +2409 +2401 +2385 +2386 +2389 +2385 +2388 +2404 +2405 +2412 +2414 +2430 +2429 +2403 +2402 +2407 +2408 +2410 +2399 +2374 +2403 +2416 +2431 +2433 +2434 +2438 +2439 +2453 +2455 +2456 +2457 +2464 +2465 +2464 +2465 +2469 +2464 +2472 +2458 +2446 +2448 +2447 +2457 +2481 +2470 +2479 +2502 +2521 +2538 +2527 +2522 +2528 +2516 +2519 +2517 +2530 +2538 +2537 +2544 +2545 +2547 +2572 +2555 +2550 +2551 +2552 +2553 +2555 +2558 +2559 +2558 +2560 +2546 +2540 +2546 +2545 +2548 +2554 +2556 +2557 +2553 +2581 +2608 +2617 +2613 +2604 +2643 +2650 +2651 +2646 +2647 +2648 +2653 +2642 +2644 +2645 +2642 +2639 +2661 +2656 +2655 +2653 +2654 +2655 +2643 +2660 +2662 +2659 +2658 +2659 +2660 +2661 +2642 +2652 +2650 +2657 +2650 +2644 +2620 +2636 +2635 +2636 +2637 +2611 +2608 +2594 +2603 +2605 +2604 +2605 +2602 +2573 +2562 +2559 +2560 +2548 +2558 +2562 +2566 +2567 +2569 +2574 +2601 +2611 +2613 +2629 +2618 +2607 +2609 +2611 +2613 +2617 +2629 +2618 +2625 +2631 +2637 +2642 +2633 +2622 +2623 +2617 +2607 +2617 +2615 +2616 +2617 +2622 +2635 +2637 +2639 +2640 +2643 +2644 +2645 +2647 +2641 +2640 +2658 +2657 +2656 +2661 +2663 +2670 +2672 +2678 +2685 +2686 +2683 +2684 +2687 +2689 +2691 +2703 +2708 +2714 +2718 +2717 +2720 +2740 +2741 +2749 +2732 +2738 +2744 +2749 +2750 +2747 +2738 +2733 +2760 +2747 +2748 +2749 +2750 +2759 +2767 +2771 +2778 +2776 +2774 +2758 +2750 +2751 +2753 +2755 +2766 +2774 +2778 +2772 +2776 +2775 +2777 +2774 +2794 +2795 +2796 +2799 +2800 +2797 +2827 +2824 +2825 +2830 +2841 +2839 +2838 +2840 +2835 +2836 +2853 +2852 +2869 +2877 +2890 +2892 +2889 +2894 +2904 +2901 +2907 +2911 +2915 +2924 +2932 +2933 +2912 +2928 +2931 +2939 +2941 +2940 +2941 +2931 +2930 +2926 +2897 +2899 +2887 +2879 +2899 +2898 +2903 +2908 +2911 +2910 +2927 +2930 +2923 +2933 +2945 +2961 +2971 +2972 +2973 +2974 +2973 +2987 +2989 +2988 +2992 +3003 +2995 +2996 +2983 +2984 +2989 +2998 +3007 +3006 +2991 +3023 +3013 +3004 +3006 +3017 +3021 +3033 +3031 +3034 +3018 +3017 +3019 +3017 +3023 +3046 +3059 +3066 +3063 +3060 +3058 +3061 +3062 +3070 +3078 +3082 +3087 +3090 +3118 +3117 +3125 +3127 +3133 +3138 +3154 +3146 +3147 +3141 +3149 +3157 +3160 +3166 +3174 +3187 +3188 +3200 +3203 +3218 +3221 +3220 +3248 +3249 +3244 +3241 +3242 +3240 +3214 +3213 +3230 +3229 +3227 +3223 +3219 +3210 +3224 +3220 +3240 +3232 +3229 +3218 +3224 +3242 +3241 +3244 +3267 +3281 +3286 +3287 +3303 +3304 +3314 +3318 +3307 +3292 +3331 +3332 +3334 +3339 +3348 +3356 +3374 +3405 +3406 +3408 +3411 +3407 +3399 +3402 +3403 +3405 +3395 +3394 +3393 +3394 +3382 +3388 +3374 +3376 +3371 +3373 +3403 +3404 +3395 +3401 +3418 +3419 +3420 +3423 +3424 +3412 +3401 +3402 +3401 +3413 +3419 +3421 +3391 +3384 +3392 +3393 +3401 +3404 +3407 +3408 +3410 +3426 +3427 +3431 +3430 +3448 +3449 +3451 +3458 +3463 +3443 +3442 +3432 +3423 +3437 +3439 +3419 +3426 +3427 +3426 +3425 +3426 +3428 +3429 +3436 +3435 +3443 +3433 +3448 +3446 +3449 +3445 +3439 +3447 +3440 +3456 +3443 +3467 +3478 +3473 +3487 +3486 +3484 +3494 +3492 +3489 +3497 +3500 +3488 +3487 +3488 +3491 +3487 +3511 +3520 +3515 +3512 +3504 +3509 +3502 +3505 +3508 +3511 +3512 +3513 +3515 +3516 +3519 +3524 +3519 +3520 +3518 +3529 +3535 +3539 +3551 +3560 +3559 +3567 +3571 +3569 +3570 +3556 +3559 +3538 +3541 +3542 +3548 +3535 +3533 +3539 +3549 +3545 +3546 +3541 +3527 +3528 +3564 +3565 +3568 +3571 +3569 +3574 +3578 +3583 +3585 +3601 +3598 +3620 +3621 +3638 +3621 +3623 +3602 +3605 +3600 +3601 +3602 +3582 +3584 +3574 +3570 +3572 +3563 +3564 +3565 +3557 +3556 +3557 +3558 +3555 +3560 +3565 +3564 +3552 +3560 +3559 +3589 +3592 +3590 +3605 +3617 +3599 +3600 +3608 +3589 +3593 +3600 +3598 +3596 +3597 +3598 +3610 +3611 +3631 +3637 +3660 +3661 +3673 +3680 +3681 +3680 +3676 +3677 +3678 +3664 +3656 +3655 +3640 +3637 +3635 +3636 +3639 +3621 +3608 +3592 +3606 +3605 +3601 +3603 +3620 +3619 +3617 +3609 +3612 +3610 +3599 +3606 +3602 +3605 +3604 +3606 +3615 +3617 +3619 +3620 +3626 +3627 +3626 +3625 +3640 +3653 +3625 +3617 +3620 +3623 +3633 +3627 +3598 +3607 +3606 +3611 +3610 +3609 +3611 +3612 +3620 +3621 +3602 +3593 +3594 +3593 +3594 +3602 +3603 +3610 +3596 +3600 +3604 +3601 +3604 +3605 +3582 +3579 +3568 +3569 +3576 +3584 +3586 +3566 +3563 +3571 +3581 +3586 +3601 +3584 +3574 +3585 +3574 +3584 +3581 +3585 +3576 +3578 +3593 +3618 +3623 +3624 +3628 +3636 +3638 +3643 +3648 +3641 +3664 +3662 +3675 +3690 +3697 +3698 +3695 +3696 +3694 +3685 +3686 +3687 +3684 +3693 +3687 +3704 +3710 +3711 +3715 +3710 +3705 +3692 +3704 +3709 +3724 +3725 +3729 +3722 +3723 +3712 +3716 +3717 +3741 +3714 +3700 +3716 +3710 +3711 +3733 +3734 +3737 +3728 +3729 +3739 +3743 +3746 +3756 +3763 +3794 +3798 \ No newline at end of file diff --git a/2021/day2/day2.py b/2021/day2/day2.py new file mode 100644 index 0000000..64021a6 --- /dev/null +++ b/2021/day2/day2.py @@ -0,0 +1,42 @@ +def part1(inp): + horizontal_pos = 0 + depth = 0 + for line in inp: + command, amount = line.split(" ") + if command == "forward": + horizontal_pos += int(amount) + elif command == "up": + depth -= int(amount) + elif command == "down": + depth += int(amount) + #print(f"horizontal {horizontal_pos}, depth {depth}") + print("Part 1", horizontal_pos * depth) + + +def part2(inp): + horizontal_pos = 0 + depth = 0 + aim = 0 + for line in inp: + command, amount = line.split(" ") + if command == "forward": + horizontal_pos += int(amount) + # It increases your depth by your aim multiplied by X. + depth += aim * int(amount) + elif command == "up": + aim -= int(amount) + elif command == "down": + aim += int(amount) + #print(f"horizontal {horizontal_pos}, depth {depth}") + print("Part 2", horizontal_pos * depth) + + +def main(input_file): + with open(input_file) as f: + entries = f.readlines() + part1(entries) + part2(entries) + + +if __name__ == "__main__": + main("input.txt") diff --git a/2021/day2/input.txt b/2021/day2/input.txt new file mode 100644 index 0000000..8aaa3e6 --- /dev/null +++ b/2021/day2/input.txt @@ -0,0 +1,1000 @@ +forward 4 +forward 6 +down 8 +forward 3 +forward 9 +down 7 +down 7 +down 1 +forward 1 +forward 5 +up 4 +forward 3 +forward 9 +forward 5 +down 8 +forward 6 +forward 8 +up 4 +forward 1 +forward 4 +up 3 +down 2 +down 2 +up 8 +forward 9 +down 5 +down 6 +down 4 +forward 1 +forward 7 +down 1 +forward 2 +down 7 +down 6 +forward 3 +forward 1 +down 7 +forward 3 +up 7 +up 1 +forward 6 +down 8 +down 4 +down 1 +down 7 +forward 9 +down 2 +forward 9 +up 4 +down 7 +forward 9 +forward 9 +up 4 +up 7 +up 2 +forward 1 +down 9 +forward 2 +forward 8 +forward 5 +down 9 +forward 5 +up 1 +forward 8 +down 6 +down 1 +down 1 +down 8 +down 6 +down 4 +down 6 +forward 7 +forward 5 +forward 4 +up 8 +up 4 +forward 6 +forward 9 +forward 6 +down 2 +up 8 +forward 3 +forward 8 +down 5 +forward 1 +down 6 +down 7 +up 4 +down 8 +forward 6 +up 4 +forward 7 +down 8 +down 8 +down 2 +down 8 +up 7 +forward 7 +forward 6 +down 2 +forward 9 +down 7 +down 4 +forward 1 +up 9 +down 6 +up 7 +down 6 +down 2 +down 2 +forward 3 +down 8 +forward 4 +down 7 +down 2 +up 8 +down 6 +forward 2 +forward 2 +down 6 +up 6 +down 7 +up 5 +up 4 +up 9 +forward 9 +forward 7 +down 4 +down 4 +up 5 +down 2 +forward 3 +down 9 +down 5 +forward 8 +up 3 +forward 7 +forward 9 +down 3 +forward 1 +up 1 +forward 5 +down 8 +down 2 +forward 3 +down 6 +down 8 +up 9 +down 9 +forward 4 +up 5 +forward 8 +forward 7 +forward 7 +down 3 +down 5 +forward 8 +down 1 +down 2 +forward 2 +up 9 +down 3 +down 9 +forward 2 +down 4 +up 9 +up 3 +down 1 +down 1 +forward 6 +down 5 +down 9 +forward 2 +up 5 +forward 3 +down 1 +down 7 +forward 1 +forward 1 +down 7 +up 3 +up 1 +up 6 +forward 9 +up 1 +down 8 +down 5 +up 6 +up 2 +down 8 +forward 3 +forward 1 +down 7 +up 1 +down 9 +down 9 +up 8 +forward 4 +up 8 +forward 8 +down 5 +forward 5 +forward 2 +forward 1 +forward 5 +down 8 +forward 6 +forward 8 +down 5 +forward 8 +up 1 +up 9 +up 7 +down 5 +down 9 +up 4 +down 7 +up 8 +up 3 +forward 6 +down 9 +forward 4 +down 4 +forward 2 +forward 3 +down 4 +down 5 +down 3 +forward 9 +forward 5 +forward 9 +forward 4 +down 5 +down 7 +down 5 +forward 8 +up 5 +down 2 +forward 3 +forward 4 +down 5 +up 8 +forward 5 +down 2 +up 4 +down 5 +down 2 +forward 1 +up 3 +down 6 +down 8 +down 3 +forward 1 +up 5 +forward 1 +down 3 +forward 4 +down 6 +forward 8 +forward 4 +forward 1 +down 8 +down 2 +forward 8 +down 5 +forward 2 +forward 2 +down 9 +forward 1 +forward 8 +up 1 +forward 1 +down 1 +down 7 +down 4 +up 4 +down 3 +forward 1 +forward 9 +down 9 +up 6 +up 8 +down 2 +down 2 +down 3 +forward 2 +forward 9 +down 1 +up 9 +down 3 +down 9 +down 1 +down 7 +forward 9 +forward 7 +down 5 +down 2 +down 9 +down 2 +down 7 +up 7 +down 2 +up 3 +up 5 +forward 8 +up 7 +forward 1 +down 9 +down 9 +down 1 +forward 6 +down 7 +up 4 +up 4 +down 9 +up 5 +up 8 +down 3 +down 5 +forward 6 +up 3 +down 8 +down 5 +forward 9 +up 6 +forward 9 +forward 5 +up 6 +up 9 +down 2 +up 5 +forward 9 +down 1 +up 1 +down 9 +forward 4 +forward 4 +forward 8 +down 5 +down 3 +down 7 +forward 5 +down 6 +forward 3 +down 5 +down 5 +up 5 +forward 8 +forward 1 +forward 2 +forward 6 +up 1 +down 5 +down 4 +up 5 +forward 3 +forward 2 +forward 2 +forward 2 +forward 7 +forward 8 +down 2 +up 8 +forward 4 +down 4 +forward 7 +down 6 +down 7 +forward 5 +down 5 +forward 1 +down 8 +forward 6 +down 1 +forward 3 +up 3 +down 7 +down 2 +up 4 +down 3 +up 2 +up 8 +down 2 +forward 5 +forward 3 +down 9 +down 9 +up 2 +forward 6 +forward 9 +down 1 +forward 6 +down 4 +up 2 +down 7 +down 3 +down 3 +forward 2 +down 5 +down 9 +down 7 +forward 7 +forward 9 +up 8 +down 8 +down 3 +up 5 +down 9 +forward 8 +forward 8 +down 1 +forward 5 +forward 2 +forward 7 +down 9 +down 7 +forward 6 +up 9 +forward 3 +forward 5 +up 7 +down 9 +forward 9 +forward 4 +forward 5 +forward 9 +forward 8 +forward 1 +forward 2 +forward 8 +down 7 +forward 3 +up 2 +up 7 +forward 1 +forward 3 +forward 9 +up 3 +down 2 +forward 3 +forward 6 +forward 3 +forward 3 +forward 3 +forward 1 +forward 1 +up 5 +down 5 +up 5 +down 5 +down 5 +forward 8 +forward 1 +down 4 +forward 7 +down 6 +down 1 +down 2 +down 2 +down 6 +up 8 +forward 3 +forward 2 +up 8 +up 2 +forward 1 +forward 6 +forward 5 +forward 6 +forward 7 +down 8 +forward 1 +down 4 +forward 2 +up 4 +forward 4 +down 1 +forward 5 +down 7 +forward 7 +up 7 +forward 1 +down 2 +forward 8 +forward 5 +up 8 +up 8 +up 2 +down 9 +forward 2 +down 4 +down 3 +down 5 +down 5 +down 2 +up 5 +forward 6 +up 7 +forward 8 +up 7 +down 4 +forward 1 +down 3 +forward 2 +forward 1 +down 2 +up 7 +forward 5 +up 8 +up 1 +down 4 +forward 6 +down 4 +up 9 +forward 5 +down 2 +down 7 +down 7 +forward 4 +forward 4 +forward 9 +down 1 +forward 6 +forward 1 +up 9 +forward 4 +forward 4 +forward 8 +forward 3 +forward 4 +down 3 +up 5 +up 1 +forward 3 +down 6 +down 4 +down 2 +forward 3 +forward 8 +up 6 +up 3 +forward 8 +down 3 +down 6 +forward 1 +up 7 +down 4 +down 5 +up 7 +forward 3 +up 4 +forward 9 +forward 6 +down 3 +forward 4 +down 6 +forward 1 +forward 6 +forward 4 +forward 2 +forward 1 +forward 3 +forward 1 +down 1 +down 9 +down 5 +down 7 +down 4 +down 8 +up 1 +down 6 +down 1 +forward 4 +down 9 +up 9 +down 6 +forward 6 +forward 8 +up 7 +forward 4 +down 3 +forward 9 +forward 6 +forward 8 +down 1 +up 2 +down 2 +down 8 +forward 4 +down 9 +down 3 +forward 5 +down 9 +down 4 +up 5 +down 8 +down 4 +down 9 +up 4 +down 5 +down 7 +down 3 +up 1 +up 1 +down 4 +down 6 +forward 8 +down 8 +down 6 +forward 6 +forward 9 +forward 3 +forward 3 +down 2 +down 4 +forward 3 +up 5 +up 3 +down 5 +down 1 +forward 5 +forward 7 +forward 1 +forward 4 +forward 5 +forward 1 +down 7 +down 8 +up 9 +down 8 +down 5 +up 3 +down 5 +down 5 +forward 8 +down 2 +forward 7 +forward 7 +down 1 +forward 2 +forward 7 +forward 5 +down 6 +forward 5 +down 5 +forward 4 +down 8 +forward 7 +up 5 +forward 5 +down 7 +down 7 +up 4 +forward 8 +up 1 +forward 3 +forward 7 +down 2 +forward 1 +down 4 +up 8 +forward 3 +forward 1 +forward 6 +forward 3 +up 4 +forward 3 +down 3 +forward 7 +forward 9 +forward 8 +down 6 +down 8 +up 6 +down 9 +forward 7 +forward 1 +up 4 +forward 5 +forward 8 +down 7 +down 9 +up 6 +up 6 +forward 9 +down 1 +forward 8 +down 9 +down 5 +forward 6 +forward 1 +down 4 +forward 8 +down 9 +down 4 +forward 5 +forward 7 +forward 3 +down 2 +forward 6 +forward 3 +forward 8 +down 1 +down 5 +up 6 +down 2 +down 1 +up 3 +down 7 +up 1 +forward 8 +down 6 +down 6 +forward 8 +up 3 +forward 8 +up 3 +forward 3 +forward 7 +forward 1 +down 1 +up 5 +forward 5 +forward 9 +forward 5 +forward 1 +forward 4 +down 8 +forward 2 +up 3 +forward 3 +down 9 +forward 2 +forward 6 +forward 3 +up 8 +up 1 +down 6 +forward 3 +down 4 +down 5 +forward 6 +forward 9 +down 4 +down 9 +down 7 +down 1 +up 3 +up 6 +forward 4 +forward 5 +down 1 +forward 3 +up 5 +forward 7 +down 9 +forward 5 +down 5 +down 1 +down 1 +down 1 +up 8 +down 4 +down 9 +forward 5 +down 5 +up 5 +up 3 +forward 1 +forward 7 +down 2 +forward 6 +forward 5 +up 4 +up 4 +forward 1 +down 7 +down 8 +up 3 +down 3 +up 4 +forward 2 +up 4 +up 4 +forward 8 +forward 1 +forward 2 +forward 7 +down 6 +forward 8 +forward 9 +forward 6 +forward 9 +down 3 +up 3 +forward 5 +down 1 +forward 1 +forward 4 +forward 2 +down 6 +up 7 +forward 9 +down 2 +up 5 +forward 6 +down 9 +down 6 +down 8 +forward 2 +down 7 +forward 6 +down 8 +forward 3 +forward 7 +forward 6 +down 7 +down 6 +forward 5 +up 8 +forward 6 +down 1 +up 9 +forward 6 +down 6 +down 5 +down 6 +up 2 +down 3 +down 7 +down 3 +forward 4 +up 9 +up 2 +forward 1 +forward 7 +forward 7 +forward 9 +forward 8 +forward 7 +down 3 +forward 4 +forward 8 +down 9 +forward 2 +forward 2 +up 9 +up 7 +forward 2 +down 8 +down 3 +down 1 +forward 1 +forward 3 +down 2 +forward 7 +up 4 +down 7 +down 1 +forward 8 +forward 2 +up 1 +down 5 +forward 8 +up 5 +up 7 +forward 4 +forward 7 +up 4 +up 3 +forward 5 +forward 9 +forward 1 +forward 3 +down 9 +up 2 +forward 8 +down 3 +forward 3 +forward 2 +down 8 +down 2 +down 3 +forward 1 +up 7 +down 1 +forward 3 +forward 8 +down 3 +down 9 +up 1 +down 9 +up 7 +up 7 +forward 7 +forward 7 +up 8 +down 2 +down 7 +down 1 +forward 5 +forward 5 +forward 7 +down 8 +forward 5 +down 9 +down 8 +forward 3 +up 9 +forward 3 +up 6 +forward 7 +up 7 +forward 3 +forward 2 +down 1 +down 4 +forward 1 +forward 8 +up 5 +down 5 +up 1 +down 5 +up 1 +up 6 +forward 5 +forward 5 +down 3 +down 3 +forward 2 +up 3 +up 2 +down 6 +down 5 +down 1 +down 1 +up 6 +forward 2 +forward 2 +up 4 +up 6 +up 6 +down 8 +up 2 +forward 4 +down 3 +forward 3 +up 4 +down 5 +forward 6 +forward 7 +up 6 +down 3 +down 7 +up 4 +down 2 +up 2 +forward 1 +down 8 +down 1 +down 1 +up 9 +down 1 +down 1 +up 1 +forward 8 +forward 4 +down 4 +forward 1 +down 4 +up 7 +forward 7 +forward 3 +down 4 +forward 9 +forward 1 +down 3 +down 2 +forward 7 +up 1 +forward 3 +up 3 +down 3 +down 7 +up 3 +up 4 +forward 7 +down 2 +up 2 +down 9 +up 1 +forward 3 +up 8 +up 8 +down 8 +down 1 +up 8 +up 4 +down 6 +forward 8 \ No newline at end of file diff --git a/2021/day3/day3.py b/2021/day3/day3.py new file mode 100644 index 0000000..7b7d0ff --- /dev/null +++ b/2021/day3/day3.py @@ -0,0 +1,67 @@ +def calculate_gamma(inp): + gamma_rate = [0] * len(inp[0]) + for line in inp: + for index, char in enumerate(line): + gamma_rate[index] += int(char) + gamma_rate = [0 if x < len(inp) // 2 else 1 for x in gamma_rate] + return gamma_rate + + +def part1(inp): + gamma = calculate_gamma(inp) + epsilon = [0 if x == 1 else 1 for x in gamma] + # power consumption = dec(gamma_rate) * dec(epsilon_rate) + power = int("".join(str(x) for x in gamma), 2) * int("".join(str(x) for x in epsilon), 2) + print("Part 1, power consumption : ", power) + + +def calculate_most_common(inp, pos): + sum = 0 + for line in inp: + sum += int(line[pos]) + return 0 if sum < len(inp) // 2 else 1 + + +def filter_oxygen(inp, pos, most_common): + result = [] + for line in inp: + if int(line[pos]) == most_common: + result.append(line) + return result + + +def oxygen_rating(inp): + result = inp[:] + for pos in range(len(inp[0])): + most_common = calculate_most_common(result, pos) + result = filter_oxygen(result, pos, most_common) + if len(result) == 1: + return result + + +def co2_rating(inp): + result = inp[:] + for pos in range(len(inp[0])): + least_common = 1 - calculate_most_common(result, pos) + result = filter_oxygen(result, pos, least_common) + if len(result) == 1: + return result + + +def part2(inp): + oxygen = oxygen_rating(inp) + co2 = co2_rating(inp) + res = int("".join(str(x) for x in oxygen), 2) * int("".join(str(x) for x in co2), 2) + print(f"Part 2 : {res}") + + + +def main(input_file): + with open(input_file) as f: + entries = [x.rstrip() for x in f.readlines()] + part1(entries) + part2(entries) + + +if __name__ == "__main__": + main("input.txt") diff --git a/2021/day3/input.txt b/2021/day3/input.txt new file mode 100644 index 0000000..6ed7c05 --- /dev/null +++ b/2021/day3/input.txt @@ -0,0 +1,1000 @@ +110001101000 +111011011100 +100001101100 +111011011010 +001001101100 +100001111100 +110110101101 +110010110000 +111101010011 +101101010110 +000010101110 +110110010001 +011101010111 +000101101011 +010110011010 +101110110000 +111110101011 +010001111110 +010011111110 +101010111001 +110010010111 +110001100101 +111111100101 +101001111010 +111011111000 +100011101000 +011100111001 +100000100110 +001101000100 +010110110100 +011000111111 +101011111000 +011011110011 +000110011101 +001000001010 +101111100000 +011000111110 +011110000001 +101001110100 +110000001110 +010010110110 +110001110011 +001100000101 +000001001101 +111011011011 +000101100111 +010111100000 +000110100010 +001000110101 +110000011100 +101011000111 +111001011110 +001110011101 +100110011000 +000000001101 +011011101101 +110100010011 +110101110011 +111110101010 +111010000001 +111000101101 +000011111111 +000110100011 +101100111011 +100011111110 +111000010000 +010001011111 +101011101001 +110111110010 +100111101100 +101110111001 +101100010000 +101100000000 +010010011111 +101000101011 +111000000000 +001101101100 +001010110100 +101110010111 +010011011111 +001101101111 +000110000010 +101010011100 +010011101010 +001001011111 +010011100100 +011001010000 +101101000110 +000000010010 +110101011000 +000011001001 +110111110110 +010100000110 +001010101100 +001010100000 +101001100110 +011111011000 +001111111101 +001000010001 +011000011001 +100110110000 +100110001100 +110101101001 +101100010111 +100111001110 +010000100101 +111100101100 +000000011001 +110010101101 +111001110100 +011111100001 +010011011011 +100100100111 +011010011000 +100100110010 +111111100000 +111010111110 +001001111101 +101010110001 +101100000011 +011011110010 +100010000101 +110111010011 +101001001111 +000011000111 +010110110001 +100010001101 +000011111110 +100111110110 +110100001110 +000010000000 +011010010101 +000110011010 +110111011101 +110111001100 +100100010100 +100101101011 +001001001001 +101111001010 +010010010100 +010101000001 +111011101100 +011100111110 +011111110010 +001011001110 +110101001110 +000000000010 +100111010000 +000010001010 +110110001111 +001101110111 +010010101010 +011100001011 +110111001010 +001001011001 +000000000100 +110111001000 +010101110011 +110000011011 +101111000000 +001011010100 +001010000100 +111110101111 +000011000100 +100011110110 +010011000010 +000110001001 +111110010110 +100110011010 +110010111101 +001111011101 +100001101111 +110100000001 +011110000000 +011010001001 +110110011111 +100110001111 +001001011100 +010010100010 +011100001100 +011001001011 +011111100010 +101001100101 +011100100110 +101000010011 +110010101010 +000111111100 +010000000101 +111100011010 +101100011010 +011111000011 +011000111101 +111000011011 +110101111101 +001000100101 +100100110101 +000111101110 +010000111011 +001011011101 +111011110101 +100110000101 +111101011111 +011101000011 +010010110101 +000001011101 +111110000100 +010100011100 +101000100011 +101010000011 +111011001011 +010101010111 +111111000111 +001011110100 +000000111010 +110100001111 +110100001001 +011111000100 +001101111101 +001000110000 +100011110100 +111110010100 +001100111110 +010010001110 +000100110101 +111011100111 +001000000110 +100100100011 +000011001101 +000111001110 +000101111101 +011101110010 +011110111111 +000111000011 +101001000101 +101111111110 +100101110100 +101010100010 +000010111010 +001011101011 +100100000111 +011000010001 +100000100101 +001010001010 +001100100001 +001000010010 +110101110001 +100111010111 +100101101001 +100010101101 +011101100101 +011111101011 +101101011010 +101001111011 +111010011101 +010010001100 +110001000010 +101011101010 +010001010011 +101111111000 +001110101000 +111000000111 +010111011100 +101000100100 +010011010111 +011000100010 +111001111101 +101010010011 +110011011000 +100111010101 +100001101110 +011011110110 +110000010111 +001100101111 +000110111111 +111001011011 +001010100100 +011111100011 +011110001110 +011011111000 +010101111101 +100001100011 +111111011111 +100010111101 +110101010100 +101000111001 +001001000001 +110011110011 +001101001000 +001100101110 +010001111100 +101100111000 +001011000011 +110001011101 +101001011010 +110011110110 +011010111101 +001001100001 +011010010100 +010111101011 +000110010101 +110010100011 +001111001101 +100110111001 +100010000110 +010111011011 +001110011110 +011010111100 +110100100010 +001010001110 +010011111010 +101010100111 +111010100101 +010000000000 +000001010101 +111001000001 +011110000010 +001010111111 +100101000001 +001000010111 +101000011011 +011100010000 +111000010100 +010001011100 +111111010001 +011010101110 +011111110001 +000110101100 +111000011001 +001010011100 +000011011100 +110111111101 +001001010101 +011110000011 +001111111001 +110010100000 +101111010100 +100100011000 +010101000111 +111100110111 +010010111011 +001101011111 +011101011000 +111111111111 +010100110110 +111000001010 +000100000110 +000110010000 +110000011000 +001000011001 +011110000100 +101011100011 +000011100001 +110100000011 +100100011100 +011110010111 +101000111111 +001010001100 +100010000100 +011100110110 +101010011000 +100000001111 +010001111111 +101101001011 +100010100101 +011011100000 +111110100111 +000100011000 +011000100001 +110000111000 +010011100101 +000100010011 +101110101011 +011111001111 +010001001101 +111100011111 +010000100000 +001111000101 +100000010010 +000111101000 +101111010000 +110110010000 +110111110001 +111110100000 +101110001010 +011111011001 +000001111101 +101000010100 +110111101010 +001111111011 +000101010101 +110110001011 +100011001010 +101010010111 +111100011110 +010000101101 +111110000010 +000100100111 +000110001100 +011110000111 +101110100001 +110001101010 +110100010100 +111010101101 +110010001001 +000010011110 +001100100110 +011111010011 +111110000001 +110111111100 +011100000110 +000011100111 +001101110100 +100111011101 +011100010010 +011001110110 +110110010010 +011000011011 +110111101100 +101100101100 +100101111001 +101001111000 +101000101001 +011101010100 +001110000100 +011100100000 +110000001010 +010001000111 +001100000110 +000100111011 +101001011101 +011100011110 +001011001000 +111101101100 +001110011111 +100011100110 +011000100101 +111010111111 +111010001110 +111011101111 +010001110011 +000011010100 +000000101001 +010100100001 +101111111011 +100111100000 +111110100101 +000100001000 +000101011101 +110001001110 +110111001001 +110111110011 +111101000111 +010111000100 +000010101000 +000110101101 +100000001010 +010111100101 +110010000100 +000011010111 +000101001001 +100111101110 +111110011111 +011001000010 +110010001101 +001111110010 +101110111111 +100100111101 +010111100110 +000101110001 +010111000001 +001011101010 +101101100110 +110010111111 +001111101010 +110110100100 +101110100101 +011001110010 +000000100011 +110000111100 +011110001011 +001110011000 +000010011011 +011100011100 +110001001010 +100000111010 +010010000111 +010010000001 +001110100100 +111110111101 +001100001011 +110101000000 +111100010110 +100010011100 +001011110011 +100000111001 +000101000111 +100011101011 +011010001000 +111111111001 +100011011001 +101001010011 +110001111001 +001100101101 +111000111100 +101000110110 +000011110011 +000110010001 +110110010011 +011100000000 +101101110001 +100001111101 +110010010000 +100001011011 +100010010000 +000000000110 +001011001101 +000001000101 +011100100010 +000100010001 +001111001110 +100001101001 +010100011111 +000001111011 +110100111110 +101000101000 +000101100101 +011000001101 +001010101000 +100100100110 +101010111101 +010000111100 +000000010000 +101011110100 +111011111010 +000011011111 +011111000010 +001001001110 +001100010111 +011001110100 +101011000110 +001000011101 +111101000010 +011101001001 +100011101110 +110000100101 +011111101100 +101000001001 +011011001110 +001100011110 +010110100001 +011100001000 +001111011010 +110001100100 +000001100011 +101011110001 +100000010111 +010011001000 +101111101110 +110001011100 +110000001011 +010001100000 +100111000111 +110000000100 +000011010010 +000101111001 +000100101000 +111000111111 +011110100111 +111110010010 +111111000010 +011111110111 +101101000100 +110101001111 +011000000101 +011010100110 +100101101000 +011001111000 +010001110101 +011011110111 +101110110100 +100011100101 +000101100000 +100101011011 +010011000110 +000110110111 +011110010010 +101111001111 +101001110111 +100001011001 +101101001111 +011111110101 +110001111111 +010111001011 +100100001010 +000100101111 +110111110111 +001101010100 +001011111100 +100110111101 +000001001001 +100111111101 +011010101101 +100010100001 +110101111110 +010010011100 +111001111000 +101011100001 +011101111110 +110101101011 +011100101100 +110110000001 +010110110110 +101011010111 +110111011011 +010011101001 +000001101010 +000010111000 +101000101010 +111100001000 +001001001100 +010110000010 +010010101111 +101101011111 +111111010011 +111111011101 +100011010000 +001011100111 +011001011110 +110010001110 +001010010100 +000010000010 +100011100001 +011011100110 +100001001111 +111000101010 +111000110100 +110100011011 +111010010101 +010010011001 +010111101111 +001111001100 +000111010011 +010010010001 +011101001100 +011111011110 +000101101010 +001011110111 +010000100011 +111101110001 +110001110010 +011000010101 +111111000110 +100110110101 +010001000110 +010000001111 +100000000110 +100101101110 +010101111111 +111000011010 +011000000100 +010100011110 +001101111111 +111011000100 +011011101100 +011001100011 +010001000010 +011001000111 +001010000101 +111001011001 +110100111101 +100101011010 +011100010101 +100000000010 +110101101110 +110011011011 +100010111000 +010110000100 +100000011000 +011001011100 +010011001110 +001100010101 +111100000000 +001000110001 +000011010101 +110101000010 +001100101001 +100110011100 +100001101011 +111010011011 +011010110010 +010101000000 +000111011111 +111110110101 +101010110101 +111011100100 +010111111111 +101010101111 +000011110010 +100101001010 +101000001100 +000000101100 +011111010001 +110101001010 +101010101110 +000101000100 +000110101001 +010011011110 +111101101111 +110100111000 +011011000111 +010000001000 +110000000110 +010100101111 +010110001001 +000111101111 +101110110001 +101000101110 +011101100001 +010001010110 +010011111111 +011111111101 +010111001010 +010010011011 +000010010001 +101100000010 +011001101001 +011100011011 +011010011100 +111101110101 +001001001011 +011110011101 +100001010100 +010010111110 +010011111101 +110000101001 +101110000101 +110010001100 +011001110111 +010011000000 +000011100101 +101010101100 +001000111000 +110101000001 +111110001000 +110000011001 +100000111000 +010111110001 +011011000001 +101101100100 +111110111011 +101000000101 +011110110110 +011001100111 +100101001011 +001110110100 +011010100101 +011110010001 +110010101110 +111101001111 +100100000100 +100000110001 +100100110011 +110011101000 +000110101011 +011110010101 +010101010000 +111111011110 +101111111111 +001111000010 +100001111001 +011110010000 +100101001101 +111010001100 +001101010010 +100100111110 +011011100001 +110010101001 +010101101101 +101000100101 +001100101011 +111111001010 +001000001011 +011101100111 +100101000010 +111011011110 +001000000101 +001000000000 +000101010110 +001010111011 +110010000000 +010011100001 +001001011011 +000101000101 +010011000111 +100100101101 +011110010110 +100100010000 +101100100010 +110101011011 +000001111111 +101001110010 +000100101011 +110001000000 +011010111011 +001111101110 +100110111111 +011100000100 +111100010101 +100001111111 +100000110000 +101111110000 +100000110101 +001101101010 +101010011110 +011000110010 +001110110110 +101000110010 +000111001111 +011101111001 +010000110010 +101011000000 +001011001010 +001101111100 +010010111001 +010101100010 +011001100000 +011110110011 +010101011100 +011000100011 +001110101010 +100111110001 +101110110111 +110100110101 +101000110111 +100011110111 +010000010010 +001001111111 +010100111110 +101111111101 +110111101111 +011010110101 +101100111001 +110001111010 +000101110010 +110000010110 +010110100011 +111011111111 +001100011010 +011010000010 +000101011000 +101000001000 +011110110101 +000001001011 +010011110001 +110101100000 +101011000001 +101001000010 +111111100111 +011010010010 +100010001100 +111011011101 +010110001111 +001001010011 +111001001111 +101000010101 +110101101111 +011111111001 +111001101010 +100111011010 +001010010011 +110001110100 +011110001010 +110111010101 +001001100110 +001001110011 +100001110011 +001001001101 +100110100100 +010001101101 +101110000111 +001110001111 +010100111100 +010110011111 +100100000011 +100110011001 +011101111011 +011000010000 +010101101001 +100010110011 +000100000010 +111001001000 +001110100000 +110000111011 +000001100111 +000110110000 +111001110000 +100001011100 +101101001101 +111101110011 +100011011100 +011010110111 +111001110001 +101011111100 +110010110110 +011100111010 +100111110101 +010011010010 +011101110111 +101110010100 +011100100001 +001011100011 +110101010001 +101000000111 +000101100100 +100011000011 +111011001101 +101001100111 +000000000111 +010100110011 +011111001101 +111101110000 +000111111000 +110100101011 +100101110001 +001010100011 +110101100001 +100101010110 +011100111011 +100100111001 +000100110001 +001101011100 +000110100110 +010001001111 +110000000000 +001010101101 +100001100001 +110010100111 +000010011111 +101110011001 +110010010101 +010101011101 +010001110010 +011101011111 +100100001011 +001100011100 +100111101001 +110010011001 +001010000011 +101010100000 +111111111110 +011010101001 +011110100010 +110000111101 +111010100001 +100111100110 +000000011010 +011011001100 +001101111011 +011111111010 +000101110111 +101100011011 +100111110000 +111011010100 +000100001001 +010000110000 +010111110000 +000110011001 +101111001110 +001100010000 +011011011011 +001001001000 +110110111011 +001000010100 +010100010000 +010101110111 +001111010100 +001010000010 +011110011001 +100001001001 +001101000101 +010001010101 +101110001001 +011100100111 +111000001111 +101001111111 +101010001011 +111110110011 +101101101000 +011001111010 +000010011001 +100000010001 +101010011111 +000100011101 +010010100000 +000001100101 +001001011110 +100101000101 +011111110110 +111101111001 +100011101100 \ No newline at end of file diff --git a/2021/day4/day4.py b/2021/day4/day4.py new file mode 100644 index 0000000..98ece64 --- /dev/null +++ b/2021/day4/day4.py @@ -0,0 +1,97 @@ +from dataclasses import dataclass +from collections import deque + +@dataclass +class BingoItem: + value: int + marked: bool = False + +def parse_grid(inp): + raw_grid = [inp.popleft() for _ in range(5)] + grid = [[BingoItem(int(y)) for y in x.rstrip().split(" ") if y != ''] for x in raw_grid] + return grid + + +def parse_grids(inp): + grids = [] + while len(inp) >= 5: + grid = parse_grid(inp) + grids.append(grid) + try: + inp.popleft() + except IndexError: + break + return grids + +def check_line_win(grid): + for line in grid: + if all(n.marked for n in line): + return True + return False + + +def check_column_win(grid): + for col_number in range(len(grid[0])): + column = [line[col_number] for line in grid] + if all(x.marked for x in column): + return True + return False + + +def calculate_score(grid, final_num): + unmarked = sum([sum([n.value for n in line if not n.marked]) for line in grid]) + return final_num * unmarked + + +def print_green(text, end): + print(f"\033[1;32;40m{text}\033[0;37;40m", end=end) + + +def print_grid(grid): + for line in grid: + for col in line: + if col.marked: + print_green(f"{str(col.value).ljust(2)}", " ") + else: + print(f"{str(col.value).ljust(2)}", end=" ") + print() + print() + + +def play_bingo(numbers, grids): + winning_grids = [] + for number in numbers: + print(number) + for grid in grids: + for line in grid: + for grid_number in line: + if grid_number.value == number: + grid_number.marked = True + + for grid in grids: + win = [check_line_win(grid), check_column_win(grid)] + if any(win): + winning_grids.append((grid, number)) + # the grid won, remove it from the game + grids.remove(grid) + + + first_winning_grid, number = winning_grids[0] + first_score = calculate_score(first_winning_grid, number) + print(f"Part 1, score = {first_score}") + + last_winning_grid, number = winning_grids[-1] + last_score = calculate_score(last_winning_grid, number) + print(f"Part 2, score {last_score}") + +def main(input_file): + with open(input_file) as f: + inp = deque(f.readlines()) + numbers = [int(x) for x in inp.popleft().split(",")] + inp.popleft() + grids = parse_grids(inp) + play_bingo(numbers, grids) + + +if __name__ == "__main__": + main("input.txt") diff --git a/2021/day4/input.txt b/2021/day4/input.txt new file mode 100644 index 0000000..29a838c --- /dev/null +++ b/2021/day4/input.txt @@ -0,0 +1,601 @@ +15,62,2,39,49,25,65,28,84,59,75,24,20,76,60,55,17,7,93,69,32,23,44,81,8,67,41,56,43,89,95,97,61,77,64,37,29,10,79,26,51,48,5,86,71,58,78,90,57,82,45,70,11,14,13,50,68,94,99,22,47,12,1,74,18,46,4,6,88,54,83,96,63,66,35,27,36,72,42,98,0,52,40,91,33,21,34,85,3,38,31,92,9,87,19,73,30,16,53,80 + +92 3 88 13 50 +90 70 24 28 52 +15 98 10 26 5 +84 34 37 73 87 +25 36 74 33 63 + +66 64 50 75 53 +73 24 80 84 5 +72 20 68 1 99 +83 57 44 60 52 +32 15 59 48 98 + +33 51 85 92 89 +38 22 93 62 75 +24 76 50 90 25 +69 6 52 77 3 +47 9 88 53 63 + +78 75 29 32 73 +22 85 42 1 23 +80 98 81 58 9 +61 76 69 83 53 +71 7 15 11 95 + +33 57 76 73 26 + 6 71 35 39 85 +54 77 36 14 87 +66 79 8 64 32 + 2 84 98 34 13 + +43 51 16 95 59 +22 53 6 49 94 +32 72 46 23 37 +40 85 39 45 74 +87 62 69 98 0 + +84 5 73 32 23 +40 64 98 27 8 +80 71 1 31 69 +46 42 7 4 70 +88 90 48 33 29 + + 3 91 8 98 50 +54 70 29 94 25 +17 77 33 46 12 +28 36 39 40 5 +22 38 51 69 45 + +90 35 94 31 44 +11 13 74 38 49 +60 96 91 63 16 +23 26 84 41 7 +73 65 32 18 81 + +62 42 35 21 87 +57 27 26 71 94 +73 92 77 53 86 + 1 60 38 75 43 +10 70 55 84 5 + + 3 58 57 66 51 +67 94 37 86 25 +33 11 4 36 83 +64 2 0 13 59 +77 19 80 93 97 + +99 13 24 49 90 +96 15 10 67 2 + 9 78 5 42 80 +28 75 51 58 82 +31 83 20 60 48 + +91 38 65 34 58 +71 28 66 64 72 +63 10 83 37 56 +84 39 19 51 74 +23 90 81 85 13 + +12 42 10 11 29 +99 60 24 94 25 + 9 40 76 33 97 +32 75 16 37 27 +15 69 54 52 22 + +31 40 33 45 89 +61 82 9 32 75 +60 88 91 27 62 +79 94 36 83 25 +56 39 8 13 55 + +39 95 92 2 56 +88 70 63 62 13 +49 43 46 0 47 +83 42 44 7 26 +60 27 69 73 29 + +54 67 26 19 45 + 8 50 86 51 92 +60 98 31 95 53 +24 71 55 22 63 + 4 38 21 35 32 + + 4 34 26 32 58 +16 67 76 78 46 +73 95 68 56 60 +35 40 42 6 87 + 7 97 54 92 24 + +98 80 66 95 14 +73 19 94 63 60 +52 18 28 72 26 +33 93 56 4 21 +59 68 74 48 3 + + 7 27 84 80 79 + 1 21 11 37 47 +88 38 30 8 72 + 4 52 13 19 26 +57 6 58 0 98 + +62 50 0 37 77 +32 31 2 53 4 +74 56 41 23 59 +60 89 94 54 39 +76 98 20 61 82 + +35 90 5 80 18 +45 20 60 8 77 +26 17 61 55 29 +24 76 3 41 64 + 4 74 85 10 82 + +62 23 27 89 61 +45 65 30 14 66 +52 72 48 99 0 + 5 40 42 81 37 +93 4 67 2 9 + +27 87 68 50 41 +18 60 12 45 48 +93 38 8 6 13 +99 37 59 94 64 +40 55 63 67 31 + +70 4 34 49 71 +36 81 52 62 55 +18 64 63 85 5 +72 99 77 76 54 +22 23 0 1 37 + +34 88 69 20 30 +73 11 93 68 56 +78 35 80 22 24 +15 95 32 51 25 +67 91 52 5 14 + + 8 54 26 34 71 +16 47 39 96 58 + 4 95 38 6 45 +94 63 18 99 72 +19 91 80 73 30 + +77 9 78 76 60 + 8 31 73 74 17 +22 25 7 64 47 +75 32 89 87 40 +13 44 10 95 49 + +78 3 90 99 6 +22 52 25 53 72 +55 98 77 56 32 +85 86 0 7 12 +74 84 33 45 1 + +57 53 26 54 69 +56 8 58 91 40 +65 97 44 51 2 +85 60 72 22 89 +66 16 67 90 93 + + 9 93 65 94 29 + 2 80 7 16 79 +11 5 21 73 50 +20 70 37 48 85 +99 3 55 58 8 + +26 37 60 63 47 +21 39 69 68 22 +83 94 55 91 80 +35 89 6 45 17 +23 85 84 73 7 + +74 36 81 41 8 +14 22 30 86 90 +84 97 11 67 77 +42 47 55 76 64 +95 92 59 93 53 + +64 16 19 68 50 +90 12 47 40 62 +86 1 48 2 58 +96 79 92 46 91 +14 85 59 45 30 + + 3 1 55 13 5 +59 85 50 42 20 +67 99 17 29 39 +30 35 23 49 25 +89 53 21 9 6 + +90 91 47 99 37 +82 24 56 27 2 +95 57 33 4 97 +51 26 29 67 98 +21 62 42 43 9 + +92 16 89 24 96 +31 18 2 64 20 + 6 34 99 50 85 +13 32 19 43 37 +48 47 23 78 77 + +95 16 87 61 6 +46 15 24 72 60 +43 56 80 35 53 +97 25 98 42 14 +51 11 10 3 45 + +96 42 4 45 40 +65 8 17 58 23 +53 38 14 12 84 +68 92 11 6 51 +87 22 5 99 0 + +45 51 26 18 91 + 7 31 95 37 74 +66 41 48 20 87 +99 96 64 53 0 + 3 28 15 46 79 + +66 34 23 78 12 +65 72 33 14 5 + 4 59 3 62 64 + 7 60 31 52 87 +80 39 27 58 74 + +91 94 64 46 28 +99 29 79 58 0 +18 19 24 59 16 + 3 73 52 9 86 +37 61 1 93 68 + +37 98 80 41 53 +85 18 55 31 17 +39 61 63 97 52 +47 22 99 50 88 +48 14 9 93 96 + +11 66 89 91 34 +98 25 53 7 65 +42 32 9 14 77 +85 87 26 12 64 +45 99 29 88 4 + +63 3 16 13 33 +28 32 37 90 11 +94 44 18 38 68 +30 87 95 52 58 +79 43 53 70 19 + +94 67 56 43 47 +77 37 93 90 92 +66 48 98 20 61 +51 2 85 57 11 +22 84 79 17 72 + +86 59 15 85 5 +93 41 23 53 62 +46 48 70 57 49 +17 45 32 79 12 +64 73 26 6 9 + +12 88 27 43 21 +66 42 84 82 62 +94 46 96 63 86 +69 79 40 39 92 +22 87 71 44 53 + +89 26 45 78 25 +21 40 70 66 33 +97 80 94 18 1 +12 55 20 24 39 + 7 32 31 37 72 + +15 56 39 57 40 +67 59 26 30 90 +84 2 41 25 7 +96 23 79 99 85 +13 10 86 51 53 + +73 8 79 19 48 +29 36 89 62 22 +13 96 59 91 10 +90 9 1 78 65 +83 50 24 88 60 + +20 61 63 82 53 +86 11 55 10 85 + 5 37 65 21 54 +89 75 59 73 48 +41 50 29 71 93 + +81 13 46 17 47 +95 19 33 91 55 + 5 73 54 50 98 +63 77 30 40 58 + 9 57 94 92 20 + +54 99 94 23 81 +32 86 50 28 8 +69 18 11 39 67 +10 79 91 15 43 +13 98 55 16 22 + +83 99 54 12 80 +94 61 49 33 62 +16 23 68 87 10 + 1 76 25 89 71 + 8 45 74 28 27 + +66 28 72 76 33 + 9 99 27 96 60 +84 67 35 50 79 +55 44 18 98 13 +94 70 42 21 65 + +96 97 79 75 46 +11 65 41 72 92 +87 59 26 70 10 +37 8 68 73 63 +55 95 84 49 50 + +51 27 63 31 24 +82 11 87 6 2 +75 57 85 1 46 +91 71 72 13 56 +10 64 65 49 69 + +36 26 67 61 84 +99 10 2 24 47 +35 28 65 57 91 +30 27 1 78 14 +96 50 70 38 37 + +62 33 41 98 35 +80 92 4 48 70 + 2 11 23 15 52 +83 39 79 81 1 +54 93 27 18 24 + +12 75 20 81 23 +77 99 47 24 82 +92 29 85 30 21 +49 45 98 4 91 + 9 53 28 1 54 + +72 46 53 3 19 +83 49 39 12 22 +47 62 58 14 79 +82 69 84 75 1 +67 7 21 45 65 + +43 21 47 84 94 +93 53 37 44 15 +48 10 59 35 41 +91 78 98 34 66 +85 75 95 92 39 + +94 6 17 16 12 +39 41 11 65 78 +97 85 49 64 72 +59 84 83 42 28 +32 96 46 89 44 + +54 29 71 64 78 +32 13 52 58 28 +84 85 95 26 86 +23 41 70 53 87 +27 15 57 16 2 + +92 99 45 81 32 +86 25 56 76 52 +95 3 6 88 1 +71 70 24 19 62 +59 16 11 2 34 + +43 56 11 7 49 + 1 50 84 89 0 +97 18 60 95 25 +42 33 75 31 29 +35 62 78 99 76 + +98 84 53 3 22 +54 87 41 76 83 +39 27 36 79 78 +55 1 89 48 81 +49 26 77 96 67 + +99 79 98 84 47 +72 14 49 3 10 +30 9 12 61 1 +21 50 75 82 8 +86 44 13 83 88 + +82 94 33 70 17 +97 22 45 53 55 +19 71 35 54 52 +41 42 63 65 3 +88 10 67 81 69 + +50 90 18 2 22 +51 85 67 40 61 + 3 71 99 93 46 +65 29 45 60 75 + 5 74 6 66 98 + +68 80 59 29 5 + 6 16 45 44 92 +74 13 64 30 25 +69 94 54 97 3 +42 47 26 19 17 + +38 79 36 61 90 +19 59 18 3 71 +70 99 16 93 22 +68 34 88 76 17 +75 54 49 85 86 + + 8 96 80 15 28 +23 98 58 84 69 +21 3 60 38 97 +43 56 34 25 64 +24 1 39 44 78 + +21 60 14 55 29 +34 61 63 18 5 +19 28 54 72 7 +32 46 92 80 73 +40 68 75 67 98 + +57 21 88 90 33 +63 5 25 24 49 +29 7 23 19 13 +85 93 75 41 68 +98 69 12 76 31 + +74 88 75 81 51 +46 77 66 60 20 +47 0 23 64 43 +68 41 38 65 48 +53 26 54 17 83 + +39 21 78 15 99 +25 97 24 70 56 +57 66 31 75 71 +47 91 30 4 65 +94 11 77 76 44 + +35 42 72 38 51 +96 32 3 64 48 +81 50 37 55 79 +90 67 54 6 12 +31 45 71 25 76 + +60 58 90 3 74 +48 16 49 30 46 +68 51 0 80 96 +26 71 36 27 28 +57 94 79 42 50 + +96 27 94 74 89 +57 19 51 5 78 +20 59 14 73 69 + 8 41 79 76 32 +24 98 63 46 13 + +20 53 42 70 86 +12 49 96 0 77 +31 26 38 22 87 +51 78 60 36 13 +57 8 73 94 7 + +75 39 93 85 99 +78 50 3 96 68 +62 10 28 80 41 +89 40 46 69 19 +37 13 16 2 67 + +85 24 99 70 20 +31 60 41 63 81 +34 87 93 39 37 +55 43 44 25 78 +97 21 3 28 40 + +44 14 92 89 62 +90 76 84 52 33 +78 54 26 32 9 +85 99 25 10 55 +28 23 22 97 94 + +13 38 37 98 15 +78 62 9 50 2 +77 68 65 18 74 +90 21 95 53 33 +60 25 17 64 1 + +45 87 64 33 66 +31 85 19 90 48 +74 3 70 77 9 +44 46 61 91 32 + 0 15 94 65 22 + +12 1 66 47 3 +63 7 2 42 21 + 6 75 44 26 82 +52 45 48 89 68 +96 92 25 15 76 + +64 16 49 71 28 + 7 45 63 74 21 +87 25 46 23 9 + 0 31 92 24 77 +65 78 22 60 75 + + 4 54 58 83 60 +25 12 82 0 73 +32 62 2 31 49 +64 18 35 19 10 +61 46 43 34 38 + +84 48 30 77 79 +15 42 4 25 72 +28 78 22 7 70 +46 6 31 24 41 +98 93 34 37 71 + +72 9 71 1 54 +97 98 91 90 92 +23 88 13 87 68 +45 36 86 41 56 +69 16 24 20 93 + +35 34 60 67 52 +12 73 99 89 61 +33 94 27 16 15 + 4 64 47 22 74 +24 53 71 66 76 + +32 62 51 58 1 +11 76 75 33 60 +55 54 39 52 48 + 6 87 3 8 99 +40 65 24 66 70 + +61 23 22 12 74 +73 25 85 11 0 +20 38 26 88 33 +63 39 50 49 83 +71 18 56 37 7 + +46 12 90 52 48 +73 24 78 34 94 +15 19 47 72 89 +60 35 74 67 30 +13 18 17 93 0 + +37 87 0 94 27 +18 56 54 4 33 +84 20 12 86 6 + 5 40 52 97 74 +63 59 69 19 77 + +15 53 20 35 14 +24 25 63 85 79 +65 96 2 60 50 +72 10 77 12 92 +32 94 95 16 71 + +78 52 55 20 40 +33 66 81 48 18 +32 69 13 5 84 +23 67 68 61 34 +11 63 4 93 65 + +51 89 37 46 29 +69 56 71 9 91 +28 54 7 16 31 +67 5 97 42 43 +98 32 65 34 30 diff --git a/2021/day5/day5.py b/2021/day5/day5.py new file mode 100644 index 0000000..3128904 --- /dev/null +++ b/2021/day5/day5.py @@ -0,0 +1,41 @@ +from collections import defaultdict + +def main(infile): + points = defaultdict(int) + with open(infile) as f: + for line in f: + start, end = line.split(" -> ") + start_x, start_y = [int(x) for x in start.split(",")] + end_x, end_y = [int(x) for x in end.split(",")] + + # column | + if start_x == end_x: + step = 1 if start_y < end_y else -1 + for y in range(start_y, end_y + step, step): + points[(start_x, y)] = points[(start_x, y)] + 1 + # line - + elif start_y == end_y: + step = 1 if start_x < end_x else -1 + for x in range(start_x, end_x + step, step): + points[(x, start_y)] = points[(x, start_y)] + 1 + # diagonal \ + elif ((start_x < end_x and start_y > end_y) + or (start_x > end_x and start_y < end_y)): + step = 1 if start_y > end_y else -1 + for dx in range(0, end_x - start_x + step, step): + points[(start_x + dx, start_y - dx)] = points[(start_x + dx, start_y + dx)] + 1 + # diagonal / + elif ((start_x < end_x and start_y < end_y) + or (start_x > end_x and start_y > end_y)): + step = 1 if start_y < end_y else -1 + for dx in range(0, end_x - start_x + step, step): + points[(start_x + dx, start_y + dx)] = points[(start_x + dx, start_y + dx)] + 1 + + res = len([x for x in points.values() if x >= 2]) + print(res) + + + + +if __name__ == "__main__": + main("input.txt") diff --git a/2021/day5/input.txt b/2021/day5/input.txt new file mode 100644 index 0000000..1239ab3 --- /dev/null +++ b/2021/day5/input.txt @@ -0,0 +1,500 @@ +299,462 -> 299,747 +855,314 -> 855,140 +981,328 -> 798,328 +610,444 -> 680,374 +797,242 -> 606,242 +217,42 -> 147,42 +735,378 -> 735,188 +247,192 -> 912,192 +377,341 -> 768,341 +472,701 -> 66,701 +48,970 -> 885,133 +893,35 -> 664,35 +617,237 -> 951,237 +540,643 -> 190,293 +575,815 -> 302,815 +146,380 -> 146,562 +568,481 -> 568,161 +38,101 -> 921,984 +613,12 -> 185,12 +967,30 -> 17,980 +823,620 -> 584,859 +672,822 -> 413,822 +259,626 -> 385,752 +752,415 -> 857,310 +758,659 -> 758,76 +909,893 -> 35,19 +964,913 -> 105,54 +697,196 -> 697,913 +389,821 -> 163,821 +783,65 -> 281,65 +775,732 -> 558,732 +818,817 -> 42,817 +499,537 -> 896,140 +81,957 -> 81,844 +851,256 -> 559,548 +268,970 -> 268,170 +106,216 -> 68,178 +107,371 -> 850,371 +160,107 -> 748,107 +300,619 -> 524,395 +940,196 -> 780,356 +752,498 -> 752,94 +807,619 -> 728,619 +831,89 -> 313,89 +56,389 -> 191,524 +206,75 -> 206,816 +486,924 -> 486,389 +280,708 -> 542,446 +562,917 -> 190,545 +40,231 -> 40,404 +804,327 -> 726,249 +538,670 -> 170,302 +473,229 -> 912,668 +645,195 -> 645,916 +502,13 -> 502,266 +639,955 -> 639,434 +87,56 -> 943,912 +143,798 -> 699,798 +469,261 -> 79,651 +715,98 -> 104,709 +914,339 -> 463,790 +456,263 -> 456,101 +656,105 -> 109,105 +28,944 -> 123,944 +981,652 -> 270,652 +953,681 -> 605,333 +474,858 -> 310,858 +542,736 -> 807,736 +234,412 -> 620,26 +615,786 -> 36,207 +169,56 -> 169,132 +133,930 -> 989,74 +342,34 -> 516,34 +210,97 -> 947,834 +43,857 -> 824,76 +673,840 -> 673,156 +718,123 -> 896,123 +673,311 -> 673,564 +639,352 -> 72,919 +552,571 -> 661,462 +819,335 -> 953,335 +756,84 -> 823,84 +250,969 -> 287,969 +551,260 -> 378,433 +417,412 -> 465,412 +621,260 -> 249,632 +226,633 -> 394,465 +475,179 -> 602,306 +272,571 -> 272,839 +820,666 -> 820,829 +988,608 -> 974,608 +124,318 -> 124,589 +303,516 -> 839,516 +983,477 -> 983,786 +299,870 -> 927,242 +284,875 -> 213,875 +427,493 -> 545,493 +74,755 -> 17,698 +294,326 -> 294,23 +331,193 -> 391,193 +381,671 -> 408,644 +805,537 -> 805,155 +721,956 -> 10,956 +918,36 -> 918,915 +981,891 -> 445,355 +591,288 -> 933,288 +199,256 -> 250,307 +318,810 -> 789,339 +250,245 -> 522,517 +592,248 -> 958,614 +722,215 -> 235,215 +654,496 -> 654,905 +76,860 -> 678,258 +29,20 -> 954,945 +379,851 -> 567,851 +722,161 -> 13,870 +965,302 -> 390,877 +114,892 -> 114,348 +265,681 -> 26,920 +94,463 -> 94,160 +340,150 -> 340,759 +727,612 -> 175,60 +457,951 -> 154,648 +602,200 -> 602,841 +487,194 -> 27,654 +356,699 -> 887,168 +915,237 -> 262,890 +81,225 -> 815,959 +227,877 -> 694,877 +441,674 -> 441,968 +865,201 -> 865,528 +969,214 -> 511,214 +802,748 -> 802,86 +662,313 -> 636,313 +308,447 -> 308,545 +245,236 -> 532,236 +621,195 -> 621,140 +889,159 -> 197,851 +68,683 -> 179,572 +859,261 -> 56,261 +982,539 -> 982,619 +144,362 -> 851,362 +304,905 -> 304,553 +551,905 -> 637,905 +432,316 -> 142,606 +104,588 -> 104,862 +316,392 -> 680,392 +372,413 -> 866,413 +874,53 -> 697,53 +499,668 -> 499,329 +32,207 -> 802,977 +403,108 -> 100,411 +578,442 -> 578,489 +134,161 -> 848,875 +851,935 -> 95,179 +485,190 -> 485,57 +411,47 -> 680,47 +378,878 -> 378,127 +908,717 -> 516,717 +432,863 -> 328,863 +212,278 -> 212,326 +552,426 -> 933,807 +419,329 -> 492,402 +975,750 -> 424,750 +40,54 -> 915,929 +570,349 -> 576,349 +32,784 -> 32,473 +854,407 -> 343,407 +18,932 -> 425,932 +223,571 -> 468,816 +939,330 -> 939,870 +126,637 -> 105,616 +84,310 -> 84,788 +491,890 -> 229,890 +737,831 -> 737,726 +137,471 -> 137,957 +642,429 -> 253,429 +319,103 -> 903,103 +38,872 -> 38,110 +809,183 -> 809,653 +877,87 -> 56,908 +455,136 -> 693,374 +218,647 -> 727,647 +626,544 -> 797,544 +147,46 -> 122,46 +316,430 -> 495,430 +608,469 -> 331,192 +353,769 -> 714,769 +649,90 -> 410,90 +105,311 -> 105,674 +594,600 -> 484,600 +822,933 -> 279,933 +478,267 -> 478,341 +114,912 -> 114,387 +843,480 -> 754,480 +747,701 -> 747,143 +646,88 -> 646,375 +195,129 -> 757,691 +470,895 -> 470,673 +450,595 -> 334,711 +165,872 -> 165,155 +744,947 -> 987,947 +153,15 -> 357,15 +272,222 -> 272,201 +535,551 -> 120,966 +102,748 -> 102,281 +348,482 -> 129,482 +499,679 -> 499,821 +399,875 -> 399,285 +695,585 -> 733,547 +40,509 -> 95,564 +199,857 -> 228,886 +491,885 -> 978,885 +926,887 -> 104,65 +492,128 -> 957,593 +302,904 -> 302,906 +706,782 -> 706,217 +721,506 -> 721,675 +847,725 -> 583,989 +225,734 -> 942,17 +141,161 -> 161,161 +858,736 -> 858,433 +183,724 -> 13,554 +299,647 -> 299,420 +623,39 -> 358,304 +657,373 -> 657,976 +452,714 -> 452,735 +857,537 -> 392,72 +758,979 -> 758,457 +141,609 -> 141,100 +266,76 -> 974,784 +527,66 -> 236,357 +971,176 -> 865,282 +961,935 -> 74,48 +328,434 -> 328,663 +384,670 -> 12,670 +534,508 -> 334,508 +336,603 -> 202,469 +690,140 -> 807,140 +491,511 -> 491,166 +265,493 -> 236,493 +552,113 -> 552,329 +370,542 -> 370,688 +919,556 -> 488,125 +142,949 -> 107,949 +917,824 -> 360,267 +866,109 -> 624,109 +714,657 -> 714,155 +727,567 -> 727,570 +235,920 -> 235,683 +329,261 -> 55,261 +672,718 -> 113,159 +469,380 -> 66,783 +884,289 -> 884,15 +412,197 -> 496,197 +971,875 -> 20,875 +831,245 -> 831,946 +22,985 -> 391,985 +984,136 -> 187,933 +845,334 -> 660,519 +367,299 -> 367,912 +25,985 -> 946,64 +487,416 -> 487,453 +89,223 -> 723,857 +890,953 -> 19,82 +199,256 -> 199,521 +785,981 -> 710,906 +673,160 -> 673,682 +65,730 -> 421,730 +957,89 -> 832,89 +647,361 -> 33,361 +243,347 -> 784,888 +50,760 -> 50,372 +564,278 -> 564,846 +344,652 -> 832,652 +219,586 -> 219,502 +976,500 -> 976,650 +515,265 -> 744,36 +930,730 -> 930,332 +479,48 -> 592,48 +979,326 -> 374,326 +790,520 -> 375,520 +129,919 -> 511,537 +203,558 -> 212,558 +688,842 -> 502,842 +979,976 -> 90,87 +78,929 -> 78,478 +578,203 -> 802,427 +788,360 -> 260,888 +847,342 -> 212,977 +256,578 -> 821,13 +493,561 -> 712,342 +90,116 -> 181,116 +317,736 -> 963,90 +453,548 -> 37,548 +15,472 -> 514,971 +972,579 -> 956,579 +456,66 -> 349,66 +102,771 -> 769,771 +320,741 -> 327,748 +981,150 -> 592,150 +739,978 -> 739,804 +861,10 -> 142,729 +457,596 -> 580,596 +358,287 -> 237,408 +705,719 -> 59,73 +27,770 -> 27,133 +846,690 -> 846,464 +138,351 -> 174,315 +380,942 -> 380,985 +490,421 -> 803,734 +559,449 -> 761,449 +709,218 -> 718,218 +271,877 -> 271,814 +89,845 -> 918,16 +613,436 -> 976,73 +27,88 -> 867,88 +541,965 -> 739,767 +187,746 -> 916,17 +294,13 -> 333,13 +600,647 -> 925,647 +915,942 -> 41,68 +38,625 -> 176,763 +468,905 -> 468,727 +337,89 -> 337,581 +48,969 -> 732,285 +555,301 -> 555,610 +155,525 -> 985,525 +235,167 -> 700,167 +728,134 -> 728,289 +696,595 -> 892,595 +983,696 -> 401,114 +581,40 -> 515,40 +171,837 -> 975,33 +588,683 -> 734,683 +880,132 -> 231,132 +847,145 -> 332,660 +657,179 -> 657,18 +49,957 -> 496,510 +791,497 -> 552,736 +988,989 -> 10,11 +937,659 -> 937,461 +403,458 -> 403,637 +237,765 -> 237,813 +35,504 -> 35,663 +556,897 -> 802,897 +382,491 -> 786,895 +103,566 -> 528,566 +598,570 -> 623,570 +345,343 -> 345,985 +537,59 -> 537,386 +207,811 -> 974,44 +463,623 -> 463,21 +966,915 -> 966,965 +569,281 -> 569,183 +470,648 -> 470,666 +441,420 -> 817,796 +451,723 -> 908,266 +300,297 -> 840,297 +902,201 -> 902,912 +598,930 -> 654,930 +874,433 -> 874,176 +551,967 -> 795,967 +892,23 -> 137,778 +306,463 -> 679,90 +16,78 -> 16,980 +782,749 -> 782,117 +235,240 -> 244,231 +461,183 -> 981,183 +608,170 -> 608,640 +75,711 -> 645,141 +49,238 -> 488,238 +14,963 -> 947,30 +120,56 -> 120,73 +630,978 -> 316,664 +219,389 -> 803,973 +106,918 -> 978,46 +941,439 -> 788,592 +313,943 -> 325,943 +325,683 -> 325,67 +774,816 -> 774,908 +608,833 -> 608,679 +447,289 -> 447,135 +405,650 -> 405,406 +622,467 -> 636,467 +855,606 -> 855,663 +918,640 -> 918,831 +640,869 -> 640,904 +481,405 -> 481,791 +185,582 -> 21,746 +556,772 -> 114,330 +490,144 -> 490,591 +60,74 -> 974,988 +967,978 -> 10,21 +159,669 -> 486,342 +302,636 -> 302,771 +841,427 -> 793,427 +670,743 -> 234,743 +47,676 -> 233,490 +877,768 -> 123,14 +139,462 -> 139,541 +204,59 -> 204,300 +720,702 -> 720,525 +171,341 -> 787,341 +699,899 -> 293,899 +431,513 -> 431,849 +904,278 -> 124,278 +919,67 -> 815,67 +401,519 -> 688,232 +675,279 -> 675,137 +376,786 -> 362,786 +57,817 -> 801,73 +809,468 -> 410,867 +669,171 -> 669,65 +520,36 -> 218,36 +702,159 -> 709,159 +399,646 -> 399,828 +853,759 -> 853,91 +58,143 -> 867,952 +896,939 -> 42,85 +677,120 -> 646,120 +947,714 -> 737,714 +515,107 -> 752,344 +793,142 -> 793,789 +86,896 -> 967,15 +663,493 -> 833,493 +986,766 -> 293,766 +71,874 -> 71,417 +471,426 -> 148,426 +444,982 -> 142,982 +124,582 -> 846,582 +336,436 -> 257,436 +877,750 -> 177,50 +69,73 -> 911,915 +315,363 -> 315,45 +620,272 -> 556,272 +616,186 -> 331,186 +766,756 -> 59,49 +555,271 -> 555,183 +437,246 -> 454,246 +169,57 -> 169,688 +448,605 -> 420,605 +194,149 -> 960,915 +597,308 -> 597,512 +328,337 -> 328,349 +506,331 -> 506,144 +608,633 -> 838,863 +37,99 -> 767,829 +128,487 -> 128,246 +473,303 -> 473,529 +754,890 -> 754,269 +854,958 -> 957,958 +704,360 -> 526,360 +613,752 -> 260,752 +179,302 -> 805,302 +916,176 -> 519,176 +318,622 -> 161,622 +783,785 -> 322,785 +148,289 -> 802,943 +944,280 -> 671,280 +758,402 -> 442,86 +988,959 -> 56,27 +716,642 -> 429,642 +899,48 -> 899,111 +981,325 -> 168,325 +603,77 -> 474,77 +103,387 -> 112,387 +100,40 -> 160,40 +969,317 -> 109,317 +938,424 -> 938,179 +834,980 -> 527,980 +875,83 -> 22,83 +89,572 -> 582,79 +642,862 -> 642,247 +499,26 -> 499,242 +873,173 -> 206,840 +314,112 -> 314,388 +778,25 -> 778,944 +902,457 -> 964,519 +822,891 -> 623,891 +538,76 -> 618,156 +418,179 -> 204,179 +138,131 -> 300,131 +51,386 -> 51,764 +756,728 -> 330,302 +801,374 -> 801,506 +239,416 -> 781,958 +311,651 -> 848,651 +67,586 -> 67,250 +794,244 -> 794,515 +228,810 -> 368,670 +399,561 -> 682,561 +919,61 -> 868,61 +204,253 -> 224,253 +74,657 -> 74,235 +208,422 -> 722,422 +246,353 -> 441,548 +362,175 -> 362,688 +403,681 -> 403,821 +146,183 -> 23,183 diff --git a/2021/day6/day6.py b/2021/day6/day6.py new file mode 100644 index 0000000..bac503d --- /dev/null +++ b/2021/day6/day6.py @@ -0,0 +1,26 @@ +from collections import defaultdict, Counter + +def calculate_fishes(inp, days): + fishes = Counter(inp) + for day in range(days): + fishes_new = defaultdict(int) + for fish, cnt in fishes.items(): + if fish == 0: + fishes_new[8] += cnt + fishes_new[6] += cnt + else: + fishes_new[fish - 1] += cnt + fishes = fishes_new + return sum(fishes.values()) + + +def main(infile): + with open(infile) as f: + inp = [int(x) for x in f.readline().split(",")] + res = calculate_fishes(inp, 80) + print(f"Part 1, {res}") + res = calculate_fishes(inp, 256) + print(f"Part 2, {res}") + +if __name__ == "__main__": + main("input.txt") diff --git a/2021/day6/day6_2.py b/2021/day6/day6_2.py new file mode 100644 index 0000000..d7f5908 --- /dev/null +++ b/2021/day6/day6_2.py @@ -0,0 +1,34 @@ + +total = 0 +numbers = [0, 0, 0, 0, 0, 0, 0, 0, 0] + +with open("input.txt") as f: + data = [int(x) for x in f.readline().split(',')] + +for i in data: + if i == 0: + numbers[0] += 1 + if i == 1: + numbers[1] += 1 + if i == 2: + numbers[2] += 1 + if i == 3: + numbers[3] += 1 + if i == 4: + numbers[4] += 1 + if i == 5: + numbers[5] += 1 + if i == 6: + numbers[6] += 1 + if i == 7: + numbers[7] += 1 + if i == 8: + numbers[8] += 1 + +def rotate(l): + return l[1:] + l[:1] + +for j in range(256): + numbers = rotate(numbers) + numbers[6] += numbers[8] + print(f'DAY {j+1} AMOUNT OF FISH: {sum(numbers)}') \ No newline at end of file diff --git a/2021/day6/input.txt b/2021/day6/input.txt new file mode 100644 index 0000000..8a6b0de --- /dev/null +++ b/2021/day6/input.txt @@ -0,0 +1 @@ +3,5,3,5,1,3,1,1,5,5,1,1,1,2,2,2,3,1,1,5,1,1,5,5,3,2,2,5,4,4,1,5,1,4,4,5,2,4,1,1,5,3,1,1,4,1,1,1,1,4,1,1,1,1,2,1,1,4,1,1,1,2,3,5,5,1,1,3,1,4,1,3,4,5,1,4,5,1,1,4,1,3,1,5,1,2,1,1,2,1,4,1,1,1,4,4,3,1,1,1,1,1,4,1,4,5,2,1,4,5,4,1,1,1,2,2,1,4,4,1,1,4,1,1,1,2,3,4,2,4,1,1,5,4,2,1,5,1,1,5,1,2,1,1,1,5,5,2,1,4,3,1,2,2,4,1,2,1,1,5,1,3,2,4,3,1,4,3,1,2,1,1,1,1,1,4,3,3,1,3,1,1,5,1,1,1,1,3,3,1,3,5,1,5,5,2,1,2,1,4,2,3,4,1,4,2,4,2,5,3,4,3,5,1,2,1,1,4,1,3,5,1,4,1,2,4,3,1,5,1,1,2,2,4,2,3,1,1,1,5,2,1,4,1,1,1,4,1,3,3,2,4,1,4,2,5,1,5,2,1,4,1,3,1,2,5,5,4,1,2,3,3,2,2,1,3,3,1,4,4,1,1,4,1,1,5,1,2,4,2,1,4,1,1,4,3,5,1,2,1 \ No newline at end of file diff --git a/2021/day7/day7.py b/2021/day7/day7.py new file mode 100644 index 0000000..d7f487a --- /dev/null +++ b/2021/day7/day7.py @@ -0,0 +1,33 @@ +from collections import OrderedDict + +def part1(crabs): + moves = OrderedDict() + for pos in range(min(crabs), max(crabs) + 1): + # calculate total fuel required to move to pos: + for crab in crabs: + fuel_cost = abs(pos - crab) + moves[pos] = moves.get(pos, 0) + fuel_cost + + min_move = min(moves, key=moves.get) + print(f"Part 1, min move {min_move}, cost {moves[min_move]}") + +def part2(crabs): + moves = OrderedDict() + for pos in range(min(crabs), max(crabs) + 1): + # calculate total fuel required to move to pos: + for crab in crabs: + dx = abs(pos - crab) + # S = (n+1)(u0 + un)/2 + fuel_cost = dx * (dx+1)//2 + moves[pos] = moves.get(pos, 0) + fuel_cost + + min_move = min(moves, key=moves.get) + print(f"Part 1, min move {min_move}, cost {moves[min_move]}") + + +if __name__ == "__main__": + with open("input.txt") as infile: + crabs = [int(x) for x in infile.readline().split(",")] + part1(crabs) + part2(crabs) + diff --git a/2021/day7/input.txt b/2021/day7/input.txt new file mode 100644 index 0000000..f79a864 --- /dev/null +++ b/2021/day7/input.txt @@ -0,0 +1 @@ +1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,76,50,481,307,723,1100,235,147,851,504,1068,780,490,126,771,831,358,711,68,281,645,644,862,705,372,217,679,1097,1005,728,1739,571,40,1293,782,378,802,245,1370,1512,9,372,448,64,36,1212,141,585,1468,278,286,476,239,285,664,277,43,665,1037,654,205,1238,36,41,276,945,624,47,91,1569,284,107,845,60,961,30,21,269,1091,304,364,339,169,430,176,156,1483,1510,111,146,447,590,1227,611,483,428,396,839,307,901,380,128,80,535,461,482,379,59,281,977,44,966,545,37,163,845,845,151,936,269,938,612,1284,863,516,927,511,825,125,599,101,394,1062,140,483,218,83,443,404,492,78,507,860,1741,43,128,60,566,181,5,554,937,163,280,655,184,480,408,127,935,613,766,46,312,770,697,275,13,84,600,168,83,281,599,607,1441,197,344,0,302,414,147,370,748,421,844,871,319,666,117,640,247,167,529,324,252,235,303,443,1305,796,109,20,364,532,1388,708,769,916,340,405,90,47,504,516,97,535,28,69,960,590,254,106,188,190,1388,698,246,264,98,229,1648,292,710,14,421,31,147,1493,552,1371,454,4,146,674,452,1267,1027,170,141,936,1341,884,558,276,631,68,39,2,464,1,839,318,881,413,2,452,352,34,89,323,884,1439,243,79,56,128,1273,1134,606,11,682,747,415,599,782,179,269,320,682,177,336,466,10,370,159,1636,367,888,573,171,682,60,9,59,332,10,1496,637,1029,413,186,1183,77,309,461,883,1079,699,233,69,259,108,1160,435,480,495,13,858,718,126,115,728,1008,133,442,7,598,1475,1156,226,162,415,3,151,72,527,792,494,763,144,64,490,273,1245,300,465,744,36,1465,251,8,494,1126,362,180,1263,175,141,1041,103,163,205,568,93,699,103,437,204,931,563,550,88,415,146,265,31,221,1123,835,375,1101,578,388,92,1417,845,308,343,499,158,293,242,4,509,574,254,1556,69,668,691,0,558,16,687,1210,166,748,400,863,66,600,771,1073,561,738,398,384,232,350,393,1113,1222,153,462,907,797,712,18,1463,1185,1055,994,57,130,265,131,52,463,902,453,38,132,783,1560,232,169,1162,173,311,5,1477,397,336,480,540,491,67,340,27,291,341,35,275,78,1525,387,218,63,79,533,4,569,1643,595,1508,851,39,1200,912,10,53,42,60,154,1174,155,275,137,677,367,1373,4,708,441,756,647,1054,872,1039,109,530,1179,939,429,567,866,1411,436,23,212,184,66,79,831,538,90,827,678,549,313,434,60,907,284,171,570,1091,603,447,122,1092,29,789,563,462,15,310,340,16,365,393,614,48,368,42,457,736,737,1008,513,61,764,366,400,525,1683,1177,909,908,112,734,16,79,917,541,127,107,79,1208,32,258,596,166,376,1313,735,1457,864,563,55,439,54,694,81,93,48,470,1028,689,1177,1331,155,412,847,250,405,387,8,456,18,619,533,729,1475,1182,935,210,55,355,958,15,32,598,85,175,471,1087,280,652,53,13,225,12,488,717,353,2,134,351,698,276,456,209,535,604,19,12,785,3,63,879,437,216,1,1275,811,786,417,33,51,733,1074,143,309,65,555,557,78,611,909,260,973,701,998,490,213,9,233,760,933,916,437,1369,1952,372,324,859,670,73,296,1391,127,407,230,52,16,547,803,883,258,308,710,343,1290,184,8,41,9,68,104,175,1034,1544,219,752,327,690,134,601,1574,214,385,1233,231,267,944,1533,349,431,97,632,278,1505,162,888,62,90,489,351,990,846,14,159,134,14,314,148,214,1153,513,114,6,49,10,14,957,219,16,204,954,863,50,482,90,696,99,253,252,433,57,385,54,343,106,154,78,1595,590,380,102,825,1933,191,1328,374,263,355,137,494,60,781,1113,391,274,325,326,14,965,269,15,32,742,81,393,730,892,982,103,890,499,58,816,292,29,480,173,831,132,1033,1511,1137,1511,22,1105,146,344,308,915,540,1371,1238,414,352,304,841,749,6,491,30,1322,415,293,1207,31,90,636,303,1551,354,23,275,18,32,623,1483,49,12,311,407,1551,296,252,647,778,1499,98,1220,264,1020,1440,377,1125,8,72,270,162,348,3,1023,965,719,62,1467,1176,663,439,557,654,85,1493,70,349,10,727,15,1173,387,529,608,1398,905,619,173,849,1493,49,88,4,708,1084,370,1007,285,4,530,770,561,26,669,1100,30,876,649,178,32,354,621,911,334,514,9,449,1019,107 \ No newline at end of file diff --git a/2021/day8/day8.py b/2021/day8/day8.py new file mode 100644 index 0000000..62d76fb --- /dev/null +++ b/2021/day8/day8.py @@ -0,0 +1,46 @@ +def part1(inp): + total = 0 + for display in inp: + _, output_values = display + for value in output_values: + if len(value) in [2, 4, 3, 7]: + total += 1 + print(f"Part 1 : {total}") + + +def part2(inp): + for display in inp: + patterns, values = display + patterns = sorted(patterns, key=lambda x: len(x)) + print(patterns) + # easy + d1 = [x for x in patterns if len(x) == 2][0] + print("1", d1) + d4 = [x for x in patterns if len(x) == 4][0] + print("4", d4) + d7 = [x for x in patterns if len(x) == 3][0] + print("7", d7) + d8 = [x for x in patterns if len(x) == 7][0] + print("8", d8) + + # 3 is the only digit that has all common segments with 1 + breakpoint() + d3 = [x for x in patterns if set(d1).issubset(set(x)) and len(x) == 5][0] + print("3", d3) + + break + +def main(infile): + inp = [] + with open(infile) as f: + for display in f: + display = display.rstrip().split(" | ") + signal_patterns = display[0].split(" ") + output_values = display[1].split(" ") + inp.append([signal_patterns, output_values]) + part1(inp) + part2(inp) + + +if __name__ == "__main__": + main("input.txt") diff --git a/2021/day8/input.txt b/2021/day8/input.txt new file mode 100644 index 0000000..0f8b376 --- /dev/null +++ b/2021/day8/input.txt @@ -0,0 +1 @@ +acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf \ No newline at end of file