diff options
author | Julien Dessaux | 2022-12-01 13:05:31 +0100 |
---|---|---|
committer | Julien Dessaux | 2022-12-01 13:07:37 +0100 |
commit | c0877054bdbcb4739ecc842e37b0f1d6ba7af8d0 (patch) | |
tree | 9a06fd81a80fcfda45c7686ea25c1b4e5ed5f292 /2022/01 | |
parent | Added first part of the third day in zig (diff) | |
download | advent-of-code-c0877054bdbcb4739ecc842e37b0f1d6ba7af8d0.tar.gz advent-of-code-c0877054bdbcb4739ecc842e37b0f1d6ba7af8d0.tar.bz2 advent-of-code-c0877054bdbcb4739ecc842e37b0f1d6ba7af8d0.zip |
2022-01 in zig
Diffstat (limited to '2022/01')
-rw-r--r-- | 2022/01/example | 14 | ||||
-rw-r--r-- | 2022/01/first.zig | 28 | ||||
-rw-r--r-- | 2022/01/input | 2267 | ||||
-rw-r--r-- | 2022/01/second.zig | 33 |
4 files changed, 2342 insertions, 0 deletions
diff --git a/2022/01/example b/2022/01/example new file mode 100644 index 0000000..2094f91 --- /dev/null +++ b/2022/01/example @@ -0,0 +1,14 @@ +1000 +2000 +3000 + +4000 + +5000 +6000 + +7000 +8000 +9000 + +10000 diff --git a/2022/01/first.zig b/2022/01/first.zig new file mode 100644 index 0000000..0c967fd --- /dev/null +++ b/2022/01/first.zig @@ -0,0 +1,28 @@ +const std = @import("std"); + +const example = @embedFile("example"); +const input = @embedFile("input"); + +pub fn main() anyerror!void { + try std.testing.expectEqual(solve(example), 24000); + const result = try solve(input); + try std.io.getStdOut().writer().print("{}\n", .{result}); +} + +fn solve(puzzle: []const u8) !u64 { + var it = std.mem.split(u8, puzzle, "\n"); + var max: u64 = 0; + var tot: u64 = 0; + while (it.next()) |value| { + const n = std.fmt.parseInt(u64, value, 10) catch 0; + if (n == 0) { + if (tot > max) { + max = tot; + } + tot = 0; + } else { + tot += n; + } + } + return max; +} diff --git a/2022/01/input b/2022/01/input new file mode 100644 index 0000000..cd532b6 --- /dev/null +++ b/2022/01/input @@ -0,0 +1,2267 @@ +6669 +6434 +6564 +1032 +1098 +1829 +2608 +7322 +2690 +7061 +1664 +5769 + +6160 +1082 +3958 +5272 +2188 +5618 +3418 +2302 +2926 +3338 +1082 +2024 +4195 +4083 + +3276 +5581 +3421 +3403 +4356 +2989 +1278 +1868 +3630 +4702 +3409 +4887 +2383 + +7104 +7496 +5265 +8078 +8788 +7402 +3601 +2641 +1252 +4994 + +4074 +7524 +8889 +9290 +8892 +5785 +2163 +1074 +6747 + +7452 +8006 +12427 +7919 +4012 +6850 + +34591 +26861 + +13028 +12695 +12762 +11006 +15937 + +17218 +12408 +17554 +11415 + +1118 +7240 +6400 +3431 +1032 +3283 +4667 +6100 +5076 +1101 +6952 +3527 + +7633 +4385 +4510 +4073 +3052 +11738 + +33554 + +3448 +16054 +10839 + +4496 +2697 +4731 +5150 +5327 +4039 +4004 +2148 +6150 +4001 +2882 +5780 +4327 + +4095 +2064 +6701 +3828 +2427 +4775 +2583 +2000 +1390 +7391 +1701 +3796 + +10355 +1262 +8781 +6350 + +11053 +11527 +1389 +6007 +12383 +11033 + +7951 +4258 +9815 +10061 +7579 +3769 +11431 + +3597 +35622 + +2129 +2994 +4260 +5269 +4179 +1612 +3509 +3581 +4544 +3713 +3202 +6458 +1203 +1095 + +17196 +13475 +12666 + +3549 +6432 +6593 +6723 +1272 +4023 +2112 +2844 +5135 +5103 +1585 +4773 +5635 + +8731 +16378 +5076 +2999 + +11136 +1525 +8684 +9731 +9753 +5366 +7399 + +8996 +1483 +4922 +1191 +2761 +9333 +9247 +3797 +3940 + +5876 +4139 +2158 +5905 +5445 +2438 +2814 +3853 +3432 +2289 +5783 +3839 +2811 +4361 + +3260 +6058 +3620 +2171 +4387 +4011 +4552 +1646 +3400 +2941 +6776 +2793 +6702 + +2318 +3118 +4920 +1293 +5593 +2308 +6819 +5949 +3819 +1742 +1074 +4565 +5891 + +5260 +8430 +6832 +6248 +2162 +7770 +1692 +4345 +7140 +6748 + +2921 +2075 +6039 +2875 +1479 +5835 +2350 +5981 +2684 +2560 +1608 +1495 +1358 +2047 +5003 + +13864 +8452 +10716 +6642 +14875 + +9704 +7477 +3664 +7751 +11616 +11067 +3496 + +12744 +11268 +7987 +15133 + +2859 +6255 +1302 +4053 +6133 +4181 +3008 +3450 +4421 +6065 +2732 +7142 + +7502 +3436 +2987 +8648 +1103 +1477 +7647 +9669 +6285 + +69741 + +60977 + +3987 +10933 +1170 +12375 + +6773 +2923 +8707 +3434 +5872 +5793 +5573 +6338 +7182 +2158 + +5522 +5345 +4425 +6178 +5236 +1397 +6165 +2173 +3547 +5225 +3949 +2238 +2594 +6032 + +7998 +6850 +1552 +3087 +6978 +7829 +1957 +1432 +7504 +2382 +3534 + +16000 +12845 +12638 +8250 + +5712 +1853 +2986 +6472 +1543 +1147 +5670 +5085 +3519 +5847 +4603 +1394 +2353 +1668 + +3542 +2995 +5413 +1108 +1783 +3902 +5173 +4228 +3997 +1759 +5652 +5896 +2109 +5697 +4198 + +3625 +7049 +4471 +11613 +1668 +9077 +3143 + +4473 +3397 +12107 +1364 +5698 +8030 +12081 + +2337 +3212 +3567 +1115 +3963 +1753 +1769 +6966 +3093 +4758 +5271 +3635 + +4387 +3770 +2931 +3029 +2657 +1109 +1592 +4884 +5784 +1911 +2938 +2501 +5851 +3793 + +1438 +7877 +7911 +2781 +1486 +4302 +6924 +3930 +6489 + +3794 +5964 +3072 +2292 +5199 +2157 +2357 +3649 +4888 +4522 +5408 +4743 +2753 +1717 + +8856 +7753 +8504 +1696 +5469 +7135 +7838 +6572 +4414 + +3860 +6400 +4018 +4506 +6979 +5493 +1950 +2544 +7426 +1405 +6440 +6253 + +8397 +2488 +4483 +3346 +8035 +3996 +6236 +4951 +2849 +6277 + +2113 +5149 +2409 +3971 +2762 +1704 +1799 +3929 +3224 +2149 +5804 +2026 +2269 +1107 +3140 + +2851 +10610 + +2901 +2215 +3138 +4848 +2612 +5624 +4878 +4137 +4093 +4660 +3369 +1970 +1176 +2141 +2298 + +50004 + +37286 +4713 + +2844 +1432 +2303 +4938 +3770 +1589 +5731 +5173 +3512 +5820 +3910 +6687 + +8039 +11639 +2325 +11278 +8313 + +7488 +15716 +1889 +4665 + +10258 +1862 +10452 +8650 +8039 +8984 +8778 +6486 + +8827 +12823 +25875 + +4658 +12284 +10171 +5966 +13525 + +7011 +2322 +8908 +8383 +3992 + +8965 +4198 +18224 +19587 + +5325 +20017 + +13271 +10542 +5223 +3178 + +1563 +4352 +6234 +2095 +1565 +1904 +6494 +6102 +3930 +4529 +5226 +5391 +1159 +4928 + +1588 +4779 +3001 +3698 +5784 +5327 +2149 +3020 +5362 +5751 +5704 +3296 +4746 +2595 +3169 + +5627 +24442 +5459 + +1867 +7808 +1287 +4179 +1395 +5759 +3916 +3549 +7235 +1740 +3753 + +11451 +8529 +1874 +6475 +1205 +8217 +9286 + +2929 +3725 +6071 +2336 +1725 +7049 +2041 +1694 +1231 + +3587 +6327 +8078 +4723 +10069 +5008 +8537 +7827 + +4148 +4758 +3453 +1189 +5086 +1045 +2429 +5157 +4454 +3198 +3080 +5980 +6011 +3575 +5044 + +7858 +7552 +7965 +3623 +7309 +4740 +6151 +1926 +2370 +6310 + +1734 +11654 +8498 + +2099 +4433 +3831 +6238 +4152 +1749 +4146 +5282 +3683 +4976 +5297 +1710 +4181 +4213 + +1981 +1318 +5839 +4815 +6083 +3841 +2842 +3246 +3884 +5193 +3451 +5147 +2518 + +2960 +3167 +5279 +2233 +6509 +5722 +3576 +6488 +4366 +1564 +6941 + +7515 +1502 +4892 +4008 +3196 +1730 +7263 +5971 +6213 +6673 +7824 + +5835 + +2444 +4473 +6654 +2285 +2184 +6397 +3411 +2602 +5380 +6952 +1234 +1474 +6842 + +3876 +7019 +1240 +6349 +4016 +2340 +8085 +1932 +5186 +4549 +6375 + +19405 +2299 +5583 + +13376 +12685 +9717 +12221 +2883 + +9537 +8867 +13398 +6781 +13954 + +2874 +1486 +5232 +5947 +2140 +7588 +1404 +8295 +6127 +7705 + +22842 +26153 + +8396 +2626 +3725 +5917 +2949 +1524 + +30131 + +5851 +1366 +3574 +1450 +5377 +6713 +3783 +3281 +4981 +6910 +5229 +4359 + +9099 +2211 +2814 +4583 +4690 +7122 +9334 + +17546 + +1378 +6567 +5300 +3819 +5604 +5316 +7159 +3564 +4302 +1442 +5763 +6610 + +1891 +8030 +3579 +4977 +3620 +2421 +7637 +1481 +9177 + +3298 +3865 +6812 +5409 +6482 +7245 +7049 +4319 +3040 +6027 +7195 +6023 + +28329 + +3409 +4600 +6903 +4511 +4341 +6742 +6967 +4247 +6802 +3453 +1515 + +2517 +13539 +3030 +11819 + +2974 +3749 +3971 +4509 +9599 +1036 +8176 + +1000 +2863 +1029 +2452 +2286 +2891 +5244 +3929 +5734 +2075 +5107 +2622 +4680 +1847 + +9653 +4720 +5078 +1589 +2431 +3149 +3261 +7308 +6953 + +5916 +7409 +3773 +7286 +4552 +9685 +2114 + +38289 + +1591 +10416 +3921 +16121 + +3217 +10969 +5198 +9305 +8671 +9606 +9759 + +60758 + +26875 + +12555 +12735 +14656 +4437 +4307 + +1528 +4259 +3310 +5563 +2281 +4305 +2573 +5733 +2725 +1519 +3299 +2055 +4674 +6146 + +21988 +17405 +15500 + +1775 +3126 +5002 +9407 +6048 +3346 +8368 +2185 +2825 + +24510 + +25200 +37377 + +9852 +1000 +2688 +11027 +11129 +11742 + +16298 +12093 +16906 +1567 + +1677 +3068 +6035 +6271 +6344 +5794 +1933 +6032 +4308 +6874 +6225 +1046 +6712 + +2194 +4404 +8838 +6737 +8595 +2478 +1033 +3489 + +5439 +14385 +13397 +3570 +13810 + +1726 +5761 +4318 +2813 +3351 +3168 +1539 +3847 +4422 +6071 +3376 +1093 +1979 +3206 +4011 + +2491 +4041 +3106 +2550 +3206 +5553 +3473 +5805 +1934 +4179 +1963 +4113 +3996 +2351 +3437 + +8747 +3481 +11813 +7672 +4711 + +4393 +2158 +3121 +6931 +7801 +3706 +4801 +3971 +1195 +1553 +1058 + +20051 +11463 + +1299 +6007 +6845 +1866 +2461 +3548 +4175 + +10323 +9098 +5566 +8553 +8768 +1022 +10088 + +5388 +4289 +5067 +5871 +5451 +4891 +8796 +6506 +3323 + +2652 +1923 +5235 +1539 +9256 +5794 +6153 +1379 +3568 + +12764 +15021 +11371 +1577 +4189 + +1960 +7462 +6862 +6710 +2350 +1689 +8322 +1575 +5367 +1074 + +4411 +4588 +3164 +1456 +6084 +2628 +5635 +2845 +3593 +3027 +6316 +5661 +2890 +2470 + +5592 +5071 +4290 +4402 +6091 +2907 +1531 +6229 +6159 +1791 +3833 +2435 +3919 +3160 + +18373 +2699 +6711 +14268 + +7693 +1264 +4217 +5299 +3786 +4555 +1112 +3392 +2535 +6768 +3618 + +11723 +6939 +7761 + +15672 +15601 +13101 +6229 + +23344 + +7787 +4343 +6661 +2629 +3189 +5155 +7999 +2351 +1308 +2726 +3177 + +18631 +18125 + +9177 +3897 +3655 +3449 +8336 +4366 +3363 +2145 +8594 + +1820 +9067 +2171 +2487 +15998 + +16221 +3430 +14946 +6869 +5305 + +4483 +7157 +1588 +2629 +5109 +7299 +1688 +4386 +6960 +6538 +1525 + +1289 +1340 +1520 +3601 +4316 +2653 +6140 +6219 +5044 +4306 +2573 +4028 +1828 +5844 + +7277 +5775 +2893 +3958 +6228 +6786 +7203 +7119 +5786 +6477 + +4367 +10361 +5973 +9197 +2832 +7900 +6711 +8279 + +9606 +6118 +10025 +10242 +8279 +8938 +6688 +7905 + +2478 +1918 +5575 +1840 +4897 +5871 +6309 +2778 +1937 +5396 +3249 +3456 +1002 + +12936 +11011 +11487 +11919 +13937 +8622 + +3853 +13287 +8285 +7842 + +11250 +7923 +8049 +3931 +7625 +4918 + +6516 +8209 +1752 +6258 +8501 +7426 + +10432 +3916 +12055 +13687 +1774 +8053 + +4682 +6779 +7258 +6929 +6899 +2643 +7021 +4960 +1732 +4065 +2854 +3593 + +13273 +11558 +5225 +3607 +5637 +11317 + +65129 + +6830 +3890 +4718 +6376 +3029 +8343 +7250 +7484 +3379 + +16188 +11694 +16583 +18745 + +4379 +2764 +4045 +3392 +1546 +3255 +2029 +3239 +1565 +5060 +1208 +1340 +5419 +3466 +4620 + +5408 +11519 +10387 +7305 +5770 +1733 +5462 + +9975 +4492 +14939 + +6365 +5846 +1825 +2851 +5483 +4801 +4109 +2581 +4729 +5149 +5573 +3345 +5426 +2060 + +15826 +6853 +10422 +12615 + +26180 + +5520 +1816 +1983 +4838 +2454 +4060 +2788 +6199 +6235 +2288 +4959 +4117 +3684 +3781 + +33317 +13949 + +1439 +1401 +4390 +4539 +5877 +2857 +3622 +3524 +4414 +1908 +2148 +1941 +4218 +1323 + +9364 +14727 +3282 + +22309 +24789 +7850 + +3434 +19391 +9665 + +1420 +3456 +4714 +4667 +2824 +3442 +1875 +2679 +4123 +1089 +2733 +4700 +5369 +1177 +1161 + +1710 +4521 +2164 +5986 +2485 +5614 +6494 +5043 +6334 +4947 +1423 +3351 +4010 +4360 + +3366 +3591 +8590 +3852 +9035 +8474 +1698 +5448 + +4560 +2248 +7197 +1821 +5411 +4522 +4914 +7806 +1387 +5702 +1780 + +3156 +2143 +5586 +2022 +3913 +5090 +5977 +4643 +3195 +5785 +6892 +5048 + +3839 +1261 +10477 +8350 +5769 +12750 + +19348 +9824 +15316 +17581 + +7925 +7634 + +2947 +4578 +1499 +1778 +10217 +5597 +3943 +8172 + +2012 +3917 +8065 +5627 +7562 +5489 +4334 +1648 +6666 +8287 + +15138 +7610 +12478 +12309 +13290 + +6296 +1234 +7587 +5783 +3061 +5613 +3283 +4447 +7188 +4577 +4102 + +8355 +2352 +1164 +1204 +2809 +6145 +6475 +3972 +1539 + +1805 +1214 +4153 +1685 +2472 +4620 +2784 +1859 +1390 +3591 +2560 +6291 +2805 + +11842 +20590 +15179 + +22669 +16927 +13861 + +2691 +9308 +3682 +1107 + +6906 +5502 +2120 +6862 +8843 +5379 +9651 +8263 +7238 + +3026 +5505 +2220 +1215 +4409 +2544 +3819 +7524 +3042 +4252 +1241 + +11244 +2129 +8844 +10557 +4463 +2319 + +5365 +2200 +1589 +4451 +5736 +1895 +4986 +3211 +1235 +4028 +1707 +1051 +6037 + +14311 +5746 + +2013 +8567 +3579 +6440 +3637 +10568 +8596 +7767 + +5653 +7015 +5853 +3670 +4265 +3003 +4139 +3132 +7538 +3629 + +3173 +1436 +3076 +5275 +4686 +5149 +6179 +2238 +1339 +2123 +4221 +6509 +5150 +2728 + +35818 + +11419 +8083 +12069 +3666 +2300 + +28844 +10855 + +30505 +18426 + +3369 +3085 +7409 +7308 +6556 +9671 +5416 +5981 +7558 + +5259 +9105 +15313 +6050 + +3203 +3414 +11117 +6231 +6049 +11632 +4816 + +11686 +5815 +11464 +7186 +9842 +5953 +8553 + +9330 +1817 +3529 +1761 +1823 +3942 +9471 +2207 + +8304 +9429 +12445 +3950 +16005 + +4809 +3869 +6968 +3204 +3621 +5925 +3967 +5180 +5238 +3013 +5291 +1371 + +4656 +2372 +1950 +4696 +1060 +3802 +5178 +5875 +3804 +5357 +3257 +1949 +3892 +5813 +3996 + +10281 +24432 +9867 + +7152 +5979 +4947 +6123 +6631 +6929 +4675 +3974 +5293 +6265 +4023 +6536 + +2200 +2917 +4479 +3671 +7268 +3545 +6762 +5921 +1814 +3704 +2767 +4692 + +9984 +5140 +2532 +4146 +13613 +11710 + +2027 +2367 +7666 +8617 +7563 +6593 +5060 +4431 +2666 +5080 + +7837 +6223 +2698 +8080 +3586 +8054 +6153 + +4364 +5979 +13237 +9841 +8704 + +5197 +4477 +5255 +8748 +11477 +1361 +4220 + +12430 +4451 +1038 +9999 +7577 + +59250 + +3857 +2158 +7069 +4813 +1980 +1636 +3646 +6760 +2842 +4217 +7578 + +1360 +2241 +2170 +2623 +2647 +5193 +3950 +4758 +5644 +2097 +3768 +5600 +3042 +3976 + +3905 +7116 +9941 +1927 +2265 +4489 + +6072 +3192 +6263 +3496 +3332 +2584 +5862 +6093 +6069 +4497 +1483 +6109 +4352 +1235 + +18487 +10619 +16127 +11370 + +2449 +2250 +2025 +3522 +5687 +5202 +4474 +5911 +3270 +5929 +4208 +2694 +5863 +3789 +2120 + +2591 +9936 +3145 +4471 +2234 +2250 +1547 +3760 + +13060 +12425 +20060 + +1786 +6317 +2087 +1998 +4111 +2185 +5446 +2732 +3731 +6082 +4513 +6370 +2494 +3197 + +5581 +5685 +1428 +1539 +1704 +5007 +2207 +4835 +1700 +4340 +1818 +3276 +1724 +1555 + +16085 +18634 + +1834 +2654 +5257 +2329 +4180 +4424 +4947 +5484 +1141 +4607 +5401 +2856 +2045 +4916 +5992 + +2925 +4945 +4969 +1400 +6410 +1093 +2398 +1949 +2701 +2763 +6314 +1105 +1580 +2804 + +52492 + +4181 +8013 +15867 + +2465 +4182 +6901 +1949 +5107 + +3078 +5461 +5477 +5344 +5974 +9751 + +26600 + +3943 +9282 +9655 +4304 +11368 + +4386 +7039 +7871 +9075 +2279 +10752 +1240 +5754 + +6914 +7359 +6766 +5693 +7719 +5043 +8665 +7241 +3498 +8771 + +12438 + +2288 +2764 +2633 +3416 +3396 +3172 +2004 +3583 +1862 +3027 +3928 +3165 + +3265 +2973 +2084 +5073 +1835 +2803 +2420 +3924 +4277 +5862 +1330 +5599 +3553 +3640 +5041 + +8709 +1629 + +5938 +9575 +1789 +6720 + +3623 +4874 +8500 +6566 +6965 +9388 +2106 +8552 + +9147 +4821 +5991 +9194 +3138 +2877 +5089 +4677 +3841 + +2865 +4858 +4642 +3755 +5958 +8076 +5599 +7931 + +5112 +2846 +2713 +3603 +5395 +4539 +4324 +2518 +1850 +5995 + +44743 + +1052 +3226 +3355 +4357 +4745 +4324 +4606 +3155 +3877 +1188 +4762 +1587 +2101 +1792 +4723 + +2071 +4440 +7135 +5825 +6083 +4034 +4572 +6942 + +3345 +5000 +1167 +1951 +4713 +2062 +4786 +7635 +1304 +6163 +5063 + +7060 +1738 +2658 +1217 +1388 +7416 +3719 +3903 +2305 +1051 +4437 +2496 + +9603 + +17966 +16434 +6253 + +8045 +8593 +9128 +6057 +10943 +11143 +2626 + +1961 +2742 +5305 +4780 +4307 +2465 +4472 +2946 +1404 +2195 +4822 +5607 +1027 +5697 + +26688 + +1004 +1382 +1194 +4638 +3773 +5037 +3731 +2676 +3567 +1159 +1136 +2883 + diff --git a/2022/01/second.zig b/2022/01/second.zig new file mode 100644 index 0000000..9863ee8 --- /dev/null +++ b/2022/01/second.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +const example = @embedFile("example"); +const input = @embedFile("input"); + +pub fn main() anyerror!void { + try std.testing.expectEqual(solve(example), 45000); + const result = try solve(input); + try std.io.getStdOut().writer().print("{}\n", .{result}); +} + +fn solve(puzzle: []const u8) !u64 { + var it = std.mem.split(u8, puzzle, "\n"); + var top3 = [_]u64{ 0, 0, 0 }; + var tot: u64 = 0; + while (it.next()) |value| { + const n = std.fmt.parseInt(u64, value, 10) catch 0; + if (n == 0) { + for (top3) |*max| { + if (tot > max.*) { + // this swapping will keep the array sorted + const prev = max.*; + max.* = tot; + tot = prev; + } + } + tot = 0; + } else { + tot += n; + } + } + return top3[0] + top3[1] + top3[2]; +} |