From 7c7a7ca9986266919f58135c7393aa9e4217813b Mon Sep 17 00:00:00 2001 From: maria Date: Mon, 11 Dec 2023 21:09:30 +0100 Subject: [PATCH] cleanup - 2021 - day 1 & 2 --- 2021/day1/day1.go | 46 +- 2021/day1/day1_test.go | 29 + 2021/day1/{input1.txt => example1.txt} | 0 2021/day1/input2.txt | 2000 ------------------------ 2021/day1/readme.md | 91 -- 2021/day2/day2.go | 75 +- 2021/day2/day2_test.go | 55 + 2021/day2/{input1.txt => example1.txt} | 0 2021/day2/input2.txt | 1000 ------------ 2021/day2/readme.md | 76 - 2021/main.go | 11 +- 2021/readme.md | 9 - readme.md | 16 +- 13 files changed, 130 insertions(+), 3278 deletions(-) create mode 100644 2021/day1/day1_test.go rename 2021/day1/{input1.txt => example1.txt} (100%) delete mode 100644 2021/day1/input2.txt delete mode 100644 2021/day1/readme.md create mode 100644 2021/day2/day2_test.go rename 2021/day2/{input1.txt => example1.txt} (100%) delete mode 100644 2021/day2/input2.txt delete mode 100644 2021/day2/readme.md delete mode 100644 2021/readme.md diff --git a/2021/day1/day1.go b/2021/day1/day1.go index c12f4bf..0da5b48 100644 --- a/2021/day1/day1.go +++ b/2021/day1/day1.go @@ -2,32 +2,21 @@ package day1 import ( "bufio" - "fmt" - "log" - "os" - "strconv" + "github.com/mariamihai/advent-of-code/util" ) -func Problem1() { - file, err := os.Open("./day1/input2.txt") - if err != nil { - log.Fatal(err) - } - - defer func(file *os.File) { - err := file.Close() - if err != nil { - log.Fatal(err) - } - }(file) - - scanner := bufio.NewScanner(file) +func Problem1(filename string) int { + file := util.ReadFile(filename) + defer util.CloseFile()(file) var count = 0 var previousValue = 10000 + hasDepthIncreased := func(currentDepth, previousValue int) bool { return currentDepth > previousValue } + + scanner := bufio.NewScanner(file) for scanner.Scan() { - currentDepth := readLine(scanner.Text()) + currentDepth := util.StringToInt(scanner.Text()) if hasDepthIncreased(currentDepth, previousValue) { count++ @@ -36,22 +25,7 @@ func Problem1() { previousValue = currentDepth } - fmt.Printf("Part 1 - Number of larger measurements: %d\n", count) - - if err := scanner.Err(); err != nil { - log.Fatal(err) - } -} - -func readLine(line string) int { - currentDepth, err := strconv.Atoi(line) - if err != nil { - log.Fatal("Error during conversion") - } - - return currentDepth -} + util.Boom(scanner.Err()) -func hasDepthIncreased(currentDepth, previousValue int) bool { - return currentDepth > previousValue + return count } diff --git a/2021/day1/day1_test.go b/2021/day1/day1_test.go new file mode 100644 index 0000000..82ba6e4 --- /dev/null +++ b/2021/day1/day1_test.go @@ -0,0 +1,29 @@ +package day1 + +import "testing" + +func TestProblem1(t *testing.T) { + tests := []struct { + description string + filename string + expected int + }{ + { + description: "Should validate PROBLEM 1 against provided EXAMPLE", + filename: "example1.txt", + expected: 7, + }, + { + description: "Should validate PROBLEM 1 against provided INPUT", + filename: "input1.txt", + expected: 1298, + }, + } + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + if actual := Problem1(tt.filename); actual != tt.expected { + t.Errorf("Problem1() = %v, want %v", actual, tt.expected) + } + }) + } +} diff --git a/2021/day1/input1.txt b/2021/day1/example1.txt similarity index 100% rename from 2021/day1/input1.txt rename to 2021/day1/example1.txt diff --git a/2021/day1/input2.txt b/2021/day1/input2.txt deleted file mode 100644 index afa39ca..0000000 --- a/2021/day1/input2.txt +++ /dev/null @@ -1,2000 +0,0 @@ -140 -154 -165 -150 -151 -161 -172 -174 -166 -168 -176 -191 -192 -189 -190 -191 -202 -203 -206 -207 -167 -179 -204 -206 -208 -209 -216 -198 -200 -207 -217 -218 -223 -227 -228 -243 -241 -246 -278 -255 -256 -273 -295 -291 -288 -290 -303 -325 -301 -292 -291 -309 -313 -315 -319 -339 -340 -327 -336 -335 -340 -331 -334 -316 -317 -323 -324 -323 -312 -298 -291 -285 -288 -266 -267 -266 -258 -259 -257 -259 -250 -265 -270 -267 -271 -272 -277 -281 -277 -284 -291 -283 -284 -291 -292 -278 -285 -296 -294 -297 -298 -301 -300 -311 -310 -322 -320 -337 -334 -333 -331 -317 -323 -358 -362 -364 -371 -383 -424 -427 -438 -443 -441 -445 -444 -440 -458 -460 -459 -457 -455 -446 -440 -439 -438 -430 -422 -424 -438 -449 -450 -451 -443 -459 -458 -479 -473 -471 -478 -496 -498 -488 -512 -513 -523 -545 -544 -543 -539 -540 -553 -554 -553 -561 -565 -572 -576 -584 -586 -591 -603 -585 -600 -601 -599 -596 -587 -586 -587 -588 -598 -601 -599 -596 -588 -589 -595 -588 -586 -591 -598 -595 -597 -616 -615 -599 -598 -576 -570 -573 -599 -600 -601 -602 -604 -605 -617 -608 -607 -606 -607 -605 -604 -603 -601 -602 -584 -590 -597 -596 -600 -583 -579 -582 -576 -580 -583 -581 -582 -558 -566 -562 -581 -589 -570 -571 -566 -562 -564 -565 -567 -564 -557 -565 -566 -562 -564 -567 -568 -570 -554 -546 -548 -511 -510 -509 -507 -500 -513 -510 -513 -500 -502 -508 -527 -531 -539 -538 -545 -546 -548 -555 -557 -558 -561 -566 -567 -571 -570 -605 -627 -625 -615 -617 -588 -597 -599 -598 -609 -612 -611 -615 -617 -618 -630 -622 -619 -627 -674 -691 -695 -715 -734 -736 -737 -739 -753 -751 -759 -762 -732 -709 -711 -730 -733 -729 -746 -752 -755 -756 -791 -792 -793 -790 -786 -808 -811 -844 -834 -846 -820 -821 -838 -842 -844 -835 -840 -843 -844 -836 -837 -841 -843 -852 -863 -865 -870 -868 -863 -865 -868 -875 -886 -904 -909 -917 -905 -906 -905 -920 -919 -917 -911 -924 -899 -900 -915 -923 -940 -941 -921 -922 -896 -908 -911 -893 -892 -912 -896 -900 -882 -884 -901 -902 -901 -907 -886 -889 -888 -898 -899 -896 -899 -883 -900 -893 -929 -907 -911 -916 -910 -912 -906 -911 -915 -920 -921 -923 -921 -922 -905 -923 -922 -938 -939 -941 -940 -931 -960 -966 -973 -986 -988 -1008 -1016 -1014 -987 -993 -1004 -1006 -1004 -1024 -1031 -1032 -1034 -1025 -1022 -1008 -1007 -1010 -1009 -997 -1002 -1007 -1010 -1018 -1014 -1016 -1023 -1031 -1032 -1048 -1049 -1056 -1055 -1064 -1065 -1084 -1081 -1084 -1088 -1090 -1109 -1115 -1144 -1146 -1144 -1142 -1143 -1160 -1158 -1163 -1167 -1183 -1184 -1195 -1182 -1181 -1169 -1181 -1175 -1188 -1187 -1188 -1189 -1187 -1195 -1196 -1197 -1216 -1215 -1223 -1222 -1209 -1218 -1223 -1222 -1225 -1196 -1193 -1198 -1200 -1208 -1207 -1216 -1202 -1197 -1172 -1164 -1178 -1179 -1167 -1177 -1178 -1175 -1178 -1191 -1198 -1200 -1208 -1212 -1215 -1225 -1233 -1240 -1243 -1235 -1237 -1244 -1245 -1248 -1249 -1251 -1250 -1256 -1261 -1262 -1244 -1250 -1240 -1234 -1225 -1229 -1216 -1213 -1214 -1206 -1209 -1211 -1205 -1228 -1230 -1232 -1233 -1226 -1216 -1204 -1194 -1196 -1165 -1176 -1186 -1198 -1195 -1204 -1218 -1216 -1219 -1220 -1225 -1229 -1227 -1231 -1241 -1244 -1241 -1249 -1248 -1253 -1254 -1247 -1248 -1249 -1258 -1266 -1270 -1269 -1283 -1298 -1295 -1306 -1309 -1330 -1335 -1339 -1326 -1327 -1291 -1302 -1301 -1313 -1314 -1315 -1318 -1315 -1316 -1315 -1338 -1336 -1341 -1347 -1343 -1368 -1374 -1377 -1378 -1383 -1384 -1367 -1368 -1367 -1378 -1371 -1372 -1373 -1365 -1367 -1366 -1369 -1365 -1367 -1361 -1360 -1368 -1366 -1363 -1366 -1374 -1380 -1364 -1339 -1341 -1340 -1339 -1340 -1341 -1348 -1356 -1358 -1359 -1351 -1356 -1345 -1322 -1323 -1324 -1325 -1326 -1336 -1335 -1331 -1330 -1339 -1331 -1329 -1346 -1343 -1350 -1357 -1358 -1334 -1336 -1335 -1338 -1348 -1346 -1363 -1365 -1368 -1365 -1386 -1383 -1381 -1383 -1379 -1386 -1381 -1384 -1378 -1379 -1372 -1389 -1393 -1389 -1414 -1415 -1414 -1405 -1417 -1418 -1415 -1427 -1429 -1428 -1447 -1430 -1426 -1425 -1410 -1412 -1413 -1427 -1424 -1413 -1401 -1402 -1423 -1431 -1436 -1440 -1447 -1446 -1447 -1446 -1447 -1446 -1451 -1450 -1451 -1450 -1446 -1451 -1456 -1453 -1456 -1459 -1490 -1494 -1493 -1502 -1504 -1524 -1523 -1522 -1539 -1546 -1545 -1552 -1554 -1555 -1557 -1563 -1562 -1566 -1567 -1566 -1567 -1566 -1565 -1568 -1570 -1572 -1573 -1586 -1588 -1590 -1591 -1589 -1607 -1608 -1595 -1596 -1595 -1594 -1599 -1604 -1612 -1608 -1609 -1606 -1603 -1602 -1601 -1613 -1597 -1598 -1585 -1604 -1625 -1626 -1627 -1634 -1636 -1637 -1642 -1637 -1638 -1630 -1632 -1631 -1630 -1639 -1640 -1668 -1647 -1663 -1668 -1646 -1653 -1665 -1667 -1668 -1671 -1679 -1673 -1678 -1679 -1684 -1677 -1678 -1688 -1687 -1686 -1691 -1697 -1699 -1705 -1717 -1685 -1708 -1707 -1708 -1706 -1709 -1699 -1700 -1699 -1710 -1709 -1708 -1714 -1728 -1721 -1715 -1717 -1694 -1706 -1720 -1722 -1730 -1722 -1728 -1730 -1737 -1755 -1763 -1747 -1742 -1743 -1727 -1736 -1730 -1742 -1744 -1734 -1726 -1707 -1720 -1719 -1725 -1683 -1682 -1683 -1688 -1682 -1681 -1682 -1684 -1689 -1649 -1650 -1652 -1655 -1662 -1665 -1666 -1667 -1689 -1684 -1677 -1678 -1667 -1666 -1670 -1672 -1670 -1675 -1672 -1659 -1660 -1689 -1688 -1691 -1696 -1687 -1689 -1688 -1689 -1690 -1695 -1705 -1710 -1713 -1715 -1706 -1703 -1707 -1683 -1677 -1670 -1664 -1675 -1676 -1681 -1682 -1679 -1673 -1678 -1668 -1685 -1703 -1716 -1712 -1709 -1715 -1720 -1730 -1735 -1741 -1754 -1740 -1749 -1750 -1753 -1746 -1753 -1768 -1780 -1781 -1818 -1821 -1831 -1830 -1838 -1839 -1838 -1843 -1829 -1833 -1835 -1847 -1845 -1849 -1850 -1852 -1828 -1830 -1836 -1839 -1855 -1857 -1837 -1838 -1843 -1830 -1825 -1848 -1838 -1840 -1843 -1831 -1813 -1816 -1815 -1810 -1809 -1817 -1816 -1818 -1806 -1802 -1803 -1807 -1805 -1770 -1759 -1757 -1756 -1759 -1751 -1752 -1753 -1747 -1755 -1762 -1766 -1767 -1777 -1778 -1772 -1784 -1783 -1789 -1795 -1797 -1823 -1824 -1820 -1823 -1824 -1843 -1866 -1868 -1864 -1860 -1861 -1864 -1862 -1849 -1850 -1862 -1860 -1862 -1863 -1862 -1870 -1893 -1896 -1897 -1888 -1889 -1913 -1914 -1911 -1924 -1928 -1943 -1958 -1957 -1951 -1939 -1929 -1931 -1930 -1928 -1930 -1946 -1954 -1955 -1944 -1934 -1935 -1950 -1957 -1956 -1957 -1958 -1952 -1951 -1954 -1953 -1941 -1949 -1950 -1968 -1961 -1960 -1955 -1949 -1946 -1918 -1919 -1920 -1933 -1934 -1936 -1937 -1942 -1913 -1919 -1920 -1921 -1925 -1924 -1925 -1932 -1935 -1937 -1927 -1913 -1916 -1922 -1906 -1907 -1908 -1901 -1897 -1901 -1899 -1900 -1902 -1903 -1896 -1901 -1904 -1909 -1901 -1912 -1914 -1910 -1913 -1914 -1915 -1917 -1921 -1922 -1920 -1919 -1912 -1909 -1911 -1908 -1909 -1950 -1953 -1983 -1989 -1993 -1992 -1994 -1997 -1980 -1990 -1991 -2011 -2016 -2015 -2026 -2030 -2033 -2044 -2039 -2040 -2042 -2044 -2053 -2054 -2033 -2030 -2029 -2030 -2013 -2016 -2027 -2021 -2017 -2018 -2011 -2009 -2008 -2007 -2004 -1994 -1992 -1973 -1974 -1969 -1963 -1968 -1969 -1967 -1980 -1981 -1984 -1981 -1977 -1982 -2002 -2007 -2003 -2000 -2019 -2013 -2014 -2017 -2054 -2081 -2079 -2077 -2078 -2064 -2063 -2065 -2061 -2051 -2069 -2095 -2093 -2098 -2095 -2084 -2085 -2087 -2088 -2082 -2080 -2081 -2082 -2079 -2068 -2072 -2073 -2092 -2096 -2097 -2078 -2083 -2090 -2081 -2093 -2077 -2084 -2087 -2074 -2075 -2061 -2052 -2058 -2065 -2052 -2053 -2055 -2067 -2063 -2072 -2077 -2075 -2081 -2082 -2089 -2109 -2110 -2107 -2102 -2098 -2085 -2089 -2091 -2097 -2087 -2077 -2073 -2079 -2074 -2076 -2077 -2078 -2061 -2076 -2077 -2080 -2091 -2074 -2075 -2080 -2085 -2090 -2095 -2103 -2106 -2108 -2116 -2120 -2128 -2129 -2130 -2146 -2153 -2166 -2165 -2169 -2173 -2158 -2149 -2154 -2162 -2161 -2162 -2164 -2165 -2166 -2167 -2159 -2180 -2181 -2177 -2193 -2208 -2206 -2214 -2216 -2239 -2218 -2219 -2228 -2203 -2204 -2212 -2211 -2219 -2227 -2228 -2229 -2230 -2231 -2228 -2237 -2236 -2245 -2246 -2244 -2252 -2262 -2260 -2270 -2269 -2270 -2289 -2290 -2313 -2289 -2292 -2293 -2295 -2300 -2301 -2302 -2314 -2317 -2329 -2330 -2310 -2313 -2314 -2293 -2294 -2299 -2300 -2323 -2325 -2320 -2317 -2323 -2310 -2296 -2297 -2293 -2320 -2310 -2311 -2315 -2319 -2320 -2328 -2338 -2345 -2349 -2370 -2374 -2376 -2375 -2370 -2372 -2373 -2374 -2377 -2388 -2392 -2355 -2361 -2397 -2398 -2386 -2400 -2398 -2399 -2383 -2384 -2394 -2397 -2400 -2401 -2406 -2409 -2411 -2426 -2447 -2441 -2443 -2418 -2417 -2418 -2407 -2408 -2409 -2423 -2420 -2394 -2393 -2401 -2410 -2404 -2423 -2429 -2431 -2432 -2405 -2406 -2407 -2400 -2401 -2390 -2389 -2391 -2387 -2388 -2399 -2415 -2416 -2421 -2420 -2424 -2432 -2436 -2419 -2422 -2421 -2411 -2426 -2427 -2441 -2433 -2439 -2431 -2429 -2431 -2434 -2442 -2456 -2439 -2440 -2443 -2452 -2453 -2456 -2459 -2458 -2470 -2471 -2470 -2471 -2474 -2475 -2480 -2477 -2473 -2472 -2501 -2500 -2507 -2516 -2530 -2548 -2557 -2589 -2587 -2589 -2593 -2598 -2600 -2603 -2605 -2604 -2605 -2606 -2614 -2615 -2623 -2626 -2638 -2632 -2635 -2641 -2642 -2641 -2660 -2661 -2685 -2683 -2689 -2693 -2674 -2676 -2698 -2727 -2725 -2716 -2735 -2729 -2753 -2756 -2765 -2766 -2774 -2775 -2774 -2775 -2779 -2785 -2806 -2803 -2813 -2803 -2801 -2802 -2781 -2782 -2780 -2785 -2788 -2784 -2779 -2755 -2758 -2759 -2773 -2774 -2776 -2779 -2802 -2787 -2781 -2785 -2779 -2778 -2776 -2779 -2781 -2777 -2772 -2771 -2756 -2758 -2764 -2776 -2775 -2768 -2771 -2769 -2764 -2765 -2764 -2754 -2755 -2753 -2767 -2770 -2774 -2783 -2782 -2783 -2778 -2782 -2784 -2775 -2778 -2774 -2773 -2774 -2776 -2784 -2796 -2797 -2796 -2807 -2798 -2799 -2800 -2801 -2802 -2805 -2817 -2818 -2809 -2819 -2818 -2800 -2796 -2795 -2796 -2797 -2799 -2805 -2833 -2835 -2804 -2809 -2810 -2784 -2806 -2815 -2824 -2849 -2837 -2832 -2831 -2839 -2841 -2843 -2844 -2856 -2855 -2858 -2857 -2852 -2858 -2850 -2845 -2841 -2831 -2825 -2818 -2820 -2819 -2822 -2831 -2832 -2825 -2824 -2821 -2822 -2807 -2808 -2810 -2823 -2811 -2821 -2813 -2822 -2810 -2818 -2824 -2837 -2851 -2852 -2835 -2831 -2832 -2820 -2801 -2802 -2803 -2812 -2840 -2838 -2841 -2843 -2846 -2854 -2856 -2857 -2886 -2900 -2906 -2907 -2902 -2910 -2918 -2925 -2926 -2924 -2925 -2918 -2925 -2913 -2912 -2932 -2938 -2936 -2941 -2948 -2950 -2951 -2954 -2958 -2961 -2966 -2995 -2999 -3005 -3017 -3025 -3024 -3025 -3027 -3030 -3034 -3030 -3032 -3035 -3034 -3033 -3032 -3033 -3024 -3027 -3028 -3029 -3030 -3034 -3064 -3050 -3056 -3053 -3065 -3072 -3067 -3065 -3068 -3086 -3089 -3100 -3109 -3104 -3105 -3108 -3098 -3105 -3110 -3111 -3129 -3137 -3138 -3121 -3113 -3125 -3133 -3135 -3137 -3143 -3133 -3134 -3133 -3137 -3145 -3164 -3169 -3170 -3184 -3165 -3168 -3166 -3159 -3158 -3165 -3162 -3145 -3133 -3134 -3149 -3153 -3151 -3158 -3160 -3161 -3164 -3165 -3159 -3145 -3146 -3147 -3148 -3131 -3140 -3148 -3144 -3167 -3149 -3150 -3142 -3115 -3129 -3134 -3136 -3141 -3164 -3165 -3162 -3161 -3169 -3170 -3169 -3171 -3167 -3173 -3181 -3186 -3187 -3186 -3190 -3191 -3193 -3194 -3204 -3205 -3222 -3221 -3231 -3232 -3233 -3234 -3231 -3240 -3246 -3258 -3260 -3277 -3278 -3274 -3275 -3273 -3276 -3270 -3290 -3292 -3300 -3320 -3324 -3323 -3321 -3320 -3301 -3308 -3305 -3306 -3301 -3309 -3306 -3327 -3334 -3333 -3338 -3339 -3341 -3342 -3353 -3325 -3333 -3342 -3329 -3330 -3323 -3320 -3334 -3347 -3336 -3324 -3330 -3328 -3324 -3321 -3322 -3325 -3311 -3313 -3311 -3340 -3342 -3346 -3350 -3351 -3334 -3360 -3344 -3347 -3341 -3344 -3358 -3350 -3344 -3354 -3349 -3363 -3380 -3372 -3375 -3369 -3365 -3376 -3375 -3381 -3380 -3385 -3405 -3407 -3408 -3411 -3406 -3408 -3410 -3414 -3438 -3459 -3456 -3457 -3449 -3452 -3455 -3460 -3465 -3466 -3468 -3466 -3465 -3466 -3467 -3472 -3468 -3462 -3489 -3492 -3494 -3470 -3466 -3478 -3494 -3481 -3497 -3498 -3499 -3500 -3502 -3492 -3489 -3502 -3503 -3518 -3523 -3526 -3534 -3536 -3542 -3545 -3548 -3549 -3551 -3554 -3546 -3570 -3584 -3585 -3552 -3551 -3558 -3559 -3560 -3558 -3551 -3556 -3549 -3541 -3544 -3559 -3577 -3581 -3562 -3564 -3565 -3547 -3551 -3552 -3557 -3569 -3571 -3563 -3575 -3587 -3591 -3595 -3596 diff --git a/2021/day1/readme.md b/2021/day1/readme.md deleted file mode 100644 index 0309726..0000000 --- a/2021/day1/readme.md +++ /dev/null @@ -1,91 +0,0 @@ -# Day 1: Sonar Sweep - -## Part 1 - -You're minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean! - -Before you know it, you're inside a submarine the Elves keep ready for situations like this. It's covered in Christmas lights (because of course it is), and it even has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough; there's a little meter that indicates the antenna's signal strength by displaying 0-50 stars. - -Your instincts tell you that in order to save Christmas, you'll need to get all fifty stars by December 25th. - -Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck! - -As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine. - -For example, suppose you had the following report: -``` -199 -200 -208 -210 -200 -207 -240 -269 -260 -263 -``` - -This report indicates that, scanning outward from the submarine, the sonar sweep found depths of `199, 200, 208, 210`, and so on. - -The first order of business is to figure out how quickly the depth increases, just so you know what you're dealing with - you never know if the keys will get carried into deeper water by an ocean current or a fish or something. - -To do this, count the number of times a depth measurement increases from the previous measurement. (There is no measurement before the first measurement.) In the example above, the changes are as follows: -``` -199 (N/A - no previous measurement) -200 (increased) -208 (increased) -210 (increased) -200 (decreased) -207 (increased) -240 (increased) -269 (increased) -260 (decreased) -263 (increased) -``` - -In this example, there are `7` measurements that are larger than the previous measurement. - -How many measurements are larger than the previous measurement? - -Your puzzle answer was `1298`. - -The first half of this puzzle is complete! It provides one gold star: `*` - -## Part Two -Considering every single measurement isn't as useful as you expected: there's just too much noise in the data. - -Instead, consider sums of a three-measurement sliding window. Again considering the above example: - -``` -199 A -200 A B -208 A B C -210 B C D -200 E C D -207 E F D -240 E F G -269 F G H -260 G H -263 H -``` - -Start by comparing the first and second three-measurement windows. The measurements in the first window are marked `A (199, 200, 208);` their sum is `199 + 200 + 208 = 607`. The second window is marked `B (200, 208, 210);` its sum is `618`. The sum of measurements in the second window is larger than the sum of the first, so this first comparison increased. - -Your goal now is to count the number of times the sum of measurements in this sliding window increases from the previous sum. So, compare A with B, then compare B with C, then C with D, and so on. Stop when there aren't enough measurements left to create a new three-measurement sum. - -In the above example, the sum of each three-measurement window is as follows: -``` -A: 607 (N/A - no previous sum) -B: 618 (increased) -C: 618 (no change) -D: 617 (decreased) -E: 647 (increased) -F: 716 (increased) -G: 769 (increased) -H: 792 (increased) -``` - -In this example, there are `5` sums that are larger than the previous sum. - -Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum? \ No newline at end of file diff --git a/2021/day2/day2.go b/2021/day2/day2.go index 9dc78e6..6b8beb8 100644 --- a/2021/day2/day2.go +++ b/2021/day2/day2.go @@ -2,37 +2,22 @@ package day2 import ( "bufio" - "fmt" - "log" - "os" - "strconv" + "github.com/mariamihai/advent-of-code/util" "strings" ) -func Problem1() { - file, err := os.Open("./day2/input2.txt") - if err != nil { - log.Fatal(err) - } - - defer func(file *os.File) { - err := file.Close() - if err != nil { - log.Fatal(err) - } - }(file) - - scanner := bufio.NewScanner(file) +func Problem1(filename string) int { + file := util.ReadFile(filename) + defer util.CloseFile()(file) var horizontalPosition = 0 var verticalPosition = 0 // depth + scanner := bufio.NewScanner(file) for scanner.Scan() { - lineStr := scanner.Text() + line := strings.Split(scanner.Text(), " ") - line := strings.Split(lineStr, " ") - - number := readLine(line[1]) + number := util.StringToInt(line[1]) if line[0] == "forward" { horizontalPosition += number } @@ -44,38 +29,24 @@ func Problem1() { } } - fmt.Printf("Part 1 - horizontal x depth: %d\n", horizontalPosition*verticalPosition) + util.Boom(scanner.Err()) - if err := scanner.Err(); err != nil { - log.Fatal(err) - } + return horizontalPosition * verticalPosition } -func Problem2() { - file, err := os.Open("./day2/input2.txt") - if err != nil { - log.Fatal(err) - } - - defer func(file *os.File) { - err := file.Close() - if err != nil { - log.Fatal(err) - } - }(file) - - scanner := bufio.NewScanner(file) +func Problem2(filename string) int { + file := util.ReadFile(filename) + defer util.CloseFile()(file) var horizontalPosition = 0 var verticalPosition = 0 // depth var aim = 0 + scanner := bufio.NewScanner(file) for scanner.Scan() { - lineStr := scanner.Text() + line := strings.Split(scanner.Text(), " ") - line := strings.Split(lineStr, " ") - - number := readLine(line[1]) + number := util.StringToInt(line[1]) if line[0] == "forward" { horizontalPosition += number verticalPosition += aim * number @@ -87,19 +58,7 @@ func Problem2() { aim -= number } } + util.Boom(scanner.Err()) - fmt.Printf("Part 2 - horizontal x depth: %d\n", horizontalPosition*verticalPosition) - - if err := scanner.Err(); err != nil { - log.Fatal(err) - } -} - -func readLine(str string) int { - currentDepth, err := strconv.Atoi(str) - if err != nil { - log.Fatal("Error during conversion") - } - - return currentDepth + return horizontalPosition * verticalPosition } diff --git a/2021/day2/day2_test.go b/2021/day2/day2_test.go new file mode 100644 index 0000000..71391ca --- /dev/null +++ b/2021/day2/day2_test.go @@ -0,0 +1,55 @@ +package day2 + +import "testing" + +func TestProblem1(t *testing.T) { + tests := []struct { + description string + filename string + expected int + }{ + { + description: "Should validate PROBLEM 1 against provided EXAMPLE", + filename: "example1.txt", + expected: 150, + }, + { + description: "Should validate PROBLEM 1 against provided INPUT", + filename: "input1.txt", + expected: 1604850, + }, + } + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + if actual := Problem1(tt.filename); actual != tt.expected { + t.Errorf("Problem1() = %v, want %v", actual, tt.expected) + } + }) + } +} + +func TestProblem2(t *testing.T) { + tests := []struct { + description string + filename string + expected int + }{ + { + description: "Should validate PROBLEM 2 against provided EXAMPLE", + filename: "example1.txt", + expected: 900, + }, + { + description: "Should validate PROBLEM 2 against provided INPUT", + filename: "input1.txt", + expected: 1685186100, + }, + } + for _, tt := range tests { + t.Run(tt.description, func(t *testing.T) { + if actual := Problem2(tt.filename); actual != tt.expected { + t.Errorf("Problem2() = %v, want %v", actual, tt.expected) + } + }) + } +} diff --git a/2021/day2/input1.txt b/2021/day2/example1.txt similarity index 100% rename from 2021/day2/input1.txt rename to 2021/day2/example1.txt diff --git a/2021/day2/input2.txt b/2021/day2/input2.txt deleted file mode 100644 index e0ec476..0000000 --- a/2021/day2/input2.txt +++ /dev/null @@ -1,1000 +0,0 @@ -forward 1 -forward 2 -down 5 -down 5 -down 4 -down 9 -up 6 -up 7 -down 2 -forward 9 -up 4 -forward 7 -forward 9 -up 3 -down 1 -forward 5 -down 3 -forward 3 -forward 3 -forward 3 -down 2 -down 5 -forward 7 -down 7 -up 7 -down 9 -down 1 -down 4 -down 9 -down 2 -forward 2 -forward 4 -up 7 -up 1 -forward 3 -forward 8 -forward 9 -forward 6 -forward 9 -forward 1 -forward 5 -down 9 -up 7 -down 9 -forward 2 -forward 9 -forward 1 -forward 5 -up 8 -down 5 -forward 4 -up 6 -up 9 -forward 2 -up 8 -down 1 -up 5 -forward 3 -down 1 -forward 6 -up 6 -forward 9 -forward 1 -forward 3 -down 4 -down 9 -down 8 -up 9 -down 9 -down 2 -down 4 -forward 2 -down 4 -down 2 -down 8 -up 3 -up 9 -forward 3 -down 5 -down 1 -up 6 -up 6 -down 4 -up 3 -forward 1 -down 2 -down 7 -forward 1 -down 4 -forward 5 -down 5 -forward 3 -forward 8 -down 4 -forward 3 -forward 2 -down 4 -forward 6 -forward 6 -down 9 -down 3 -up 7 -up 6 -down 8 -down 4 -down 4 -down 8 -down 4 -forward 5 -up 7 -down 8 -down 4 -down 5 -down 3 -forward 1 -up 1 -forward 9 -forward 4 -down 9 -forward 5 -up 4 -down 3 -up 7 -up 2 -down 5 -down 2 -forward 8 -up 1 -down 8 -forward 2 -forward 8 -down 9 -forward 3 -forward 7 -forward 1 -down 2 -down 8 -forward 1 -forward 9 -forward 9 -up 3 -up 7 -forward 9 -down 4 -up 6 -forward 2 -down 2 -down 4 -down 4 -down 6 -down 5 -forward 2 -down 8 -down 1 -up 6 -up 1 -up 4 -down 5 -forward 5 -forward 4 -forward 1 -forward 9 -up 9 -down 9 -down 5 -down 7 -up 6 -up 2 -forward 5 -down 5 -down 3 -down 8 -down 6 -forward 4 -down 9 -up 3 -forward 2 -forward 9 -forward 6 -forward 5 -down 5 -down 1 -down 2 -forward 9 -down 2 -down 2 -down 3 -forward 3 -forward 9 -forward 1 -down 3 -down 8 -forward 7 -down 9 -forward 4 -up 3 -up 7 -up 4 -down 5 -forward 9 -forward 2 -forward 2 -down 3 -up 5 -down 5 -down 4 -forward 2 -forward 7 -up 2 -down 8 -up 2 -up 2 -forward 2 -down 2 -forward 3 -down 3 -up 8 -forward 7 -up 5 -forward 4 -down 6 -down 8 -forward 4 -forward 3 -up 9 -up 2 -down 2 -down 5 -down 8 -down 1 -forward 9 -down 6 -forward 1 -down 9 -down 4 -up 6 -up 3 -forward 1 -down 8 -up 3 -down 6 -down 7 -down 2 -forward 5 -down 6 -forward 8 -up 7 -down 5 -down 3 -forward 4 -forward 5 -forward 3 -forward 4 -forward 6 -forward 2 -forward 1 -down 3 -down 5 -down 3 -down 5 -forward 4 -down 7 -forward 8 -forward 5 -up 7 -up 3 -forward 9 -up 1 -forward 9 -up 8 -down 3 -forward 1 -forward 6 -forward 9 -down 1 -up 9 -forward 5 -down 6 -forward 8 -down 7 -down 3 -up 4 -down 6 -forward 5 -forward 6 -forward 5 -up 2 -down 5 -up 7 -up 4 -up 5 -forward 3 -down 9 -up 4 -forward 9 -forward 8 -forward 6 -down 5 -down 4 -down 2 -up 5 -up 7 -up 2 -forward 9 -forward 9 -down 9 -down 4 -up 2 -forward 3 -up 3 -up 2 -down 9 -forward 8 -forward 6 -down 6 -forward 3 -down 1 -forward 4 -forward 9 -forward 5 -down 2 -down 7 -up 6 -down 3 -forward 7 -down 3 -up 3 -up 8 -forward 3 -up 7 -forward 5 -down 7 -forward 7 -forward 3 -down 6 -down 3 -forward 5 -forward 9 -up 8 -down 7 -down 3 -down 7 -down 4 -down 1 -down 7 -up 6 -forward 8 -up 7 -down 9 -forward 6 -down 4 -forward 6 -up 9 -forward 4 -down 5 -up 3 -forward 5 -forward 6 -down 8 -up 9 -forward 4 -up 5 -forward 4 -forward 2 -forward 8 -down 7 -forward 1 -down 8 -forward 8 -forward 4 -down 4 -forward 5 -down 2 -down 5 -forward 9 -down 7 -forward 1 -down 1 -forward 9 -forward 3 -forward 9 -forward 8 -down 5 -down 6 -forward 8 -up 9 -forward 7 -down 1 -forward 9 -up 7 -forward 2 -forward 6 -forward 1 -down 8 -down 6 -down 7 -down 6 -up 5 -down 5 -forward 9 -down 6 -down 9 -forward 9 -down 7 -up 7 -forward 1 -down 5 -down 8 -up 5 -down 6 -up 5 -up 7 -forward 3 -forward 2 -down 5 -down 6 -forward 3 -down 4 -down 5 -up 4 -forward 5 -down 4 -down 5 -forward 4 -down 1 -forward 1 -down 1 -forward 4 -forward 2 -down 3 -forward 1 -down 1 -forward 2 -forward 2 -forward 6 -up 5 -forward 5 -down 9 -down 1 -forward 7 -up 9 -down 2 -down 1 -forward 3 -up 5 -down 8 -forward 2 -forward 1 -down 7 -forward 5 -up 6 -forward 2 -up 5 -forward 8 -up 6 -down 6 -down 3 -forward 1 -forward 7 -down 7 -down 1 -up 7 -forward 2 -up 5 -forward 4 -forward 9 -forward 4 -forward 2 -forward 4 -down 3 -down 7 -forward 1 -up 9 -up 2 -forward 1 -down 5 -up 9 -forward 6 -down 7 -down 2 -down 7 -down 2 -down 1 -down 7 -down 6 -up 1 -up 4 -down 9 -up 3 -forward 1 -down 2 -forward 4 -up 4 -up 9 -down 4 -forward 6 -down 1 -down 1 -down 8 -up 5 -forward 1 -up 6 -down 5 -forward 4 -up 8 -down 4 -forward 4 -forward 3 -down 7 -down 1 -forward 3 -forward 1 -up 6 -down 1 -down 8 -forward 6 -down 4 -down 6 -forward 5 -forward 3 -forward 5 -down 4 -forward 7 -down 6 -down 6 -down 9 -up 9 -forward 5 -up 9 -up 4 -up 6 -down 4 -forward 3 -up 2 -down 7 -down 8 -forward 7 -down 4 -down 3 -down 5 -down 1 -forward 5 -up 4 -down 3 -down 3 -down 6 -forward 9 -down 1 -forward 4 -down 9 -forward 1 -forward 4 -down 1 -up 5 -down 6 -forward 5 -up 5 -forward 5 -down 8 -down 1 -down 8 -up 1 -down 1 -forward 8 -forward 3 -up 2 -forward 9 -forward 1 -forward 3 -down 2 -down 7 -down 2 -up 4 -up 3 -down 2 -forward 2 -forward 9 -forward 8 -down 8 -forward 3 -up 9 -up 6 -down 9 -down 1 -up 3 -up 2 -forward 2 -down 6 -up 2 -up 1 -down 9 -down 3 -down 6 -up 7 -up 5 -forward 8 -down 1 -forward 7 -down 6 -down 1 -up 9 -forward 9 -forward 8 -down 3 -down 9 -down 5 -forward 7 -up 1 -up 4 -up 4 -down 7 -down 1 -up 2 -down 2 -forward 8 -forward 7 -up 8 -down 1 -down 8 -up 7 -forward 1 -down 9 -forward 7 -forward 1 -down 4 -down 8 -down 1 -forward 5 -forward 8 -forward 5 -down 8 -down 7 -up 5 -forward 8 -down 5 -up 9 -down 5 -down 9 -forward 2 -forward 6 -forward 2 -up 1 -forward 4 -forward 9 -forward 7 -down 7 -down 3 -forward 9 -forward 6 -up 5 -forward 5 -forward 7 -down 9 -forward 6 -down 7 -forward 5 -forward 5 -forward 4 -forward 1 -forward 1 -up 7 -forward 3 -up 3 -forward 6 -up 3 -down 9 -forward 9 -up 6 -up 3 -forward 2 -down 2 -forward 9 -down 7 -up 7 -forward 6 -forward 2 -down 2 -down 4 -forward 1 -forward 4 -down 4 -up 9 -down 4 -down 4 -down 3 -forward 6 -forward 3 -down 3 -forward 5 -forward 7 -up 4 -forward 1 -forward 5 -forward 2 -forward 5 -forward 5 -forward 2 -up 8 -down 7 -up 7 -down 7 -forward 1 -forward 5 -forward 3 -forward 7 -forward 5 -forward 6 -up 7 -forward 3 -down 7 -down 2 -up 9 -forward 6 -down 7 -forward 9 -up 8 -down 1 -up 8 -up 2 -down 7 -down 6 -down 5 -up 7 -forward 6 -down 5 -forward 7 -down 6 -down 2 -up 3 -up 7 -up 5 -forward 9 -forward 2 -down 1 -down 5 -up 9 -forward 8 -down 7 -forward 1 -up 6 -down 1 -down 3 -forward 3 -forward 6 -down 4 -forward 8 -up 2 -down 8 -up 4 -up 9 -forward 8 -down 9 -forward 3 -forward 7 -down 5 -forward 4 -up 3 -up 1 -forward 7 -down 6 -up 4 -down 3 -forward 8 -down 9 -forward 2 -down 8 -forward 9 -up 7 -forward 2 -up 1 -down 7 -down 1 -forward 4 -forward 5 -down 4 -down 9 -forward 2 -down 9 -down 5 -up 2 -down 6 -forward 8 -up 6 -down 3 -down 5 -down 1 -up 7 -down 5 -forward 8 -up 4 -down 7 -down 4 -down 4 -down 2 -down 3 -down 1 -down 7 -forward 4 -down 4 -down 7 -down 7 -down 9 -up 1 -up 7 -forward 4 -up 7 -forward 6 -forward 8 -forward 2 -forward 8 -up 8 -down 3 -forward 7 -down 9 -forward 9 -forward 6 -up 3 -down 4 -down 3 -forward 6 -forward 2 -forward 3 -down 8 -forward 1 -forward 5 -up 3 -down 8 -forward 7 -forward 4 -down 3 -forward 2 -down 9 -down 9 -forward 9 -down 7 -forward 6 -forward 4 -down 5 -forward 9 -forward 3 -down 1 -down 1 -down 7 -forward 8 -down 3 -forward 7 -forward 8 -up 3 -forward 8 -forward 8 -up 6 -forward 2 -down 3 -down 4 -forward 9 -up 8 -down 9 -forward 5 -down 3 -up 7 -forward 5 -down 2 -forward 2 -forward 1 -down 6 -down 1 -up 4 -forward 4 -down 7 -up 3 -down 3 -forward 4 -forward 2 -forward 1 -forward 9 -forward 7 -forward 9 -down 1 -forward 6 -down 5 -up 7 -down 9 -down 2 -up 4 -down 2 -down 2 -down 1 -down 2 -down 5 -down 4 -down 6 -forward 4 -forward 9 -forward 6 -forward 7 -up 9 -up 2 -forward 7 -forward 9 -up 5 -forward 2 -up 5 -up 9 -forward 9 -down 8 -forward 6 -down 8 -forward 4 -down 1 -down 4 -forward 4 -down 3 -forward 4 -forward 3 -forward 9 -down 5 -forward 3 -forward 2 -forward 9 -forward 8 -down 2 -forward 5 -up 2 -forward 1 -down 3 -forward 9 -forward 8 -down 9 -forward 5 -forward 1 -down 1 -down 9 -down 6 -forward 8 -forward 1 -down 5 -up 5 -forward 8 -up 4 -down 6 -forward 1 -up 2 -down 3 -down 1 -down 8 -down 2 -up 6 -down 5 -down 2 -down 3 -forward 2 -up 7 -down 9 -up 1 -up 1 -forward 7 -forward 4 -down 7 -up 7 -down 7 -forward 5 -up 2 -down 7 -forward 9 -down 7 -up 4 -forward 2 -forward 1 -up 6 -down 8 -up 6 -down 2 -down 4 -up 8 -up 8 -up 5 -down 6 -up 6 -down 5 -up 1 -down 1 -forward 6 -up 7 -forward 8 -up 9 -down 8 -up 7 -forward 9 -up 4 -down 5 -forward 3 -forward 6 -forward 4 -forward 4 -down 7 -forward 9 -down 6 -down 2 -forward 9 -forward 3 diff --git a/2021/day2/readme.md b/2021/day2/readme.md deleted file mode 100644 index caf27b5..0000000 --- a/2021/day2/readme.md +++ /dev/null @@ -1,76 +0,0 @@ -# Day 2: Dive! - -AoC 2021 - [day 1](https://adventofcode.com/2021/day/1) - -## Part 1 - -Now, you need to figure out how to pilot this thing. - -It seems like the submarine can take a series of commands like `forward 1`, `down 2`, or `up 3`: - -- `forward X` increases the horizontal position by `X` units. -- `down X` increases the depth by `X` units. -- `up X` decreases the depth by `X` units. - -Note that since you're on a submarine, down and up affect your depth, and so they have the opposite result of what you might expect. - -The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it's going. For example: - -``` -forward 5 -down 5 -forward 8 -up 3 -down 8 -forward 2 -``` - -Your horizontal position and depth both start at `0`. The steps above would then modify them as follows: - -``` -forward 5 adds 5 to your horizontal position, a total of 5. -down 5 adds 5 to your depth, resulting in a value of 5. -forward 8 adds 8 to your horizontal position, a total of 13. -up 3 decreases your depth by 3, resulting in a value of 2. -down 8 adds 8 to your depth, resulting in a value of 10. -forward 2 adds 2 to your horizontal position, a total of 15. -``` - -After following these instructions, you would have a horizontal position of `15` and a depth of `10`. (Multiplying these together produces `150`.) - -Calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth? - -Your puzzle answer was `1604850`. - -## Part Two - -Based on your calculations, the planned course doesn't seem to make any sense. You find the submarine manual and discover that the process is actually slightly more complicated. - -In addition to horizontal position and depth, you'll also need to track a third value, aim, which also starts at `0`. The commands also mean something entirely different than you first thought: - -``` -down X increases your aim by X units. -up X decreases your aim by X units. -forward X does two things: -It increases your horizontal position by X units. -It increases your depth by your aim multiplied by X. -``` - -Again note that since you're on a submarine, down and up do the opposite of what you might expect: "down" means aiming in the positive direction. - -Now, the above example does something different: - -- `forward 5` adds `5` to your horizontal position, a total of `5`. Because your aim is `0`, your depth does not change. -- `down 5` adds `5` to your aim, resulting in a value of `5`. -- `forward 8` adds `8` to your horizontal position, a total of `13`. Because your aim is `5`, your depth increases by `8*5=40`. -- `up 3` decreases your aim by `3`, resulting in a value of `2`. -- `down 8` adds `8` to your aim, resulting in a value of `10`. -- `forward 2` adds `2` to your horizontal position, a total of `15`. Because your aim is `10`, your depth increases by `2*10=20` to a total of `60`. - -After following these new instructions, you would have a horizontal position of `15` and a depth of `60`. (Multiplying these produces `900`.) - -Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth? - -Your puzzle answer was `1685186100`. - -Both parts of this puzzle are complete! They provide two gold stars: `**` \ No newline at end of file diff --git a/2021/main.go b/2021/main.go index b3be009..f27da0d 100644 --- a/2021/main.go +++ b/2021/main.go @@ -2,15 +2,14 @@ package main import ( "fmt" - "github.com/mariamihai/advent-of-code/2021/day1" "github.com/mariamihai/advent-of-code/2021/day2" ) func main() { - fmt.Println("--- Day 1: Sonar Sweep ---") - day1.Problem1() - fmt.Println("--- Day 2: Dive! ---") - day2.Problem1() - day2.Problem2() + fmt.Println("Day 2, problem 1: ", day2.Problem1("./day2/input1.txt")) + fmt.Println("Day 2, problem 2: ", day2.Problem2("./day2/input1.txt")) + + //fmt.Println("------- Day 1: Sonar Sweep -------") + //fmt.Println("Day 1, problem 1: ", day1.Problem1("./day1/input1.txt")) } diff --git a/2021/readme.md b/2021/readme.md deleted file mode 100644 index 311c994..0000000 --- a/2021/readme.md +++ /dev/null @@ -1,9 +0,0 @@ -# Advent of Code 2021 - -🎄✨ 🎁 [AoC 2021](https://adventofcode.com/2022/): - -- [ ] [Day 1](./day1) (half) -- [x] [Day 2](./day2) -- [ ] Day 3 -- [ ] Day 4 -- [ ] Day 5 \ No newline at end of file diff --git a/readme.md b/readme.md index c3d7e91..d89cd9e 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,7 @@ ✖️ ✖️ Day 10 [problem](https://adventofcode.com/2023/day/10)
✔️ ✔️ Day 11 [problem](https://adventofcode.com/2023/day/11) and [solution](2023/day11)
-# 2022 +## 2022 🎄✨ 🎁 [AoC 2022](https://adventofcode.com/2022/): @@ -31,4 +31,16 @@ - [ ] Day 8 [problem](day8) - [ ] Day 9 [problem](day9) - [x] Day 10 [problem](day10) -- [x] Day 11 [problem](day11) (to refactor) \ No newline at end of file +- [x] Day 11 [problem](day11) (to refactor) + +## 2021 + +🎄✨🎁 [AoC 2021](https://adventofcode.com/2021/): + +✔️ ✖️ Day   1 [problem](https://adventofcode.com/2021/day/1) and [solution](2021/day1)
+✔️ ✔️ Day   2 [problem](https://adventofcode.com/2021/day/2) and [solution](2021/day2)
+ +## Resources + +- 2015 - 2021 problems structured by category [here](https://www.reddit.com/r/adventofcode/comments/z0vmy0/350_stars_a_categorization_and_megaguide/) +- \ No newline at end of file