commit c4e841b4bc752b8c11ba0bb8b2426c08ac206c96 Author: timeshifter Date: Fri Sep 4 16:08:03 2026 +0200 add initial data diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..307cf36 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +**/target +**/debug +**/.idea diff --git a/README.md b/README.md new file mode 100644 index 0000000..e7d2ce6 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# Advent of Code 2021 diff --git a/aoc01/Cargo.lock b/aoc01/Cargo.lock new file mode 100644 index 0000000..295f643 --- /dev/null +++ b/aoc01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc01" +version = "0.1.0" diff --git a/aoc01/Cargo.toml b/aoc01/Cargo.toml new file mode 100644 index 0000000..7729e6c --- /dev/null +++ b/aoc01/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc01" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc01/data.txt b/aoc01/data.txt new file mode 100644 index 0000000..900b6a4 --- /dev/null +++ b/aoc01/data.txt @@ -0,0 +1,2000 @@ +191 +192 +201 +205 +206 +203 +188 +189 +199 +206 +224 +230 +233 +232 +260 +257 +258 +259 +251 +233 +236 +237 +238 +241 +242 +243 +250 +259 +251 +255 +263 +265 +266 +259 +265 +260 +267 +270 +271 +284 +286 +301 +302 +304 +309 +333 +335 +342 +350 +349 +342 +343 +345 +346 +331 +332 +317 +324 +328 +327 +332 +320 +340 +341 +344 +353 +361 +372 +376 +387 +397 +433 +431 +432 +436 +435 +449 +460 +474 +476 +483 +490 +487 +488 +495 +500 +511 +531 +537 +525 +503 +506 +515 +517 +518 +517 +514 +516 +521 +515 +516 +517 +526 +514 +490 +487 +484 +482 +474 +476 +480 +479 +474 +482 +480 +481 +483 +484 +482 +481 +477 +478 +482 +483 +494 +517 +520 +517 +510 +497 +481 +519 +522 +524 +528 +530 +543 +542 +543 +531 +529 +528 +526 +499 +521 +526 +518 +533 +535 +538 +542 +572 +573 +574 +548 +550 +559 +567 +569 +570 +571 +570 +587 +592 +600 +620 +630 +627 +652 +659 +656 +663 +661 +666 +681 +687 +702 +714 +715 +711 +714 +722 +719 +723 +721 +720 +731 +724 +726 +732 +735 +736 +739 +745 +739 +747 +745 +758 +755 +754 +755 +756 +757 +758 +759 +760 +763 +799 +800 +810 +825 +826 +829 +841 +848 +852 +855 +858 +856 +874 +894 +897 +892 +925 +932 +935 +931 +953 +971 +967 +953 +957 +941 +950 +952 +923 +922 +933 +938 +943 +944 +964 +952 +935 +913 +901 +902 +903 +904 +901 +909 +910 +909 +929 +930 +929 +924 +916 +904 +897 +905 +908 +899 +898 +903 +907 +924 +915 +919 +911 +915 +916 +926 +931 +933 +965 +967 +968 +979 +981 +997 +1009 +1012 +1010 +1011 +1036 +1035 +1049 +1050 +1048 +1053 +1054 +1055 +1053 +1046 +1055 +1069 +1067 +1062 +1081 +1079 +1091 +1093 +1095 +1093 +1100 +1091 +1093 +1107 +1109 +1105 +1111 +1121 +1140 +1141 +1148 +1149 +1162 +1166 +1165 +1163 +1164 +1165 +1169 +1182 +1188 +1186 +1185 +1165 +1169 +1176 +1177 +1179 +1183 +1182 +1188 +1191 +1181 +1183 +1204 +1203 +1204 +1206 +1201 +1215 +1232 +1234 +1239 +1243 +1235 +1239 +1242 +1238 +1240 +1241 +1263 +1259 +1267 +1257 +1261 +1265 +1264 +1261 +1260 +1262 +1265 +1263 +1266 +1262 +1263 +1268 +1271 +1282 +1286 +1287 +1284 +1280 +1279 +1280 +1288 +1289 +1292 +1287 +1293 +1301 +1329 +1343 +1350 +1347 +1348 +1354 +1356 +1359 +1360 +1363 +1369 +1366 +1369 +1370 +1368 +1373 +1376 +1385 +1391 +1400 +1411 +1415 +1396 +1398 +1428 +1433 +1442 +1444 +1436 +1467 +1469 +1470 +1484 +1485 +1489 +1498 +1468 +1469 +1468 +1465 +1459 +1460 +1458 +1459 +1460 +1462 +1467 +1479 +1499 +1501 +1512 +1515 +1516 +1519 +1524 +1538 +1547 +1558 +1563 +1573 +1584 +1592 +1606 +1632 +1630 +1637 +1640 +1633 +1662 +1677 +1681 +1679 +1681 +1683 +1684 +1704 +1709 +1705 +1702 +1701 +1700 +1707 +1708 +1717 +1709 +1718 +1721 +1738 +1740 +1746 +1748 +1750 +1751 +1757 +1758 +1765 +1767 +1749 +1730 +1732 +1745 +1751 +1752 +1750 +1747 +1746 +1751 +1756 +1766 +1767 +1769 +1767 +1775 +1806 +1812 +1808 +1809 +1845 +1846 +1848 +1860 +1864 +1866 +1868 +1874 +1853 +1852 +1848 +1835 +1851 +1850 +1843 +1842 +1835 +1862 +1865 +1859 +1886 +1887 +1888 +1900 +1926 +1942 +1944 +1945 +1946 +1945 +1939 +1946 +1951 +1946 +1951 +1957 +1971 +1985 +1986 +1987 +1990 +1991 +1992 +1991 +1986 +1989 +1991 +1998 +1996 +2002 +2003 +2007 +2009 +2010 +1989 +1991 +1990 +1991 +2001 +2016 +2017 +2026 +2028 +2039 +2038 +2053 +2047 +2052 +2049 +2050 +2052 +2057 +2055 +2053 +2051 +2042 +2027 +2036 +2026 +2040 +2042 +2046 +2045 +2050 +2051 +2072 +2073 +2075 +2072 +2078 +2077 +2082 +2087 +2074 +2079 +2074 +2070 +2071 +2070 +2074 +2076 +2077 +2084 +2086 +2089 +2102 +2100 +2102 +2104 +2110 +2112 +2093 +2104 +2107 +2104 +2130 +2101 +2103 +2102 +2103 +2111 +2116 +2117 +2122 +2119 +2126 +2127 +2128 +2148 +2159 +2160 +2157 +2165 +2173 +2169 +2170 +2175 +2161 +2151 +2163 +2172 +2209 +2211 +2209 +2223 +2222 +2221 +2220 +2226 +2228 +2229 +2226 +2234 +2214 +2212 +2216 +2215 +2220 +2223 +2228 +2234 +2233 +2235 +2233 +2235 +2264 +2263 +2262 +2285 +2287 +2303 +2286 +2287 +2279 +2280 +2285 +2279 +2281 +2291 +2295 +2322 +2323 +2332 +2337 +2340 +2343 +2344 +2370 +2371 +2370 +2377 +2385 +2390 +2389 +2396 +2401 +2407 +2410 +2411 +2412 +2413 +2415 +2422 +2419 +2431 +2439 +2444 +2447 +2457 +2443 +2455 +2466 +2483 +2471 +2472 +2481 +2486 +2490 +2470 +2471 +2483 +2481 +2480 +2490 +2529 +2541 +2546 +2547 +2570 +2574 +2560 +2558 +2562 +2561 +2566 +2565 +2563 +2569 +2571 +2582 +2596 +2594 +2593 +2594 +2585 +2566 +2567 +2550 +2559 +2551 +2561 +2542 +2544 +2553 +2557 +2561 +2574 +2577 +2593 +2601 +2613 +2621 +2623 +2628 +2627 +2648 +2640 +2639 +2664 +2649 +2659 +2663 +2662 +2664 +2659 +2660 +2659 +2675 +2654 +2655 +2657 +2656 +2662 +2669 +2663 +2652 +2645 +2656 +2664 +2660 +2680 +2684 +2673 +2678 +2675 +2662 +2663 +2666 +2668 +2681 +2676 +2687 +2688 +2689 +2688 +2689 +2692 +2693 +2704 +2703 +2702 +2701 +2707 +2710 +2711 +2713 +2712 +2687 +2697 +2698 +2699 +2733 +2731 +2738 +2739 +2740 +2746 +2741 +2744 +2741 +2746 +2744 +2731 +2733 +2740 +2741 +2745 +2747 +2748 +2765 +2774 +2787 +2789 +2790 +2794 +2797 +2778 +2788 +2790 +2789 +2784 +2788 +2789 +2807 +2826 +2842 +2835 +2834 +2837 +2846 +2847 +2849 +2866 +2868 +2880 +2881 +2885 +2892 +2893 +2895 +2896 +2905 +2907 +2914 +2917 +2915 +2910 +2914 +2916 +2917 +2926 +2927 +2946 +2961 +2962 +2952 +2954 +2960 +2972 +2976 +2978 +2999 +3007 +3009 +2998 +3001 +3021 +3017 +3011 +3013 +3010 +3007 +3019 +3020 +3037 +3038 +3037 +3041 +3046 +3060 +3064 +3066 +3056 +3059 +3058 +3049 +3056 +3063 +3065 +3055 +3056 +3055 +3054 +3067 +3066 +3071 +3075 +3057 +3056 +3057 +3070 +3082 +3107 +3109 +3107 +3102 +3101 +3103 +3098 +3096 +3097 +3093 +3095 +3097 +3106 +3124 +3125 +3128 +3137 +3145 +3143 +3146 +3120 +3122 +3135 +3129 +3158 +3159 +3176 +3212 +3216 +3220 +3222 +3225 +3229 +3235 +3252 +3247 +3252 +3256 +3259 +3270 +3277 +3286 +3296 +3311 +3322 +3324 +3331 +3363 +3364 +3376 +3372 +3373 +3384 +3392 +3397 +3393 +3395 +3407 +3408 +3409 +3404 +3409 +3414 +3422 +3427 +3426 +3428 +3410 +3418 +3416 +3408 +3406 +3411 +3412 +3418 +3427 +3425 +3413 +3400 +3399 +3400 +3393 +3394 +3395 +3394 +3393 +3392 +3389 +3390 +3394 +3395 +3402 +3394 +3395 +3415 +3405 +3407 +3432 +3439 +3437 +3439 +3458 +3461 +3469 +3464 +3463 +3464 +3453 +3458 +3452 +3455 +3456 +3459 +3464 +3468 +3471 +3474 +3476 +3475 +3477 +3479 +3481 +3482 +3492 +3501 +3506 +3501 +3497 +3498 +3501 +3502 +3496 +3510 +3513 +3515 +3533 +3534 +3545 +3552 +3561 +3554 +3561 +3562 +3563 +3564 +3553 +3554 +3558 +3546 +3547 +3561 +3563 +3547 +3570 +3572 +3579 +3590 +3604 +3634 +3627 +3630 +3629 +3632 +3598 +3599 +3619 +3617 +3630 +3624 +3625 +3627 +3636 +3632 +3633 +3638 +3642 +3640 +3621 +3623 +3620 +3627 +3625 +3624 +3616 +3648 +3630 +3625 +3628 +3634 +3629 +3649 +3651 +3652 +3653 +3660 +3672 +3674 +3684 +3706 +3707 +3713 +3708 +3707 +3697 +3674 +3670 +3671 +3670 +3674 +3676 +3677 +3674 +3693 +3691 +3693 +3694 +3698 +3662 +3671 +3664 +3665 +3666 +3667 +3665 +3666 +3667 +3666 +3667 +3663 +3662 +3665 +3659 +3651 +3657 +3656 +3663 +3659 +3669 +3666 +3652 +3643 +3635 +3665 +3673 +3682 +3681 +3674 +3673 +3677 +3674 +3672 +3674 +3677 +3685 +3697 +3696 +3708 +3704 +3702 +3705 +3709 +3740 +3741 +3740 +3734 +3738 +3741 +3743 +3741 +3723 +3693 +3703 +3702 +3712 +3713 +3705 +3706 +3703 +3718 +3749 +3761 +3760 +3763 +3765 +3757 +3733 +3740 +3736 +3762 +3761 +3771 +3764 +3761 +3763 +3762 +3756 +3771 +3753 +3759 +3789 +3791 +3792 +3780 +3787 +3788 +3786 +3792 +3793 +3790 +3794 +3795 +3794 +3791 +3801 +3802 +3811 +3813 +3815 +3818 +3824 +3835 +3837 +3836 +3838 +3840 +3841 +3842 +3840 +3841 +3843 +3872 +3879 +3884 +3871 +3878 +3896 +3897 +3896 +3895 +3893 +3887 +3882 +3885 +3887 +3875 +3876 +3856 +3859 +3853 +3831 +3832 +3831 +3830 +3814 +3815 +3799 +3814 +3811 +3834 +3845 +3842 +3847 +3853 +3854 +3853 +3851 +3870 +3869 +3870 +3868 +3876 +3874 +3875 +3872 +3866 +3874 +3873 +3857 +3858 +3857 +3865 +3874 +3865 +3876 +3878 +3881 +3883 +3884 +3885 +3887 +3863 +3862 +3864 +3867 +3872 +3875 +3876 +3884 +3880 +3891 +3897 +3898 +3894 +3891 +3895 +3893 +3912 +3913 +3910 +3916 +3912 +3917 +3925 +3937 +3942 +3941 +3947 +3952 +3955 +3952 +3944 +3945 +3946 +3960 +3985 +4012 +4013 +4014 +4012 +4032 +4045 +4029 +4030 +4048 +4055 +4054 +4072 +4073 +4083 +4085 +4086 +4091 +4094 +4097 +4112 +4090 +4102 +4108 +4134 +4147 +4148 +4149 +4172 +4175 +4182 +4176 +4154 +4170 +4180 +4179 +4186 +4187 +4182 +4170 +4172 +4178 +4181 +4193 +4188 +4193 +4192 +4202 +4205 +4207 +4185 +4183 +4187 +4189 +4204 +4207 +4202 +4204 +4201 +4214 +4219 +4216 +4211 +4199 +4196 +4195 +4199 +4200 +4201 +4192 +4191 +4195 +4217 +4205 +4210 +4218 +4204 +4207 +4213 +4214 +4215 +4233 +4240 +4253 +4251 +4253 +4262 +4266 +4257 +4261 +4262 +4271 +4263 +4262 +4284 +4288 +4267 +4276 +4283 +4284 +4283 +4284 +4281 +4278 +4266 +4261 +4260 +4276 +4281 +4283 +4272 +4297 +4302 +4301 +4312 +4311 +4329 +4346 +4336 +4347 +4365 +4364 +4366 +4370 +4372 +4365 +4364 +4367 +4365 +4366 +4368 +4369 +4372 +4379 +4373 +4391 +4390 +4393 +4389 +4370 +4372 +4375 +4376 +4378 +4387 +4388 +4408 +4419 +4421 +4414 +4415 +4413 +4415 +4419 +4420 +4405 +4422 +4421 +4423 +4435 +4434 +4439 +4441 +4443 +4450 +4446 +4450 +4452 +4453 +4454 +4458 +4462 +4475 +4492 +4489 +4494 +4498 +4497 +4498 +4499 +4511 +4515 +4516 +4529 +4534 +4538 +4540 +4537 +4544 +4561 +4562 +4563 +4565 +4569 +4564 +4569 +4576 +4580 +4619 +4618 +4620 +4633 +4634 +4613 +4611 +4614 +4632 +4618 +4629 +4631 +4643 +4640 +4644 +4628 +4630 +4632 +4631 +4648 +4652 +4651 +4640 +4637 +4638 +4641 +4649 +4654 +4650 +4648 +4651 +4650 +4630 +4631 +4632 +4628 +4634 +4633 +4635 +4641 +4633 +4632 +4634 +4646 +4658 +4654 +4639 +4658 +4659 +4664 +4673 +4679 +4681 +4687 +4685 +4719 +4720 +4721 +4731 +4741 +4744 +4727 +4729 +4735 +4738 +4748 +4749 +4750 +4746 +4753 +4754 +4762 +4773 +4774 +4777 +4783 +4782 +4793 +4799 +4805 +4810 +4813 +4812 +4824 +4817 +4815 +4817 +4816 +4804 +4809 +4827 +4820 +4819 +4849 +4850 +4851 +4850 +4862 +4861 +4873 +4875 +4878 +4879 +4882 +4883 +4878 +4880 +4893 +4890 +4891 +4886 +4889 +4890 +4904 +4902 +4925 +4927 +4923 +4920 +4915 +4924 +4896 +4898 +4891 +4894 +4898 +4900 +4901 +4902 +4912 +4913 +4911 +4908 +4917 +4905 +4908 +4931 +4934 +4969 +4973 +4972 +4982 +4983 +5001 +5000 +5004 +5009 +5007 +5002 +5000 +4999 +5000 +5002 +4991 +4993 +4997 +4998 +5000 +5003 +4998 +4999 +4996 +5005 +5004 +4993 +4994 +4995 +4996 +5000 +5004 +5011 +5013 +5012 +5002 +4999 +4979 +4989 +4992 +5002 +5004 +5005 +5004 +5027 +5026 +5027 +5031 +5035 +5037 +5044 +5050 +5057 +5062 +5065 +5062 +5056 +5058 +5065 +5073 +5093 +5092 +5109 +5110 +5130 +5137 +5159 +5172 +5164 +5194 +5196 +5197 +5234 +5233 +5222 +5225 +5227 +5231 +5248 +5249 +5261 +5254 +5255 +5256 +5257 +5254 +5252 +5253 +5261 +5279 +5266 +5267 +5259 +5283 +5309 +5332 +5335 +5336 +5337 +5336 +5344 +5349 +5356 +5386 +5396 +5403 +5409 +5410 +5413 +5415 +5417 +5431 +5433 +5436 +5434 +5436 +5440 +5450 +5449 +5461 +5480 +5482 +5479 +5481 +5482 +5494 +5495 +5494 +5495 +5498 +5497 +5510 +5512 +5508 +5499 +5509 +5520 +5540 +5553 +5554 +5555 +5568 +5550 +5540 +5535 +5564 +5567 +5575 +5574 +5586 +5579 +5580 +5579 +5577 +5588 +5592 +5562 +5568 +5589 +5595 +5606 +5607 +5596 +5608 +5627 +5635 +5632 +5631 +5630 +5629 +5628 +5626 +5625 +5629 +5630 +5629 +5630 +5633 +5634 +5639 +5640 +5660 +5666 +5660 +5663 +5662 +5672 +5671 +5652 +5650 +5667 +5666 +5642 +5643 +5642 +5640 +5642 +5644 +5649 +5650 +5647 +5648 +5647 +5651 +5650 +5651 +5653 +5646 +5627 +5636 +5635 +5623 +5624 +5625 +5624 +5633 +5630 +5622 +5619 +5616 +5634 +5635 +5630 +5664 +5671 +5673 +5671 +5683 +5693 +5699 +5700 +5701 +5697 +5693 +5664 +5665 +5677 +5670 +5684 +5653 +5648 +5646 +5648 +5645 +5651 +5650 +5651 +5652 +5653 +5654 +5639 +5648 +5640 +5657 +5667 +5685 +5706 +5697 +5698 +5702 +5709 +5705 +5720 +5723 +5716 +5712 +5708 +5707 +5668 +5665 +5666 +5667 +5666 +5670 +5669 +5670 +5671 +5675 +5676 +5682 +5683 +5673 +5675 +5666 +5675 +5677 +5686 +5693 +5694 +5676 +5677 +5678 +5680 +5689 +5712 +5714 +5730 +5729 +5740 +5743 +5776 +5777 +5788 +5787 +5783 +5791 diff --git a/aoc01/src/data.txt b/aoc01/src/data.txt new file mode 100644 index 0000000..167e291 --- /dev/null +++ b/aoc01/src/data.txt @@ -0,0 +1,10 @@ +199 +200 +208 +210 +200 +207 +240 +269 +260 +263 diff --git a/aoc01/src/main.rs b/aoc01/src/main.rs new file mode 100644 index 0000000..0116201 --- /dev/null +++ b/aoc01/src/main.rs @@ -0,0 +1,43 @@ +use std::fs; + +fn main() { + let data = read_data("data.txt"); + let increase_count = process(&data); + println!("Increasing values: {}", increase_count); + let increase_count = process_windowed(&data); + println!("Increasing values windowed: {}", increase_count); +} + +fn read_data(filename: &str) -> String { + fs::read_to_string(filename).expect("Could not read file") +} + +fn process(data: &str) -> i32 { + let mut last_value = i32::MAX; + let mut increase_count = 0; + for line in data.lines() { + let value = line.parse::().unwrap(); + if value > last_value { + increase_count += 1 + } + last_value = value; + } + increase_count +} + +fn process_windowed(data: &str) -> i32 { + let mut last_value = i32::MAX; + let mut increase_count = 0; + for i in 0..1998 { + let mut value = 0; + for j in 0..3 { + value += data.lines().nth(i + j).unwrap().parse::().unwrap(); + } + if value > last_value { + increase_count += 1 + } + last_value = value; + } + increase_count +} + diff --git a/aoc02/Cargo.lock b/aoc02/Cargo.lock new file mode 100644 index 0000000..1590d3e --- /dev/null +++ b/aoc02/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc02" +version = "0.1.0" diff --git a/aoc02/Cargo.toml b/aoc02/Cargo.toml new file mode 100644 index 0000000..e8d2c92 --- /dev/null +++ b/aoc02/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc02" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc02/data.txt b/aoc02/data.txt new file mode 100644 index 0000000..6f79fbc --- /dev/null +++ b/aoc02/data.txt @@ -0,0 +1,1000 @@ +forward 7 +down 1 +forward 9 +forward 4 +forward 7 +down 8 +forward 9 +down 2 +forward 5 +down 3 +forward 5 +up 7 +forward 3 +down 7 +down 8 +up 2 +down 4 +down 7 +forward 1 +forward 9 +down 9 +forward 7 +forward 8 +up 9 +down 6 +down 7 +forward 1 +forward 5 +forward 3 +down 3 +down 4 +forward 1 +forward 1 +forward 7 +down 4 +forward 6 +forward 1 +forward 9 +forward 4 +forward 6 +forward 6 +down 5 +down 4 +down 8 +up 9 +up 9 +down 6 +down 6 +forward 4 +forward 1 +down 1 +forward 7 +down 1 +forward 3 +forward 7 +forward 3 +up 8 +down 7 +down 4 +forward 7 +forward 1 +forward 2 +down 1 +down 4 +up 2 +up 8 +forward 6 +forward 5 +down 9 +up 8 +forward 1 +up 9 +down 7 +forward 1 +forward 5 +up 1 +up 7 +down 1 +down 5 +forward 9 +down 5 +up 5 +forward 8 +forward 9 +down 8 +forward 1 +down 1 +forward 7 +down 8 +up 4 +forward 9 +up 1 +down 7 +forward 4 +up 8 +down 5 +down 2 +down 1 +forward 5 +forward 3 +up 5 +forward 6 +up 1 +up 6 +up 2 +up 6 +up 5 +down 6 +forward 5 +down 4 +forward 7 +up 5 +down 3 +forward 9 +down 5 +down 2 +up 2 +forward 2 +down 1 +up 3 +down 8 +forward 8 +down 8 +forward 6 +forward 4 +forward 7 +forward 6 +up 6 +down 4 +forward 7 +forward 9 +forward 2 +forward 5 +down 4 +forward 4 +forward 9 +forward 2 +down 2 +forward 8 +up 8 +forward 1 +down 6 +forward 5 +down 4 +down 4 +down 7 +down 5 +forward 7 +forward 7 +down 9 +up 9 +forward 4 +down 2 +forward 6 +down 4 +forward 8 +forward 4 +up 5 +forward 8 +down 2 +forward 8 +forward 6 +forward 9 +down 7 +forward 1 +forward 1 +forward 1 +down 3 +forward 2 +down 5 +forward 3 +forward 8 +forward 4 +forward 7 +forward 8 +up 7 +down 2 +up 7 +forward 4 +forward 7 +down 4 +down 7 +down 3 +down 8 +forward 2 +down 2 +forward 2 +down 6 +forward 2 +down 8 +forward 2 +forward 5 +down 5 +forward 4 +down 7 +up 8 +up 7 +down 2 +down 1 +forward 4 +down 2 +forward 9 +down 2 +down 2 +forward 7 +forward 3 +forward 6 +up 8 +up 7 +forward 1 +forward 3 +down 9 +down 1 +forward 8 +down 7 +up 8 +down 3 +up 4 +forward 9 +forward 6 +up 6 +up 4 +down 2 +down 4 +forward 7 +down 2 +down 9 +forward 1 +forward 9 +down 6 +forward 1 +down 8 +forward 9 +down 9 +down 6 +up 4 +forward 5 +up 9 +down 2 +forward 7 +down 1 +down 1 +down 7 +down 4 +down 1 +down 6 +forward 4 +forward 6 +up 1 +down 8 +down 4 +forward 1 +down 5 +down 4 +up 1 +down 9 +forward 1 +down 1 +up 8 +forward 8 +forward 8 +down 5 +down 2 +down 3 +forward 1 +down 1 +forward 3 +down 7 +forward 7 +forward 1 +forward 3 +forward 5 +forward 3 +down 8 +forward 1 +up 2 +up 3 +down 4 +down 4 +up 4 +down 4 +up 8 +down 8 +down 7 +down 6 +forward 5 +down 3 +forward 2 +up 9 +down 7 +forward 2 +down 1 +down 3 +down 7 +down 1 +forward 7 +down 2 +down 9 +down 7 +up 1 +down 5 +forward 7 +forward 2 +forward 7 +forward 3 +down 2 +down 8 +up 3 +down 3 +forward 3 +down 2 +up 7 +up 1 +forward 5 +down 6 +down 3 +forward 6 +forward 8 +forward 2 +down 9 +down 1 +down 1 +down 4 +forward 8 +down 3 +up 3 +up 4 +forward 3 +forward 7 +down 3 +up 6 +down 8 +up 4 +up 3 +forward 5 +down 9 +down 2 +forward 6 +forward 4 +up 6 +up 1 +forward 4 +forward 7 +forward 1 +forward 8 +forward 3 +forward 3 +forward 9 +forward 4 +forward 4 +forward 4 +forward 2 +forward 7 +down 8 +forward 5 +up 1 +forward 3 +forward 7 +down 3 +forward 7 +up 6 +forward 4 +up 2 +down 7 +down 4 +up 3 +up 7 +down 1 +forward 5 +up 7 +up 7 +down 7 +down 9 +down 7 +forward 5 +forward 7 +down 1 +down 5 +down 6 +forward 6 +up 7 +forward 7 +forward 6 +down 4 +down 3 +down 9 +up 5 +down 9 +down 3 +forward 8 +forward 1 +forward 5 +down 6 +forward 7 +down 7 +forward 8 +down 9 +up 1 +up 2 +forward 2 +down 4 +up 1 +forward 2 +forward 9 +down 8 +forward 3 +up 3 +up 3 +forward 3 +down 7 +down 5 +forward 2 +up 3 +forward 3 +forward 5 +up 5 +forward 3 +down 1 +down 3 +down 6 +down 1 +forward 7 +forward 6 +down 5 +down 2 +forward 7 +down 3 +forward 2 +forward 2 +forward 2 +down 1 +forward 4 +forward 5 +down 8 +up 8 +up 7 +down 7 +forward 9 +down 8 +forward 1 +forward 6 +up 3 +forward 8 +up 9 +down 6 +down 9 +up 4 +down 1 +forward 1 +up 7 +down 4 +forward 7 +forward 4 +down 2 +forward 3 +forward 4 +down 7 +forward 5 +up 8 +down 9 +down 3 +forward 3 +forward 8 +down 7 +forward 1 +up 7 +down 7 +forward 7 +down 9 +down 6 +down 3 +down 1 +up 5 +up 6 +forward 8 +down 8 +up 1 +forward 6 +up 2 +down 6 +down 7 +down 5 +forward 7 +up 3 +up 6 +down 3 +up 9 +forward 9 +down 2 +down 4 +up 2 +forward 7 +down 7 +up 4 +forward 2 +down 4 +forward 7 +up 9 +forward 4 +forward 9 +down 8 +up 2 +up 2 +forward 2 +up 8 +forward 1 +forward 8 +forward 8 +down 9 +forward 6 +up 1 +down 3 +down 4 +down 6 +forward 6 +up 6 +down 2 +down 1 +down 1 +forward 9 +forward 2 +down 7 +down 8 +down 6 +down 3 +forward 1 +up 6 +up 4 +forward 3 +down 1 +forward 6 +down 8 +forward 6 +forward 5 +down 3 +down 5 +up 2 +down 7 +forward 8 +down 8 +up 3 +down 4 +down 3 +down 4 +up 9 +up 6 +forward 5 +forward 8 +down 8 +forward 1 +forward 8 +down 1 +down 3 +down 4 +down 6 +forward 3 +down 8 +down 9 +down 3 +forward 3 +forward 2 +forward 2 +down 1 +up 7 +down 6 +up 2 +down 9 +down 4 +down 3 +down 5 +down 4 +forward 4 +up 8 +forward 1 +down 9 +down 3 +down 3 +down 8 +down 5 +up 6 +up 5 +down 7 +down 3 +down 7 +forward 1 +up 5 +down 1 +forward 1 +down 7 +down 9 +down 3 +forward 1 +forward 7 +down 8 +forward 5 +up 5 +forward 2 +down 6 +forward 2 +down 2 +forward 2 +up 7 +down 2 +down 5 +down 2 +forward 7 +forward 2 +forward 4 +down 2 +down 7 +down 6 +forward 9 +down 8 +down 3 +down 6 +forward 7 +down 3 +down 4 +down 7 +down 7 +down 1 +up 8 +up 7 +up 6 +down 8 +up 8 +forward 8 +down 2 +down 4 +forward 7 +forward 4 +down 5 +down 4 +down 3 +forward 6 +up 9 +down 1 +down 3 +down 4 +down 7 +up 3 +up 4 +forward 1 +up 4 +down 9 +up 4 +up 3 +down 1 +down 2 +forward 5 +up 4 +down 1 +down 4 +down 8 +down 1 +up 6 +down 6 +forward 8 +forward 7 +down 5 +down 6 +down 5 +down 6 +down 9 +forward 6 +down 6 +down 7 +forward 3 +down 2 +down 9 +up 2 +down 6 +forward 2 +forward 6 +forward 4 +down 4 +forward 8 +forward 2 +forward 7 +forward 4 +down 9 +forward 6 +down 6 +forward 1 +down 4 +down 5 +forward 5 +up 5 +forward 7 +up 2 +down 9 +forward 9 +down 9 +forward 9 +forward 3 +forward 7 +forward 5 +forward 1 +down 5 +forward 5 +down 3 +down 2 +up 9 +down 7 +down 2 +forward 3 +down 8 +down 6 +down 7 +up 5 +up 9 +down 6 +down 8 +down 5 +down 8 +forward 4 +forward 5 +down 2 +down 1 +forward 6 +down 6 +forward 9 +forward 2 +forward 1 +down 3 +forward 8 +forward 6 +up 7 +forward 5 +down 4 +up 7 +down 6 +down 3 +up 8 +forward 5 +forward 5 +down 1 +down 7 +forward 2 +down 5 +forward 3 +forward 6 +forward 6 +up 5 +down 4 +down 2 +forward 7 +up 8 +forward 5 +up 7 +forward 6 +forward 6 +forward 6 +up 7 +down 1 +down 4 +down 4 +forward 5 +down 5 +up 7 +down 7 +forward 8 +down 6 +down 2 +forward 3 +down 8 +up 3 +down 2 +forward 7 +down 1 +forward 3 +up 1 +up 4 +down 4 +up 6 +down 1 +down 2 +forward 9 +up 8 +forward 3 +down 2 +forward 3 +down 7 +up 6 +forward 9 +down 3 +down 9 +up 5 +down 9 +down 6 +forward 8 +forward 5 +down 8 +forward 5 +forward 9 +up 1 +down 3 +up 2 +down 1 +down 2 +down 8 +forward 9 +forward 9 +down 7 +down 7 +down 5 +down 1 +down 7 +up 1 +up 1 +forward 8 +down 6 +up 8 +down 7 +down 1 +forward 1 +forward 4 +down 9 +up 2 +down 6 +forward 2 +down 4 +forward 7 +up 8 +down 3 +forward 9 +down 7 +up 7 +down 6 +down 9 +down 2 +up 7 +down 3 +down 9 +forward 7 +down 7 +up 6 +down 5 +down 4 +up 1 +down 1 +up 7 +up 9 +down 1 +forward 6 +forward 3 +down 5 +down 1 +up 8 +forward 9 +forward 6 +down 1 +up 3 +down 2 +forward 3 +up 6 +down 5 +down 3 +up 7 +down 8 +down 5 +down 3 +forward 6 +up 8 +down 4 +forward 3 +forward 5 +forward 9 +forward 1 +up 4 +forward 8 +forward 9 +forward 1 +up 9 +up 4 +forward 4 +forward 4 +forward 8 +up 5 +up 2 +down 7 +down 4 +up 5 +down 5 +forward 6 +up 8 +down 5 +down 7 +down 3 +up 2 +up 2 +forward 3 +forward 5 +forward 1 +forward 9 +forward 1 +down 2 +down 2 +up 6 +up 6 +down 5 +down 3 +down 4 +forward 4 +down 6 +up 6 +forward 8 +down 9 +forward 2 +forward 6 +down 7 +forward 3 +down 2 +down 5 +up 3 +forward 6 +forward 7 +forward 3 +forward 4 +down 9 +up 9 +forward 7 +down 8 +forward 1 +down 2 +up 5 +up 9 +forward 8 +up 5 +forward 7 +up 6 +forward 9 +down 6 +forward 4 +down 4 +forward 7 +up 8 +down 8 +down 7 +down 9 +up 1 +down 5 +forward 6 +down 9 +down 2 +down 2 +down 6 +down 6 +forward 8 +forward 5 +up 7 +forward 9 +forward 3 +forward 8 +forward 8 +down 7 +up 1 +down 4 +forward 7 +up 5 +down 9 +down 7 +forward 8 +down 3 +up 2 +down 7 +forward 3 +up 8 +forward 4 +up 2 +forward 1 +forward 4 +forward 5 +forward 3 +forward 6 +down 9 +up 7 +forward 7 +up 6 +down 2 +down 5 +forward 2 +forward 8 +down 3 +forward 6 +down 6 +forward 4 +up 3 +forward 3 +down 4 +up 3 +forward 6 +forward 7 +down 5 +forward 5 +down 9 +forward 7 +down 5 +down 6 +forward 9 diff --git a/aoc02/src/main.rs b/aoc02/src/main.rs new file mode 100644 index 0000000..68a7031 --- /dev/null +++ b/aoc02/src/main.rs @@ -0,0 +1,77 @@ +trait Move { + fn single_command(&mut self, direction: Direction, amount: i32); + fn multiple_commands(&mut self, sequence_of_movements: Vec<(Direction, i32)>); + fn result(&mut self) -> i32; +} + +#[derive(Debug)] +struct Submarine{ + horizontal: i32, + depth: i32, + aim: i32, +} + +impl Move for Submarine { + fn single_command(&mut self, direction: Direction, amount: i32) { + println!("{:?} {}", direction, amount); + if direction == Direction::Down { + self.aim += amount; + } else if direction == Direction::Up { + self.aim -= amount; + } else if direction == Direction::Forward { + self.horizontal += amount; + self.depth += self.aim * amount; + } + } + + fn multiple_commands(&mut self, sequence_of_movements: Vec<(Direction, i32)>) { + for (direction, amount) in sequence_of_movements { + println!("{:#?}", self); + self.single_command(direction, amount); + } + } + + fn result(&mut self) -> i32 { + return self.horizontal * self.depth + } +} + +#[derive(Debug, PartialEq)] +enum Direction{ + Down, + Up, + Forward, + Unknown, +} + +fn main() { + let sequence_of_movements = read_sequence_of_movements("data.txt"); + let mut submarine = Submarine{ horizontal: 0, depth: 0, aim: 0}; + submarine.multiple_commands(sequence_of_movements); + println!("result: {}", submarine.result()); +} + + +fn read_sequence_of_movements(filename: &str) -> Vec<(Direction, i32)> { + let data = std::fs::read_to_string(filename).unwrap(); + let mut sequence_of_movements: Vec<(Direction, i32)> = Vec::new(); + for line in data.lines() { + let direction = line.split(" ") + .nth(0) + .unwrap(); + let direction_actual = match direction { + "down" => Direction::Down, + "up" => Direction::Up, + "forward" => Direction::Forward, + _ => Direction::Unknown, + }; + let amount = line.split(" ") + .nth(1) + .unwrap() + .parse::() + .unwrap(); + sequence_of_movements.push((direction_actual, amount)) + } + return sequence_of_movements + +} \ No newline at end of file diff --git a/aoc02/testdata.txt b/aoc02/testdata.txt new file mode 100644 index 0000000..b7172ac --- /dev/null +++ b/aoc02/testdata.txt @@ -0,0 +1,6 @@ +forward 5 +down 5 +forward 8 +up 3 +down 8 +forward 2 diff --git a/aoc03/Cargo.lock b/aoc03/Cargo.lock new file mode 100644 index 0000000..ad57c87 --- /dev/null +++ b/aoc03/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc03" +version = "0.1.0" diff --git a/aoc03/Cargo.toml b/aoc03/Cargo.toml new file mode 100644 index 0000000..31b7ba1 --- /dev/null +++ b/aoc03/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc03" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc03/data.txt b/aoc03/data.txt new file mode 100644 index 0000000..6a7de1a --- /dev/null +++ b/aoc03/data.txt @@ -0,0 +1,1000 @@ +101011111001 +100100110111 +101010101011 +010100010000 +001101101101 +110001101010 +001001011001 +101111101111 +110001001000 +111111000000 +111100110001 +101100010100 +000010000011 +000110000011 +101010100101 +000111001010 +100101101110 +111111010101 +110010001100 +000011001101 +001001111110 +100101001100 +000011100100 +000100110011 +000101101000 +101010011011 +101011110000 +010010110011 +110110101110 +001110000110 +001110111101 +110111100101 +000010101010 +101010101010 +011011001101 +110110000111 +011010011000 +000111000101 +100000101011 +010010101100 +110000101110 +000010001001 +101010001100 +101111110101 +101111011110 +110011100001 +011011100100 +111100011010 +110110101001 +101011100010 +111000110110 +111011100101 +111000001111 +101110110011 +111101000000 +101111100110 +011111101101 +100111110111 +000111011011 +001001001111 +100010110000 +001101010000 +100101101001 +100001010000 +110101110001 +100111111111 +101101000101 +001110100110 +101010110000 +101111010010 +101011100101 +001110010100 +011110110100 +100011000000 +001000000100 +010000010001 +101101111011 +111010001011 +101010100000 +101001000000 +001000010101 +010011000111 +101010111100 +111101000101 +111111100010 +100010011101 +101001111101 +010111001111 +111111110110 +100111101010 +010101000101 +100101010010 +010110010001 +011101000111 +011110011010 +100000111100 +011111111000 +001111010001 +110110100111 +111000001011 +110010010011 +101010110010 +110000110111 +101111010000 +110111010000 +110011100011 +010000100111 +001010110001 +011101011010 +110111010010 +011001001101 +101101001111 +011101110100 +001101101110 +011011101101 +111001010001 +001100101000 +010011100000 +011000010100 +111000111111 +001001100001 +010011001010 +001110101100 +010010111010 +000111111011 +010010000110 +110001100100 +001110110111 +000010001111 +100011100001 +100001101110 +100001110111 +010110100000 +100101101111 +001100000111 +101100111001 +100100001100 +100000101100 +010101000110 +101110001000 +101110000110 +101110011100 +001001010001 +110111101011 +101001000001 +111100111110 +001100010100 +010001111111 +110011000011 +100110011101 +011010100100 +011111000011 +000110001000 +010010111111 +010011111011 +001000111110 +000111101011 +100010111100 +010110001111 +011011000000 +010111000011 +010001011111 +111000101100 +010110110000 +001100111011 +011110100101 +100011100011 +001000101000 +101011001011 +101111001111 +111000100101 +111101100111 +111010101000 +001110000111 +010101011110 +001111010100 +101011011101 +000111101010 +000011001011 +111000010001 +011001011011 +011011011001 +011110111000 +000010001100 +011110010000 +010000101001 +101011011111 +100111001100 +101000011100 +111000000100 +011100110000 +011100101010 +010001110011 +100100100010 +011110010010 +011000100111 +110101000101 +111011000001 +011000010111 +010011110011 +011001000001 +000100101110 +100111110001 +010010011010 +111101011001 +100110101111 +100010001100 +010010000111 +100011100100 +111001111011 +110000010111 +101001010001 +011111001010 +110111111100 +000101111101 +101011000111 +000100010000 +111100001111 +100000110010 +110001110100 +000001001100 +000101000001 +100111101011 +011000110110 +101111000000 +010100101100 +101111100011 +100111001101 +100000010010 +100101001111 +011110100010 +110110100110 +100000100001 +110100001110 +010011001111 +011010010000 +000000010101 +111111011100 +000011100000 +011110111101 +110000011100 +011011011000 +010100111010 +010010100101 +001110100011 +000110000100 +100111000101 +000010001000 +110110110000 +010011000100 +011001100010 +110000011110 +000110100111 +010111111011 +011100000011 +100011101110 +001100011010 +000010100100 +000011110000 +000000101000 +101001101001 +101111010101 +101100101101 +110000000000 +111011001110 +001100101110 +100101111000 +010001000111 +111100111010 +000100011101 +010001000011 +100000101001 +111111101101 +000000011101 +001101110110 +001100000101 +010110011110 +001011100100 +110001100101 +001011110011 +011101111000 +001001100110 +000001011000 +001011010000 +110001010100 +000100011011 +010101101010 +101000101010 +101111011000 +110001010111 +111100110011 +101011101000 +000100010110 +111010000110 +000100000001 +101011110101 +010010010011 +100101011001 +100101001010 +110100110101 +010011100111 +100110010101 +111010000011 +011011110110 +111000101110 +001011101100 +010110100010 +011001101011 +011000101101 +011011111110 +011011111001 +100001001101 +101110011011 +001111011100 +111110010011 +000010101011 +001111101010 +100001101000 +111100100000 +010011100100 +011101101100 +000001101011 +001100001110 +000011000111 +100110001000 +101101101011 +001011011110 +101010111101 +101110100110 +011111000100 +111101111000 +001011110111 +010010011001 +111110110001 +100101000100 +011011010101 +100010010000 +100010101000 +110010000100 +000001001111 +101110011010 +010000111111 +111101010001 +100000000000 +011101100110 +001110101101 +001001110010 +110011101001 +101101011001 +000000110101 +011101100011 +010100000011 +101011110010 +100101000010 +100110001100 +000111111010 +111100011001 +111110101001 +010000111000 +100110000011 +100011001110 +000100010010 +001101100100 +000000001100 +011101001001 +100001001000 +101100111000 +110011011111 +111110101110 +101101001010 +111000011001 +111110011001 +110111010100 +011001111011 +101101111000 +010010010100 +100000110110 +011110010100 +010010101010 +001111100100 +100000011101 +110100010010 +101101100000 +111001111010 +010001100110 +011000110101 +011110010011 +011111100000 +001000101100 +000100010111 +011000101010 +011000010110 +010001101011 +110101010000 +000100100100 +011011001100 +010100011101 +100111110110 +111010000001 +001001111111 +010100001111 +011111101011 +011000011011 +111010000100 +110001001111 +111011011010 +001110001110 +100100101001 +101101101001 +001100001001 +001000001110 +101111000010 +000110001110 +101111110010 +110011001011 +000000010100 +110100110001 +111000011111 +100000001110 +100100011011 +101100100011 +011101000010 +001100100101 +111110101101 +011100001001 +101100000110 +101011010011 +000101101100 +001011101001 +110000000101 +011111011110 +000001100110 +100110011011 +111010100100 +011001101001 +010111110011 +110111000000 +011100101011 +010110100111 +110011001001 +000010111011 +101110010110 +000001011001 +100101000111 +110110111011 +000101110001 +100000111101 +011110001011 +000101010101 +101011110011 +000110011001 +101010000001 +100001011110 +010000000111 +000011100110 +100111100010 +110101000111 +001010101110 +111000100000 +001100101101 +000111010111 +101110000000 +011111000010 +111000001110 +001100110101 +101001000100 +010111011101 +000100000100 +111111000111 +111011001111 +101000011110 +110011101101 +111101000011 +101101111001 +010010010101 +110000100010 +000100100111 +000110111111 +000011001000 +011011100001 +101100111101 +011111000001 +000110101100 +010010111001 +111100101110 +100111010100 +011111000000 +001011001000 +101111000011 +000100001010 +110100010000 +110001000011 +010010101111 +001010001010 +010001110001 +011000001101 +101001010111 +000000011100 +010011111000 +110010011011 +001110100111 +011101000011 +111110011101 +100111000000 +101011101110 +111000010111 +101001000011 +011110011001 +000100010001 +101110000100 +111101101101 +000100100110 +010011010001 +000101100110 +010111110001 +001110111100 +100000111111 +101100000011 +110011000100 +100000001011 +010101100000 +101010000101 +010110101101 +010011100001 +001101001111 +101100100111 +100000001101 +100100101100 +101111010111 +010101001100 +011001111111 +010111110000 +010100110101 +101111010001 +111101111100 +011010011011 +111100110100 +111110001010 +011110011111 +000110001100 +011001010111 +000010010111 +011100101111 +111110111000 +110010110010 +000101111010 +001010010100 +100110110001 +101101010001 +100001111011 +010101001001 +000001000010 +100101100011 +010100101010 +001001001100 +101000110110 +111110001000 +100101110010 +001000111011 +100111010111 +000000110000 +110101111000 +011000101110 +110101001111 +101000010010 +101011000010 +100110000111 +110101011001 +110010010100 +010000001111 +110011011001 +111010110100 +010000101010 +101100111011 +000000011010 +110110001000 +001011011011 +101110100001 +010100011011 +111101001101 +010000011001 +110010100100 +101101011010 +110000100001 +010101111000 +101000111010 +011011010100 +011110011101 +010000100100 +001111101001 +010011110000 +111000100111 +010101010101 +000001001010 +010001001100 +111011111100 +001110000011 +110101101000 +111010011110 +110011010011 +001010101101 +001111110001 +110001011110 +011110010001 +010010000100 +111010111001 +000100111111 +000001010101 +001010110111 +000110010011 +000010011111 +011110100001 +011111010110 +001110010110 +001001000011 +101111011101 +011100110111 +010000001101 +010010100111 +010000110011 +011110110111 +010000110110 +011101011011 +000010100001 +001100011111 +011110110000 +111011011011 +101011101111 +010001000101 +010100001000 +010100101110 +001000000101 +001001011011 +111001000101 +110101110100 +111111010111 +101010100111 +110000011010 +000100100000 +011101000001 +000011000001 +010101010001 +010010011100 +010001110101 +010110110111 +010011110010 +011011100110 +000110100011 +100010100011 +101111110011 +001011100101 +011101110000 +110001110000 +111010010000 +111111000011 +010010001100 +010111101010 +010010101000 +111001010111 +010110110011 +001101110010 +011111001111 +010100110100 +010010100100 +101011101100 +000111101001 +000001011010 +101010111000 +100011111111 +100100011111 +111000010010 +011011100010 +000111010010 +101010101111 +010000100101 +100110110101 +011011100011 +011011110011 +111011000100 +100010001010 +110000010000 +100101011111 +101100001100 +111110101111 +100111000100 +011101001110 +100011011101 +111101011010 +110101111001 +001000110101 +111110101100 +001010010111 +100110000000 +110101010100 +110110001101 +011110101101 +100110101110 +000000000010 +011010111001 +000001101111 +000100000101 +110000100011 +001010000101 +100111011010 +000001101001 +001011000001 +000111100110 +111111110011 +000101000000 +111000110000 +000010000111 +000101010001 +000011110001 +101100110100 +100001111110 +001111111111 +100011000110 +011100011001 +101011110100 +001010001111 +100010100001 +001111100110 +010110110010 +110010110101 +011000110011 +000101011110 +101000100100 +010110010110 +001000000110 +110000100100 +011101101011 +100100000111 +000111010101 +110111000100 +001010110000 +101001110001 +001001101011 +101000100101 +111111000010 +000010110101 +110000000110 +000100110000 +111111011011 +101101100101 +011001010010 +111111101111 +000111101101 +100110110011 +110000001000 +111000001101 +110011010010 +110110111110 +000001011111 +001100001010 +111101011100 +010010100010 +110100011011 +110001101011 +010111111010 +011111100100 +001010010110 +001010111111 +111110111110 +011010000110 +111111111001 +011011011110 +001010011101 +001110010111 +001001111100 +110010111000 +000111000100 +001100001000 +110110110100 +101001010000 +000101000101 +010101101100 +101010110110 +111101011000 +101010011100 +110010110011 +111110001110 +100100101110 +001010000000 +001000110010 +101000111111 +111110111101 +000110010110 +101100100110 +100010011001 +100110100010 +111101100110 +111100111111 +010111011011 +111100011111 +001010010011 +111101010010 +110111001010 +010110000000 +011111011001 +101011100011 +011100110011 +111100011110 +101001001001 +100101100110 +000100110010 +010011101110 +000011110111 +000000011111 +110011011100 +111110100100 +101110101111 +001101011000 +101101110001 +101011000100 +111001101100 +110111111001 +011010101001 +000000000000 +111100000101 +110010111001 +000100001110 +110101111100 +110010011111 +001001110111 +111111111111 +000110011100 +110101111010 +011010111011 +111000000101 +011110100111 +001010101111 +001011011001 +101010001011 +100001000000 +111110111010 +001110111110 +000000110100 +111110111001 +111000010101 +110011110111 +110111001101 +111011010010 +110100000110 +100111111100 +010001100010 +111100010000 +100111011101 +101001000111 +111110110011 +010101010100 +101000110111 +000111101100 +100001011001 +010000011111 +000100101011 +011011011010 +001111000010 +111001101011 +100011010111 +110111100111 +100001010110 +100001110101 +100110110111 +011111111011 +111111101110 +111111001000 +101010000110 +110010010110 +100110100111 +011101011100 +011010011101 +101111101101 +110111010110 +110000000100 +110000110100 +001100000110 +001011001101 +001010011111 +011001110000 +100111100100 +001111000000 +110100011001 +110000110011 +000000101101 +111101111111 +111111010010 +011101101110 +111101101100 +100000110000 +110101010001 +101001110100 +010010110110 +111011000111 +101100000100 +010011010101 +111101000001 +101001111000 +101110000011 +001011011101 +111010000010 +001001110100 +111111001001 +001001010000 +001110000001 +111100000000 +010101110100 +000101011011 +001100011001 +001110001100 +001110100010 +000000100111 +000100111000 +110100101100 +110100111010 +111001101101 +111100101011 +011101100000 +100000100011 +010111110010 +010011001000 +101001100110 +100110001111 +000110110101 +111001110110 +000101010110 +011000011010 +010100110110 +110100111111 +111000110011 +001011111000 +001000000000 +111111010100 +111111100110 +000000001111 +110001000001 +010000101000 +101111111010 +100100001101 +111011110100 +110101110011 +010111111110 +001000101111 +100110101100 +110010010010 +010010001000 +011111101100 +111100100011 +110111101000 +100000111001 +001011100011 +001010111100 +000100001100 +010111000110 +001111000101 +001110000010 +110100101000 +001110000101 +101000110000 +001001011101 +101001011001 +011110100011 +101010010110 +101000001101 +110111111111 +010010001101 +011100010110 +000100100001 +001111111101 +001001111000 +001100101111 +111010111100 +110100010111 +000000010011 +100110110000 +101100101111 +100011010100 +001001101110 +110001101000 +110101101110 +010011110101 +010000110001 +100011111000 +111110000010 +100111100000 +100001100110 +110111000110 +000101100011 +010110001100 +100000100010 +001011000000 +110101000010 +101110101001 +011010100110 +111111110000 +001010110010 +010000001011 +100000010011 +010111010111 +101111100010 +010100010011 diff --git a/aoc03/src/main.rs b/aoc03/src/main.rs new file mode 100644 index 0000000..d08dcf8 --- /dev/null +++ b/aoc03/src/main.rs @@ -0,0 +1,104 @@ +use std::convert::TryInto; + +trait Matrix { + fn column_sums(&self) -> Vec; + fn gamma_epsilon_rate(&self) -> (i32, i32); + fn power_consumption(&self) -> i32; +} + +struct Data { + data: Vec>, + rows: i32, + columns: i32, +} + +impl Matrix for Data { + + fn column_sums(&self) -> Vec { + let mut result: Vec = Vec::new(); + + for c in 0..self.columns { + let mut column_result = 0; + for r in 0..self.rows { + column_result += self.data + .iter() + .nth(r as usize) + .unwrap() + .iter() + .nth(c + .try_into() + .unwrap() + ) + .unwrap(); + } + result.push(column_result); + } + result + } + + fn gamma_epsilon_rate(&self) -> (i32, i32) { + let mut result: Vec = Vec::new(); + for value in self.column_sums() { + if value > self.rows / 2 { + result.push(1) + } else { + result.push(0) + } + } + return (bin2dec(complement(&result)), bin2dec(result)); + } + + fn power_consumption(&self) -> i32 { + let (gamma, epsilon) = self.gamma_epsilon_rate(); + gamma * epsilon + } +} + +fn bin2dec(bin: Vec) -> i32 { + let mut result = 0; + let length = bin.len() as u32; + for i in 0..length { + result += 2_i32.pow(length-i-1) * bin. + iter() + .nth(i.try_into().unwrap()) + .unwrap(); + } + return result +} + +fn complement(input: &Vec) -> Vec { + let mut result: Vec = Vec::new(); + for value in input { + if value.to_owned() == 0 { + result.push(1); + } else { + result.push(0) + } + } + result +} + +fn read_data(filename: &str) -> Data { + let content = std::fs::read_to_string(filename).unwrap(); + let mut data: Vec> = Vec::new(); + for line in content.lines() { + let mut row: Vec = Vec::new(); + for char in line.chars() { + row.push(char as i32 - 0x30); + } + data.push(row); + } + let rows = data.len() as i32; + let columns = data.iter().nth(0).unwrap().len() as i32; + return Data{data, rows, columns}; +} + +fn main() { + let data = read_data("data.txt"); + + println!("{:?}", data.gamma_epsilon_rate()); + println!("{:?}", data.power_consumption()); + + +} + diff --git a/aoc06/Cargo.lock b/aoc06/Cargo.lock new file mode 100644 index 0000000..1099fdc --- /dev/null +++ b/aoc06/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc06" +version = "0.1.0" diff --git a/aoc06/Cargo.toml b/aoc06/Cargo.toml new file mode 100644 index 0000000..1398cd2 --- /dev/null +++ b/aoc06/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc06" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc06/data.txt b/aoc06/data.txt new file mode 100644 index 0000000..f6051c6 --- /dev/null +++ b/aoc06/data.txt @@ -0,0 +1 @@ +1,3,4,1,1,1,1,1,1,1,1,2,2,1,4,2,4,1,1,1,1,1,5,4,1,1,2,1,1,1,1,4,1,1,1,4,4,1,1,1,1,1,1,1,2,4,1,3,1,1,2,1,2,1,1,4,1,1,1,4,3,1,3,1,5,1,1,3,4,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,5,2,5,5,3,2,1,5,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,1,1,1,1,5,1,1,1,1,1,4,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,3,1,2,4,1,5,5,1,1,5,3,4,4,4,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,5,3,1,4,1,1,2,2,1,2,2,5,1,1,1,2,1,1,1,1,3,4,5,1,2,1,1,1,1,1,5,2,1,1,1,1,1,1,5,1,1,1,1,1,1,1,5,1,4,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,4,5,1,1,1,1,1,1,1,5,1,1,3,1,1,1,3,1,4,2,1,5,1,3,5,5,2,1,3,1,1,1,1,1,3,1,3,1,1,2,4,3,1,4,2,2,1,1,1,1,1,1,1,5,2,1,1,1,2 \ No newline at end of file diff --git a/aoc06/src/main.rs b/aoc06/src/main.rs new file mode 100644 index 0000000..35c7964 --- /dev/null +++ b/aoc06/src/main.rs @@ -0,0 +1,58 @@ +const SIZE: usize = 10; +const REPRODUCTIVE_CYCLE: usize = 6; +const SPAWN_GENERATION: usize = 8; +const OVERFLOW: usize = 9; + +struct Population { + data: [i128; SIZE] + +} + +impl Population { + fn get_new_spawn(&self) -> i128 { + self.data[OVERFLOW] + } + + fn reset_new_spawn(&mut self) { + self.data[OVERFLOW] = 0 + } + + fn iterate(&mut self, generations: i32) { + for _i in 0..generations { + self.data.rotate_left(1); + self.data[REPRODUCTIVE_CYCLE] += self.get_new_spawn(); + self.data[SPAWN_GENERATION] = self.get_new_spawn(); + self.reset_new_spawn(); + } + } + + fn calc_sum(&self) -> i128 { + let mut result = 0; + for item in self.data { + result += item; + } + result + } +} + +fn main() { + let mut data = load_data("data.txt"); + data.iterate(256); + let sum = data.calc_sum(); + println!("lanternfish: {}", sum); +} + +fn load_data(filename: &str) -> Population { + let content = std::fs::read_to_string(filename).unwrap(); + let mut data: [i128; SIZE] = [0; SIZE]; + for elements in content.split(",") { + println!("{:?}", elements); + let value = elements.parse::(); + let x = match value { + Ok(x) => x as usize, + Err(_e) => break + }; + data[x] += 1; + } + Population{data} +} diff --git a/aoc06/testdata.txt b/aoc06/testdata.txt new file mode 100644 index 0000000..a7af2b1 --- /dev/null +++ b/aoc06/testdata.txt @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file diff --git a/aoc07/Cargo.lock b/aoc07/Cargo.lock new file mode 100644 index 0000000..e7403ae --- /dev/null +++ b/aoc07/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc07" +version = "0.1.0" diff --git a/aoc07/Cargo.toml b/aoc07/Cargo.toml new file mode 100644 index 0000000..9f6b1bd --- /dev/null +++ b/aoc07/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc07" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc07/src/main.rs b/aoc07/src/main.rs new file mode 100644 index 0000000..5de551f --- /dev/null +++ b/aoc07/src/main.rs @@ -0,0 +1,36 @@ +fn main() { + let data = vec![16,1,2,0,4,2,7,1,2,14]; + let (fuel, position) = optimise(&data); + println!("fuel: {}, position: {}", fuel, position); +} + +fn fuel_for_pos(data: &Vec, position: i32) -> i32 { + let mut result = 0; + for item in data { + result += (item - position).abs(); + } + return result +} + +fn get_min(vec: &Vec) -> i32 { + return vec.iter().min().unwrap().to_owned(); +} + +fn get_max(vec: &Vec) -> i32 { + return vec.iter().max().unwrap().to_owned(); +} + +fn optimise(data: &Vec) -> (i32, i32) { + let mut positions: Vec = Vec::new(); + let mut fuel: Vec = Vec::new(); + for i in get_min(&data)..get_max(&data) { + positions.push(i); + fuel.push(fuel_for_pos(&data, i)); + } + let min = get_min(&fuel); + let index = fuel.iter().position(|&val| val == min).unwrap(); + return (min, index as i32) + + + +} \ No newline at end of file diff --git a/aoc09/Cargo.lock b/aoc09/Cargo.lock new file mode 100644 index 0000000..83031a7 --- /dev/null +++ b/aoc09/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc09" +version = "0.1.0" diff --git a/aoc09/Cargo.toml b/aoc09/Cargo.toml new file mode 100644 index 0000000..a5ce805 --- /dev/null +++ b/aoc09/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc09" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc09/example.txt b/aoc09/example.txt new file mode 100644 index 0000000..6dee4a4 --- /dev/null +++ b/aoc09/example.txt @@ -0,0 +1,5 @@ +2199943210 +3987894921 +9856789892 +8767896789 +9899965678 diff --git a/aoc09/input.txt b/aoc09/input.txt new file mode 100644 index 0000000..0495790 --- /dev/null +++ b/aoc09/input.txt @@ -0,0 +1,100 @@ +2101234567895421012347899878510986566799987654567986456995439976789764347996567899989943245679988767 +3212349678989533234556998965329877345689976563459874348789598865678953256789456798878894556798765657 +4323458989678944345678987895498767234567896432398763212678987674679954103598967987658789987899854545 +5434767895587895456889456976987654395978954321239654343459976543458893212367899876545678998999973234 +6545878954376789569994345897898765989899765210198785454598865432346789323457998765434789109498765123 +7976989654245679678965456789999879878789874334239876565987654321257895444567899876523489912349871014 +8998999762156789789876667896899998767678998485346989879876543210456989555678976987634567893497932123 +9349678954367899897987788945679999654567897576457899999989655421245678967899895498547679989986544254 +3234569765478934956799899235989986543456789986568998789998765434657889878976789999967899878987654345 +2124758976589320245698932149898997632367893497978989678999887565767899989765679899878987654598776576 +3012347897695434346987894298787998321299912999899876567899998676778998795634598787989876573649887989 +4124456789789545459876796987676799420189909898789765456789639897989459654523987656699985432233998992 +4245678999899957867965789876545678921467898769678932367896510998992398993212396543549876321012349891 +5678989656989899979854345997634567892379999654567891246789439889901987889323987992023986535123556789 +6789994349878789998765234598745679943489998323478910345899998767899876778934799889934597943234589897 +9899989499765676899632123987658789874799986214599321234567897656789965567895699778899679874445678976 +1999878989974565696543034798769899965678974323678932356679976545678954357679986565798989965567899765 +0998967976543234987654245679978969878789765434567893458989989324679843234568975434667899876678987654 +9876756899432123998768368789989345989898978545679954589998998939798756457789964323456901998789998653 +9965645678921019899989479897591299999987989757889765679877887698999887568899877634789212349899987654 +8654235677932298789996589976410987999876599968999899898766976587899998679976976545695443959998999765 +9843123456893989678987898765329876898763459878945910999954383456789898789897987656789659898787899877 +9732012587899878576798939876498985989652345999936929998843212345998769896789699787898798767656789998 +7643223456798765435899319987987699876543478999899898987652101236789652945679569898909899854345698999 +7654654577899871012978998798998789997667569998778767898767432345678941036789323979212998768767897899 +8765766689978989153567897649459898998778878987654546989889543766789632123499214567993459979878956798 +9878878794569993254568989432398987899899989876543235678987679879896543334568923456789567989989548987 +3989989893498754385678978940987856799923498965432123989198789989987656545678994567899678991098659896 +2399899932198765476899567891976545689212567896551029893239899998698769756989389878998999542989798785 +1298789431019896589912377899875434978909678989762998789347999879549978897895478989987889659878987674 +0987678993234987692101256798754323569998989878979897678956989965432989998976568999896578998967896543 +9876543789945698789322345679654212398987898769998756567969978986544997899987679999789489997856987654 +5987674599896789896543456898732101987876797657987643456798767897959876791098989987654349876545999765 +4398765689789893987664687999654219876565789543498732345689458999899865689989899876543234997657898977 +5499876895567965998778789998765698765454698955569821234592345698789654578978789989432145698769987899 +6985987953456899879899898999876798654323457896898930123591234987699943589965679998743456789898986789 +9876798912347998765912997898989987743212348997987651234989959877578892397894998999656567899987775679 +0997989543458999894323986967999876532103456789999875445978898766456789456953877898767678999876544589 +1299878994569789989499875456892997944212347897899986599865799954345678967892366789998989988987323699 +2986566789689679978989764345891299894333458956789797987954689876756789898901345998999393976543212789 +9875445999794598867878973226789989789546789545395679876543499989767898769219459897893212987654304589 +8764329898943987654569543015678975689987998733234598765432359999898998654398998776794343499875423678 +9865498787892198733678952123459954578998999710123499876745678931999999965987687675989954569765434567 +9876997676789989421789965234599869699129987621234589987956799210198989878996576534679895679878545689 +1999876545678976562597894345789998989012398542349678998767989321987878989965432323456789989997657894 +0989987434579987673456789456899987678994987643498989989899878999876567899875321012348995699989769943 +9876543212389998984567896567929876567989998754987899976998765689765459901987632123467894329878998952 +6987654343578919987678987678909765458678999869876789765459976789979878929876543248978965499768987893 +5398765454689101298799998989996543234567892978975678975234989892989989439998656767899996987659436789 +4239898765678914459899999899987652123478931999854567894125999943498999997899767978999889996543125697 +4146999876789323567999898798998783235789949876743569993235799857567899896989978999998778987621013456 +3256789998895437678998765656989854345897898765412398989349979768978998765679899123987656798842134567 +4345892349976545789899654345678965656976999989324987878998768979989997654568788939876545987653257878 +5457943456997656896798775457789876767899895495439876769987657899995986543234567894998756998764567899 +6798954599789767965679897668999987898998789989598765459896545989894398732125679943239867899998778989 +7899769987678978976789998789789998909989677679999894398765435679789496521014678994956978910199899678 +8978998458569989989899869895698999319876586567899989209876546789678987433123489589897989329987934569 +9567987353478999999998756934597998998765432456789979912989656892569876554439593476799998998996549978 +3459876212347898989765542129986767899965321245999867893498778921456987675678902345678967987897997899 +2999965423456987679654321098795456799876442456898979994589989210378998988789213456989456976799876989 +9889876834579876567999532197664345678998593567987696987678994321289549099895324567894349865798765678 +6778998748698795457888943498543235689999989679996545698789976562458932134965435898999298954987654599 +5465987657798654323567894999860134999899878989988434569999897673567893245976546789998987893498785699 +4344598788987643212456989876543239898768768999876523459998789994678954369899677899987656932349896789 +2123459899999876301345678998654398767654356789985434578999656789789765498788998999876547899456987899 +3234569998998765212346799029795459654321237898899565989986545678993987989657989998932123468967898989 +4349678987999654357856893234987598765434548997698977899995434578932799878945678987659994567899989878 +5458789456898765678977954345698679876576656789567898909876523457891698767236789898798789698945678969 +6569996568999879789798995458798789987989789895457899212987612356943497652179998799987689789996989654 +9878997979899989897629789567899890198999996942368994323998723457899998741067896678976578999989899543 +5989998989789992987545678998976931239789965321236789439876534569998765432359954599987429678976798954 +4597999896599891297656789679999842345679876433345696559987676678999876543878963989987634567895657895 +3456898765456789398767896543987653458789989874576789698798787889898988764989879879876545898943246789 +2367999854346789999878987632198785669894498765689898987659898998787899895699998765997656789532135892 +1567998765235899898989876544349997978912349876996967898749999879656689987789889954398767898643234943 +0346898754346798767899987655498899899103567987895456789898789765434578998996779893239878987655345694 +1234789765457987656789999776987768789214678998954323899987678976655689999545656789123989699867458789 +2345679876567898778999976989886547678925889329765834789986567897896999987432345679954694569878569899 +4456989989678929989129965498765636569996789409876745678976458999989899876541234578895893456989678978 +5768993599899219991098799597654325458987993212988876799986569998978798765410123456789932597898789567 +6978987678923998932998678998765212346798954323499989895697678987665659898321239967897643789999899434 +7989698799019897899897567979854303467899766534567897914598989976544446996543998798987654679989978921 +8996549997998786789765434567963214679998987645698976723459391985433234987659887689999767789876567892 +9989239876892675778976323678977634567897898756989895434569210986321013498798756579899898899983456943 +9879959865781534667895434589876547679946789869876799547678999875432125799987644467799999999832169659 +6567898774210123456789645699987658789434578998965978956789987986543234589876532345678989398743298998 +5456987654321434597998756789198767896545689987654568987895456599654565679874321567889879197654997987 +5323498767434545989899767899239878987956789898543459998954343498765696789965452378998968989769896596 +4212999998545669878799879998945989599897898765432368989768212349876789891986565459987957679898789498 +4345899987656989765678998987896792498789999974321245678965401234989899932987876567896543599987678989 +5786799998767998954789897676989899989678898765210176989876212345698999743498987878965432789876564879 +6997898989878987323456789565678998878545789896325237891995454597897898654569898989998543679876433768 +7898967878999976212345689434989987567438699965434346990987566989986799765699789995987654567984321057 +8989655567898765303458796579898765432123578986557656789799679879865689897987678954398786788985432348 +9876544466789873214567899698798765321012459987868867897698799767654799989876567943219899899876543456 +8765433345678984323678998987659876432133569998978879933569987654423689876987678955323999976987656567 +9654321234589995644567897898243998873244567899989989212456798743314578965699899876456789765398797678 +8765430355679876789678976543012397654356798935697694302345989832105689654556998987987897573239898789 +9974321366789987899899987632123498767459899224599543213459876543216798532348967898998965432145999890 +9765432377890198975910199743435679878567892101987656323567998765323897621012345999249986551026789921 diff --git a/aoc09/src/main.rs b/aoc09/src/main.rs new file mode 100644 index 0000000..39571c3 --- /dev/null +++ b/aoc09/src/main.rs @@ -0,0 +1,131 @@ +use std::{collections::HashSet, fs}; + +const NEIGHBOUR_IDXS: [isize; 3] = [-1, 0, 1]; + +struct Grid { + data: Vec>, + low_points: Vec<(usize, usize)>, + basin_sizes: Vec, + nx: usize, + ny: usize, +} + +fn main() { + let mut grid = Grid::from_file("input.txt"); + grid.find_low_points(); + println!("{}", grid.get_result_1()); + + grid.find_basins(); + grid.basin_sizes.sort_unstable(); + grid.basin_sizes.reverse(); + + let result = grid + .basin_sizes + .into_iter() + .take(3) + .reduce(|x, y| x * y) + .unwrap(); + println!("{result}"); +} + +impl Grid { + fn from_file(filename: &str) -> Self { + let input = fs::read_to_string(filename).unwrap(); + let data: Vec> = input + .split_whitespace() + .map(|line| line.chars().map(|c| c.to_digit(10).unwrap()).collect()) + .collect(); + + let low_points = vec![]; + let basin_sizes = vec![]; + + let ny = data.len(); + let nx = data.get(0).unwrap().len(); + + Self { + data, + low_points, + basin_sizes, + nx, + ny, + } + } + + fn get_neighbours(&self, x: usize, y: usize) -> Vec<(usize, usize)> { + let mut result = vec![]; + for dx in NEIGHBOUR_IDXS { + for dy in NEIGHBOUR_IDXS { + if dx.abs() == dy.abs() { + continue; + } + let new_x = x as isize + dx; + let new_y = y as isize + dy; + if 0 <= new_x && new_x < self.nx as isize && 0 <= new_y && new_y < self.ny as isize + { + result.push((new_x as usize, new_y as usize)); + } + } + } + result + } + + fn get(&self, x: usize, y: usize) -> u32 { + *self.data.get(y).unwrap().get(x).unwrap() + } + + fn find_low_points(&mut self) { + for x in 0..self.nx { + for y in 0..self.ny { + if self + .get_neighbours(x, y) + .iter() + .all(|(ox, oy)| self.get(x, y) < self.get(*ox, *oy)) + { + self.low_points.push((x, y)); + }; + } + } + } + + fn get_result_1(&self) -> u32 { + self.low_points + .iter() + .map(|lp| self.get(lp.0, lp.1) + 1) + .reduce(|x, y| x + y) + .unwrap() + } + + #[allow(clippy::never_loop)] + fn find_basins(&mut self) { + for (x, y) in &self.low_points { + let mut basin = HashSet::new(); + basin.insert((*x, *y)); + + loop { + let old_len = basin.len(); + let extend: HashSet<(usize, usize)> = basin + .iter() + .flat_map(|(x, y)| self.get_neighbours(*x, *y)) + .filter_map(|(x, y)| { + if basin.contains(&(x, y)) { + None + } else if self.get(x, y) != 9 { + Some((x, y)) + } else { + None + } + }) + .collect(); + + for item in extend { + basin.insert(item); + } + if basin.len() == old_len { + break; + } + } + + self.basin_sizes.push(basin.len()); + } + } +} diff --git a/aoc10/Cargo.lock b/aoc10/Cargo.lock new file mode 100644 index 0000000..a400577 --- /dev/null +++ b/aoc10/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc10" +version = "0.1.0" diff --git a/aoc10/Cargo.toml b/aoc10/Cargo.toml new file mode 100644 index 0000000..ae9bf51 --- /dev/null +++ b/aoc10/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc10" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc10/example.txt b/aoc10/example.txt new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/aoc10/example.txt @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] diff --git a/aoc10/input1.txt b/aoc10/input1.txt new file mode 100644 index 0000000..c029f1e --- /dev/null +++ b/aoc10/input1.txt @@ -0,0 +1,90 @@ +({{[<{[{[[<([[<><>][{}()]]<([]{})[<>()]>)><<<([]())>>{([()<>][{}{}])<[<>()]{()()}>}>]<(<[[[]<>]({}{})]{{{}<>} +[(<<<[[[{({(([{}{}]<()<>>)[<()<>>[[]<>]])}([{{<>()}}]))}<[{{[<[]()>][[<>()][[][]]]}{{(()()){{}{})}} +<(<(<<{<(({(<[()()]<<>[]>>((()[])(<><>)))}))[{<{<[(){}]>}><[<{{}[]}{[][]}>(<{}{}>)]([(<><>)(<>{})]{{(){}}[<>( +[{<[<{[[[<{<(<[]<>><<><>>)<{{}()}>>{[<()>{[]<>}]}}>[(<{[{}()]<<>()>}{(<>[])({}<>)}>)]]}{({< +[<[(<<<([[[<(<<>{}><()>)(<[]<>>([][]))>(<{{}<>}{{}[]}>)]](<(<({}){<>()}>{<{}<>>})[{[{}<>][< +[([(<([[[([([<[]()>[()()]]{{<>{}}([][])})<{<[][]>{()[])}[{<>{}}<<>()>]>][<[<{}<>>[[]{}]](<() +{({{<{[[(<[[{[()()][()[]]}<{<>[]}<{}()>>]({<<>>}(({}{})(<>)))]<([[()[]]<(){}>]{{{}<>}([]<>)})([{[][]}{<> +<(<{{([[<<({[<(){}>[()<>]]{[{}<>]<(){}>}}(<[[]{}]<[][]>>))<<[{{}()}][{<>[]}[{}<>]]>[([<>{}]{[]{}}) +[<{<((([{<(<(<{}>[{}()])>)<{[{<>()}[{}[]]][{[]}<{}()}]}((<{}>{[]<>})({()()}<[]{}>))>>}]))) +<{(<{((([((({({}<>)([]<>)}({()<>}[{}<>])){(<[][]><()[]>)<[{}[]]<[][]>>})[[{{<>{}}(<><>)}{[()()][()[]]}]])[[{ +{<[<[[[(({(<<{[][]}(<>{})>{<[]()><()()>}>(({[]<>}{()<>}){[()()]}))})<({{<(<>{})>[(<>[]){() +([[[{{[<([<<({[]{}}{(){}})(<[]<>><[]<>>)>{{{[]}{[]{}}}}><[[[<><>]{{}[]}]]<<[{}{}][(){}]>{<()[]>[{}[]]}>>] +{[[[<<({([<<(((){})<{}()>)[({}[])<()[]>]><<[[]{}]<<>()>>{(<>())}>](<<{[]{}}[<>()]>>[{({}<>)[()[]]} +[<{{<({<<[(<<<{}<>>{[]{}}><((){})((){})>>(<[[]<>][(){}]>[<{}()>({}<>)]])]<[<{[[]()]({}[])}>(< +<{(({{(({({<[<{}<>><[][]>]<{(){}}>>}[(<[[]{}](()())>[[()()][{}[]]])])<[(<[<><>](()[])><{{}{} +[[(({(({([[<{<{}<>>{(){}}}{(<>[])<[]()>}>][[[<[]()>[()]][<<>{}><{}()>]]([([][])])]]<({[[<>[]][{}<>]]<{ +<<[{{{<<[<{<<[<><>]{[]}>>{<([]<>)<<>[]>>[(<>{})(()())]}}{<[{{}<>}(<>[])]>[[{{}{}}<{}<>>](([ +((({{[[<[([[<{()<>}({}{})><<<>[]><()<>>>]{{<[]{}>[{}()]}(<[]<>><()[]>)}][{{<[][]>}<[()[]]<[]()>>}])]>]]}([{{[ +[([{({<<[{[[{{()()}}[[<>()]([]())]]{{(()<>)[{}()]}{[(){}][<>()]}}]}]>(([{[{[[]{}]{[]<>]}]{{<[]< +<(<(<({{<<([<{[]{}}[{}<>]>{<<><>>{{}{}}}])({(<<><>>((){}))({[]<>}<{}<>>)})>{{[<<()<>><[]()>><((){})<{}<>>>]< +([{<{{[{([{[<[(){}]{(){}}><{[]()}[{}{}]>]({<<>{}>({}[])})}[{[{[]()}<(){}>]({[]{}}{()()})}( +(<[([[(<([([<[[]()]>][<[()<>]([][]}>[(()[])<[]()>]])<{[<[][]>(()[])](([]()))}{[<{}<>>[[]()]]((()<>) +[([((([<<{[({{{}()}(<>())}([()<>]<{}()>))<<(()[])[()<>]>{<()>}>]}{{((({}{})[{}<>])[{[]()}([]{})])<<{<>[]}( +<([{(<<[(<({<<{}{}>{{}[]})<<<>[]><[]<>>>}(<([]<>)<(){}>>[[{}{}]<[]{}>]))>{([<<{}{}>>({[]()})])})]( +[(<[((<{[(((([[]]<<>[]>){<{}<>><()()>}))<[[{{}()}<<>()>]]>)]<<(<{(<>{})}{({}<>){{}()}}>([{()<>}{{}() +<{(<<[{<<[({<<{}{}><()[]>>}){[([{}{}]([]{}])(((){})[[]<>])]<<<()<>><<><>>>([{}()]<[][]>)>}]>>(<(<({({} +(([{{<{{(<{[{{<>[]}}({{}[]}<(){}>)]{(([]())[()<>])<[[]][[]()]>}}{<({(){}})<[{}{}]>>[((()()))]} +[(({[<{([{[[{[[]()]<[]{}>}]{{<{}[]>{<>{}}}{(()<>)<()<>>}}][<<<{}()>(()())>>]}(([{{<>[]}[(){}]}[([]<>)< +[<(<{{(<[(<<({()<>}<()()>)([<><>]<(){}>)>>)]>{<<[([[(){})<<>[]>])({<()>(()[])})][[({{}}(<>())){ +(((<<[<(([<[<[()()][{}()]>[([]())[[]()]]][<<<><>>{<><>}>{<<>[]>[<>()]}]>}{<{[[()[]]<()()>]{[[][]][[] +{{(<<{(<[<({{<<>()>{{}[]}}([[]{}]<[][]>)}<(({}()){()()})>)>({<[[<><>]{[]}]{(<>())([]{})}>[[<<>[]>({} +<<<[[[<[<{[[<({}())[[]()]><{(){}}{{}}>]][[[[{}[]]<(){}>]]<[{()()}<[]{}]]<(<>())[<><>]>>]}<[[({<>}<{}{}>)< +(<(<[<<{<[({([[]{}]{[]}){[(){}](<>{})}}{[[[]][[]()]]})<[<[<>()]{{}()}>][((()[]){(){}})[[() +<[{{{({(([{[{<{}<>>}<[{}<>]>]}]<([{{(){}}{[][]}}[<<>()><{}<>>]][{<{}[]>{()()}}])[({[{}<>][[][]]}{[{ +<[<{[([<<[[<[<()[]>[<>{}]]{([][]){[]<>}}>({{()<>}[()<>]}[{<>()}{<>{}}])]<<{({}())(<>)}[[()()][[]{}]]>>]>><( +{[{<([(<<[[<[[<>{}}{{}{}}][{{}{}}[{}{}]]>][{([[][]](()[]))(([][])(()<>))}{[<<>{}>{[]}]{((){})(<>)}}]]{[<<< +(({<<[<[<(<[<[<>{}]([]{})>(<[]<>><()>)][<[<>()]{()[]}>]>)[[[({{}{}){<><>})[(<>()){{}<>}]]]<<[<()[]>([][])]<<[ +<[({<<<<[{<<([{}[]][[]<>])[({}())<()<>>]>{<{{}<>}{[]}>[([]())({}()))}>}]{[<<<(<>())(()<>)>>{[<<><>>({}{})][ +<<((<{<{[{<<{<()<>>{<>{}}}>>}{<{{<<>>(()())}<{[]()}[()<>]>}{[<()>{<><>}]}>([<{[]<>}[{}<>]>{[[]<>]<< +([<[<[<<{{{{{(()<>)[<>()]}[({}{})[{}{}]]}(((()()))<{<><>}>)}<[<[<>()]{<><>}>[{<><>}{()}]]( +[([[((<({{([[([]{}){{}<>}]([[]()]<<>[]>)][[<()()>{{}}]<((){})[<>()]>])<<{<[][]>}[[[]<>]<{}<>>]>{{({}[])( +<(<<[<{{[([([(<>()}{()()}][{{}{}}[<>]]){<{<><>}<[]<>>>{{<><>}[[]()]}}]<<[([]<>)<()[]>]><[<[][]>]<[{}{}]{{}[]} +[({(({(({{([{[{}()]<()<>>}(([][])({}))])({(<{}()>)}{{([]<>)(<>{})}})}({<[<{}<>>{[][]}]({<>[]}<{ +[([[<[[{{<(<{<()<>><<>()]}<<[]{}>([]())>><<[[]<>]{{}<>}>>)><([<({}())>((<>{})[(){}])][<<[]()>( +[[(((<{({<<(<({}[])({}[])>[{[]{}}{<>{}}])[<<[][]>{<><>}>([[]()](<>[]))]>({<<<><>><[]()>>[([][])[[]{}]]})>}< +{[<(<<[{{{{<<{{}{}}{()<>}>(<[]()>({}<>))>}({<{{}()}{()()}>(<[]{}>({}<>))}([[<>()]({}())}(( +{[{([{<([({{{[{}<>){{}}}(<<>{}>{<><>})}[<<()()><<>[]>>]})(<<{([]<>)<{}()>}>>)])(<{<((<()[]>{{}{}})){({[]<>}( +<[[<(<{{{{{((<()<>>(<>{}))([{}{}]<<>[]>))<{<[]>(<>{})}>}}[(((([]<>){<>[]}))(({<>()}({}[]))))]}([{[<{[] +{<({[{[{<{{{[[<><>]{{}<>})<<[]()>([])>}[{{<>[]}{()<>}}]}<<{<{}<>>({}<>)}[<{}()>[[]<>]]>[<<<>{}>[(){} +<({({{([<[[[<{<>{}}([])>](<{[]}<[]<>>>)]]>)<[[<([<{}[]>[()()]]<(<>{})<{}[]>>){{({}<>)[[]<>]}<{{}{}}{()< +<{<{({<<{{{(<{[]{}}(()())>({<>[]}(<>[])))<{([][]){{}()}}(<{}[]>)>}{[<<()<>><{}[]>>{{[]{}}[[]() +{[<({<<{[(<{([{}[]]<[]<>>)<<{}[]>[()()]>}<<{()[]}{<>{}}>[[{}()]([]<>)]>>({[([]{})]<<[]{}><[][]>>}))][{[[<{[] +[<<{<({<(<{{[{()()}<[][]>]{{<><>}({}{})}}}<<(<()[]>{[][]})[[{}()]]><[<<>[]>(()<>)][(<>{})([]())]>>>[[<({ +{{((([[<[<[[[{()<>}(<><>)](<{}()>({}()))][{<{}()>[{}[]]}{<{}[]>[{}<>]}]]({{[<>[]]<<><>>}([<>]{[]})}([({ +<<([[{[{[<(({[{}{}]<()<>}}[(<>{})[(){}]])([[{}{}]]{[()<>]<()<>>})){{[([][])<()[]>]}}>]((<(<({ +(<({[{[{(<<<<([]<>)<[]()>><{()[]}{{}()}>>{{[[]()](<><>)}({<><>}{<>{}})}>{([((){})(<>())]{<<><>>({}()}})([[{} +<<<{(([(((({{<()())}<((){})[<>{}]>})(<[{<>{}}<{}()>]([{}[]][{}()])>[{{[]{}}{[]<>}}{{<><>}({}<>)}]) +(<{{[{{<([[[[([]()){(){}}][<[]()>{<><>}]]{[([])(<><>)]}]])((<[[({}{})[[]{}]]<({}){()}>][[( +{(((({([<[<<<[()()](<>{})>[{{}[]}<<>{}>]>>([{<{}()>[[]()]}{({}())<{}()>}])]{({<{[]{}}<[][]>><[[] +(<{[[<{{[((((<(){}><{}[]>)<{()()}<{}()>>)<<[(){}]>>}){(({{[]()}}<<<>[]>([]<>)>){{[<>{}]{[]() +<<{{{<<[{<[<[{[]{}}[{}<>]]<([]{})[<>{}]>>({[()()]}(([][])[<>()]>)][{<<<><>>(()<>)>[{<>()}<[]{ +[[<[[[<([((({{{}<>}[()[]]}{([]<>)<{}()>})[(<<>()>{<><>})]))<[({(()()){{}<>}}(({}())<[]{}>))({<<>{}>[[]{}]}( +{[[({[(<<{<((<{}()>({}<>))<<{}()><{}[]>>)>(<[<()()>(<>)]{([][])[(){}]}){([{}()]({}()))[[() +(<({[{<{<({{{({}<>)}{[()<>]<{}{}>}}{<<{}{}>([]<>)>(<{}<>>{<>{}})}}{[{<(){}>><[[]()](<>())> +[<{(({[{[<(<({[]{}}[<>])[{()()}(()<>)]>{{[[]{}]}<({}[])[{}{}]>})[{[[()]{<>{}}][<()[]>[[]()]] +(<[{{<(<({{{({[][]}{()()}){([]())<[]{}>}}}{(<<{}()>{{}{}}>[(<>[])])}]{[<[<(){}><{}<>>]<(<>[])(()[])>>[[{() +<<[<{[(<{({(((<><>))[[{}{}}(()())])<{<()[]>}>})<[{<<{}<>>(()())>[<<>[]>[{}()]]}{{<{}()>[<>{} +<({((<([{{<[(<{}[]><<>()>)(({}()){[]{}})]{{<()[]>[()<>]}<{()[]}{[]<>}>}><<{((){})[{}<>]}{(()[] +<{{[[<{<{<{{{[[]<>]}{{{}{}}<[]()>}}[<{()<>}(()())><<{}<>><()[]>>]}<([[<>()]([]{})]{{()}{<>[]}}){<<[]<>>>(( +<<({<{{[<{(((<(){}>{[]<>}){[(){}]})<({[]})[<()<>>{{}<>}]>)}><[({{<[][]>}}<{{[][]}<[]{}>}>>([(<{}<>>(()( +{<([{((({<((<<[]{}><{}<>>>[{{}()}[[][]]]))[<{[{}[]][<>[]]}>[<<{}()>{{}<>}>((<><>))]]>}(((<([[][]){[]( +[[<{(<(([<[{(<{}[]>[{}<>])<<{}<>><<>()>>]<{[()<>]{{}{}}}{<<><>>[[]{}]}>]<(<{()()}<<>[]>><({ +([{<(<<<([[{[[[]()]({}[])]({[]{}}(<>[]))}<{[[]{}]({}())}>]{([([][])[{}<>]]([<>()]<()[]>))}](<({({}[])((){})} +[<[{{[[[[[<({({}<>){{}{}}}{{[]<>}{<>}}){((<>())<[]<>>)[<()>[{}[]]]}>[{[[{}<>]]{{()<>}<()()>}>[[{[]< +(<{{{<([([([[([]())<[]{}>]<<()><[]()>>])])(<{[<(<>())<{}>>]<{[{}<>]<{}[]>}(([]<>)(<>()))>}(({<<>{}>[[] +{[(<[<{<<[{<<(()<>)<(){}>>[[<>[]](<>())]>{{[[]()][<>[]]}({{}[]}<<><>>)}}<<(([]<>)[()()])[[()<>][ +(<<[(([<([<<[[()[]]([]<>)]{(<>())([][])}>>{((<{}[]>{<>{}})[<<>()>])}]{([{{()()}<<>()]}{[[][]][(){}]}] +{{<[<(((<(<{{([]())<{}()>}<<<>[]>{{}<>}>}<<<[]{}>>(<[]{}>({}<>))>><<{[(){}][[]()]}{{<>[]}([] +[{{<[[([{[({[({}<>){{}()}]([{}<>]({}()))}(({()<>}<[][]>)({{}{}}{<>[]})))([([()[]][[]{}])]<<([ +([<(({<<(<([<([]()){(){}}>[([]{}){()[]})]{{(<>())(<>[])}<[<>[]][{}[]]>})><[[{(()<>){{}()}}{ +<((<<[{[[[[{[[<>{}][[]()]]{{[][]}<[][]>}>]]{({{[()[]]<{}[]>}<[<><>]<[]<>>>}<<<()<>>>[{()()}]>)(<<{<>()}>([{} +([(([{([((<{<([]<>)<{}<>>>(<{}{}>)}{{[{}]{<><>}}[[{}<>]([][]}]}>){<<<[{}()][{}{}]>>>[[{<{}{}>[{}[]]}]<[ +{[[(<([([{([<[()[]]<(){}>>[(<><>)[()()]]])}<{[[(<>[])([]{}>]{[[]<>]<{}[]>}]}[(((()<>)<()[]> +{{<(({([<<<<{([]()){()[]}}({()()})><(<()[]>[<>[]]}({<>{}}{[][]})>>{({<()[]>[{}<>]}((()[])(()<>)))}>>[( +{([[(({{{[[{<[{}()]<<>()>>[[()<>]{[]()}]}<[<{}()>{{}()}]{[[]<>](<>())}>]{({<(){}>(<>{})}{<[]>[[]()]}){<( +(<{<[(<[[<[{[[(){}][()()]]<[()<>]((){})>}[(<(){}>{<>{}>)]]{({{()}}{[{}<>]<<><>>})(({<>()}<{}()>)[{()}<< +[([([<{<((([[<[][]>{[]()}][<()<>><{}()>]]({[{}()]}[<()()>{[]<>}]))<[(<()()><()[]>){{[]<>}[()()]}] +{([<({[<((({(<{}<>>[{}()])<[<>{}]>})){{<<<[]()><[]<>>>{{<>()}[<>()]}>{[<()[]>[(){}]]})})<({ +<<<[[[((<{[{[([])[()<>]]((<>{}}(()()))}<{<[][]><[]()>}{<(){}>{{}<>}}>]<([<<>>]<([][])({}{})> +<[[({[<([{<<<(()[])>{{(){}}<[]<>>)>>[{{{()<>}[{}()]}<{()[]}[()<>]>}<<{()()}([]())>[(<>()){<>{}}]>]}])[((((<{ diff --git a/aoc10/src/main.rs b/aoc10/src/main.rs new file mode 100644 index 0000000..28c1267 --- /dev/null +++ b/aoc10/src/main.rs @@ -0,0 +1,146 @@ +use core::panic; +use std::fs; + +const CLOSING: [char; 4] = [')', ']', '}', '>']; + +type Result = u64; + +struct Line<'a> { + input: &'a str, + illegal_char: Option, + opened: Vec, + value: Result, + is_incomplete: Option, +} + +impl<'a> Line<'a> { + fn new(input: &'a str) -> Self { + Self { + input, + illegal_char: None, + opened: vec![], + value: 0, + is_incomplete: None, + } + } + + pub fn run(&mut self) { + self.illegal_char = self.find_illegal_character(); + self.value += match self.illegal_char { + Some(c) => get_value_for_illegal_character(c), + None => 0, + }; + if self.is_incomplete == Some(true) { + self.value = self.evaluate_opened_chars(); + } + } + + fn evaluate_opened_chars(&mut self) -> Result { + let mut value = 0; + self.opened.reverse(); + for character in &self.opened { + value *= 5; + value += get_value_for_unclosed_delimiter(character); + } + + for c in &self.opened { + print!("{c}"); + } + println!(" {value}"); + value + } + + fn find_illegal_character(&mut self) -> Option { + let mut chars = self.input.chars(); + self.opened.push(chars.next().unwrap()); + + for character in chars { + if CLOSING.contains(&character) { + if self.opened.last().unwrap() == &get_matching_delimiter(&character) { + self.opened.pop(); + } else { + self.is_incomplete = Some(false); + return Some(character); + } + } else { + self.opened.push(character); + } + } + self.is_incomplete = Some(true); + None + } +} + +fn main() { + let file = fs::read_to_string("input1.txt").unwrap(); + + let mut instances = vec![]; + for line in file.split_whitespace() { + let mut instance = Line::new(line); + instance.run(); + + instances.push(instance); + } + let result1 = instances + .iter() + .filter(|item| !item.is_incomplete.unwrap()) + .map(|item| item.value) + .reduce(|accum, item| accum + item) + .unwrap(); + + println!("\n{result1}"); + + let mut result2: Vec<_> = instances + .iter() + .filter(|item| item.is_incomplete.unwrap()) + .map(|item| item.value) + .collect(); + + result2.sort_unstable(); + let index = (result2.len() - 1) / 2; + println!("{}", result2.get(index).unwrap()); +} + +fn get_matching_delimiter(input: &char) -> char { + if input == &')' { + '(' + } else if input == &'(' { + ')' + } else if input == &']' { + '[' + } else if input == &'[' { + ']' + } else if input == &'{' { + '}' + } else if input == &'}' { + '{' + } else if input == &'<' { + '>' + } else if input == &'>' { + '<' + } else { + panic!() + } +} + +fn get_value_for_illegal_character(character: char) -> Result { + if character == ')' { + 3 + } else if character == ']' { + 57 + } else if character == '}' { + 1197 + } else { + 25137 + } +} + +fn get_value_for_unclosed_delimiter(character: &char) -> Result { + match character { + '(' => 1, + '[' => 2, + '{' => 3, + '<' => 4, + _ => panic!(), + } +} diff --git a/aoc11/Cargo.lock b/aoc11/Cargo.lock new file mode 100644 index 0000000..9584b0c --- /dev/null +++ b/aoc11/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc11" +version = "0.1.0" diff --git a/aoc11/Cargo.toml b/aoc11/Cargo.toml new file mode 100644 index 0000000..84e2f1a --- /dev/null +++ b/aoc11/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc11" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc11/example.txt b/aoc11/example.txt new file mode 100644 index 0000000..03743f6 --- /dev/null +++ b/aoc11/example.txt @@ -0,0 +1,10 @@ +5483143223 +2745854711 +5264556173 +6141336146 +6357385478 +4167524645 +2176841721 +6882881134 +4846848554 +5283751526 diff --git a/aoc11/input.txt b/aoc11/input.txt new file mode 100644 index 0000000..df77777 --- /dev/null +++ b/aoc11/input.txt @@ -0,0 +1,10 @@ +4112256372 +3143253712 +4516848631 +3783477137 +3746723582 +5861358884 +4843351774 +2316447621 +6643817745 +6366815868 diff --git a/aoc11/src/main.rs b/aoc11/src/main.rs new file mode 100644 index 0000000..6d33390 --- /dev/null +++ b/aoc11/src/main.rs @@ -0,0 +1,169 @@ +use std::{collections::HashSet, fs}; + +const NEIGHBOUR_INDICES: [isize; 3] = [-1, 0, 1]; + +struct Grid { + data: Vec>, + nx: usize, + ny: usize, + flashes: usize, +} + +impl Grid { + fn from_file(filename: &str) -> Self { + let data: Vec> = fs::read_to_string(filename) + .unwrap() + .split_whitespace() + .map(|line| line.chars().map(|c| c.to_digit(10).unwrap()).collect()) + .collect(); + let nx = data[0].len(); + let ny = data.len(); + let flashes = 0; + Grid { + data, + nx, + ny, + flashes, + } + } + + fn iterate(&mut self) { + self.increment_all_by_one(); + self.flash_where_level_above_nine(); + self.reset_flashed(); + } + + fn print(&self) { + for y in 0..self.ny { + for x in 0..self.nx { + let val = self.data[y][x]; + if val > 9 { + print!("."); + } else { + print!("{}", val); + } + } + println!(); + } + } + + fn reset_flashed(&mut self) { + for y in 0..self.ny { + for x in 0..self.nx { + if self.data[y][x] > 9 { + self.data[y][x] = 0; + } + } + } + } + + fn increment_all_by_one(&mut self) { + for y in 0..self.ny { + for x in 0..self.nx { + self.data[y][x] += 1; + } + } + } + + #[allow(clippy::all)] + fn flash_where_level_above_nine(&mut self) { + let mut have_flashed = HashSet::new(); + loop { + let mut increased = 0; + for y in 0..self.ny { + for x in 0..self.nx { + if self.data[y][x] > 9 && !have_flashed.contains(&(y, x)) { + have_flashed.insert((y, x)); + self.flashes += 1; + increased += self.increase_neighbors_by_one(&x, &y); + } + } + } + if increased == 0 { + break; + } + } + } + + fn increase_neighbors_by_one(&mut self, x: &usize, y: &usize) -> i32 { + let mut increased = 0; + for (ox, oy) in self.get_neighbours(x, y) { + self.data[oy][ox] += 1; + increased += 1; + } + increased + } + + fn get_neighbours(&self, x: &usize, y: &usize) -> Vec<(usize, usize)> { + let mut result = vec![]; + for dx in NEIGHBOUR_INDICES { + for dy in NEIGHBOUR_INDICES { + let ox = (*x as isize) + dx; + let oy = (*y as isize) + dy; + if ox < 0 + || oy < 0 + || ox >= self.nx as isize + || oy >= self.ny as isize + || (dx == 0 && dy == 0) + { + continue; + } + result.push((ox as usize, oy as usize)); + } + } + result + } +} + +fn main() { + let mut grid = Grid::from_file("input.txt"); + grid.print(); + println!(); + + let mut i = 0; + for _ in 0..100 { + grid.iterate(); + i += 1; + } + + grid.print(); + println!(); + println!("{}", grid.flashes); + + loop { + let before = grid.flashes; + grid.iterate(); + let after = grid.flashes; + i += 1; + if after - before == grid.nx * grid.ny { + println!(); + println!("{i}"); + break; + } + } +} + +#[cfg(test)] +mod test { + use crate::Grid; + #[test] + fn get_neighbours() { + let data = vec![ + vec![1, 1, 1, 1], + vec![1, 1, 1, 1], + vec![1, 1, 1, 1], + vec![1, 1, 1, 1], + ]; + let nx = 4; + let ny = 4; + let grid = Grid { + data, + nx, + ny, + flashes: 0, + }; + println!("{:?}\n", grid.get_neighbours(&1, &1)); + println!("{:?}\n", grid.get_neighbours(&2, &2)); + println!("{:?}\n", grid.get_neighbours(&0, &0)); + } +} diff --git a/aoc12/Cargo.lock b/aoc12/Cargo.lock new file mode 100644 index 0000000..eda6a0c --- /dev/null +++ b/aoc12/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aoc12" +version = "0.1.0" diff --git a/aoc12/Cargo.toml b/aoc12/Cargo.toml new file mode 100644 index 0000000..9ca1f08 --- /dev/null +++ b/aoc12/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "aoc12" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/aoc12/example1.txt b/aoc12/example1.txt new file mode 100644 index 0000000..6fd8c41 --- /dev/null +++ b/aoc12/example1.txt @@ -0,0 +1,7 @@ +start-A +start-b +A-c +A-b +b-d +A-end +b-end diff --git a/aoc12/src/main.rs b/aoc12/src/main.rs new file mode 100644 index 0000000..bdccc78 --- /dev/null +++ b/aoc12/src/main.rs @@ -0,0 +1,131 @@ +use std::collections::HashSet; +use std::fs; + +#[derive(Eq, PartialEq, Hash, Clone, Debug)] +enum CaveType { + Start, + End, + Big, + Small, +} + +#[derive(Eq, PartialEq, Hash, Clone, Debug)] +struct Cave { + cave_type: CaveType, + name: String, +} + +struct Map { + caves: HashSet, + connections: HashSet<(Cave, Cave)>, +} + +impl Map { + fn from_file(filename: &str) -> Self { + let data = fs::read_to_string(filename).unwrap(); + + let mut caves = HashSet::new(); + let mut connections = HashSet::new(); + + for line in data.split_whitespace() { + let (before, after) = line.split_once('-').unwrap(); + let cave1 = Cave::from_string(before); + let cave2 = Cave::from_string(after); + caves.insert(cave1.clone()); + caves.insert(cave2.clone()); + connections.insert((cave1, cave2)); + } + + Self { caves, connections } + } + + fn get_connected_caves(&self, cave: &Cave) -> Vec<&Cave> { + self.connections + .iter() + .filter_map(|conn| { + if &conn.0 == cave { + Some(&conn.1) + } else if &conn.1 == cave { + Some(&conn.0) + } else { + None + } + }) + .collect() + } +} + +impl Cave { + fn new(cave_type: CaveType, name: String) -> Self { + Self { cave_type, name } + } + + fn from_string(value: &str) -> Self { + if value == "start" { + Self::get_start() + } else if value == "end" { + Self::get_end() + } else { + assert_eq!(value.len(), 1); + let char = value.chars().next().unwrap(); + if char.is_uppercase() { + Self::new(CaveType::Big, value.to_string()) + } else if char.is_lowercase() { + Self::new(CaveType::Small, value.to_string()) + } else { + panic!() + } + } + } + + fn get_start() -> Self { + Self::new(CaveType::Start, "start".to_string()) + } + + fn get_end() -> Self { + Self::new(CaveType::End, "end".to_string()) + } +} + +struct Solver<'a> { + map: Map, + start: Cave, + end: Cave, + routes: Vec>, +} + +impl<'a> Solver<'a> { + fn new(map: Map, start: Cave, end: Cave) -> Self { + let routes = vec![]; + Solver { + map, + start, + end, + routes, + } + } + + fn find_routes(&mut self) { + let mut route: Vec = self.new_route(); + let mut connected = self.map.get_connected_caves(&self.start); + + let cave = connected.get(0).unwrap(); + route.push(**cave); + + self.routes.push(route); + } + + fn new_route(&self) -> Vec { + vec![self.start] + } +} + +fn main() { + let map = Map::from_file("example1.txt"); + + let start = Cave::get_start(); + let end = Cave::get_end(); + + let mut solver = Solver::new(map, start, end); + solver.find_routes(); +}