From 18bdad10c27d199a4085655ce93902d4bba0d534 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Wed, 1 Dec 2021 08:44:23 +0200 Subject: Day 1 --- .gitignore | 2 + Cargo.lock | 86 +++ Cargo.toml | 9 + inputs/day_1.txt | 2000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.org | 2 + src/bin/day_1.rs | 78 +++ src/lib.rs | 1 + src/parsers.rs | 6 + 8 files changed, 2184 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 inputs/day_1.txt create mode 100644 readme.org create mode 100644 src/bin/day_1.rs create mode 100644 src/lib.rs create mode 100644 src/parsers.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..f047963 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,86 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc2021" +version = "0.1.0" +dependencies = [ + "derive_more", + "nom", +] + +[[package]] +name = "derive_more" +version = "0.99.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2159be042979966de68315bce7034bb000c775f22e3e834e1c52ff78f041cae8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d11e1ef389c76fe5b81bcaf2ea32cf88b62bc494e19f493d0b30e7a930109" +dependencies = [ + "memchr", + "minimal-lexical", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c9e470a8dc4aeae2dee2f335e8f533e2d4b347e1434e5671afc49b054592f27" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dff0acdb207ae2fe6d5976617f887eb1e35a2ba52c13c7234c790960cdad9238" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" + +[[package]] +name = "version_check" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8906e7e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "aoc2021" +version = "0.1.0" +authors = ["Justin Wernick "] +edition = "2021" + +[dependencies] +derive_more = "0.99.2" +nom = "7.0.0" diff --git a/inputs/day_1.txt b/inputs/day_1.txt new file mode 100644 index 0000000..42f77f8 --- /dev/null +++ b/inputs/day_1.txt @@ -0,0 +1,2000 @@ +196 +197 +176 +182 +179 +177 +171 +172 +170 +147 +143 +153 +158 +157 +147 +151 +153 +159 +150 +167 +162 +160 +159 +161 +165 +150 +158 +157 +146 +141 +137 +127 +140 +127 +115 +114 +116 +118 +127 +133 +141 +151 +150 +148 +146 +170 +171 +168 +189 +194 +191 +205 +210 +194 +195 +191 +231 +241 +239 +241 +245 +255 +239 +244 +245 +246 +260 +262 +253 +262 +268 +289 +278 +279 +293 +312 +307 +308 +333 +336 +325 +329 +338 +334 +337 +330 +340 +341 +338 +344 +359 +361 +359 +377 +411 +450 +459 +458 +455 +450 +444 +460 +457 +458 +454 +456 +460 +465 +470 +469 +468 +462 +454 +446 +448 +449 +451 +448 +463 +470 +492 +504 +506 +508 +501 +535 +539 +547 +549 +548 +563 +565 +564 +571 +586 +599 +593 +588 +591 +597 +611 +610 +617 +619 +617 +618 +614 +604 +603 +604 +590 +593 +592 +581 +577 +604 +611 +612 +610 +634 +651 +638 +636 +633 +634 +629 +624 +611 +623 +626 +631 +632 +631 +659 +672 +686 +681 +682 +680 +666 +668 +682 +680 +681 +700 +701 +690 +685 +693 +692 +691 +693 +692 +700 +704 +713 +716 +715 +727 +726 +725 +713 +718 +714 +715 +710 +709 +718 +717 +719 +720 +716 +705 +716 +719 +735 +746 +729 +735 +726 +743 +744 +740 +739 +730 +735 +738 +736 +743 +752 +754 +758 +767 +753 +754 +756 +754 +758 +755 +750 +754 +750 +749 +745 +743 +740 +739 +740 +738 +740 +746 +747 +772 +775 +768 +769 +781 +782 +788 +799 +813 +792 +782 +783 +785 +770 +761 +771 +798 +808 +810 +811 +829 +840 +826 +833 +829 +833 +815 +816 +818 +809 +810 +813 +819 +809 +787 +770 +765 +786 +793 +792 +787 +790 +791 +794 +790 +801 +804 +803 +781 +794 +778 +779 +777 +778 +786 +773 +775 +783 +781 +790 +781 +782 +791 +792 +815 +816 +808 +803 +786 +785 +781 +780 +781 +785 +754 +741 +725 +724 +725 +724 +726 +725 +727 +721 +718 +706 +721 +731 +739 +759 +752 +749 +767 +766 +787 +788 +812 +826 +828 +829 +835 +834 +831 +818 +808 +813 +827 +847 +848 +853 +857 +859 +860 +864 +867 +871 +870 +877 +874 +858 +857 +840 +844 +848 +846 +853 +860 +866 +862 +867 +885 +898 +929 +935 +944 +946 +952 +961 +975 +974 +970 +966 +983 +981 +986 +1001 +996 +995 +982 +984 +991 +982 +990 +984 +991 +992 +997 +995 +994 +998 +1004 +1010 +1011 +1010 +1013 +1016 +1025 +1027 +1029 +1040 +1038 +1041 +1045 +1047 +1048 +1050 +1023 +1024 +1019 +1025 +1035 +1036 +1045 +1052 +1053 +1050 +1049 +1050 +1048 +1049 +1059 +1061 +1059 +1031 +1032 +1042 +1051 +1055 +1064 +1058 +1032 +1037 +1039 +1061 +1087 +1088 +1080 +1085 +1069 +1052 +1054 +1060 +1062 +1063 +1062 +1063 +1062 +1048 +1054 +1050 +1031 +1028 +1022 +1034 +1038 +1039 +1036 +1045 +1055 +1054 +1053 +1058 +1088 +1089 +1086 +1095 +1096 +1090 +1087 +1095 +1099 +1097 +1091 +1093 +1068 +1061 +1078 +1073 +1079 +1082 +1068 +1079 +1078 +1067 +1068 +1084 +1091 +1096 +1070 +1061 +1052 +1047 +1065 +1056 +1069 +1070 +1072 +1081 +1083 +1084 +1078 +1079 +1080 +1082 +1085 +1081 +1085 +1086 +1094 +1096 +1097 +1079 +1081 +1082 +1080 +1077 +1084 +1085 +1084 +1088 +1091 +1092 +1101 +1102 +1101 +1116 +1115 +1114 +1117 +1123 +1120 +1113 +1111 +1116 +1119 +1118 +1130 +1116 +1119 +1122 +1120 +1121 +1150 +1153 +1156 +1162 +1163 +1189 +1191 +1192 +1189 +1187 +1186 +1177 +1167 +1140 +1149 +1160 +1142 +1147 +1148 +1170 +1169 +1188 +1190 +1189 +1193 +1187 +1181 +1188 +1193 +1192 +1194 +1216 +1223 +1209 +1199 +1214 +1218 +1211 +1212 +1222 +1227 +1234 +1244 +1235 +1238 +1232 +1238 +1236 +1231 +1223 +1217 +1218 +1229 +1212 +1236 +1237 +1240 +1255 +1257 +1258 +1261 +1258 +1255 +1258 +1256 +1258 +1279 +1275 +1274 +1282 +1292 +1293 +1296 +1287 +1323 +1322 +1332 +1333 +1316 +1327 +1332 +1334 +1314 +1311 +1315 +1317 +1320 +1359 +1362 +1360 +1362 +1372 +1377 +1389 +1382 +1373 +1380 +1388 +1395 +1384 +1375 +1382 +1383 +1368 +1363 +1356 +1352 +1356 +1360 +1355 +1367 +1371 +1382 +1409 +1379 +1421 +1429 +1430 +1448 +1451 +1452 +1449 +1439 +1460 +1431 +1429 +1430 +1427 +1416 +1417 +1454 +1472 +1473 +1476 +1475 +1477 +1476 +1482 +1481 +1489 +1488 +1490 +1495 +1493 +1500 +1517 +1500 +1498 +1494 +1502 +1505 +1536 +1537 +1538 +1543 +1542 +1541 +1554 +1553 +1555 +1558 +1549 +1550 +1557 +1558 +1560 +1561 +1565 +1560 +1565 +1572 +1573 +1575 +1571 +1581 +1605 +1614 +1611 +1619 +1618 +1614 +1612 +1616 +1629 +1628 +1632 +1635 +1644 +1637 +1638 +1632 +1613 +1614 +1615 +1614 +1609 +1611 +1620 +1615 +1608 +1612 +1646 +1647 +1665 +1668 +1665 +1668 +1676 +1677 +1682 +1695 +1696 +1708 +1709 +1686 +1681 +1668 +1669 +1655 +1667 +1681 +1689 +1710 +1718 +1749 +1748 +1746 +1749 +1752 +1748 +1745 +1744 +1743 +1745 +1743 +1744 +1726 +1725 +1724 +1728 +1727 +1739 +1740 +1733 +1739 +1740 +1757 +1759 +1758 +1759 +1762 +1802 +1799 +1792 +1786 +1787 +1793 +1794 +1797 +1798 +1800 +1803 +1802 +1792 +1793 +1795 +1783 +1782 +1772 +1771 +1772 +1774 +1778 +1765 +1766 +1767 +1769 +1771 +1772 +1788 +1781 +1790 +1789 +1791 +1813 +1809 +1810 +1813 +1814 +1819 +1823 +1815 +1814 +1822 +1826 +1812 +1817 +1814 +1818 +1800 +1801 +1804 +1800 +1802 +1813 +1825 +1822 +1832 +1840 +1841 +1830 +1834 +1835 +1841 +1840 +1845 +1881 +1883 +1887 +1886 +1890 +1889 +1896 +1897 +1917 +1923 +1925 +1923 +1924 +1909 +1899 +1904 +1905 +1907 +1905 +1908 +1922 +1912 +1920 +1924 +1920 +1921 +1920 +1919 +1935 +1937 +1955 +1956 +1967 +1972 +1977 +1984 +1982 +1986 +1980 +1975 +2005 +2007 +2018 +2027 +2040 +2039 +2040 +2042 +2040 +2041 +2040 +2034 +2060 +2066 +2074 +2076 +2080 +2081 +2082 +2066 +2071 +2074 +2069 +2070 +2076 +2075 +2079 +2082 +2081 +2080 +2085 +2093 +2097 +2091 +2096 +2097 +2106 +2103 +2110 +2107 +2098 +2097 +2110 +2113 +2097 +2102 +2103 +2107 +2103 +2117 +2097 +2105 +2103 +2099 +2101 +2094 +2102 +2099 +2069 +2067 +2057 +2071 +2073 +2075 +2073 +2060 +2064 +2081 +2057 +2055 +2056 +2064 +2066 +2067 +2069 +2070 +2076 +2075 +2074 +2101 +2099 +2116 +2115 +2119 +2129 +2126 +2124 +2122 +2126 +2143 +2140 +2141 +2146 +2145 +2148 +2164 +2154 +2151 +2148 +2152 +2130 +2129 +2131 +2118 +2123 +2129 +2130 +2127 +2133 +2132 +2142 +2143 +2157 +2156 +2140 +2139 +2141 +2139 +2140 +2142 +2134 +2126 +2128 +2129 +2131 +2126 +2146 +2145 +2134 +2153 +2151 +2140 +2139 +2161 +2165 +2169 +2150 +2151 +2153 +2176 +2183 +2182 +2188 +2165 +2160 +2161 +2165 +2173 +2155 +2154 +2141 +2140 +2161 +2158 +2148 +2164 +2165 +2176 +2163 +2168 +2158 +2159 +2194 +2201 +2203 +2202 +2211 +2213 +2210 +2193 +2199 +2201 +2199 +2206 +2231 +2239 +2246 +2249 +2265 +2266 +2267 +2281 +2291 +2292 +2293 +2296 +2295 +2275 +2273 +2275 +2274 +2286 +2291 +2301 +2300 +2302 +2298 +2300 +2288 +2298 +2300 +2291 +2309 +2303 +2304 +2309 +2318 +2308 +2315 +2314 +2313 +2315 +2314 +2315 +2325 +2326 +2310 +2308 +2314 +2313 +2312 +2311 +2312 +2311 +2310 +2304 +2310 +2308 +2311 +2296 +2295 +2289 +2290 +2292 +2291 +2289 +2290 +2288 +2300 +2302 +2307 +2313 +2312 +2309 +2310 +2312 +2313 +2314 +2315 +2314 +2308 +2309 +2313 +2325 +2324 +2322 +2300 +2298 +2291 +2301 +2298 +2307 +2309 +2308 +2345 +2341 +2342 +2345 +2360 +2367 +2371 +2374 +2368 +2369 +2375 +2406 +2405 +2422 +2423 +2422 +2419 +2417 +2416 +2417 +2421 +2427 +2445 +2449 +2432 +2448 +2454 +2437 +2429 +2430 +2439 +2415 +2418 +2425 +2421 +2401 +2414 +2412 +2416 +2432 +2450 +2443 +2426 +2427 +2428 +2427 +2429 +2430 +2434 +2438 +2431 +2433 +2431 +2427 +2433 +2455 +2462 +2463 +2465 +2461 +2462 +2463 +2496 +2497 +2493 +2486 +2472 +2470 +2475 +2483 +2491 +2492 +2505 +2510 +2511 +2507 +2508 +2506 +2507 +2505 +2506 +2504 +2524 +2528 +2519 +2527 +2543 +2549 +2555 +2567 +2555 +2561 +2560 +2561 +2560 +2541 +2540 +2537 +2530 +2532 +2528 +2529 +2528 +2522 +2516 +2506 +2520 +2526 +2527 +2531 +2534 +2544 +2545 +2542 +2541 +2543 +2544 +2543 +2542 +2558 +2560 +2558 +2559 +2552 +2551 +2558 +2573 +2590 +2589 +2583 +2574 +2565 +2569 +2580 +2596 +2597 +2596 +2585 +2586 +2587 +2598 +2587 +2588 +2571 +2570 +2567 +2579 +2581 +2579 +2572 +2577 +2575 +2574 +2582 +2585 +2592 +2600 +2601 +2608 +2606 +2609 +2612 +2619 +2631 +2636 +2633 +2640 +2654 +2649 +2611 +2598 +2600 +2604 +2608 +2600 +2607 +2623 +2624 +2633 +2632 +2637 +2655 +2658 +2662 +2675 +2676 +2699 +2711 +2714 +2710 +2718 +2750 +2719 +2713 +2714 +2737 +2739 +2733 +2736 +2754 +2773 +2776 +2794 +2795 +2823 +2825 +2826 +2828 +2830 +2815 +2823 +2821 +2825 +2829 +2833 +2834 +2833 +2832 +2844 +2838 +2840 +2845 +2837 +2849 +2878 +2859 +2858 +2859 +2864 +2868 +2870 +2863 +2859 +2860 +2858 +2860 +2857 +2851 +2852 +2856 +2818 +2820 +2822 +2819 +2818 +2819 +2823 +2827 +2836 +2834 +2835 +2842 +2856 +2854 +2853 +2854 +2855 +2854 +2858 +2859 +2866 +2872 +2871 +2869 +2875 +2874 +2888 +2881 +2883 +2900 +2897 +2885 +2880 +2882 +2883 +2886 +2887 +2903 +2894 +2884 +2876 +2877 +2876 +2875 +2887 +2863 +2865 +2880 +2885 +2866 +2882 +2883 +2875 +2873 +2876 +2875 +2912 +2916 +2932 +2927 +2914 +2906 +2911 +2912 +2910 +2908 +2903 +2929 +2925 +2907 +2905 +2906 +2909 +2897 +2895 +2903 +2918 +2945 +2946 +2937 +2919 +2923 +2943 +2947 +2950 +2951 +2963 +2978 +2972 +2954 +2955 +2957 +2969 +2972 +2984 +2987 +2969 +2970 +2974 +2981 +2949 +2946 +2952 +2966 +2968 +2969 +2970 +2956 +2947 +2941 +2949 +2939 +2940 +2953 +2959 +2947 +2957 +2963 +2968 +2964 +2939 +2912 +2904 +2907 +2928 +2948 +2961 +2969 +2963 +2980 +2985 +2986 +2987 +3015 +3017 +3027 +3028 +3031 +3040 +3043 +3044 +3043 +3054 +3053 +3055 +3053 +3047 +3051 +3025 +3030 +3040 +3044 +3046 +3049 +3054 +3057 +3059 +3056 +3060 +3068 +3085 +3104 +3108 +3114 +3115 +3118 +3121 +3118 +3132 +3134 +3129 +3126 +3123 +3111 +3112 +3080 +3097 +3086 +3088 +3089 +3093 +3092 +3096 +3126 +3127 +3128 +3133 +3134 +3137 +3138 +3135 +3141 +3142 +3143 +3162 +3152 +3149 +3166 +3172 +3170 +3171 +3185 +3187 +3193 +3192 +3196 +3186 +3203 +3213 +3216 +3238 +3237 +3246 +3247 +3252 +3250 +3249 +3241 +3246 +3253 +3251 +3258 +3271 +3274 +3268 +3274 +3273 +3269 +3272 +3271 +3266 +3277 +3270 +3274 +3285 +3270 +3263 +3255 +3256 +3247 +3248 +3252 +3259 +3252 +3253 +3259 +3263 +3259 +3240 +3254 +3257 +3241 +3250 +3251 +3254 +3271 +3273 +3281 +3295 +3296 +3293 +3299 +3292 +3301 +3304 +3306 +3303 +3301 +3303 +3304 +3303 +3275 +3273 +3276 +3302 +3307 +3316 +3317 +3288 +3296 +3298 +3313 +3312 +3313 +3306 +3292 +3295 +3315 +3316 +3308 +3311 +3310 +3294 +3327 +3331 +3330 +3333 +3352 +3360 +3362 +3366 +3357 +3361 +3360 +3351 +3345 +3342 +3339 +3338 +3364 +3365 +3368 +3375 +3385 +3389 +3390 +3389 +3407 +3408 +3402 +3433 +3437 +3436 +3435 +3439 +3454 +3425 +3426 +3422 +3426 +3423 +3419 +3420 +3416 +3417 +3426 +3427 +3403 +3406 +3395 +3396 +3397 +3398 +3399 +3407 +3409 +3410 +3397 +3391 +3408 +3419 +3425 +3428 +3420 +3423 +3424 +3426 +3437 +3440 +3434 +3436 +3440 +3443 +3444 +3450 +3454 +3455 +3460 +3484 +3482 +3487 +3489 +3492 +3493 +3498 +3493 +3478 +3479 +3478 +3505 +3506 +3508 +3505 +3500 +3499 +3498 +3499 +3495 +3494 +3492 +3489 +3490 +3492 +3490 +3492 +3487 +3488 +3491 +3484 +3486 +3487 +3483 +3461 +3459 +3463 +3476 +3466 +3464 +3465 +3474 +3487 +3485 +3477 +3464 +3463 +3469 +3465 +3462 +3448 +3452 +3461 +3463 +3466 +3470 +3475 +3488 +3477 +3473 +3472 +3475 +3469 +3458 +3455 +3440 +3430 +3439 +3440 +3437 +3442 +3443 +3446 +3447 +3442 +3453 +3462 +3463 +3462 +3463 +3461 +3455 +3454 +3450 +3449 +3450 +3447 +3451 +3454 +3459 +3460 +3461 +3463 +3466 +3467 +3468 +3469 +3474 +3481 +3480 +3481 +3482 +3474 +3472 +3484 +3502 +3522 +3507 +3509 +3504 +3497 +3487 +3476 +3473 +3495 +3494 +3495 +3493 +3500 +3503 +3516 +3518 +3517 +3524 +3528 +3531 +3530 +3526 +3525 +3527 +3521 +3520 +3526 +3527 +3521 +3527 +3521 +3524 +3525 +3519 +3520 +3540 +3566 +3577 +3575 +3576 +3561 +3562 +3563 +3571 +3572 +3577 +3575 +3607 +3606 +3611 +3612 +3613 +3614 +3615 +3614 +3605 +3619 +3618 +3624 +3634 +3639 +3638 +3660 +3661 +3653 +3650 +3651 +3652 +3650 +3648 +3649 +3653 +3655 +3650 +3651 +3650 +3685 +3656 +3670 +3687 +3696 +3695 +3682 +3685 +3688 +3686 +3672 +3664 +3663 +3659 +3664 +3676 +3689 +3694 +3697 +3713 +3705 +3706 +3725 +3724 +3723 +3728 diff --git a/readme.org b/readme.org new file mode 100644 index 0000000..642364f --- /dev/null +++ b/readme.org @@ -0,0 +1,2 @@ +* Advent of Code 2021 + diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs new file mode 100644 index 0000000..7b89d69 --- /dev/null +++ b/src/bin/day_1.rs @@ -0,0 +1,78 @@ +use aoc2021::parsers; +use nom::{character::complete::line_ending, combinator::map, multi::separated_list1, IResult}; +use std::fs; + +fn main() -> Result<(), Box> { + let input = fs::read_to_string("inputs/day_1.txt")?; + let sonar_scan = parse_sonar_scan(&input).unwrap().1; + + { + let mut simple_increase_counter = DepthIncreaseCounter::default(); + for (depth, next_depth) in sonar_scan.iter().zip(sonar_scan.iter().skip(1)) { + if next_depth > depth { + simple_increase_counter.increment(); + } + } + dbg!(simple_increase_counter); + } + + { + let windowed_sonar_scan = sonar_scan + .iter() + .zip(sonar_scan.iter().skip(1)) + .zip(sonar_scan.iter().skip(2)) + .map(|((depth1, depth2), depth3)| ThreeDepthWindowSum::new([*depth1, *depth2, *depth3])) + .collect::>(); + + let mut windowed_increase_counter = DepthIncreaseCounter::default(); + for (depth, next_depth) in windowed_sonar_scan + .iter() + .zip(windowed_sonar_scan.iter().skip(1)) + { + if next_depth > depth { + windowed_increase_counter.increment(); + } + } + dbg!(windowed_increase_counter); + } + + Ok(()) +} + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] +struct Depth(u64); + +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] +struct ThreeDepthWindowSum(u64); + +impl ThreeDepthWindowSum { + fn new(depths: [Depth; 3]) -> ThreeDepthWindowSum { + ThreeDepthWindowSum(depths.into_iter().map(|d| d.0).sum()) + } +} + +fn parse_sonar_scan(input: &str) -> IResult<&str, Vec> { + separated_list1(line_ending, parse_depth)(input) +} + +fn parse_depth(input: &str) -> IResult<&str, Depth> { + map(parsers::u64, |number| Depth(number))(input) +} + +#[derive(Default, Debug)] +struct DepthIncreaseCounter(u64); + +impl DepthIncreaseCounter { + fn increment(&mut self) { + self.0 += 1; + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn parses_a_depth() { + assert_eq!(parse_depth("96\n"), Ok(("\n", Depth(96)))); + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..be756a0 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod parsers; diff --git a/src/parsers.rs b/src/parsers.rs new file mode 100644 index 0000000..27b2aad --- /dev/null +++ b/src/parsers.rs @@ -0,0 +1,6 @@ +use nom::{character::complete::digit1, combinator::map_res, IResult}; +use std::str::FromStr; + +pub fn u64(input: &str) -> IResult<&str, u64> { + map_res(digit1, |number| u64::from_str(number))(input) +} -- cgit v1.2.3