diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e84951b --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +.dub +docs.json +__dummy.html +docs/ +/adventofcode2021 +adventofcode2021.so +adventofcode2021.dylib +adventofcode2021.dll +adventofcode2021.a +adventofcode2021.lib +adventofcode2021-test-* +*.exe +*.o +*.obj +*.lst diff --git a/dub.json b/dub.json new file mode 100644 index 0000000..b57702a --- /dev/null +++ b/dub.json @@ -0,0 +1,9 @@ +{ + "authors": [ + "Andrew Lalis" + ], + "copyright": "Copyright © 2021, Andrew Lalis", + "description": "Advent of Code 2021 attempts.", + "license": "MIT", + "name": "adventofcode2021" +} \ No newline at end of file diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..b03f9df --- /dev/null +++ b/source/app.d @@ -0,0 +1,8 @@ +import std.stdio; + +import day1.part1; +import day1.part2; + +void main() { + slidingSum(); +} diff --git a/source/day1/input.txt b/source/day1/input.txt new file mode 100644 index 0000000..13c1d1f --- /dev/null +++ b/source/day1/input.txt @@ -0,0 +1,2000 @@ +199 +203 +200 +201 +196 +195 +196 +214 +227 +225 +229 +260 +246 +253 +271 +281 +280 +295 +310 +293 +295 +297 +298 +297 +306 +299 +290 +292 +289 +287 +291 +296 +294 +287 +286 +287 +289 +290 +281 +283 +282 +275 +256 +265 +242 +231 +226 +216 +227 +226 +249 +238 +237 +247 +245 +250 +264 +267 +259 +258 +264 +261 +249 +246 +239 +240 +259 +258 +280 +279 +278 +281 +272 +256 +255 +271 +286 +290 +286 +288 +290 +294 +292 +294 +292 +293 +294 +291 +298 +303 +301 +330 +331 +336 +362 +371 +374 +368 +367 +368 +366 +364 +371 +377 +378 +384 +386 +388 +393 +409 +423 +422 +411 +412 +426 +430 +441 +460 +462 +464 +469 +461 +483 +452 +455 +457 +468 +506 +509 +514 +525 +512 +524 +523 +522 +531 +549 +548 +549 +548 +524 +525 +528 +526 +537 +544 +549 +546 +525 +532 +535 +543 +544 +545 +525 +536 +538 +541 +524 +534 +542 +538 +539 +544 +543 +539 +538 +527 +553 +552 +562 +560 +561 +573 +574 +570 +571 +568 +569 +571 +573 +566 +567 +584 +591 +607 +605 +604 +605 +617 +615 +617 +606 +608 +609 +613 +616 +635 +634 +637 +635 +634 +655 +639 +646 +637 +628 +622 +648 +645 +640 +652 +653 +659 +661 +658 +650 +667 +670 +668 +680 +682 +673 +668 +678 +677 +675 +676 +677 +675 +688 +681 +676 +684 +675 +664 +665 +654 +660 +664 +663 +667 +658 +661 +676 +675 +680 +681 +690 +693 +697 +721 +733 +739 +740 +739 +751 +765 +766 +752 +748 +763 +772 +771 +782 +784 +797 +799 +817 +812 +811 +795 +783 +785 +790 +785 +784 +782 +771 +756 +759 +783 +772 +774 +779 +783 +786 +780 +783 +779 +777 +778 +777 +762 +778 +777 +770 +773 +769 +770 +771 +764 +765 +761 +745 +754 +758 +759 +746 +738 +740 +742 +767 +765 +754 +755 +758 +762 +760 +761 +757 +761 +770 +773 +777 +776 +777 +769 +767 +793 +805 +798 +793 +803 +809 +808 +817 +804 +809 +805 +804 +806 +808 +812 +830 +831 +832 +831 +819 +820 +823 +822 +812 +814 +812 +842 +858 +855 +856 +862 +860 +855 +839 +840 +841 +840 +846 +850 +859 +861 +852 +846 +848 +845 +833 +843 +846 +849 +889 +890 +894 +904 +907 +904 +905 +906 +907 +910 +902 +906 +900 +907 +906 +899 +887 +867 +866 +873 +878 +885 +880 +890 +863 +860 +859 +885 +856 +853 +854 +852 +864 +855 +874 +875 +874 +875 +871 +872 +867 +870 +873 +874 +860 +862 +879 +878 +879 +880 +881 +882 +878 +882 +885 +890 +902 +903 +905 +906 +909 +917 +916 +934 +932 +935 +937 +935 +910 +911 +907 +902 +904 +910 +917 +918 +943 +945 +959 +973 +972 +969 +973 +972 +958 +957 +959 +963 +968 +961 +960 +973 +971 +970 +977 +1002 +1012 +1011 +1007 +1017 +1024 +1014 +1013 +1019 +1005 +984 +983 +980 +993 +999 +982 +988 +989 +990 +992 +988 +989 +988 +981 +958 +973 +966 +965 +942 +926 +933 +928 +932 +940 +927 +930 +920 +930 +897 +905 +907 +889 +882 +888 +889 +872 +873 +885 +884 +886 +887 +898 +901 +902 +895 +880 +894 +886 +882 +885 +890 +896 +880 +881 +866 +872 +875 +903 +901 +907 +899 +920 +921 +934 +935 +937 +928 +929 +927 +928 +935 +906 +908 +906 +882 +883 +887 +891 +892 +898 +903 +907 +909 +916 +917 +945 +935 +937 +928 +918 +927 +916 +929 +919 +931 +954 +953 +954 +956 +953 +947 +937 +941 +960 +968 +951 +955 +954 +938 +939 +946 +945 +947 +948 +951 +943 +949 +946 +954 +952 +949 +943 +936 +944 +941 +950 +952 +948 +947 +971 +970 +969 +974 +971 +1000 +1005 +1007 +1009 +1008 +1000 +998 +999 +1001 +1008 +1001 +1024 +1005 +998 +1016 +1018 +1019 +1020 +1021 +1057 +1056 +1052 +1044 +1045 +1057 +1058 +1074 +1075 +1069 +1056 +1055 +1056 +1052 +1059 +1070 +1043 +1039 +1035 +1025 +1015 +1018 +1029 +1033 +1042 +1038 +1031 +1033 +1023 +1016 +1019 +1018 +1016 +1018 +1020 +1009 +1008 +997 +973 +983 +990 +996 +1002 +1001 +1000 +1002 +1001 +1015 +1016 +1015 +1019 +1020 +1016 +1010 +1009 +1008 +1016 +1018 +1015 +1011 +1004 +999 +1000 +1027 +1035 +1050 +1051 +1068 +1058 +1072 +1086 +1089 +1060 +1043 +1028 +1026 +1028 +1052 +1050 +1042 +1040 +1036 +1055 +1050 +1051 +1049 +1053 +1054 +1059 +1058 +1066 +1059 +1061 +1070 +1071 +1068 +1059 +1060 +1065 +1066 +1068 +1060 +1059 +1070 +1069 +1068 +1067 +1068 +1070 +1069 +1066 +1073 +1065 +1066 +1050 +1052 +1050 +1056 +1055 +1063 +1062 +1057 +1054 +1048 +1047 +1051 +1049 +1043 +1017 +1016 +1002 +1005 +992 +994 +999 +1001 +1017 +1026 +1024 +1028 +1040 +1039 +1046 +1022 +1023 +992 +995 +1019 +1022 +1023 +1011 +1004 +1005 +993 +991 +992 +995 +996 +997 +1009 +1011 +1012 +1017 +1030 +1029 +1031 +1024 +1033 +1008 +1006 +1018 +1020 +1015 +1020 +1031 +1029 +1026 +1030 +1042 +1039 +1029 +1027 +1008 +1003 +1019 +1000 +1001 +995 +996 +994 +988 +989 +988 +987 +994 +997 +1000 +994 +997 +989 +991 +985 +975 +982 +981 +986 +980 +979 +973 +984 +985 +983 +981 +956 +925 +924 +919 +913 +915 +917 +914 +916 +895 +910 +924 +919 +914 +928 +935 +927 +930 +932 +936 +939 +944 +948 +947 +946 +945 +948 +944 +942 +944 +958 +956 +947 +955 +990 +995 +996 +1007 +1009 +1013 +1012 +1011 +1020 +1019 +1021 +1016 +1038 +1050 +1043 +1046 +1047 +1043 +1040 +1039 +1045 +1046 +1030 +1031 +1034 +1035 +1045 +1066 +1085 +1092 +1101 +1100 +1101 +1099 +1084 +1085 +1086 +1090 +1091 +1087 +1100 +1099 +1092 +1095 +1101 +1102 +1118 +1117 +1138 +1139 +1132 +1127 +1131 +1132 +1130 +1128 +1130 +1150 +1149 +1157 +1158 +1156 +1157 +1155 +1170 +1175 +1167 +1166 +1177 +1174 +1186 +1187 +1186 +1185 +1189 +1193 +1204 +1191 +1196 +1198 +1186 +1198 +1215 +1210 +1211 +1212 +1211 +1207 +1206 +1198 +1202 +1190 +1185 +1190 +1199 +1222 +1225 +1222 +1234 +1236 +1260 +1261 +1262 +1272 +1289 +1286 +1290 +1297 +1295 +1270 +1291 +1299 +1303 +1318 +1342 +1356 +1361 +1362 +1326 +1325 +1333 +1329 +1349 +1353 +1367 +1368 +1371 +1369 +1353 +1351 +1349 +1352 +1354 +1353 +1356 +1355 +1362 +1366 +1368 +1370 +1349 +1354 +1357 +1356 +1360 +1357 +1361 +1363 +1364 +1345 +1338 +1335 +1334 +1322 +1329 +1333 +1334 +1331 +1330 +1337 +1348 +1347 +1348 +1317 +1307 +1313 +1330 +1323 +1324 +1327 +1334 +1336 +1334 +1332 +1338 +1341 +1342 +1373 +1375 +1374 +1372 +1373 +1371 +1370 +1375 +1363 +1361 +1366 +1389 +1390 +1366 +1380 +1381 +1379 +1383 +1400 +1402 +1404 +1409 +1410 +1434 +1439 +1443 +1448 +1434 +1462 +1463 +1462 +1464 +1465 +1471 +1465 +1464 +1469 +1473 +1480 +1486 +1488 +1492 +1494 +1495 +1496 +1504 +1496 +1495 +1472 +1469 +1465 +1466 +1455 +1464 +1465 +1474 +1475 +1490 +1499 +1490 +1492 +1489 +1500 +1504 +1500 +1499 +1500 +1510 +1511 +1516 +1511 +1515 +1512 +1499 +1506 +1509 +1493 +1495 +1496 +1485 +1486 +1476 +1475 +1484 +1485 +1469 +1477 +1478 +1467 +1468 +1469 +1477 +1478 +1475 +1473 +1461 +1468 +1470 +1496 +1491 +1506 +1505 +1526 +1530 +1528 +1529 +1533 +1555 +1558 +1557 +1554 +1557 +1559 +1560 +1562 +1561 +1563 +1564 +1550 +1559 +1558 +1559 +1554 +1535 +1538 +1541 +1540 +1529 +1528 +1519 +1527 +1535 +1518 +1523 +1519 +1536 +1562 +1561 +1566 +1568 +1583 +1584 +1599 +1585 +1581 +1588 +1559 +1561 +1574 +1587 +1574 +1576 +1589 +1588 +1582 +1581 +1584 +1604 +1563 +1555 +1561 +1564 +1548 +1541 +1542 +1546 +1548 +1550 +1566 +1544 +1523 +1525 +1521 +1541 +1550 +1551 +1548 +1558 +1561 +1560 +1550 +1563 +1578 +1582 +1563 +1557 +1558 +1557 +1559 +1574 +1569 +1577 +1576 +1577 +1571 +1573 +1572 +1574 +1577 +1581 +1566 +1535 +1539 +1540 +1536 +1545 +1544 +1513 +1511 +1509 +1502 +1489 +1490 +1495 +1488 +1492 +1513 +1519 +1520 +1521 +1530 +1512 +1514 +1512 +1517 +1518 +1520 +1519 +1527 +1526 +1527 +1528 +1529 +1522 +1525 +1529 +1525 +1524 +1525 +1527 +1529 +1524 +1523 +1525 +1533 +1535 +1536 +1555 +1557 +1573 +1572 +1571 +1590 +1593 +1594 +1593 +1565 +1566 +1554 +1572 +1567 +1569 +1566 +1597 +1616 +1630 +1641 +1638 +1634 +1647 +1651 +1649 +1652 +1659 +1660 +1651 +1656 +1653 +1654 +1653 +1658 +1643 +1642 +1641 +1654 +1655 +1656 +1666 +1673 +1682 +1665 +1663 +1670 +1654 +1668 +1667 +1656 +1646 +1655 +1662 +1680 +1683 +1682 +1683 +1684 +1722 +1702 +1710 +1711 +1715 +1728 +1730 +1735 +1736 +1750 +1755 +1756 +1755 +1752 +1761 +1768 +1767 +1760 +1751 +1747 +1764 +1763 +1762 +1748 +1753 +1745 +1752 +1754 +1746 +1747 +1748 +1756 +1761 +1756 +1734 +1711 +1712 +1721 +1727 +1726 +1731 +1729 +1715 +1716 +1733 +1738 +1740 +1756 +1755 +1748 +1744 +1751 +1756 +1774 +1775 +1755 +1746 +1754 +1758 +1767 +1785 +1786 +1787 +1788 +1766 +1787 +1786 +1788 +1762 +1757 +1752 +1760 +1781 +1750 +1757 +1762 +1767 +1770 +1744 +1743 +1747 +1756 +1763 +1775 +1769 +1760 +1759 +1766 +1768 +1764 +1759 +1761 +1792 +1798 +1800 +1802 +1813 +1810 +1822 +1824 +1816 +1842 +1843 +1835 +1833 +1834 +1837 +1826 +1830 +1850 +1855 +1857 +1862 +1856 +1877 +1878 +1875 +1850 +1847 +1846 +1847 +1846 +1826 +1829 +1825 +1817 +1818 +1815 +1834 +1833 +1822 +1825 +1815 +1817 +1815 +1816 +1810 +1822 +1815 +1817 +1821 +1820 +1814 +1802 +1799 +1814 +1834 +1830 +1842 +1828 +1836 +1849 +1851 +1849 +1848 +1849 +1865 +1885 +1895 +1904 +1910 +1921 +1937 +1941 +1970 +1968 +1975 +1982 +2003 +2012 +2011 +2008 +2006 +1997 +1994 +1991 +1989 +1988 +1995 +1976 +2006 +2018 +2015 +2023 +2022 +2023 +2048 +2038 +2039 +2041 +2057 +2058 +2059 +2060 +2059 +2061 +2031 +2032 +2033 +2020 +2019 +2023 +2032 +2034 +2027 +2029 +2027 +2018 +2028 +2055 +2054 +2043 +2047 +2046 +2055 +2047 +2056 +2037 +2032 +2003 +2002 +1999 +2001 +1996 +2000 +2002 +2004 +1988 +1990 +1999 +1986 +1988 +1986 +1985 +1977 +1978 +1977 +1973 +1977 +1978 +1985 +1988 +1964 +1967 +1989 +1984 +1980 +1979 +1980 +1999 +2006 +2004 +2018 +2051 +2024 +2023 +2025 +2026 +2012 +2000 +2017 +2018 +2017 +2019 +2017 +2025 +2030 +2038 +2016 +2001 +2020 +2018 +2010 +2013 +2028 +2031 +2018 +2019 +2009 +2015 +2011 +2012 +2024 +2027 +2028 +2018 +2007 +2009 +2011 +2009 +2008 +2006 +2008 +2013 +2017 +2027 +2030 +2031 +2029 +2045 +2064 +2065 +2064 +2077 +2075 +2066 +2037 +2038 +2043 +2047 +2049 +2055 +2090 +2092 +2101 +2115 +2117 +2130 +2131 +2130 +2136 +2141 +2145 +2151 +2150 +2149 +2150 +2143 +2150 +2139 +2142 +2140 +2146 +2147 +2148 +2146 +2141 +2158 +2176 +2195 +2194 +2190 +2187 +2188 +2205 +2206 +2208 +2207 +2208 +2200 +2199 +2193 +2169 +2188 +2189 +2183 +2202 +2170 +2164 +2165 +2166 +2177 +2181 +2198 +2197 +2207 +2201 +2206 +2203 +2209 +2213 +2214 +2216 +2218 +2219 +2214 +2235 +2239 +2235 +2253 +2246 +2247 +2246 +2247 +2246 +2251 +2249 +2275 +2285 +2293 +2294 +2295 +2299 +2303 +2302 +2303 +2276 +2282 +2287 +2294 +2299 +2301 +2300 +2306 +2294 +2301 +2296 +2289 +2291 +2300 +2301 +2287 +2286 +2307 +2305 +2315 +2305 +2309 +2334 +2337 +2348 +2349 +2350 +2332 +2342 +2341 +2338 +2310 +2316 +2333 +2335 +2330 +2342 +2349 +2341 +2363 +2368 +2374 +2360 +2359 +2350 +2348 +2359 +2365 +2364 +2355 +2341 +2339 +2345 +2338 +2335 +2323 +2324 +2318 +2294 +2296 +2297 +2286 +2282 +2278 +2276 +2272 +2267 +2269 +2270 +2269 +2266 +2268 +2264 +2268 +2260 +2263 +2297 +2311 +2319 +2301 +2292 +2302 +2309 +2307 +2303 +2306 +2311 +2293 +2278 +2284 +2288 +2271 +2273 +2275 +2278 +2279 +2283 +2318 +2315 +2312 +2325 +2346 +2339 +2314 +2303 +2304 +2279 +2278 +2269 +2267 +2270 +2277 +2287 +2285 +2288 +2289 +2288 +2284 +2283 +2281 +2289 +2288 +2275 +2278 +2265 +2267 +2262 +2259 +2260 +2261 +2251 +2250 +2254 +2253 +2242 +2240 +2241 +2226 +2232 +2236 +2201 +2182 +2183 +2191 +2185 +2192 +2197 +2211 +2210 +2208 +2218 +2219 +2228 +2227 +2225 +2226 +2225 +2226 +2229 +2233 +2237 +2236 +2211 +2215 +2216 +2236 +2227 +2228 +2227 +2226 +2244 +2260 +2258 +2257 +2258 +2250 +2248 +2229 +2225 +2223 +2222 +2221 +2220 +2219 +2213 +2214 +2212 +2216 +2217 +2220 +2216 +2217 +2223 +2227 +2231 +2225 +2228 +2230 +2233 +2226 +2224 +2241 +2221 +2220 +2224 +2221 +2220 +2223 +2221 +2246 +2247 +2246 +2222 +2226 +2237 +2250 +2251 +2276 +2288 +2309 +2304 +2330 +2323 +2327 +2350 +2349 +2348 +2346 +2354 +2350 +2323 +2320 +2313 +2315 +2314 +2316 +2332 +2338 +2341 diff --git a/source/day1/part1.d b/source/day1/part1.d new file mode 100644 index 0000000..17edeb4 --- /dev/null +++ b/source/day1/part1.d @@ -0,0 +1,17 @@ +module day1.part1; + +import std.stdio; +import util.fileutils; + +void sonarSweep() { + int increaseCount = 0; + auto values = readInts("source/day1/input.txt"); + int previousValue = values[0]; + foreach(value; values[1..$]) { + if (value > previousValue) { + increaseCount++; + } + previousValue = value; + } + writefln("%d", increaseCount); +} \ No newline at end of file diff --git a/source/day1/part2.d b/source/day1/part2.d new file mode 100644 index 0000000..83d0ec7 --- /dev/null +++ b/source/day1/part2.d @@ -0,0 +1,20 @@ +module day1.part2; + +import std.stdio; +import util.fileutils; + +void slidingSum() { + int[] values = readInts("source/day1/input.txt"); + int previousSum = values[0] + values[1] + values[2]; + int increaseCount = 0; + foreach (i; 1 .. (values.length - 2)) { + int currentSum = values[i] + values[i + 1] + values[i + 2]; + bool increase = false; + if (currentSum > previousSum) { + increaseCount++; + increase = true; + } + previousSum = currentSum; + } + writefln("%d", increaseCount); +} \ No newline at end of file diff --git a/source/util/fileutils.d b/source/util/fileutils.d new file mode 100644 index 0000000..3a12b9c --- /dev/null +++ b/source/util/fileutils.d @@ -0,0 +1,16 @@ +module util.fileutils; + +import std.file; +import std.string; +import std.algorithm; +import std.array; +import std.conv; + +int[] readInts(string filename) { + return readText("source/day1/input.txt") + .split("\n") + .map!(s => s.strip()) + .filter!(s => s.length > 0) + .map!(s => s.to!int) + .array; +} \ No newline at end of file