From 9046b205b908bf6793c2171adeb38d018bc811c5 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Thu, 1 Dec 2022 19:04:12 +0100 Subject: [PATCH 01/25] feat(day_01): added solution for day 1 --- .gitignore | 3 + Cargo.toml | 7 + README.md | 29 + day_01/Cargo.toml | 15 + day_01/src/lib.rs | 40 + day_01/src/main.rs | 12 + day_01/src/part_01.rs | 52 + day_01/src/part_02.rs | 72 ++ input/day_01 | 2255 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 2485 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 day_01/Cargo.toml create mode 100644 day_01/src/lib.rs create mode 100644 day_01/src/main.rs create mode 100644 day_01/src/part_01.rs create mode 100644 day_01/src/part_02.rs create mode 100644 input/day_01 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..835ad4a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +debug*/ +target*/ +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d948d6b --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[workspace] +members = ["day_*"] + +[profile.release] +lto = true +panic = "abort" +opt-level = 3 diff --git a/README.md b/README.md new file mode 100644 index 0000000..656cce4 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Advent of Code 2021 + +## Progress: + +- [Day 1](https://github.com/ankjevel/adventofcode/tree/2022/day_01) ⭐️ ⭐️ +- [Day 2](#) +- [Day 3](#) +- [Day 4](#) +- [Day 5](#) +- [Day 6](#) +- [Day 7](#) +- [Day 8](#) +- [Day 9](#) +- [Day 10](#) +- [Day 11](#) +- [Day 12](#) +- [Day 13](#) +- [Day 14](#) +- [Day 15](#) +- [Day 16](#) +- [Day 17](#) +- [Day 18](#) +- [Day 19](#) +- [Day 20](#) +- [Day 21](#) +- [Day 22](#) +- [Day 23](#) +- [Day 24](#) +- [Day 25](#) diff --git a/day_01/Cargo.toml b/day_01/Cargo.toml new file mode 100644 index 0000000..ed77270 --- /dev/null +++ b/day_01/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_01" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_01" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_01/src/lib.rs b/day_01/src/lib.rs new file mode 100644 index 0000000..9232524 --- /dev/null +++ b/day_01/src/lib.rs @@ -0,0 +1,40 @@ +pub mod part_01; +pub mod part_02; + +pub type Input = Vec>; + +pub fn parse_input(input: &str) -> Input { + input + .trim_start() + .trim_end() + .lines() + .map(str::trim) + .map(|string| { + if string.is_empty() { + None + } else { + Some(string.parse().unwrap()) + } + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + 1000 + + 2000 + + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + vec![Some(1000), None, Some(2000)] + ); + } +} diff --git a/day_01/src/main.rs b/day_01/src/main.rs new file mode 100644 index 0000000..879aa49 --- /dev/null +++ b/day_01/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_01::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_01")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_01/src/part_01.rs b/day_01/src/part_01.rs new file mode 100644 index 0000000..53762a5 --- /dev/null +++ b/day_01/src/part_01.rs @@ -0,0 +1,52 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + let mut max = 0; + let mut current = 0; + for &val in input { + match val { + None => { + if current > max { + max = current; + } + current = 0; + } + Some(num) => { + current += num; + } + } + } + Ok(max) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + 1000 + 2000 + 3000 + + 4000 + + 5000 + 6000 + + 7000 + 8000 + 9000 + + 10000 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 24000); + Ok(()) + } +} diff --git a/day_01/src/part_02.rs b/day_01/src/part_02.rs new file mode 100644 index 0000000..a01ec57 --- /dev/null +++ b/day_01/src/part_02.rs @@ -0,0 +1,72 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + let mut max = vec![0, 0, 0]; + let mut current = 0; + + let mut check = |current: &u32| { + for (i, n) in max.iter().enumerate() { + if current < n { + continue; + } + + let mut result = vec![]; + let split = max.split_at(i); + + result.extend(split.0.to_owned()); + result.extend(vec![current]); + result.extend(split.1.to_owned()); + + max = result[0..=2].to_owned(); + + break; + } + }; + + for &val in input { + match val { + Some(num) => { + current += num; + } + None => { + check(¤t); + current = 0; + } + } + } + check(¤t); + + Ok(max.into_iter().reduce(|a, b| a + b).unwrap_or(0)) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + 1000 + 2000 + 3000 + + 4000 + + 5000 + 6000 + + 7000 + 8000 + 9000 + + 10000 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 45000); + Ok(()) + } +} diff --git a/input/day_01 b/input/day_01 new file mode 100644 index 0000000..ff31e85 --- /dev/null +++ b/input/day_01 @@ -0,0 +1,2255 @@ +5324 +5176 +2197 +2701 +6185 +3901 +5392 +2065 +6467 +6085 +5062 +1841 +1197 +1318 + +4901 +3443 +1403 +5570 +4336 +5672 +2532 +5627 +6038 +1099 +4305 +2317 +1382 +3226 +4427 + +2612 +15638 + +4118 +4687 +2243 +3532 +2089 +3937 +1146 +5069 +5728 +2962 +3099 +5882 +5448 +6064 + +2642 +7996 +5334 +10384 +1106 +2742 + +3064 +3506 +2566 +5241 +2705 +5973 +5482 +1689 +1797 +6098 +3964 +2592 +2700 +5569 +4305 + +1307 +2204 +3618 +4795 +7853 +6103 +5248 +5989 +1451 +5524 +2572 + +7207 +5428 +8504 +3861 +1025 +3600 +2850 +3363 +4283 +3695 + +21536 +34998 + +7332 +7390 +4755 +1006 +6221 +2081 +4894 +6376 +3893 +2797 +4448 +2961 + +6544 +13080 +9331 +4452 +11569 + +8130 +7731 +4130 +2905 +5849 +3086 +1386 +8160 +4405 +4456 + +1673 +5591 +6628 +3577 +2097 +2540 +4884 +3704 +4340 +5609 +5671 +2386 + +1454 +2458 +2099 +2123 +3529 +1500 +2574 +1414 +1570 +5540 +4674 +2596 +5112 +4553 +3538 + +3089 +20620 +8662 + +7318 +6430 +3187 +4619 +7214 +7950 +5435 +1408 +4418 +6811 +6619 + +14465 +13716 +4698 +18846 + +3262 +3532 +3052 +8765 +7883 +4037 +3531 +1275 +1699 + +7331 +1567 +1820 +2593 +3537 +2140 +6760 +5755 +4144 +2073 +4602 +4128 + +6104 +8972 +19892 + +17252 +21573 +9351 + +67201 + +4294 +7766 + +19774 + +11565 +11046 +2842 +8393 + +7017 +9675 +6745 +13481 +13556 +4249 + +2107 +5225 +4963 +1119 +1573 +7933 +5257 +6803 +3414 +6156 +2593 + +3598 +2420 +2557 +2910 +6750 +2253 +5729 +6724 +6355 +1731 +4128 +4827 +6062 + +5367 +1916 +4804 +5871 +1210 +2265 +3945 +6557 +5711 +6611 +2373 + +10064 + +9038 +27653 + +2574 +6005 +11713 +2748 +10206 + +3404 +3885 +6447 +6270 +3738 +6098 +4455 +4285 +3386 +2148 +6238 +2457 +3971 +5009 + +1964 +7018 +3023 +13938 +2509 +9362 + +5570 +5928 +7596 +7327 +3867 +2127 +2473 +6631 +2514 +5685 + +17379 +5566 +7377 +11680 + +11618 +9050 +4052 +6359 +9340 +10670 +2206 + +3801 +7994 +1783 +8331 +8378 +8567 +6575 +6466 +6327 +7675 + +1727 +4854 +13316 +2277 +1002 + +11508 + +1466 +2783 +1248 +6891 +5062 +4676 +5392 + +2309 +4345 +4061 +2291 +6176 +2020 +1352 +4323 +5949 +3386 +1453 +6271 +2406 +3702 + +9874 +9051 +11016 +5825 +8846 +1554 +7739 + +23528 + +1043 +8194 +3368 +9066 +9089 +8174 + +10746 +9730 +11740 +10055 +7193 +13904 + +6215 +1885 +5229 +6669 +6959 +4974 +2120 +1298 +1430 +6121 + +14723 +32715 + +5783 +1098 +5175 +4285 +4773 +3161 +5007 +2226 +5436 +6077 +4460 +4532 +2250 +4365 +2067 + +6400 +2528 +2425 +3648 +2881 +3142 +6677 +5898 +2358 +6551 +2718 +1068 + +26003 +35903 + +5639 +2631 +5958 +5768 +1598 +2399 +2034 +5312 +5296 +2416 +4102 +5487 +4703 + +2570 +8366 +5181 +2445 +5162 +7602 +7173 +7287 +5544 + +7471 +1860 +6054 +4806 +8003 +7528 +1229 +8685 +7621 +8658 + +2772 +3368 +2152 +5375 +2609 +8107 +6980 +4483 +5277 +2887 + +5247 +2757 +5563 +2715 +1667 +1226 +5100 +5406 +3361 +1873 +1254 +1455 +5119 +6059 +5763 + +36308 + +10652 +2469 +5899 +3109 +9516 +1320 +5497 +8118 + +1633 +36728 + +15311 +15800 +2345 + +8798 +7581 +2277 +8154 +10875 +7322 +6145 + +9637 +3697 +4013 +3531 +10327 +7563 +9885 +2997 + +4043 +2569 +6039 +1967 +7443 +3359 +2323 +6144 +6260 +2931 +1316 + +4511 +10608 +16203 +5885 +5809 + +4708 +9406 +8620 +7918 +3530 +6432 +1294 +1129 + +5619 +2899 +1447 +4966 +2602 +4203 +1151 +4142 +5444 +1628 +2951 +3237 +2622 +1772 +4763 + +2587 +2284 +2831 +8008 +2176 +11829 +2512 + +13915 +13264 +10711 +5834 +8227 + +9012 +13250 +9137 +3782 +8746 +2746 + +3215 +2767 +1979 +3831 +5022 +1171 +1315 +4688 +2993 +1727 +1632 +2576 +1148 +3347 +5848 + +4528 +1066 +24591 + +1107 +5616 +6397 +3615 +1922 +7825 +4068 +2282 +4969 +4928 +1956 + +7563 +8015 +1599 +7393 +8540 +5662 +3296 +7456 +7456 +6788 + +8739 +3875 + +5167 +1477 +5247 +8143 +1505 +10220 +7960 +4732 + +16912 + +4976 +7525 +1736 +1008 +4977 +5674 +4444 +6232 +6049 +3639 +4522 + +22528 +23111 + +13556 +17076 +3627 +17255 + +1016 +1029 +3348 +5944 +5495 +2094 +4820 +2525 +4933 +5956 +2824 +3723 +5823 +6486 + +19290 +4670 +15993 +1076 + +9721 +7648 +17076 +7349 + +9628 +8035 +7244 +8595 + +29847 +31827 + +13167 +5522 +5306 +8929 +11649 +10464 + +4502 +3131 +1443 +5337 +9086 +4409 +6998 +4804 + +5638 +4221 +1322 +7392 +3318 +4711 +7797 +7945 + +5089 +2885 +1516 +6707 +4162 +2750 +3852 +4179 +4237 +1514 +3948 + +5584 +2739 +2969 +1821 +6459 +2524 +1315 +2437 +5509 +1931 +2268 +2997 + +5234 +2321 +4232 +5685 +3984 +2906 +1432 +4343 +3281 +2528 +2488 +1718 +5990 +3522 +4354 + +26195 +6781 + +5142 +4073 +5532 +7427 +7419 +1747 +3380 +6364 +6631 +3779 +4765 +5835 + +3705 +1536 +6790 +2033 +1455 +6505 +4919 +3024 +2474 +5873 +3471 +3505 +1316 + +6749 +4727 +3858 +5056 + +22223 + +17648 +4274 +12725 + +2280 +9924 + +15172 +7628 +7641 +7194 +1968 + +5982 +5614 +2452 +5362 +8434 +3174 +2176 +2698 +2412 +2189 + +13340 +12621 +14111 +14941 +13566 + +1361 +5862 +4128 +6746 +4467 +1838 +2139 +2664 +5443 +6834 +4677 +1098 +6784 + +10515 +2760 +10711 +5115 +12137 +2781 + +5049 +6241 +2530 +5856 +2433 +4728 +5363 +6403 +3327 +4275 +4158 +3801 +2371 +6359 + +29965 +8489 + +56825 + +11312 +9212 +1929 +14504 + +6465 +9629 +10359 +5838 +9425 +3058 +9753 +5888 + +5406 +13482 +3204 +2628 +9957 +10256 + +6179 +4133 +5327 +2896 +5640 +6104 +2780 +7809 +3987 +1638 +2541 + +33667 +14175 + +6110 +11612 +2695 +9999 + +5704 +6108 +2967 +1017 +1635 +3585 +6748 +1582 +6924 +1444 +3855 +1401 +2205 + +6732 +1197 +6953 +2207 +5612 +4231 +7347 +5711 +7897 + +13701 +10913 +16370 +19244 + +2674 +7112 +6486 +7851 +4414 +5481 +6726 +6598 +2871 +6893 +1811 + +1943 +3602 +4825 +11299 +14128 + +3270 +5562 +4320 +1632 +2765 +5645 +4944 +5241 +5292 +8473 + +2052 +2661 +4128 +5271 +1213 +4609 +3655 +6794 +2479 +5906 +6116 +4915 + +13118 +3888 +4049 +14650 +8187 + +7607 +7667 +4020 +2573 +3144 +10783 +3750 + +3572 +1577 +5913 +1191 +2013 +5357 +1334 +4691 +4955 +3044 +3587 +4261 +1879 +5174 + +21378 + +3856 +7070 +6705 +8507 +9457 +9742 +2451 + +2451 +6025 +10161 +10060 +11159 +4572 + +6103 +14326 +18133 +15332 + +5227 +2699 +2870 +1360 +3827 +2691 +5508 +1526 +2144 +4458 +4799 +3028 +5663 +3352 +5890 + +6639 +12924 +5358 +9307 +1789 + +4548 +8584 +2044 +1171 +8894 +7690 +2456 +2225 +6462 + +17817 +18601 +22573 + +5232 +7406 +2372 +9508 +6245 +5918 +3184 +7341 +4055 + +5583 +6410 +2211 +1752 +5614 +6256 +3575 +5114 +5707 +5439 +6576 +6152 +4253 + +1588 +4856 +1078 +9837 +10431 +3509 +2755 +2086 + +4108 +1603 +5413 +3281 +2084 +4070 +5170 +6002 +2991 +5885 +2073 +5249 +5078 +2229 + +2486 +1600 +2956 +5086 +3521 +2302 +5989 +5187 +2070 +3637 +4734 +2812 +2929 +1017 +1030 + +2302 +3395 +8743 +10214 +9177 +11084 +11280 + +3789 +5462 +5929 +2823 +5276 +5236 +1262 +1046 +6508 +5734 +5138 +5299 +3707 + +18103 +23600 +9666 + +9572 +5689 +8247 +6325 +5766 +6474 +4341 +2272 + +11951 +3817 +9665 +9371 +11456 + +18948 + +5760 +4181 +5350 +4406 +3333 +1192 +3369 +1739 +1417 +2549 +6452 +5864 +3340 +2264 + +1353 +4974 +1110 +3219 +3022 +1763 +2457 +1242 +1015 +5118 +2844 +1289 +5798 +2370 +1328 + +2420 +3955 +5165 +6471 +1506 +7366 +2845 +7015 +3494 +3631 +3366 + +5595 +1173 +5367 +2745 +5405 +3846 +4318 +4974 +1798 +5729 +2531 +3837 +4751 +1773 + +2917 +16933 +3801 +1609 + +24309 +17327 +3185 + +3608 +5934 +10329 +13659 +8898 + +13593 + +34109 +14389 + +5060 +2976 +1252 +3496 +2184 +2118 +2705 +4575 +1960 +1399 +4248 +1096 +5987 + +22148 + +6872 +3921 +6311 +6941 +5690 +5889 +5491 +2539 +6307 +6881 +6341 +4722 +6489 + +1294 +4268 +5230 +4534 +4271 +3299 +5569 +2424 +1169 +3393 +2191 +4614 +3407 +6061 + +5949 +3315 +3465 +1766 +6758 +3424 +6719 +3146 +5540 +5761 +3082 +5684 + +2622 +5651 +5396 +6061 +1771 +1924 +5686 +5660 +1867 +5316 +5457 +6073 +2739 +5972 +5036 + +6479 +4340 +4935 +1261 +3876 +1382 +3676 +7756 +6854 +1713 +2231 + +4951 +9899 +5241 +6767 +1821 +7499 +10630 +2795 + +6056 +4547 +1637 +9041 +8543 +2658 +4432 +8485 +2344 + +13655 +9543 +10307 +13701 +7088 +3037 + +11024 +13887 +13483 +9086 +3240 + +25938 +5207 +12019 + +7149 +1287 +6648 +2064 +1192 +4329 +3562 +6906 +4932 +1275 +2770 +5506 + +3937 +2237 +3689 +1612 +5617 +5771 +1582 +4790 +2307 +5041 +2624 +2838 +2795 +2206 +1635 + +17589 +8653 +17117 + +4989 +4956 +5419 +6835 +2196 +7712 +2424 +4662 +1602 +3939 +1272 + +16436 +12889 +6171 +9114 +2522 + +7624 +7560 +2198 +9091 +4732 +4600 +5329 +6004 +1019 + +5856 +1678 +1413 +1159 +2087 +1688 +2705 +2914 +4541 +6192 +3899 + +5894 +7004 +6434 +7397 +1628 +10169 +10771 +2815 + +5066 +4856 +1640 +1711 +11255 + +4073 +1430 +3137 +4448 +6758 +6601 +5361 +1551 +1172 +6521 +1984 +6439 +6856 + +9142 +5332 +6587 +6221 +7395 +3916 +3028 +6746 + +1888 +4873 +4385 +1612 +2542 +5366 +4580 +4885 +5067 +2973 +3746 +2312 + +9887 +2252 +9326 +9370 +4463 +2315 +3341 +8055 + +2044 +2280 +7734 +3207 +7745 +7176 +4730 +2999 +1333 +5779 +7820 + +2176 +5977 +6915 +5025 +2534 +5837 +5051 +2789 +2710 +3725 +4542 +5014 +6533 + +5040 +7527 +7004 +6962 +2013 +7318 +1072 +1885 +5706 +2064 +2112 + +32698 +5926 + +7743 +4843 +6126 +6327 +4515 +5568 +7777 +5099 +5645 +5375 +4282 + +10281 +7177 +8041 +9502 +4617 +6776 +5955 +9100 + +13793 +5079 +11978 +9184 +5004 +6506 + +11597 +22056 +11123 + +1966 +2459 +6579 +4048 +4820 +3866 +7306 +3019 +4028 +3280 +1280 +6118 + +2629 +6241 +5263 +4006 +7169 +6295 +1916 +7067 +5163 +7417 +7183 + +5296 +1587 +6900 +7476 +2815 +6000 +5377 +5690 +6823 +6629 + +10444 +7392 +8982 +6994 +9345 +1164 + +4648 +18356 +5229 +18479 + +37019 + +1595 +14516 +11072 +12073 +10524 + +7074 +4499 +3528 +6918 +7634 +7018 +4064 +2346 +6254 +4557 +5201 + +1304 +4826 +7327 +7594 +5661 +6148 +8777 +4703 +3436 +4898 + +69863 + +8498 +10599 +9617 +8728 +9386 +1506 +2857 + +7081 +3260 +16812 + +1978 +5986 +5234 +6330 +5033 +3496 +6263 +7934 +8236 +3702 + +1453 +2526 +5851 +2084 +3092 +6085 +6055 +3543 +2803 +1552 +1021 +5182 +4564 +3912 +3854 + +1003 +6113 +5847 +1479 +3534 +1497 +2047 +6062 +3713 +3888 +1847 +1798 +3207 +2560 +3212 + +4904 +7090 +2494 +8864 +1494 +3313 +1009 +7531 +4235 + +3024 +7888 +3711 +7637 +4804 +10269 +4739 +3124 + +10068 +9428 +5562 +2721 + +6320 +6421 +2922 +3893 +3632 +5560 +3579 +5181 +1927 +3860 +1432 +4818 +2868 +2184 + +11927 +4764 +7755 +10159 +6954 +9559 + +8985 +5396 + +5821 +15081 +12926 +16298 +11228 + +2555 +4710 +5259 +5744 +2489 +2068 +1502 +4496 +3785 +3377 +2321 +3902 + +10993 +10833 +24463 + +4011 +1089 +2087 +5185 +3488 +3736 +4926 +4891 +3448 +3271 +4634 +3751 +5303 +1994 + +5919 +3347 +4708 +1965 +5606 +6273 +2042 +1773 +3026 +4585 +2239 +2520 +4567 +4964 + +11355 +11798 +7831 +7480 +8790 +6294 +11922 + +3780 +4205 +2006 +4419 +4193 +2482 +4835 +4758 +3600 +1117 +3185 +1808 +1233 +4082 +3326 + +43241 + +5617 +1489 +5975 +5619 +2877 +1722 +4756 +5164 +3192 +2945 +5610 +6021 +4883 +2218 + +1579 +3888 +8895 +3815 +11782 +7598 + +2480 +2152 +7361 +6604 +3773 +1116 +4720 +6142 +6743 +7900 +1503 + +8257 +6224 +5073 +5153 +6286 +5930 +2198 +7322 +1244 +3028 + +9040 +9861 +11717 + +1692 +2885 +9293 +6439 +6822 +4663 + +11327 +6426 +4039 +9769 +3730 +6972 +6462 + +6202 +2857 +3687 +4333 +3206 +5999 +3844 +4236 +3496 +7349 +1637 +5892 + +21290 +13265 +16574 + +1501 +8323 +2174 +7603 +1417 +5507 +3152 +8574 +6952 +6893 + +2057 +5770 +6945 +5910 +2066 +2373 +6613 +6703 +5795 +7033 +6063 +3443 + +10064 +12696 +1625 +1604 +8552 + +4518 +3559 +4612 +4990 +6122 +1829 +8607 +2051 +6314 +2596 + +1898 +8330 +6823 +14127 +4122 + +2494 +6727 +4587 +2321 +4617 +4813 +7347 +3794 +1446 +5291 +4913 + +15290 +9852 +5973 +7334 + +5995 +4539 +8371 +9076 +12656 +12625 + +2198 +1139 +4259 +3010 +9045 +2929 +8392 +4307 +5738 + +12958 +5954 + +2276 +6600 +5912 +1305 +6350 +5827 +2640 +6856 +7562 +7882 +3944 + +7186 +4975 +9015 +12599 +13902 + +55492 + +4832 +5290 +2044 +1011 +4110 +1236 +4706 +5218 +1300 +3238 +1836 +1309 +4742 +3964 +4682 + +5611 +3648 +8179 +9855 +2938 +8541 +9237 + +6779 +7885 +7768 +7979 +1604 +2719 +3879 +2771 +1197 + +2775 +12978 +7208 + +19188 +16982 +8339 + +12742 +18586 +18256 + +15946 +28794 + +5010 +1439 +1176 +5112 +6225 +11059 +1380 + +2636 +5109 +7016 +4966 +7277 +1646 +1209 +4458 +4147 +3169 + +18833 +18515 +8638 +6254 + +7158 +2439 +12116 +2417 +6262 +9579 +6050 + +7881 +8103 +6525 +5610 +4287 +7642 +2084 +8453 +1724 + +3535 +10683 +10350 +11645 +11293 +7447 +6324 + +17973 +11656 +19267 +18227 + +5568 +3346 +5735 +5136 +5528 +5605 +3106 +4635 +2530 +3111 +2916 +1510 +2617 + +6132 +1462 +2779 +4977 +1771 +8308 +2896 +6441 +3027 +1452 + +50057 + +1733 +4620 +5879 +3456 +3043 +1039 +5811 +3572 +1371 +4420 +5788 +1722 +1173 +5371 + +4890 +4902 +5587 +5794 +4689 +6264 +3451 +6440 +5713 +4760 +4073 +6571 +5257 + +2882 +1784 +4373 +4672 +3418 +5014 +3662 +1249 +5828 +5153 +5729 +3709 +5709 +3007 + +13324 +9643 +14954 +14195 + +4739 +5997 +2183 +3111 +6721 +8025 +1373 +4982 +2199 +6187 + +6561 +6690 +9276 +7602 +7353 +7961 +1247 +6694 +6641 + +6626 +5971 +6702 +1726 +3909 +2262 +6701 +4754 +4256 +5897 +3946 +1457 +4469 From b4750dfb0a1cd51e9ee66922d208b6c021f28b71 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Fri, 2 Dec 2022 07:24:17 +0000 Subject: [PATCH 02/25] feat(day_02): added both solutions --- README.md | 2 +- day_02/Cargo.toml | 15 + day_02/src/hand.rs | 52 + day_02/src/lib.rs | 42 + day_02/src/main.rs | 12 + day_02/src/part_01.rs | 29 + day_02/src/part_02.rs | 55 + input/day_02 | 2500 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 2706 insertions(+), 1 deletion(-) create mode 100644 day_02/Cargo.toml create mode 100644 day_02/src/hand.rs create mode 100644 day_02/src/lib.rs create mode 100644 day_02/src/main.rs create mode 100644 day_02/src/part_01.rs create mode 100644 day_02/src/part_02.rs create mode 100644 input/day_02 diff --git a/README.md b/README.md index 656cce4..f63e746 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ## Progress: - [Day 1](https://github.com/ankjevel/adventofcode/tree/2022/day_01) ⭐️ ⭐️ -- [Day 2](#) +- [Day 2](https://github.com/ankjevel/adventofcode/tree/2022/day_02) ⭐️ ⭐️ - [Day 3](#) - [Day 4](#) - [Day 5](#) diff --git a/day_02/Cargo.toml b/day_02/Cargo.toml new file mode 100644 index 0000000..b9b0001 --- /dev/null +++ b/day_02/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_02" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_02" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_02/src/hand.rs b/day_02/src/hand.rs new file mode 100644 index 0000000..3a7f964 --- /dev/null +++ b/day_02/src/hand.rs @@ -0,0 +1,52 @@ +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum Hand { + Rock, + Paper, + Scissors, +} + +use Hand::*; + +impl Hand { + pub fn new(input: &char) -> Self { + match input { + 'A' | 'X' => Rock, + 'B' | 'Y' => Paper, + _ => Scissors, + } + } + + fn shape_sum(&self) -> u32 { + match self { + Rock => 1, + Paper => 2, + Scissors => 3, + } + } + + fn outcome(&self, opponent: &Hand) -> u32 { + match opponent { + Rock => match self { + Rock => 3, + Paper => 6, + Scissors => 0, + }, + Paper => match self { + Rock => 0, + Paper => 3, + Scissors => 6, + }, + Scissors => match self { + Rock => 6, + Paper => 0, + Scissors => 3, + }, + } + } + + pub fn round(&self, opponent: &Hand) -> u32 { + let shape_sum = self.shape_sum(); + let outcome = self.outcome(opponent); + shape_sum + outcome + } +} diff --git a/day_02/src/lib.rs b/day_02/src/lib.rs new file mode 100644 index 0000000..d5bee2d --- /dev/null +++ b/day_02/src/lib.rs @@ -0,0 +1,42 @@ +pub mod hand; +pub mod part_01; +pub mod part_02; + +use hand::*; + +pub type Input = Vec<(Hand, Hand)>; + +pub fn parse_input(input: &str) -> Input { + input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .map(|row| { + ( + Hand::new(&row.chars().nth(0).unwrap()), + Hand::new(&row.chars().nth(2).unwrap()), + ) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + use Hand::*; + + const EXAMPLE_DATA: &'static str = " + A X + B Y + C Z + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + vec![(Rock, Rock), (Paper, Paper), (Scissors, Scissors)] + ); + } +} diff --git a/day_02/src/main.rs b/day_02/src/main.rs new file mode 100644 index 0000000..a41bdc7 --- /dev/null +++ b/day_02/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_02::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_02")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_02/src/part_01.rs b/day_02/src/part_01.rs new file mode 100644 index 0000000..95cb5a6 --- /dev/null +++ b/day_02/src/part_01.rs @@ -0,0 +1,29 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input + .into_iter() + .map(|(opponent, elf)| elf.round(opponent)) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + A Y + B X + C Z + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 15); + Ok(()) + } +} diff --git a/day_02/src/part_02.rs b/day_02/src/part_02.rs new file mode 100644 index 0000000..1c90393 --- /dev/null +++ b/day_02/src/part_02.rs @@ -0,0 +1,55 @@ +use std::io::Result; + +use crate::Hand::*; +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input + .into_iter() + .map(|(opponent, outcome)| match outcome { + // lose + Rock => { + 0 + match opponent { + Rock => 3, + Paper => 1, + Scissors => 2, + } + } + // draw + Paper => { + 3 + match opponent { + Rock => 1, + Paper => 2, + Scissors => 3, + } + } + // win + Scissors => { + 6 + match opponent { + Rock => 2, + Paper => 3, + Scissors => 1, + } + } + }) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + A Y + B X + C Z + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 12); + Ok(()) + } +} diff --git a/input/day_02 b/input/day_02 new file mode 100644 index 0000000..60a97a4 --- /dev/null +++ b/input/day_02 @@ -0,0 +1,2500 @@ +B Z +B Z +B Y +C Y +B Y +C Y +C Y +A Z +B Y +A Y +C X +A Y +A Y +C Y +A Y +B Y +B Y +C Y +A Y +A Y +A Y +C Y +A Y +B Y +A Y +B Y +C Y +B X +A X +A Y +C X +B Y +C Y +B Y +B X +C X +B Y +B Y +A Y +A X +A X +A Y +B Y +C Y +C X +A X +B Y +C Z +B Y +C X +A Y +A X +C X +A Y +A Y +A Y +A Y +B X +C Y +C Z +B Y +C Y +A Y +A Y +B Y +A Y +C X +B Y +C Y +C Y +A Y +C X +B Y +A Y +B Y +C Y +C Y +A Y +B Y +C Y +C Y +C Y +A X +A X +B Y +B Y +C Z +C Y +B Y +B Y +B Y +C Y +A Y +C Y +A Y +A Y +A X +C Y +A X +A Y +A Y +B Y +B Y +C Y +C Y +A Y +A Y +A X +B Z +C Y +A Y +A X +C Y +B Y +C Y +A Y +A X +A X +A X +A Y +A X +B Y +A Y +C Y +A X +B Y +C Y +B Y +A X +B Y +B Y +C Z +C Y +C X +A Y +C X +B Y +B Y +C Y +A X +A X +C Z +B Z +C Y +C Y +A X +A X +B Y +B Z +C Y +B Y +B Y +B Y +A X +A X +B Y +C Y +A Y +C Y +C Y +A X +B Y +A Y +A X +C Y +C X +A Y +A Y +C Y +C X +C Y +B Y +A Y +A Y +C Y +B Y +C X +A Y +C X +A X +B Y +C X +A Y +A Y +A Y +C Y +C Y +A Y +A X +C Y +C Z +B X +C Z +B Z +A Z +A Y +B Y +A Y +B Y +B Y +A Y +A Y +C X +C Y +A Y +A Y +B Y +A Y +B Y +B Z +A Y +B Y +C Y +A Y +C Y +B X +A Y +A X +A Y +A X +A Y +C Y +A X +C Y +C Y +A X +B Y +A Y +B Y +A Y +A Z +C Y +B Z +C X +C Y +C X +B Y +C X +B Y +A Y +C Y +A Y +A X +C Y +A X +A X +C X +C X +C Y +C X +C Y +A X +B Z +A Y +B X +B Y +C Z +B Y +B Y +B Y +C X +B Y +A Y +B Y +A Y +C Y +C Y +B Z +B Y +A X +A Y +C Y +B Y +C Y +B Y +B Y +A X +B Y +B Z +C Y +A Y +A Y +A Y +A Y +A Y +A Y +C X +B Y +B Y +C Y +B Y +B Y +A Z +C Y +A Y +A Y +C Y +C X +A Y +B Y +A Y +C X +B Y +A Y +C Y +B Y +B Z +A Y +B Y +A Y +C Y +A Y +B Y +C Y +B Y +A X +C Z +C Y +A Y +C Y +B Y +C Z +A Y +A Y +C Y +C X +B Y +C Y +C X +B Y +C X +A Y +C X +C Y +B Y +B Y +A Y +B Y +A X +C X +C Y +B Y +A Y +A X +C Y +C Y +C Y +C X +B Z +B Y +C Y +A X +C Y +A X +C Y +B Y +B Y +B Y +B Y +C X +C Y +C Y +A Z +B Y +B Y +A Y +A Y +B X +A Y +B Y +B Y +B Z +C Y +A X +A X +A Y +B Z +B Y +C Y +B Y +B Y +B Y +B Y +A X +B Y +C Y +A Y +B Y +A X +C Y +C Y +B Y +B Y +C Y +B Y +A Y +A Y +A X +A Y +B X +B Y +C Y +C Y +A Y +A Y +B Y +B Y +A X +B Y +A X +C Y +C Y +C Y +B Y +A Y +B Y +A Y +A Y +A X +C Y +A Y +A Y +B Z +C Y +A X +C X +B Y +C X +C Y +C X +C X +C Z +A Y +A X +A Y +B Z +B Y +A X +A Y +A X +B Y +A Y +C X +B Y +B Y +A Y +A Y +C Y +A Y +A Z +C Y +C Y +C Z +A Y +C Y +C Y +C Y +C Y +A Y +B Y +B Y +C Y +A Y +C Y +C Y +A X +B X +B Y +A Y +B Y +A Y +C Y +A Y +C Y +B Y +B Y +B Y +A Y +C Y +C X +C X +C X +C X +C Y +C Y +C X +B Y +A Y +A X +B Y +C X +B X +C Y +B Y +C Y +C X +C Y +B Y +A Y +A Y +A Y +C Y +C Y +A X +A X +C X +A Y +A Y +B Y +B X +B Z +C Z +B Y +B Y +B Y +B Y +B Y +A Y +A Y +B Y +A Y +C Z +A Y +C X +A Y +A Y +C Y +C X +B Y +A Y +B Y +B Y +C Z +A Y +A Y +A X +C Y +C Z +B Y +A X +C Y +C X +B Y +C Z +A X +A X +A Y +C Y +A Z +B Y +B Y +B Y +B Y +C Y +B Y +C X +A Y +C Y +B Y +B Y +A Y +A X +A Y +C Y +C Y +A X +C Y +B Y +B Y +C Y +C Y +A Y +B Y +A Y +B Y +A Y +B Y +C X +A X +C X +B Y +B Y +B Y +C Y +B Y +C Y +B X +A Y +A Y +C Y +C X +C X +C Y +C X +A X +A Y +A Z +A Y +A Y +B Y +B Y +C X +C Y +A Y +B Z +A Y +A X +A Y +C Y +A Y +A X +C Y +B Y +B Y +C X +A X +A Z +C Y +B Y +A X +A Y +C Y +C Y +A X +C Y +B Y +A Y +A X +C Y +B Y +A Y +A Y +C X +B Y +B Y +B Y +C Z +C Y +C X +C X +B Y +A X +A Y +B X +A Y +C Y +B Y +B Y +B Y +C Y +C Z +A Y +C X +C Y +A Y +A Y +B Y +B X +B X +B Y +A Y +A Y +A Y +C Y +C Z +B X +C Y +A Z +A X +B Y +C Y +B Y +B X +A Y +A Y +C Y +B Y +A X +C X +B Y +C Y +B Y +B Y +A X +A Y +A X +C Y +A Y +C Y +B Y +A Y +C Y +A X +A Y +C Y +C X +C Y +A X +B Y +B Y +B Z +A Y +C Y +B Y +B Y +C Y +A X +C X +C Y +C Y +B Y +C Y +B Y +A Y +B Y +A Y +B Y +A Y +C Y +B Y +C X +B Y +C X +A Y +C Y +A X +C X +B Y +A Y +B Y +C X +B Y +C Y +C Z +A Y +C Z +C X +C X +A Y +A X +A Y +B Z +C Y +C Y +B Y +A X +A Y +B Y +C Y +A Y +A X +B Y +C Y +A Z +C Y +B Y +B Y +B Y +C Y +A Y +B Y +A Y +C X +A X +A Y +B Y +C Y +B X +C Y +A X +A Y +A Y +C Y +A Y +C Y +A Y +A X +B Y +C Y +B Y +C Y +B Y +B Y +A Y +A X +A Y +C Y +A X +C Y +B Y +B X +C Y +B Z +C Y +B X +B Y +C X +C Y +A X +C Y +C Y +A X +C Z +C X +B Y +A X +A X +C Y +B Y +A Y +A Y +B Y +B Y +A X +C Y +B Y +A X +A Y +B Y +C Y +A X +A X +A Y +A X +A X +A Y +A Y +A Y +A X +C Y +A X +A Y +A Y +A Y +B Y +C Y +C Y +B Y +A Y +B Y +B Z +B Y +B Y +C Y +B Y +C X +A Y +C Y +A Y +C Y +B Y +C X +B Y +B Y +C Z +C Y +B Z +B X +B Y +A Z +A Y +B Y +A Y +C Y +A X +B Y +C Y +A Z +A Y +C Y +C Y +B Y +C Y +C X +B Y +C Y +C X +B X +C X +A X +A X +A Y +C Y +B Y +C X +B X +A X +B Y +C Y +A Y +C X +C Y +B Y +C Y +B Y +A Y +B Y +A Y +A Y +A Y +A Y +B X +C X +A Y +B Y +A X +B Y +C Y +B Y +A X +A Y +C Y +A X +C Z +C Y +A X +B Y +A Y +B X +A Y +A Y +A Y +A Y +A Y +B Y +B X +A Y +B Y +C X +C Y +B Y +B Y +B Y +B Y +B X +A Y +A X +B Y +A X +A X +A Y +B Y +A Y +A Y +C Y +B X +B Y +B Y +A Y +A X +C Y +A Y +C Y +A X +B Y +B Y +A X +A X +A X +A Y +A X +C Y +C Z +B Y +B Y +A X +C Y +B X +A Y +B Y +A Y +B Z +B Y +B Y +A Z +B Y +A Y +B Y +B Y +C X +B Y +A X +B Y +B Y +C Y +B Y +A X +C Y +C Y +C X +C Y +B Y +B Z +A Y +B Y +C X +A Y +B Y +B Y +A X +B Y +A Z +B Y +A Y +C X +B Y +A Y +B X +A X +C X +C Y +B Y +A X +B Y +B Y +C Y +B Y +C Y +A Y +C X +C Y +A Y +C Y +A Y +C Z +A Y +B Y +A Y +C Z +A Y +C Y +A Y +C X +C Y +C Y +A Y +B Y +B Y +B Y +A Y +A Y +B Y +B Y +B Y +B Y +B Y +C Z +A X +A Y +B Y +B Y +A Y +B Y +B Z +B X +A Y +A Y +A Y +C X +B Y +A Y +C Y +A Y +A Z +B Y +B Y +A X +B Y +B Y +A Y +C X +A X +A Y +A Y +C Y +B X +A Y +C Y +C X +A Z +B Y +B Y +A X +A Y +C X +B Y +C Y +A Y +C Y +B Y +A Y +A Y +B Y +B Y +B X +C Y +A X +A Y +C X +A Y +C Y +A Y +C X +C Y +B Y +A Y +A Y +A Y +B Y +A Y +A X +B Y +B Y +B Y +A Y +B Y +B Y +B Y +C X +B Y +A Y +C X +B Y +B Y +A Y +C Y +B Y +A Y +B Y +C Y +B Y +B Y +C Y +C X +C X +C Y +B Y +B Y +A Y +A Y +A Z +B Y +C X +B Y +B Y +C Y +C Y +C Y +A Y +A X +A Y +A Y +C X +C X +B Y +C Z +C X +A Z +B Y +A Y +B Y +C X +A Y +B Z +C Y +A Y +B Y +C Y +C Y +C Y +A Y +B Y +C Y +A X +B Y +A Y +C Y +A Y +C X +B Z +B X +C Y +C Y +C Y +B Y +C X +A Y +A Y +B Y +C Y +A Y +A Y +B Y +B Y +A X +A Y +A Y +B Z +A X +A X +C Y +C Y +C X +B X +A Z +B Y +A X +A Y +B Y +A Y +B X +A Y +A Z +B Y +A X +B Y +A Y +A X +A Y +A Y +B Y +A Y +A X +A Y +C Y +B Y +B Y +A Y +A Y +A X +C Y +A Y +B Z +C Y +B Y +A Y +C Z +A Y +B Y +A Y +C Y +B Z +B Y +B Y +C Y +B Y +C Y +C Z +B Y +B Y +A X +B Y +C Y +A Y +B Y +A X +B Y +C Y +C Y +A Y +C X +C X +B Y +C Y +B Y +A Y +A X +B Z +B Y +C Y +A Y +C X +B Y +C Z +B Y +A Y +B Y +A X +A X +A Y +B Y +A Y +A X +C Y +C Y +C X +A X +A X +A Z +C Y +A Y +A X +A Y +A Y +B Y +B Y +C X +B Y +C Y +A Y +A Y +A Y +A X +C X +A Y +C X +A Y +B Y +A Y +B Y +A X +C Y +A X +B Y +A X +B X +A X +B X +C Y +B X +B Y +B Y +A X +A Z +C Z +B Y +C X +A Y +C Y +A Y +B Y +C X +B Y +C Y +C Y +B Y +A X +B X +C Z +C X +A X +C Y +B Y +A X +C Y +C Y +A Y +B Y +C Y +C Y +B Y +B Z +A Y +A X +C X +B Y +B Y +C X +A Y +A Y +B Y +A X +B Y +B Y +C X +A Y +B Y +B Z +C Y +A X +B Y +A Y +A Y +B Y +C Y +A Y +A Y +A Y +B Y +A Y +B Y +B Y +B X +A Z +B Z +A Z +A Y +B Y +C Y +B Y +B Y +B Y +B Y +A Y +A Y +B Y +B Y +A Y +C Y +A Y +C X +A Y +C X +B Y +B X +C Y +C X +B Y +B Y +B Y +B Y +C Y +A Z +A Y +B Z +C Y +A Y +A Y +B Y +C X +A X +A Y +C Y +A Y +C Y +A X +A X +C Y +B Z +A X +A Y +B Y +B Y +A Y +A Y +A Y +B Y +B X +A Y +C X +B Y +C X +C Y +C Z +C Y +A X +C Y +A Y +C Y +C X +A Y +A Z +A X +A X +A X +B Y +A Y +C Y +C Y +C X +A Y +B Y +A Y +A X +C X +A Y +B Y +A Y +C Y +C Y +C Y +C Y +C X +B Y +C X +B X +A X +C Y +B X +C Z +B Z +B Y +A Y +B Y +B X +C X +C X +B Y +C Y +C Y +A X +A X +C Y +A X +A Y +A Y +A Y +C Y +A Z +C X +C X +B Y +B Y +A Z +B Y +A Y +A Y +A Y +C Y +B Y +C Y +B Y +B Y +A Y +B Y +A Y +B Y +C Y +C X +A Y +A Y +C Y +A X +C Y +B Y +A Y +C X +C X +B Y +A Y +A Y +C Y +C Z +A X +C Z +B Y +B Y +B Y +C X +A Y +B Y +C Y +B Y +C Y +B Y +B Y +A Y +B Y +A Z +A Y +A X +A Y +A X +A Y +A X +A Y +A Y +C Y +A Y +A Y +B Z +A Y +A Y +C Y +B X +C Y +A Y +C X +C Y +A Y +C Y +B Y +A X +A Y +A X +A Y +B Y +C X +C Y +C X +C X +A Y +C Y +A Y +B Y +C X +C X +A Y +C Y +B X +C X +B Y +C X +A Y +C Y +A X +B X +C Y +C Y +C Y +B Y +A Y +A X +A Y +A Y +B Y +B Y +A Y +C Y +C Y +B Y +B Y +B Y +B Y +B Y +B Z +A Y +A X +C Y +B Y +C Y +C X +B Y +A Y +B Y +C Y +B Z +A X +B X +B Y +A X +B Z +A X +A X +B Y +B X +C X +A Y +A Y +B Y +B Z +A Y +B X +C Y +C Y +B Y +A Y +A Y +B Y +A Y +B Z +C Y +B Y +A X +C X +A Y +B Y +B Y +A Y +C X +C Y +C Z +B Y +B Y +A Y +C Y +A X +C X +A X +C X +C Y +B Y +A Z +A Y +A X +C Y +A X +C X +A Y +B Y +C Z +B Y +C Z +B Y +B Y +A Y +A Z +B Z +A Y +A Y +B Y +A Y +A Z +B Y +B Z +B Y +B Z +B Y +A Y +A Y +B Y +B Z +B Y +A Y +B Y +C X +B Y +C X +A Y +B Y +C Y +A Y +A Y +B Y +B Z +A Y +A Y +B Y +C X +C Y +C Y +A Y +B Y +A Y +A X +B Z +A Y +A X +A X +A Z +A Z +A Y +C Y +A Z +A Y +C Y +A Y +B Z +C X +C Y +C Y +B Y +C X +C X +A Y +C Y +B X +C Y +B Y +B Y +B Y +B X +C Y +C Y +A X +A Y +B Z +A Y +B Y +A Y +B Y +B Y +A Y +B Y +B Z +A X +C Y +A Y +A X +A Y +B Y +A Y +B Y +B Y +C Y +B Y +B Y +C Y +B Y +C X +C Y +A X +B X +C Y +B Y +C X +A Y +A Y +B Y +A Y +B Y +A Y +B Z +C Y +B Y +B Y +C Y +A Y +A X +B X +A Y +B Y +B Y +C Y +A Y +C Y +A Y +A Y +B X +C X +A Y +B Y +B Y +B Y +B Y +A Y +C Y +A X +B Y +A Y +C Y +A X +A Y +A Y +C Y +A Y +B Y +A X +A Y +B Y +A X +C X +C Y +A Y +A Y +B Y +C Y +C Y +A X +C Y +C Y +B Y +A Y +C X +B Y +A Y +A Y +A Y +A Y +B Y +A Y +B Z +A Y +B Z +A X +A Y +A Y +A X +C Y +A X +A X +C Y +A X +A X +A Y +C Y +A Y +B Y +A X +C Z +A Y +B X +A Y +B Y +C Y +A Y +B Y +A Y +C Y +B X +C Y +B Y +A Y +B Y +C Y +C Y +A Y +A Y +C X +A X +A Y +B Y +C Y +C Y +B Z +C Y +C Y +B Y +B Z +A Y +A Y +B Y +B Y +B Y +A X +B Y +C Y +B Y +A Y +C Y +C Y +C X +B Y +A Y +B Y +C Y +B Y +C Y +A Y +A Y +C Z +C X +B Y +A Y +A X +B Y +C Y +A Y +C Y +B Y +C Y +C X +A X +C Y +A Y +A X +A X +A X +C Y +C Y +A X +C Y +C Y +C Z +A X +C Y +C X +B Z +C Y +A Y +A X +C X +A Y +C Z +A Y +C X +B Y +A Y +C X +A X +B Y +B Y +A X +C Y +B Y +C X +C X +B Y +A Y +C X +C Z +C X +C X +A Y +C Y +C Y +C Y +A X +B Y +C Y +A X +C X +C Y +B X +B Y +C Y +A Y +A Y +B Y +B Y +B Y +B Y +B Y +B Y +A Y +A X +B Y +B Y +B Y +B Z +B X +C Y +A X +A Y +A Y +C Y +A Y +A X +C X +A Y +A Y +C X +A Y +B Y +A Y +B Y +C Y +C X +A X +C Y +C Y +B Z +A Y +C Z +A X +A X +B Y +A X +A Y +B Y +C Y +B Y +C Y +A Y +A X +C Y +C Y +C Y +A X +C Y +A Y +A Z +A X +C Z +C Y +C X +B Y +C Y +B Y +C Y +A Y +C X +B Y +C Y +C X +A Y +B Y +C X +C Y +C Y +A Y +A Y +B Y +A X +B Y +A Y +C Y +B X +C Y +B Y +B Y +B Y +B Y +A X +C Y +A X +A Y +A Y +B Z +C X +B Y +B Y +A X +C Y +A Y +A Y +B Y +A X +B Z +B Y +B Y +A Y +A X +A X +A Y +A Y +C X +C X +A X +B Y +C X +C Z +C Y +B Y +C Z +A Y +A Y +B Y +B Y +C X +A Z +A X +A Y +B Y +B Y +A Y +A X +A X +A Y +A Y +C Y +B Y +B Y +A X +A X +C Y +C X +C Z +A Y +A Y +A Y +C Y +C X +B Y +C Y +A X +B Y +C Y +A Y +A X +B Y +C Y +A Y +B Y +C X +A Y +A Y +B Y +B Y +C Y +A Y +A Y +A X +B Z +A X +A Y +C Y +C Y +A Y +A Y +C Y +C Y +B Y +A Y +A X +A Y +C X +C X +B Y +B Y +A Y +A X +C X +A Y +B Y +B Y +A Y +C Y +B Y +C X +C Y +A Y +A Y +C Y +B Y +B X +C Y +C Y +B Y +B X +A Y +B X +A X +B Y +C X +A X +B Y +A Y +A Y +A Y +C Y +B X +A Y +B Y +A Y +A X +C Z +B Y +A Y +B Y +B Y +A Y +A Y +B Y +C Y +A Y +C Y +B Y +C Y +A X +A Y +C Y +A Y +C Y +B Y +B Y +C Y +A Y +A Y +B Y +A Y +C Y +A Y +A Y +B Y +B Y +B Y +B Y +A X +A X +C Y +C Y +B X +C Z +C Y +A X +A Y +B Y +A Y +C Y +C X +B Y +B Z +C Y +A X +A Y +A X +B Y +B X +B Y +B X +C Y +A Z +B Y +C Y +A Y +A X +C Y +C X +B Y +A Y +A Y +A X +C X +A Y +C X +A Y +C Y +C X +A X +C Z +B Y +A X +B Y +A Y +A X +A X +A Y +A Y +A Y +C X +C X +A Y +A Y +B Y +C Y +C Y +B Y +C Y +A Y +C Y +A Y +B Y +B Y +A Y +B Y +C X +B X +A X +A X +B Y +B Y +B Y +C Z +C Y +B X +A X +B Y +A X +B Y +A Y +B Y +C Y +A Y +A Y +C Y +B Y +A Y +A Y +C Y +C Y +C Y +B Y +A Y +C Y +C Y +B X +B Y +B Y +A Y +B X +A Y +B Y +C Z +B Z +B Y +C Y +B Z +C Y +C Y +B Z +A Y +A Y +A Y +C Z +B Y +A Y +A Y +C Z +A Y +A Y +A Y +B Y +C X +A Y +A Y +A Y +A Y +A Z +C Y +A X +A Y +A Y +A Y +B Y +B Y +A X +B Y +A Y +A Z +A Y +A Y +A Y +A Y +A Y +C Y +B Y +C X +B Y +C Y +C X +B Y +A Y +B Y +A Y +B Y +B Y +A Y +A X +A Y +B Y +B Y +B Y +A X +C Y +A Y +B Y +B X +A Y +C X +B Y +A X +A Y +A Y +C Z +C Y +B Y +C Y +B Z +A X +B Y +C Y +B Y +A Y +A Y +C Y +A Y +C Y +A Y +C Y +A X +A X +A Y +B Y +B Y +B X +A X +A Y +A Y +A X +A Y +C Y +C Y +B Y +A X +C Y +A Y +C X +B Y +A Y +C Y +C X +A Y +C Y +B Y +B Y +A X +A Y +C Y +A X +A X +B Y +B Y +A Y +A X +A Y +B Y +A X +B Y +A X +A X +C Z +C Z +B Y +A X +B Y +A Y +B Y +B Z +A Y +A X +C Y +C Y +C Z +C Y +C Z +A X +A Y +C Y +C Y +C Y +A Y +B Y +A Y +A Y +B Y +C Y +B Z +A Y +C Y From 907e968762ae33d1652c6996f67129d25f4cb0cb Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Fri, 2 Dec 2022 07:25:34 +0000 Subject: [PATCH 03/25] fix: wrong year in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f63e746..9202eef 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Advent of Code 2021 +# Advent of Code 2022 ## Progress: From 3b2705c3930d4d90c97c13e087ba784b1be1b342 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Fri, 2 Dec 2022 07:41:55 +0000 Subject: [PATCH 04/25] fix(day_02): remove inline solution --- day_02/src/hand.rs | 70 +++++++++++++++++++++++++++++++++++-------- day_02/src/part_01.rs | 2 +- day_02/src/part_02.rs | 28 +---------------- 3 files changed, 60 insertions(+), 40 deletions(-) diff --git a/day_02/src/hand.rs b/day_02/src/hand.rs index 3a7f964..f2bca9a 100644 --- a/day_02/src/hand.rs +++ b/day_02/src/hand.rs @@ -5,7 +5,24 @@ pub enum Hand { Scissors, } +pub enum Outcome { + Win, + Draw, + Lose, +} + +impl Outcome { + pub fn sum(&self) -> u32 { + match self { + Win => 6, + Draw => 3, + Lose => 0, + } + } +} + use Hand::*; +use Outcome::*; impl Hand { pub fn new(input: &char) -> Self { @@ -16,7 +33,7 @@ impl Hand { } } - fn shape_sum(&self) -> u32 { + fn sum(&self) -> u32 { match self { Rock => 1, Paper => 2, @@ -27,25 +44,54 @@ impl Hand { fn outcome(&self, opponent: &Hand) -> u32 { match opponent { Rock => match self { - Rock => 3, - Paper => 6, - Scissors => 0, + Rock => Draw.sum(), + Paper => Win.sum(), + Scissors => Lose.sum(), }, Paper => match self { - Rock => 0, - Paper => 3, - Scissors => 6, + Rock => Lose.sum(), + Paper => Draw.sum(), + Scissors => Win.sum(), }, Scissors => match self { - Rock => 6, - Paper => 0, - Scissors => 3, + Rock => Win.sum(), + Paper => Lose.sum(), + Scissors => Draw.sum(), }, } } - pub fn round(&self, opponent: &Hand) -> u32 { - let shape_sum = self.shape_sum(); + pub fn cheat(&self, opponent: &Hand) -> u32 { + match self { + Rock => { + Lose.sum() + + match opponent { + Rock => Scissors.sum(), + Paper => Rock.sum(), + Scissors => Paper.sum(), + } + } + Paper => { + Draw.sum() + + match opponent { + Rock => Rock.sum(), + Paper => Paper.sum(), + Scissors => Scissors.sum(), + } + } + Scissors => { + Win.sum() + + match opponent { + Rock => Paper.sum(), + Paper => Scissors.sum(), + Scissors => Rock.sum(), + } + } + } + } + + pub fn play(&self, opponent: &Hand) -> u32 { + let shape_sum = self.sum(); let outcome = self.outcome(opponent); shape_sum + outcome } diff --git a/day_02/src/part_01.rs b/day_02/src/part_01.rs index 95cb5a6..e89a68f 100644 --- a/day_02/src/part_01.rs +++ b/day_02/src/part_01.rs @@ -5,7 +5,7 @@ use crate::Input; pub fn main(input: &Input) -> Result { Ok(input .into_iter() - .map(|(opponent, elf)| elf.round(opponent)) + .map(|(opponent, elf)| elf.play(opponent)) .sum()) } diff --git a/day_02/src/part_02.rs b/day_02/src/part_02.rs index 1c90393..5e599f1 100644 --- a/day_02/src/part_02.rs +++ b/day_02/src/part_02.rs @@ -1,37 +1,11 @@ use std::io::Result; -use crate::Hand::*; use crate::Input; pub fn main(input: &Input) -> Result { Ok(input .into_iter() - .map(|(opponent, outcome)| match outcome { - // lose - Rock => { - 0 + match opponent { - Rock => 3, - Paper => 1, - Scissors => 2, - } - } - // draw - Paper => { - 3 + match opponent { - Rock => 1, - Paper => 2, - Scissors => 3, - } - } - // win - Scissors => { - 6 + match opponent { - Rock => 2, - Paper => 3, - Scissors => 1, - } - } - }) + .map(|(opponent, instruction)| instruction.cheat(opponent)) .sum()) } From 7b24d1de332f84a1ccbdb54d58e7506b3991f180 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sat, 3 Dec 2022 13:09:05 +0100 Subject: [PATCH 05/25] fix(day_03): added solution for day 3 --- README.md | 2 +- day_03/Cargo.toml | 15 +++ day_03/src/lib.rs | 27 ++++ day_03/src/main.rs | 12 ++ day_03/src/part_01.rs | 56 ++++++++ day_03/src/part_02.rs | 62 +++++++++ input/day_03 | 300 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 473 insertions(+), 1 deletion(-) create mode 100644 day_03/Cargo.toml create mode 100644 day_03/src/lib.rs create mode 100644 day_03/src/main.rs create mode 100644 day_03/src/part_01.rs create mode 100644 day_03/src/part_02.rs create mode 100644 input/day_03 diff --git a/README.md b/README.md index 9202eef..3577646 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - [Day 1](https://github.com/ankjevel/adventofcode/tree/2022/day_01) ⭐️ ⭐️ - [Day 2](https://github.com/ankjevel/adventofcode/tree/2022/day_02) ⭐️ ⭐️ -- [Day 3](#) +- [Day 3](https://github.com/ankjevel/adventofcode/tree/2022/day_03) ⭐️ ⭐️ - [Day 4](#) - [Day 5](#) - [Day 6](#) diff --git a/day_03/Cargo.toml b/day_03/Cargo.toml new file mode 100644 index 0000000..8d44c96 --- /dev/null +++ b/day_03/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_03" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_03" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_03/src/lib.rs b/day_03/src/lib.rs new file mode 100644 index 0000000..4949d40 --- /dev/null +++ b/day_03/src/lib.rs @@ -0,0 +1,27 @@ +pub mod part_01; +pub mod part_02; + +pub type Input = Vec; + +pub fn parse_input(input: &str) -> Input { + input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .map(str::to_owned) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + example + "; + + #[test] + fn it_parses_example() { + assert_eq!(parse_input(&EXAMPLE_DATA), vec!["example"]); + } +} diff --git a/day_03/src/main.rs b/day_03/src/main.rs new file mode 100644 index 0000000..61dd442 --- /dev/null +++ b/day_03/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_03::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_03")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_03/src/part_01.rs b/day_03/src/part_01.rs new file mode 100644 index 0000000..e47589b --- /dev/null +++ b/day_03/src/part_01.rs @@ -0,0 +1,56 @@ +use std::collections::HashSet; +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input + .into_iter() + .map(|val| { + let (left, right) = val.split_at(val.len() / 2); + let left_chars = left.chars().into_iter(); + let right_chars = right.chars().into_iter(); + + let mut matched = HashSet::new(); + for char in left_chars { + if let Some(result) = right_chars.to_owned().find(|r_char| r_char == &char) { + matched.insert(result); + } + } + + matched + .into_iter() + .map(|c| { + let n = c as u32; + if n > 90 { + n - 96 + } else { + n - 38 + } + }) + .sum::() + }) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + vJrwpWtwJgWrhcsFMMfFFhFp + jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL + PmmdzqPrVvPwwTWBwg + wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn + ttgJtRGJQctTZtZT + CrZsJsPPZsGzwwsLwLmpwMDw + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 157); + Ok(()) + } +} diff --git a/day_03/src/part_02.rs b/day_03/src/part_02.rs new file mode 100644 index 0000000..3f52399 --- /dev/null +++ b/day_03/src/part_02.rs @@ -0,0 +1,62 @@ +use std::collections::HashSet; +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input + .to_owned() + .chunks(3) + .map(|rows| { + let chars: Vec> = + rows.iter().map(|chars| chars.chars().collect()).collect(); + + let (n0, n1, n2) = ( + chars[0].to_owned(), + chars[1].to_owned(), + chars[2].to_owned(), + ); + + let mut matched = HashSet::new(); + for char in n0 { + if n1.contains(&char) && n2.contains(&char) { + matched.insert(char); + } + } + + matched + .into_iter() + .map(|c| { + let n = c as u32; + if n > 90 { + n - 96 + } else { + n - 38 + } + }) + .sum::() + }) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + vJrwpWtwJgWrhcsFMMfFFhFp + jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL + PmmdzqPrVvPwwTWBwg + wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn + ttgJtRGJQctTZtZT + CrZsJsPPZsGzwwsLwLmpwMDw + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 70); + Ok(()) + } +} diff --git a/input/day_03 b/input/day_03 new file mode 100644 index 0000000..9b16a3c --- /dev/null +++ b/input/day_03 @@ -0,0 +1,300 @@ +WVHGHwddqSsNjsjwqVvdwZRCbcJcZTCcsZbLcJJsCZ +hngprFFhFDFhrDpzzQDhtnBJJRJZbZvTcvbfRCJfBRcBJl +DmptngtFwvvMmwmm +HFddrJnLdqtHBMQBmmVm +gbvNsbhsvQtmZTbQPT +vDshDlczcDhcssscwzQwslLJrSJLpqrrzpnCrSfLSnqq +pDGQDSpFDGzFDQSJqzDhjhQMTjTrwTstbTBTjTtLtbTMBT +zgzVNHHgMwMLbLNB +WRWPgdHCZccggJmJGzJmzGhGCD +sVJNlhldShpdpnnwVnwCwtwggt +WLFFcHWQLPPZQCgtnCgmbtbHwt +MPLWzRMMcGgRrWNDpSSSfDflMlTd +BBMZJcgBRjCZZzBpSvHQvbLvvHCQLQ +VlVTFwDTVGnfWSQPtsDPbvrpDS +wWdqhWlwGVfGwlfTVqFWfWWjzZZBJmMZMNdzZJMpjzNjgJ +FBWFphQBmDmpmMSpDWVcVcvsPcRbrjPMcMsr +HHtdnHnwNCHCTJRTPTzrbvVbcVRr +lHqHwlnlqnGCNGGmWDvvZfpZvG +mfVtmPtsccMmHcHCFfmhVmnpgZLbWPZqWnpqZbZWpgPW +zzvwBrzdQDvpZJfQJZJpLf +BrTBwRdNcfNmfStc +sTlhFLfZTTLcfsLlLDZflvQvRNqRJFNvRMRNvQQRBQ +CWcgwStWwCWWwvgNQvJBvQMQRB +wptGz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MpbGQbQmhhHhmfHQmh +hTbddhQCtdNmdtwtdhTBbCddRSWscczwcRSWLJzcFJzDsFsR +NflgfPZPcgSLJcWD +lPVNZMMMpZlZZvfrMvpbQHQhtbtqdTQHthrqhd +JlWSStwhWJSRJpJvJBjTwTqcwTsDjsCTCB +dqFzgFZGGQNVmTcCrjrzsBrB +fdgLFQLnPdnqShRMPhlJMpWW +TMPcsPDjdDhsDcDcTTTDvdvghBNFGGtmNrSrgSSBGNtNFg +CVCbJqlRVVWWpRqRQZRWVWJZBtmSFGNmggGmtmmBFbrGMGMt +JRqHVJVCRLZWTjMnfLTPcfLd +TRTZFTTrghrZVhVWdWZpMmbzbdzBmtDpDDzmzB +wcsSSsjfPfGPqQwqsQcfJJCtJGpppCBJzCbzJzCb +sPjflcwljfjfvqNcTZTRhtVWrNrVLnrR +rVLLsmwmCWTmsCTdwQrdTmqWDjDHjNGNPbjDBPNDNsZRDBjH +cFcSvgJvfhfLnShtMJtPHRRvRbBBGBPNBHPbND +hgLcgcLpJSMwzmrmzqQrmp From 316aafeea1b901476071a764b2f9cd88806de2f6 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sat, 3 Dec 2022 13:42:20 +0100 Subject: [PATCH 06/25] fix(day_03): chaining --- day_03/src/part_01.rs | 22 ++++++++-------------- day_03/src/part_02.rs | 22 ++++++++-------------- 2 files changed, 16 insertions(+), 28 deletions(-) diff --git a/day_03/src/part_01.rs b/day_03/src/part_01.rs index e47589b..8e11944 100644 --- a/day_03/src/part_01.rs +++ b/day_03/src/part_01.rs @@ -10,24 +10,18 @@ pub fn main(input: &Input) -> Result { let (left, right) = val.split_at(val.len() / 2); let left_chars = left.chars().into_iter(); let right_chars = right.chars().into_iter(); - - let mut matched = HashSet::new(); - for char in left_chars { - if let Some(result) = right_chars.to_owned().find(|r_char| r_char == &char) { - matched.insert(result); - } - } - - matched + left_chars .into_iter() - .map(|c| { - let n = c as u32; - if n > 90 { - n - 96 + .filter_map(|char| { + if let Some(result) = right_chars.to_owned().find(|r_char| r_char == &char) { + Some(result as u32) } else { - n - 38 + None } }) + .collect::>() + .iter() + .map(|n| if n > &90 { n - 96 } else { n - 38 }) .sum::() }) .sum()) diff --git a/day_03/src/part_02.rs b/day_03/src/part_02.rs index 3f52399..b61f773 100644 --- a/day_03/src/part_02.rs +++ b/day_03/src/part_02.rs @@ -17,23 +17,17 @@ pub fn main(input: &Input) -> Result { chars[2].to_owned(), ); - let mut matched = HashSet::new(); - for char in n0 { - if n1.contains(&char) && n2.contains(&char) { - matched.insert(char); - } - } - - matched - .into_iter() - .map(|c| { - let n = c as u32; - if n > 90 { - n - 96 + n0.into_iter() + .filter_map(|char| { + if n1.contains(&char) && n2.contains(&char) { + Some(char as u32) } else { - n - 38 + None } }) + .collect::>() + .iter() + .map(|n| if n > &90 { n - 96 } else { n - 38 }) .sum::() }) .sum()) From e1d02af9cab8d26458faa26f5e8ea45e32724881 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sat, 3 Dec 2022 13:45:53 +0100 Subject: [PATCH 07/25] fix(day_03): less declarations --- day_03/src/part_01.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/day_03/src/part_01.rs b/day_03/src/part_01.rs index 8e11944..b978568 100644 --- a/day_03/src/part_01.rs +++ b/day_03/src/part_01.rs @@ -8,13 +8,12 @@ pub fn main(input: &Input) -> Result { .into_iter() .map(|val| { let (left, right) = val.split_at(val.len() / 2); - let left_chars = left.chars().into_iter(); - let right_chars = right.chars().into_iter(); - left_chars + let right_chars = right.chars().into_iter().collect::>(); + left.chars() .into_iter() .filter_map(|char| { - if let Some(result) = right_chars.to_owned().find(|r_char| r_char == &char) { - Some(result as u32) + if right_chars.contains(&char) { + Some(char as u32) } else { None } From f9ed794577ce92adbfff32df1ca3a8df433c32d8 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sun, 4 Dec 2022 17:45:55 +0100 Subject: [PATCH 08/25] feat(day_04): thats at least one way to solve it --- README.md | 2 +- day_04/Cargo.toml | 17 + day_04/src/lib.rs | 65 +++ day_04/src/main.rs | 12 + day_04/src/part_01.rs | 35 ++ day_04/src/part_02.rs | 37 ++ input/day_04 | 1000 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 1167 insertions(+), 1 deletion(-) create mode 100644 day_04/Cargo.toml create mode 100644 day_04/src/lib.rs create mode 100644 day_04/src/main.rs create mode 100644 day_04/src/part_01.rs create mode 100644 day_04/src/part_02.rs create mode 100644 input/day_04 diff --git a/README.md b/README.md index 3577646..58bb21d 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ - [Day 1](https://github.com/ankjevel/adventofcode/tree/2022/day_01) ⭐️ ⭐️ - [Day 2](https://github.com/ankjevel/adventofcode/tree/2022/day_02) ⭐️ ⭐️ - [Day 3](https://github.com/ankjevel/adventofcode/tree/2022/day_03) ⭐️ ⭐️ -- [Day 4](#) +- [Day 4](https://github.com/ankjevel/adventofcode/tree/2022/day_04) ⭐️ ⭐️ - [Day 5](#) - [Day 6](#) - [Day 7](#) diff --git a/day_04/Cargo.toml b/day_04/Cargo.toml new file mode 100644 index 0000000..7252ca2 --- /dev/null +++ b/day_04/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "day_04" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_04" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1.7.0" +lazy_static = "1.4.0" diff --git a/day_04/src/lib.rs b/day_04/src/lib.rs new file mode 100644 index 0000000..79fd05e --- /dev/null +++ b/day_04/src/lib.rs @@ -0,0 +1,65 @@ +#[macro_use(lazy_static)] +extern crate lazy_static; +extern crate regex; + +pub mod part_01; +pub mod part_02; + +use regex::Regex; + +lazy_static! { + static ref ROW: Regex = Regex::new(r"(\d+)-(\d+),(\d+)-(\d+)").unwrap(); +} + +pub type Input = Vec<(u32, u32, u32, u32)>; + +pub fn parse_input(input: &str) -> Input { + input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .map(|string| { + let unwrapped = ROW + .captures(&string) + .unwrap() + .iter() + .map(|c| c.unwrap().to_owned().as_str().parse::().unwrap_or(0)) + .collect::>(); + ( + unwrapped[1].to_owned(), + unwrapped[2].to_owned(), + unwrapped[3].to_owned(), + unwrapped[4].to_owned(), + ) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + 2-4,6-8 + 2-3,4-5 + 2-93,2-9 + 3-82,82-8 + 2-91,38-91 + 2-90,91-96 + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + vec![ + (2, 4, 6, 8), + (2, 3, 4, 5), + (2, 93, 2, 9), + (3, 82, 82, 8), + (2, 91, 38, 91), + (2, 90, 91, 96) + ] + ); + } +} diff --git a/day_04/src/main.rs b/day_04/src/main.rs new file mode 100644 index 0000000..6286c1f --- /dev/null +++ b/day_04/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_04::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_04")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_04/src/part_01.rs b/day_04/src/part_01.rs new file mode 100644 index 0000000..d6b7251 --- /dev/null +++ b/day_04/src/part_01.rs @@ -0,0 +1,35 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input.into_iter().fold(0u32, |sum, (n0, n1, n2, n3)| { + if n0 <= n2 && n1 >= n3 || n2 <= n0 && n3 >= n1 { + sum + 1 + } else { + sum + } + })) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + 2-4,6-8 + 2-3,4-5 + 5-7,7-9 + 2-8,3-7 + 6-6,4-6 + 2-6,4-8 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 2); + Ok(()) + } +} diff --git a/day_04/src/part_02.rs b/day_04/src/part_02.rs new file mode 100644 index 0000000..b891c51 --- /dev/null +++ b/day_04/src/part_02.rs @@ -0,0 +1,37 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + Ok(input.into_iter().fold(0u32, |sum, (n0, n1, n2, n3)| { + let r0 = n0..=n1; + let r1 = n2..=n3; + if r0.contains(&n2) || r0.contains(&n3) || r1.contains(&n0) || r1.contains(&n1) { + sum + 1 + } else { + sum + } + })) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + 2-4,6-8 + 2-3,4-5 + 5-7,7-9 + 2-8,3-7 + 6-6,4-6 + 2-6,4-8 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 4); + Ok(()) + } +} diff --git a/input/day_04 b/input/day_04 new file mode 100644 index 0000000..f8b658d --- /dev/null +++ b/input/day_04 @@ -0,0 +1,1000 @@ +13-53,17-82 +32-32,32-42 +85-85,8-86 +78-80,79-91 +60-71,59-70 +91-92,4-90 +90-90,1-90 +35-49,38-50 +31-51,31-52 +78-80,81-81 +85-86,86-94 +52-95,96-97 +1-39,1-40 +8-68,7-24 +83-89,58-78 +96-98,98-99 +26-27,26-74 +9-43,8-44 +3-8,9-89 +3-77,7-26 +1-88,2-2 +30-53,31-52 +3-47,46-86 +8-78,7-9 +53-92,6-52 +84-97,49-97 +38-67,38-68 +13-13,12-95 +7-13,52-87 +18-18,17-79 +65-65,64-64 +12-92,11-91 +67-67,2-68 +14-89,13-92 +13-44,12-14 +20-42,21-24 +9-47,7-11 +99-99,32-97 +22-37,21-36 +24-98,24-99 +62-76,62-77 +49-50,49-98 +92-98,26-79 +5-10,9-95 +24-92,24-91 +13-13,14-30 +56-97,5-97 +8-33,9-34 +18-76,18-75 +16-80,12-17 +8-99,8-95 +4-97,3-96 +11-12,11-86 +5-90,1-89 +32-71,31-84 +12-96,14-91 +9-9,12-71 +13-93,8-92 +4-77,78-93 +4-6,5-89 +18-52,17-51 +35-65,11-64 +76-77,1-76 +24-24,26-43 +4-8,7-48 +85-85,5-86 +1-86,4-85 +10-44,10-45 +19-91,36-79 +63-95,75-98 +59-86,59-87 +87-91,42-90 +49-98,48-96 +2-28,2-27 +4-91,4-90 +93-98,28-91 +5-91,90-90 +67-77,82-85 +98-98,66-94 +19-36,33-46 +6-54,6-53 +52-82,87-90 +94-99,1-94 +2-2,1-96 +43-46,42-45 +20-23,25-30 +7-93,6-92 +18-56,17-19 +40-95,39-96 +35-90,91-99 +15-17,15-19 +29-31,37-99 +2-91,6-92 +11-28,7-7 +10-50,9-11 +27-89,10-26 +33-60,33-61 +66-68,55-67 +11-11,10-90 +10-20,9-11 +73-96,20-98 +6-85,6-86 +7-82,6-8 +37-54,10-57 +36-87,35-86 +46-86,86-93 +4-68,7-69 +25-26,20-26 +99-99,2-89 +1-10,3-17 +6-92,5-5 +29-87,1-30 +7-74,12-74 +26-36,25-97 +29-83,16-84 +13-97,13-98 +34-72,35-72 +55-62,61-61 +24-95,59-95 +4-38,4-37 +62-73,8-77 +5-35,2-2 +79-79,4-78 +18-51,18-50 +10-12,9-9 +44-83,6-44 +71-81,95-98 +36-98,26-97 +67-74,19-74 +81-89,40-80 +26-81,53-94 +64-64,26-63 +34-67,11-75 +13-56,14-56 +55-55,44-56 +3-51,3-97 +76-80,75-77 +5-66,77-89 +49-70,18-49 +32-53,14-33 +30-30,30-31 +3-7,14-47 +33-33,1-33 +7-9,11-95 +92-92,37-92 +40-90,4-90 +33-79,7-46 +50-64,26-49 +3-94,37-94 +32-68,68-77 +5-17,1-6 +6-86,3-6 +26-46,21-76 +14-74,38-75 +3-86,20-85 +60-70,60-69 +10-32,11-33 +43-43,11-43 +17-96,9-95 +36-96,37-95 +11-94,87-95 +9-94,8-94 +60-61,45-60 +25-58,24-58 +46-81,45-92 +37-77,38-80 +95-95,2-96 +57-71,56-70 +28-93,59-94 +16-88,15-90 +12-38,12-22 +13-88,87-99 +7-38,7-37 +3-70,4-69 +44-77,45-76 +4-69,2-98 +45-79,44-78 +39-91,2-33 +33-78,33-77 +91-91,16-90 +63-94,95-97 +1-97,1-97 +19-38,3-19 +95-95,91-96 +53-77,15-78 +8-53,7-53 +20-41,28-42 +5-7,7-90 +55-86,85-87 +2-92,9-80 +17-79,79-89 +43-77,44-78 +61-66,60-60 +8-76,5-8 +40-83,39-83 +69-79,79-80 +51-51,50-92 +29-99,18-99 +18-75,17-18 +17-39,8-59 +9-35,34-79 +8-52,7-53 +51-95,75-94 +94-94,1-94 +37-55,32-56 +6-84,1-6 +19-19,18-40 +95-97,28-95 +89-99,82-92 +11-47,46-47 +4-98,4-99 +12-16,23-94 +18-50,17-49 +37-72,35-57 +23-77,22-24 +54-65,6-54 +61-74,39-73 +83-94,52-90 +13-99,2-14 +10-98,11-99 +30-98,29-99 +38-90,37-90 +63-66,64-68 +49-93,49-95 +84-88,79-88 +11-35,35-83 +2-59,3-59 +42-64,57-64 +14-62,15-88 +52-54,53-71 +37-66,38-67 +20-96,21-92 +5-82,4-6 +13-52,13-52 +10-69,4-21 +5-69,26-98 +41-57,40-42 +8-94,9-93 +6-27,26-61 +27-65,27-66 +97-98,1-97 +46-85,45-86 +43-48,38-43 +5-49,48-48 +14-59,12-58 +30-97,7-96 +62-70,64-69 +31-79,12-78 +10-18,9-17 +74-76,72-76 +29-69,29-70 +15-93,93-95 +66-68,67-73 +2-89,1-2 +2-95,9-96 +4-89,6-45 +98-98,28-99 +16-77,4-77 +16-86,87-87 +17-17,18-83 +18-82,29-81 +21-99,7-98 +36-85,1-85 +9-70,70-90 +38-95,60-94 +94-94,24-95 +4-63,3-63 +40-72,73-88 +3-4,3-99 +1-27,3-93 +15-26,16-27 +10-89,20-34 +16-18,6-39 +34-48,28-34 +5-83,6-6 +37-49,38-50 +39-86,9-38 +12-74,74-75 +16-80,15-81 +26-67,24-85 +5-7,12-19 +40-68,69-69 +17-79,80-95 +17-63,16-18 +69-72,16-75 +15-24,48-89 +12-12,13-96 +4-4,6-89 +79-84,5-39 +23-25,24-61 +26-97,6-96 +4-97,5-97 +55-91,56-91 +25-80,80-81 +52-76,53-75 +45-61,44-61 +65-81,64-82 +65-74,3-64 +4-73,72-72 +54-54,53-57 +51-51,29-50 +1-14,3-36 +93-95,57-92 +85-86,6-85 +3-98,3-15 +21-21,20-54 +18-72,19-73 +27-49,47-53 +20-77,21-78 +52-76,38-76 +82-87,14-87 +3-79,1-4 +1-97,1-1 +39-40,40-91 +6-11,5-10 +5-98,4-6 +36-36,9-37 +6-69,7-7 +77-93,76-95 +41-94,26-93 +70-81,7-70 +1-72,71-72 +30-90,29-98 +68-76,12-76 +28-42,15-19 +76-96,39-77 +30-32,46-90 +83-98,83-92 +1-98,9-98 +17-93,16-92 +17-89,3-8 +61-67,62-67 +19-63,18-20 +51-53,52-98 +20-21,20-58 +5-86,6-87 +23-85,24-84 +34-54,22-53 +20-67,20-68 +26-88,26-26 +26-94,27-94 +27-29,29-84 +42-73,2-42 +68-72,56-71 +13-85,16-84 +54-87,88-95 +26-29,32-35 +2-21,20-45 +18-18,19-48 +91-98,31-88 +5-59,41-60 +94-96,70-94 +8-85,86-89 +3-97,98-99 +89-90,16-92 +13-44,43-74 +6-87,25-99 +5-5,6-53 +3-85,1-84 +25-94,35-87 +17-95,17-64 +12-85,13-85 +2-4,4-75 +7-25,8-24 +47-96,24-96 +95-96,94-94 +94-94,93-93 +22-77,3-90 +17-77,78-83 +35-42,48-75 +12-12,13-97 +13-19,18-77 +32-83,26-83 +31-78,3-32 +3-17,17-95 +45-61,44-61 +73-81,13-80 +2-99,6-93 +80-88,33-74 +45-86,3-44 +39-75,39-93 +2-96,95-96 +10-64,9-65 +4-76,5-5 +72-96,72-73 +78-85,77-79 +20-94,19-98 +14-87,3-15 +21-35,35-53 +2-87,88-93 +15-90,15-87 +3-99,2-99 +9-95,9-96 +5-89,1-90 +6-73,3-74 +52-76,6-51 +5-52,66-69 +17-19,18-36 +8-43,2-9 +56-72,63-73 +73-91,66-92 +4-89,5-89 +95-99,9-93 +91-98,92-99 +18-78,6-7 +11-71,71-94 +1-99,3-97 +7-90,1-8 +5-87,5-88 +46-48,47-97 +3-92,7-91 +33-33,32-73 +39-95,38-98 +35-82,36-81 +41-90,86-95 +21-45,44-44 +5-5,6-49 +49-67,50-66 +8-76,8-77 +15-17,29-73 +35-95,20-34 +29-64,30-64 +11-45,11-46 +14-87,4-13 +38-69,37-37 +11-76,6-9 +70-98,62-97 +17-99,16-18 +2-82,5-81 +15-93,4-94 +56-57,56-57 +1-6,6-88 +48-93,48-49 +7-75,39-74 +1-3,2-90 +27-31,28-31 +10-10,9-63 +11-64,65-79 +57-57,56-69 +50-50,49-88 +43-43,2-44 +15-59,11-59 +28-44,7-28 +5-39,37-42 +88-95,95-95 +2-63,63-63 +5-91,14-98 +7-89,6-98 +16-36,36-56 +40-80,81-83 +4-99,2-98 +70-70,2-71 +6-45,7-46 +9-97,10-96 +34-79,33-34 +51-55,50-55 +27-72,58-79 +9-65,8-64 +3-5,4-99 +17-43,11-16 +9-29,10-28 +23-91,24-90 +13-80,10-14 +47-85,86-86 +9-25,4-8 +43-92,43-92 +89-90,76-89 +32-88,4-31 +33-52,32-51 +3-97,1-99 +39-91,40-51 +19-92,22-92 +4-89,23-92 +5-6,6-66 +6-99,7-78 +8-93,92-93 +5-95,58-97 +2-96,99-99 +34-39,35-35 +92-92,3-93 +38-54,11-53 +45-59,59-87 +55-78,54-54 +42-66,42-66 +19-91,22-92 +39-46,38-91 +42-89,41-41 +33-66,34-65 +7-71,6-70 +7-80,30-61 +58-91,57-90 +30-95,88-95 +4-97,96-99 +12-99,12-95 +39-41,40-50 +34-87,35-87 +14-86,2-12 +14-87,15-86 +7-97,96-96 +7-36,31-46 +6-75,7-49 +29-29,28-81 +58-67,68-68 +18-97,18-91 +17-31,27-30 +39-46,51-96 +68-89,69-69 +15-57,15-57 +24-24,16-77 +40-92,91-93 +43-61,41-76 +49-49,42-48 +34-76,15-75 +2-73,74-74 +16-98,16-97 +7-80,6-80 +23-66,24-65 +39-81,40-82 +74-99,33-37 +81-93,64-93 +19-86,86-95 +97-97,9-98 +45-70,62-71 +54-67,20-53 +85-85,18-85 +11-79,28-76 +40-96,12-40 +19-79,35-80 +3-91,1-92 +5-90,5-89 +42-93,43-92 +40-70,34-38 +12-43,11-12 +22-22,3-21 +53-90,7-65 +22-90,11-15 +24-24,24-36 +3-3,5-55 +53-54,53-53 +53-53,26-54 +3-9,9-97 +43-58,58-59 +31-64,17-64 +34-36,35-35 +4-11,10-48 +12-39,15-40 +44-44,43-92 +9-68,68-68 +20-95,20-81 +6-27,45-62 +7-91,7-91 +2-3,5-88 +11-77,22-61 +8-95,30-77 +85-97,86-97 +3-60,8-18 +14-70,13-69 +34-81,61-82 +42-82,43-82 +9-99,10-10 +2-75,19-53 +26-77,69-86 +82-97,27-81 +17-92,9-93 +5-6,5-5 +46-46,45-47 +21-67,21-68 +36-95,17-36 +41-88,41-89 +6-81,11-80 +26-45,45-46 +4-75,2-4 +27-36,24-35 +49-64,50-97 +86-86,40-85 +5-5,5-92 +81-82,6-82 +14-92,93-93 +28-36,35-65 +12-17,18-27 +28-68,6-68 +47-93,48-48 +20-99,11-97 +32-75,9-18 +53-96,96-96 +33-64,32-63 +79-83,3-78 +38-46,47-79 +3-88,4-98 +6-97,96-96 +68-68,42-69 +24-99,23-23 +27-98,65-99 +9-94,5-5 +22-30,23-23 +71-72,23-71 +2-97,2-96 +6-58,7-57 +44-88,45-74 +2-97,98-99 +27-30,9-27 +10-89,48-89 +19-99,18-88 +95-95,15-78 +10-30,10-31 +10-97,11-79 +5-97,1-98 +17-99,1-99 +27-29,28-95 +90-90,33-90 +64-65,3-64 +34-98,35-97 +23-62,22-63 +76-85,85-85 +56-97,57-98 +43-43,43-43 +17-83,16-18 +4-15,6-15 +18-70,66-70 +62-63,9-62 +81-85,32-80 +35-95,24-90 +6-62,7-62 +17-19,18-29 +24-38,37-37 +2-42,2-43 +11-50,10-12 +20-50,19-51 +22-22,2-22 +54-54,8-53 +27-47,46-46 +11-97,5-6 +47-99,1-99 +31-46,32-45 +8-56,2-39 +57-95,10-96 +17-17,25-75 +44-46,10-45 +30-33,29-32 +1-94,24-95 +31-32,8-32 +19-21,20-49 +34-34,19-35 +33-79,86-96 +12-98,12-97 +27-35,26-36 +37-89,90-90 +2-5,4-81 +29-38,38-39 +3-88,58-82 +5-8,7-65 +6-14,13-73 +57-98,90-99 +29-53,30-54 +1-78,59-60 +34-34,24-35 +4-12,11-11 +41-84,9-83 +5-88,6-6 +1-78,8-75 +80-80,19-81 +95-97,72-96 +13-85,84-84 +31-81,28-32 +53-58,37-45 +13-69,62-82 +11-69,10-69 +46-46,45-74 +60-93,77-98 +14-58,14-59 +73-73,11-72 +3-91,32-91 +4-6,5-87 +11-85,16-78 +2-99,89-97 +21-88,20-87 +4-52,51-51 +22-22,1-21 +21-76,8-94 +4-6,5-40 +97-97,5-93 +22-85,11-23 +6-74,5-5 +67-91,66-92 +24-99,16-98 +2-58,1-1 +21-52,20-52 +71-82,76-96 +5-65,5-64 +4-6,9-70 +73-75,68-88 +39-52,38-40 +29-32,29-31 +65-65,37-66 +3-69,2-68 +36-97,36-85 +52-68,15-69 +3-42,42-43 +19-25,25-58 +9-28,8-27 +23-99,24-99 +21-98,22-95 +6-12,12-13 +42-93,41-99 +57-83,58-84 +91-91,90-94 +71-72,71-88 +2-97,1-3 +9-12,11-49 +75-81,38-82 +38-75,16-52 +16-18,17-91 +29-72,30-71 +83-83,45-82 +17-56,19-57 +50-52,51-78 +17-67,66-66 +3-81,3-80 +20-48,49-65 +17-51,10-18 +1-9,2-10 +49-49,46-48 +95-96,55-96 +27-89,28-74 +47-76,28-82 +9-95,10-96 +87-88,5-88 +27-74,28-47 +74-93,46-92 +3-95,98-98 +49-50,26-50 +28-79,27-79 +15-53,14-93 +17-36,17-18 +7-72,64-75 +7-96,32-99 +87-99,21-77 +12-98,13-97 +14-79,2-79 +2-2,2-3 +38-73,73-74 +11-82,4-7 +67-67,68-71 +86-87,13-84 +52-80,51-52 +71-97,71-96 +4-5,5-87 +14-31,31-73 +28-28,27-55 +3-3,2-28 +37-67,65-73 +79-80,73-79 +16-95,17-94 +7-77,8-76 +16-82,18-82 +34-34,33-93 +13-84,88-88 +37-98,9-97 +16-40,7-59 +3-96,1-3 +3-95,30-99 +1-84,2-85 +2-3,4-56 +5-54,6-55 +69-93,36-93 +31-89,44-88 +5-88,23-87 +5-87,4-88 +85-90,42-84 +44-72,33-37 +7-11,21-99 +32-91,31-91 +99-99,61-99 +5-79,10-44 +59-94,58-84 +4-6,5-99 +24-29,28-82 +23-41,23-40 +6-95,95-96 +18-99,17-53 +87-95,32-85 +28-66,28-67 +4-94,99-99 +22-97,22-98 +30-96,14-31 +30-70,31-69 +56-74,3-89 +4-85,86-86 +57-97,58-97 +41-95,41-96 +78-79,37-78 +25-70,70-75 +92-92,4-93 +46-65,66-73 +18-20,19-23 +32-90,32-89 +38-73,38-95 +30-69,31-68 +49-68,1-78 +18-98,10-97 +25-65,14-22 +14-46,12-45 +19-48,20-32 +52-83,29-52 +57-95,57-94 +4-97,4-96 +35-91,90-92 +5-99,4-99 +4-26,4-27 +54-73,6-54 +6-98,7-99 +63-81,7-80 +58-85,59-59 +1-2,1-88 +71-85,51-93 +5-65,6-6 +3-49,33-93 +44-84,5-84 +49-62,48-61 +35-98,36-99 +2-3,10-64 +95-95,43-95 +2-55,3-56 +20-53,20-52 +15-97,11-13 +81-81,3-81 +40-45,1-44 +1-3,5-73 +4-97,97-98 +91-92,12-91 +31-64,30-63 +18-55,17-54 +35-40,34-35 +3-6,5-95 +26-70,79-94 +4-97,8-97 +6-63,28-63 +28-95,11-21 +5-81,1-6 +38-41,39-40 +49-96,48-88 +59-77,58-77 +29-40,28-40 +45-75,46-46 +3-87,4-67 +58-99,4-92 +47-57,46-47 +24-84,15-80 +85-92,38-85 +55-89,88-90 +32-68,67-67 +7-89,8-89 +14-92,13-92 +29-41,30-41 +5-92,4-92 +9-29,19-41 +65-89,90-99 +91-91,60-91 +5-70,21-68 +11-95,35-92 +25-65,14-26 +52-93,88-94 +52-89,51-90 +74-87,86-86 +32-96,99-99 +5-97,4-6 +19-91,20-91 +65-76,64-77 +36-90,37-90 +37-82,25-92 +76-78,77-84 +5-66,5-66 +4-9,12-50 +2-52,10-52 +15-99,14-98 +18-96,6-18 +3-81,4-33 +21-66,15-71 +49-49,27-67 +4-4,5-94 +50-50,9-51 +31-95,30-94 +82-91,87-93 +61-96,17-95 +2-95,17-95 +34-37,7-35 +2-94,7-85 +96-96,49-95 +41-41,42-59 +2-97,3-97 +22-97,22-98 +33-95,34-82 +33-47,47-72 +32-93,33-93 +36-58,35-35 +10-58,52-58 +77-89,60-67 +3-13,12-81 +14-98,40-98 +11-94,7-12 +51-63,43-63 +22-82,2-81 +31-34,35-60 +8-79,8-42 +67-67,66-88 +64-81,4-64 +16-96,15-97 +22-65,23-65 +59-97,60-96 +18-89,90-99 +2-2,3-97 +12-63,11-63 +12-42,41-59 +36-37,31-36 +45-91,2-59 +7-89,6-8 +21-49,21-50 +13-13,12-87 +48-89,47-90 +3-22,2-55 +19-19,20-41 +48-67,47-94 +11-13,12-83 +16-62,16-61 +11-35,12-41 +71-78,5-71 +66-67,67-87 +22-71,14-71 +19-72,15-72 +20-42,21-41 +8-94,7-93 +97-98,19-96 +3-98,2-98 +33-71,72-73 +33-35,34-81 +18-98,18-97 +85-85,24-86 +7-97,26-96 +47-47,46-49 +5-14,3-3 +1-83,14-83 +10-91,3-75 +62-62,32-63 +16-84,83-84 +52-53,52-70 +2-47,2-47 +8-93,8-24 +56-97,97-97 +8-84,27-84 +71-89,89-89 +96-98,30-96 +11-92,49-92 +66-66,19-67 +55-57,54-56 +3-69,3-70 +5-99,4-6 +1-40,1-39 +1-87,88-89 +11-98,20-96 +22-92,21-94 +5-88,5-86 +79-98,79-97 +34-88,82-88 +67-80,66-79 +16-87,9-17 +78-91,92-92 +46-57,35-57 +45-64,44-44 +77-82,12-81 +3-98,1-3 +42-62,62-89 +2-70,95-95 +62-63,61-63 +39-97,38-98 +3-99,6-98 +26-84,83-83 +18-27,17-28 +52-58,7-52 +25-82,1-82 +29-66,67-67 +43-72,21-71 +23-29,5-28 +43-90,43-44 +42-68,42-67 +67-69,66-70 +19-40,18-57 +1-1,8-9 +56-89,97-99 +87-98,25-86 +88-98,10-88 +2-93,2-96 +3-82,82-89 +7-14,8-14 +3-37,36-56 +51-52,4-51 +38-88,37-96 +74-81,22-81 +69-83,68-70 +2-91,38-91 +2-90,91-96 From 3fede6206ce73fd48f6364fef2c2da97a426e799 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Mon, 5 Dec 2022 09:14:45 +0100 Subject: [PATCH 09/25] feat(day_05): lazy implementation of parser --- README.md | 2 +- day_05/Cargo.toml | 17 ++ day_05/src/lib.rs | 125 ++++++++++ day_05/src/main.rs | 12 + day_05/src/part_01.rs | 49 ++++ day_05/src/part_02.rs | 52 +++++ input/day_05 | 513 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 769 insertions(+), 1 deletion(-) create mode 100644 day_05/Cargo.toml create mode 100644 day_05/src/lib.rs create mode 100644 day_05/src/main.rs create mode 100644 day_05/src/part_01.rs create mode 100644 day_05/src/part_02.rs create mode 100644 input/day_05 diff --git a/README.md b/README.md index 58bb21d..2b97453 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ - [Day 2](https://github.com/ankjevel/adventofcode/tree/2022/day_02) ⭐️ ⭐️ - [Day 3](https://github.com/ankjevel/adventofcode/tree/2022/day_03) ⭐️ ⭐️ - [Day 4](https://github.com/ankjevel/adventofcode/tree/2022/day_04) ⭐️ ⭐️ -- [Day 5](#) +- [Day 5](https://github.com/ankjevel/adventofcode/tree/2022/day_05) ⭐️ ⭐️ - [Day 6](#) - [Day 7](#) - [Day 8](#) diff --git a/day_05/Cargo.toml b/day_05/Cargo.toml new file mode 100644 index 0000000..a0bede1 --- /dev/null +++ b/day_05/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "day_05" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_05" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1.7.0" +lazy_static = "1.4.0" diff --git a/day_05/src/lib.rs b/day_05/src/lib.rs new file mode 100644 index 0000000..a5b2c59 --- /dev/null +++ b/day_05/src/lib.rs @@ -0,0 +1,125 @@ +#[macro_use(lazy_static)] +extern crate lazy_static; +extern crate regex; + +pub mod part_01; +pub mod part_02; + +use std::collections::{BTreeMap, VecDeque}; + +use regex::Regex; + +lazy_static! { + static ref ROW: Regex = Regex::new(r"move (\d+) from (\d+) to (\d+)").unwrap(); + static ref INDICES: Regex = Regex::new(r"(\d+)").unwrap(); + static ref STACK: Regex = Regex::new(r"[(\w)]").unwrap(); +} + +pub type InputStack = BTreeMap>; +pub type Input = (InputStack, Vec<(u32, usize, usize)>); + +pub fn parse_input(input: &str) -> Input { + let mut stacks: InputStack = BTreeMap::new(); + + let mut stack_lines = input + .lines() + .filter(|string| !string.contains("move") && !string.trim().is_empty()) + .map(str::to_owned) + .collect::>(); + + let indices = stack_lines.pop().unwrap(); + let trim_to = indices.find(char::is_alphanumeric).unwrap() - 1; + + let lines = stack_lines + .to_owned() + .into_iter() + .map(|line| { + let (_, line) = line.trim_end().split_at(trim_to); + line.chars() + .collect::>() + .chunks(4) + .map(|c| c.iter().collect::()) + .map(|string| string.chars().nth(1).unwrap()) + .collect() + }) + .collect::>>(); + INDICES + .find_iter(&indices) + .map(|c| c.to_owned().as_str().parse::().unwrap_or(0)) + .into_iter() + .enumerate() + .for_each(|(i, n)| { + stacks.insert( + n, + lines + .to_owned() + .iter() + .filter_map(|line| { + if let Some(row) = line.get(i) { + if row.is_whitespace() { + None + } else { + Some(row.to_owned()) + } + } else { + None + } + }) + .collect(), + ); + }); + + ( + stacks, + input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty() && string.starts_with('m')) + .map(str::to_owned) + .map(|string| { + let unwrapped = ROW + .captures(&string) + .unwrap() + .iter() + .map(|c| c.unwrap().to_owned().as_str()) + .collect::>(); + + ( + unwrapped[1].parse::().unwrap_or(0).to_owned(), + unwrapped[2].parse::().unwrap_or(0).to_owned(), + unwrapped[3].parse::().unwrap_or(0).to_owned(), + ) + }) + .collect(), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + [F] + [A] [E] + [B] [C] [D] + 1 2 3 + + move 1 from 2 to 1 + move 3 from 1 to 3 + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + ( + BTreeMap::from([ + (1, VecDeque::from(['A', 'B'])), + (2, VecDeque::from(['C'])), + (3, VecDeque::from(['F', 'E', 'D'])) + ]), + vec![(1, 2, 1), (3, 1, 3)] + ) + ); + } +} diff --git a/day_05/src/main.rs b/day_05/src/main.rs new file mode 100644 index 0000000..3e6c10d --- /dev/null +++ b/day_05/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_05::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_05")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_05/src/part_01.rs b/day_05/src/part_01.rs new file mode 100644 index 0000000..cd17073 --- /dev/null +++ b/day_05/src/part_01.rs @@ -0,0 +1,49 @@ +use std::io::Result; + +use crate::Input; + +pub fn main((stacks, commands): &Input) -> Result { + let mut stacks = stacks.to_owned(); + for (n, from, to) in commands.to_owned() { + let from_stack = stacks.get_mut(&from).unwrap(); + let mut new_vals: Vec = vec![]; + for _ in 1..=n { + let val = from_stack.pop_front().unwrap(); + new_vals.push(val); + } + + new_vals + .iter() + .for_each(|val| stacks.get_mut(&to).unwrap().push_front(val.to_owned())); + } + + Ok(stacks + .into_iter() + .map(|(_, stack)| stack.front().unwrap().to_owned()) + .collect::()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + [D] + [N] [C] + [Z] [M] [P] + 1 2 3 + + move 1 from 2 to 1 + move 3 from 1 to 3 + move 2 from 2 to 1 + move 1 from 1 to 2 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, "CMZ"); + Ok(()) + } +} diff --git a/day_05/src/part_02.rs b/day_05/src/part_02.rs new file mode 100644 index 0000000..c4e5fa4 --- /dev/null +++ b/day_05/src/part_02.rs @@ -0,0 +1,52 @@ +use std::io::Result; + +use crate::Input; + +pub fn main((stacks, commands): &Input) -> Result { + let mut stacks = stacks.to_owned(); + + for (n, from, to) in commands.to_owned() { + let from_stack = stacks.get_mut(&from).unwrap(); + let mut new_vals: Vec = vec![]; + for _ in 1..=n { + let val = from_stack.pop_front().unwrap(); + new_vals.push(val); + } + + new_vals.reverse(); + + new_vals + .iter() + .for_each(|val| stacks.get_mut(&to).unwrap().push_front(val.to_owned())); + } + + Ok(stacks + .into_iter() + .map(|(_, stack)| stack.front().unwrap().to_owned()) + .collect::()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + [D] + [N] [C] + [Z] [M] [P] + 1 2 3 + + move 1 from 2 to 1 + move 3 from 1 to 3 + move 2 from 2 to 1 + move 1 from 1 to 2 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, "MCD"); + Ok(()) + } +} diff --git a/input/day_05 b/input/day_05 new file mode 100644 index 0000000..cf16741 --- /dev/null +++ b/input/day_05 @@ -0,0 +1,513 @@ + [M] [V] [L] +[G] [V] [C] [G] [D] +[J] [Q] [W] [Z] [C] [J] +[W] [W] [G] [V] [D] [G] [C] +[R] [G] [N] [B] [D] [C] [M] [W] +[F] [M] [H] [C] [S] [T] [N] [N] [N] +[T] [W] [N] [R] [F] [R] [B] [J] [P] +[Z] [G] [J] [J] [W] [S] [H] [S] [G] + 1 2 3 4 5 6 7 8 9 + +move 1 from 5 to 2 +move 7 from 7 to 1 +move 1 from 1 to 7 +move 1 from 4 to 1 +move 7 from 9 to 1 +move 1 from 3 to 7 +move 4 from 5 to 4 +move 6 from 4 to 9 +move 2 from 7 to 6 +move 6 from 8 to 2 +move 2 from 4 to 5 +move 2 from 3 to 7 +move 11 from 1 to 4 +move 6 from 6 to 1 +move 3 from 5 to 3 +move 5 from 9 to 8 +move 1 from 2 to 3 +move 2 from 7 to 9 +move 7 from 1 to 2 +move 1 from 5 to 3 +move 1 from 5 to 3 +move 5 from 8 to 5 +move 3 from 5 to 4 +move 1 from 1 to 7 +move 1 from 3 to 8 +move 2 from 6 to 3 +move 3 from 3 to 4 +move 1 from 6 to 2 +move 5 from 4 to 2 +move 2 from 5 to 3 +move 2 from 7 to 1 +move 1 from 8 to 1 +move 7 from 1 to 7 +move 4 from 4 to 2 +move 7 from 4 to 1 +move 10 from 1 to 5 +move 10 from 5 to 2 +move 11 from 2 to 3 +move 1 from 1 to 6 +move 1 from 4 to 7 +move 4 from 7 to 1 +move 6 from 2 to 5 +move 2 from 1 to 3 +move 1 from 9 to 5 +move 2 from 9 to 6 +move 1 from 6 to 1 +move 3 from 5 to 4 +move 20 from 3 to 9 +move 3 from 7 to 1 +move 3 from 5 to 2 +move 3 from 4 to 8 +move 3 from 1 to 3 +move 3 from 1 to 2 +move 2 from 6 to 1 +move 10 from 9 to 6 +move 6 from 6 to 7 +move 4 from 6 to 3 +move 11 from 2 to 6 +move 1 from 8 to 9 +move 13 from 2 to 3 +move 1 from 1 to 9 +move 1 from 9 to 4 +move 1 from 8 to 2 +move 1 from 8 to 2 +move 4 from 7 to 8 +move 8 from 6 to 9 +move 3 from 2 to 3 +move 3 from 8 to 4 +move 11 from 9 to 2 +move 7 from 9 to 6 +move 1 from 1 to 5 +move 4 from 4 to 9 +move 21 from 3 to 1 +move 1 from 3 to 9 +move 7 from 6 to 3 +move 6 from 1 to 2 +move 13 from 1 to 5 +move 2 from 1 to 2 +move 3 from 9 to 3 +move 2 from 2 to 3 +move 2 from 6 to 4 +move 3 from 3 to 5 +move 13 from 5 to 2 +move 5 from 3 to 4 +move 2 from 7 to 9 +move 2 from 4 to 2 +move 1 from 3 to 8 +move 1 from 6 to 1 +move 4 from 3 to 7 +move 2 from 5 to 7 +move 1 from 7 to 2 +move 1 from 5 to 9 +move 4 from 7 to 8 +move 1 from 1 to 9 +move 6 from 8 to 1 +move 4 from 4 to 8 +move 25 from 2 to 9 +move 1 from 4 to 3 +move 1 from 3 to 7 +move 4 from 8 to 1 +move 1 from 7 to 4 +move 3 from 1 to 6 +move 5 from 2 to 1 +move 1 from 5 to 1 +move 1 from 4 to 1 +move 24 from 9 to 6 +move 9 from 1 to 6 +move 1 from 5 to 6 +move 1 from 1 to 9 +move 1 from 2 to 8 +move 1 from 8 to 1 +move 3 from 1 to 8 +move 36 from 6 to 3 +move 2 from 7 to 3 +move 1 from 2 to 5 +move 1 from 5 to 2 +move 1 from 6 to 2 +move 10 from 3 to 2 +move 3 from 8 to 2 +move 1 from 1 to 7 +move 2 from 2 to 6 +move 10 from 9 to 1 +move 2 from 6 to 4 +move 13 from 3 to 4 +move 8 from 3 to 7 +move 8 from 1 to 2 +move 5 from 3 to 8 +move 3 from 1 to 9 +move 1 from 7 to 1 +move 7 from 4 to 5 +move 1 from 1 to 2 +move 14 from 2 to 6 +move 2 from 7 to 2 +move 8 from 4 to 8 +move 3 from 7 to 9 +move 2 from 9 to 8 +move 2 from 7 to 1 +move 1 from 7 to 8 +move 1 from 6 to 8 +move 1 from 9 to 3 +move 4 from 2 to 7 +move 6 from 6 to 1 +move 3 from 1 to 9 +move 1 from 1 to 7 +move 6 from 5 to 6 +move 1 from 5 to 2 +move 1 from 6 to 8 +move 5 from 7 to 5 +move 1 from 2 to 9 +move 2 from 3 to 4 +move 9 from 8 to 4 +move 8 from 4 to 8 +move 6 from 6 to 7 +move 5 from 6 to 4 +move 7 from 9 to 7 +move 7 from 8 to 7 +move 5 from 8 to 4 +move 3 from 1 to 6 +move 1 from 2 to 7 +move 1 from 1 to 4 +move 4 from 5 to 2 +move 2 from 6 to 9 +move 1 from 3 to 7 +move 1 from 5 to 1 +move 1 from 8 to 9 +move 1 from 6 to 1 +move 1 from 2 to 7 +move 2 from 8 to 1 +move 2 from 1 to 8 +move 3 from 2 to 4 +move 1 from 6 to 1 +move 17 from 4 to 1 +move 3 from 2 to 7 +move 13 from 7 to 8 +move 1 from 2 to 6 +move 14 from 1 to 4 +move 2 from 8 to 5 +move 1 from 9 to 7 +move 2 from 5 to 4 +move 1 from 9 to 3 +move 5 from 1 to 5 +move 3 from 4 to 1 +move 1 from 3 to 2 +move 7 from 4 to 5 +move 9 from 7 to 8 +move 5 from 4 to 2 +move 1 from 1 to 3 +move 1 from 9 to 2 +move 15 from 8 to 6 +move 1 from 3 to 7 +move 11 from 6 to 5 +move 1 from 4 to 8 +move 3 from 1 to 7 +move 5 from 7 to 5 +move 27 from 5 to 1 +move 8 from 8 to 4 +move 1 from 2 to 6 +move 3 from 6 to 1 +move 9 from 1 to 5 +move 5 from 5 to 7 +move 2 from 2 to 1 +move 2 from 5 to 4 +move 6 from 7 to 6 +move 1 from 5 to 2 +move 1 from 7 to 8 +move 4 from 6 to 8 +move 5 from 6 to 3 +move 1 from 7 to 1 +move 5 from 4 to 3 +move 6 from 8 to 2 +move 1 from 7 to 8 +move 2 from 8 to 9 +move 10 from 3 to 5 +move 9 from 5 to 2 +move 3 from 4 to 8 +move 1 from 5 to 7 +move 2 from 9 to 7 +move 2 from 8 to 3 +move 1 from 3 to 8 +move 19 from 1 to 7 +move 4 from 2 to 7 +move 2 from 4 to 3 +move 3 from 3 to 2 +move 2 from 8 to 3 +move 2 from 5 to 8 +move 1 from 2 to 3 +move 2 from 8 to 3 +move 5 from 2 to 5 +move 9 from 7 to 5 +move 13 from 5 to 9 +move 7 from 2 to 6 +move 2 from 6 to 9 +move 1 from 2 to 1 +move 5 from 6 to 7 +move 1 from 5 to 7 +move 6 from 1 to 2 +move 5 from 3 to 6 +move 6 from 7 to 2 +move 3 from 6 to 4 +move 3 from 7 to 4 +move 12 from 7 to 6 +move 5 from 4 to 1 +move 2 from 7 to 4 +move 3 from 4 to 6 +move 16 from 6 to 3 +move 4 from 1 to 4 +move 1 from 1 to 9 +move 3 from 9 to 2 +move 1 from 4 to 6 +move 9 from 3 to 7 +move 2 from 6 to 3 +move 3 from 3 to 9 +move 15 from 2 to 7 +move 19 from 7 to 4 +move 15 from 9 to 2 +move 16 from 2 to 8 +move 6 from 3 to 5 +move 4 from 7 to 5 +move 15 from 8 to 7 +move 19 from 4 to 2 +move 1 from 8 to 3 +move 16 from 2 to 1 +move 9 from 7 to 6 +move 7 from 2 to 8 +move 2 from 2 to 7 +move 1 from 9 to 5 +move 1 from 3 to 4 +move 6 from 1 to 2 +move 8 from 5 to 1 +move 1 from 5 to 1 +move 18 from 1 to 8 +move 7 from 7 to 5 +move 7 from 5 to 3 +move 4 from 3 to 6 +move 13 from 8 to 5 +move 12 from 8 to 1 +move 5 from 1 to 6 +move 15 from 5 to 4 +move 1 from 1 to 6 +move 12 from 6 to 3 +move 8 from 3 to 4 +move 2 from 7 to 3 +move 9 from 3 to 1 +move 5 from 2 to 9 +move 16 from 4 to 3 +move 10 from 1 to 3 +move 2 from 1 to 5 +move 1 from 3 to 1 +move 5 from 6 to 1 +move 4 from 9 to 3 +move 1 from 2 to 8 +move 1 from 8 to 1 +move 1 from 9 to 8 +move 2 from 5 to 9 +move 9 from 4 to 1 +move 3 from 1 to 3 +move 2 from 6 to 8 +move 3 from 8 to 5 +move 2 from 1 to 5 +move 2 from 9 to 8 +move 1 from 8 to 6 +move 2 from 5 to 3 +move 19 from 3 to 1 +move 2 from 4 to 2 +move 1 from 5 to 6 +move 2 from 2 to 3 +move 1 from 8 to 6 +move 8 from 3 to 9 +move 6 from 3 to 7 +move 2 from 6 to 2 +move 1 from 6 to 1 +move 1 from 1 to 8 +move 1 from 8 to 9 +move 1 from 7 to 3 +move 19 from 1 to 5 +move 21 from 5 to 2 +move 13 from 2 to 6 +move 13 from 1 to 8 +move 7 from 9 to 7 +move 2 from 9 to 2 +move 10 from 8 to 3 +move 1 from 1 to 6 +move 10 from 2 to 4 +move 11 from 3 to 5 +move 8 from 5 to 6 +move 1 from 3 to 7 +move 2 from 8 to 6 +move 2 from 2 to 8 +move 3 from 7 to 6 +move 2 from 8 to 6 +move 1 from 1 to 2 +move 24 from 6 to 5 +move 2 from 3 to 8 +move 1 from 8 to 6 +move 7 from 7 to 9 +move 4 from 6 to 9 +move 1 from 8 to 9 +move 21 from 5 to 9 +move 2 from 7 to 2 +move 1 from 8 to 5 +move 1 from 7 to 3 +move 12 from 9 to 6 +move 6 from 6 to 3 +move 12 from 9 to 4 +move 4 from 5 to 6 +move 13 from 4 to 2 +move 8 from 4 to 8 +move 10 from 6 to 8 +move 11 from 8 to 9 +move 4 from 8 to 4 +move 2 from 4 to 3 +move 8 from 3 to 8 +move 2 from 6 to 8 +move 1 from 3 to 8 +move 6 from 2 to 4 +move 1 from 4 to 8 +move 1 from 9 to 7 +move 13 from 8 to 4 +move 1 from 7 to 1 +move 1 from 1 to 4 +move 8 from 4 to 7 +move 3 from 5 to 7 +move 19 from 9 to 7 +move 3 from 2 to 7 +move 1 from 8 to 2 +move 13 from 7 to 6 +move 1 from 2 to 4 +move 4 from 6 to 2 +move 1 from 8 to 3 +move 7 from 6 to 8 +move 1 from 6 to 2 +move 1 from 2 to 7 +move 9 from 2 to 3 +move 1 from 6 to 2 +move 21 from 7 to 5 +move 9 from 5 to 3 +move 19 from 3 to 9 +move 5 from 8 to 5 +move 2 from 2 to 1 +move 2 from 1 to 8 +move 6 from 4 to 5 +move 3 from 8 to 7 +move 15 from 9 to 2 +move 2 from 2 to 5 +move 3 from 9 to 6 +move 5 from 4 to 5 +move 11 from 2 to 6 +move 1 from 8 to 6 +move 1 from 9 to 5 +move 1 from 7 to 3 +move 6 from 5 to 6 +move 1 from 4 to 6 +move 1 from 3 to 4 +move 13 from 5 to 2 +move 16 from 6 to 9 +move 4 from 4 to 5 +move 2 from 6 to 2 +move 2 from 6 to 4 +move 2 from 4 to 5 +move 2 from 7 to 8 +move 2 from 6 to 3 +move 2 from 5 to 8 +move 14 from 5 to 7 +move 4 from 8 to 1 +move 4 from 1 to 6 +move 1 from 3 to 9 +move 1 from 6 to 1 +move 2 from 7 to 3 +move 2 from 3 to 7 +move 2 from 5 to 2 +move 9 from 9 to 2 +move 13 from 7 to 3 +move 12 from 3 to 9 +move 2 from 6 to 8 +move 14 from 2 to 9 +move 2 from 8 to 9 +move 10 from 2 to 1 +move 1 from 7 to 4 +move 2 from 3 to 8 +move 4 from 2 to 1 +move 1 from 8 to 3 +move 1 from 2 to 6 +move 1 from 8 to 3 +move 4 from 9 to 4 +move 1 from 3 to 5 +move 1 from 5 to 1 +move 1 from 3 to 9 +move 12 from 1 to 8 +move 10 from 8 to 5 +move 7 from 5 to 6 +move 1 from 1 to 9 +move 3 from 5 to 1 +move 1 from 1 to 3 +move 16 from 9 to 7 +move 4 from 4 to 3 +move 1 from 4 to 9 +move 15 from 7 to 8 +move 15 from 9 to 1 +move 8 from 1 to 6 +move 1 from 9 to 3 +move 17 from 6 to 2 +move 1 from 9 to 1 +move 15 from 2 to 7 +move 14 from 8 to 9 +move 12 from 7 to 9 +move 12 from 9 to 3 +move 3 from 7 to 9 +move 1 from 7 to 4 +move 7 from 9 to 6 +move 1 from 4 to 6 +move 11 from 9 to 6 +move 2 from 1 to 2 +move 18 from 6 to 4 +move 4 from 2 to 7 +move 2 from 7 to 3 +move 2 from 7 to 8 +move 4 from 1 to 5 +move 1 from 9 to 2 +move 2 from 5 to 4 +move 5 from 1 to 3 +move 2 from 3 to 7 +move 2 from 3 to 9 +move 1 from 6 to 7 +move 1 from 2 to 9 +move 2 from 8 to 1 +move 3 from 1 to 3 +move 2 from 5 to 8 +move 2 from 3 to 5 +move 1 from 5 to 2 +move 1 from 1 to 3 +move 1 from 9 to 2 +move 1 from 9 to 1 +move 3 from 7 to 6 +move 1 from 1 to 9 +move 2 from 8 to 9 +move 1 from 2 to 3 +move 2 from 8 to 2 +move 2 from 6 to 5 +move 1 from 8 to 5 +move 3 from 2 to 5 +move 3 from 4 to 8 +move 1 from 8 to 2 +move 3 from 9 to 7 +move 3 from 7 to 1 +move 1 from 9 to 6 +move 3 from 1 to 2 +move 2 from 8 to 7 +move 2 from 7 to 9 +move 2 from 6 to 5 +move 3 from 5 to 3 +move 1 from 2 to 5 +move 3 from 2 to 7 +move 2 from 5 to 6 +move 15 from 4 to 9 +move 1 from 3 to 1 +move 25 from 3 to 4 +move 3 from 7 to 3 +move 5 from 9 to 5 +move 10 from 9 to 5 +move 9 from 5 to 1 +move 5 from 5 to 2 +move 1 from 6 to 7 +move 5 from 5 to 8 From 7a022bbe5b134ee34b5cf063c546c09c0c9083ac Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Tue, 6 Dec 2022 08:40:06 +0100 Subject: [PATCH 10/25] feat(day_06): should be simplified, but works --- README.md | 2 +- day_06/Cargo.toml | 15 ++++++++++++++ day_06/src/lib.rs | 27 ++++++++++++++++++++++++ day_06/src/main.rs | 12 +++++++++++ day_06/src/part_01.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ day_06/src/part_02.rs | 48 +++++++++++++++++++++++++++++++++++++++++++ input/day_06 | 1 + 7 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 day_06/Cargo.toml create mode 100644 day_06/src/lib.rs create mode 100644 day_06/src/main.rs create mode 100644 day_06/src/part_01.rs create mode 100644 day_06/src/part_02.rs create mode 100644 input/day_06 diff --git a/README.md b/README.md index 2b97453..28a5863 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ - [Day 3](https://github.com/ankjevel/adventofcode/tree/2022/day_03) ⭐️ ⭐️ - [Day 4](https://github.com/ankjevel/adventofcode/tree/2022/day_04) ⭐️ ⭐️ - [Day 5](https://github.com/ankjevel/adventofcode/tree/2022/day_05) ⭐️ ⭐️ -- [Day 6](#) +- [Day 6](https://github.com/ankjevel/adventofcode/tree/2022/day_06) ⭐️ ⭐️ - [Day 7](#) - [Day 8](#) - [Day 9](#) diff --git a/day_06/Cargo.toml b/day_06/Cargo.toml new file mode 100644 index 0000000..a22aa97 --- /dev/null +++ b/day_06/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_06" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_06" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_06/src/lib.rs b/day_06/src/lib.rs new file mode 100644 index 0000000..cfe84bf --- /dev/null +++ b/day_06/src/lib.rs @@ -0,0 +1,27 @@ +use std::collections::VecDeque; + +pub mod part_01; +pub mod part_02; + +pub type Input = VecDeque; + +pub fn parse_input(input: &str) -> Input { + input.trim().chars().collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + example + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + VecDeque::from(['e', 'x', 'a', 'm', 'p', 'l', 'e']) + ); + } +} diff --git a/day_06/src/main.rs b/day_06/src/main.rs new file mode 100644 index 0000000..1209313 --- /dev/null +++ b/day_06/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_06::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_06")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_06/src/part_01.rs b/day_06/src/part_01.rs new file mode 100644 index 0000000..947867a --- /dev/null +++ b/day_06/src/part_01.rs @@ -0,0 +1,48 @@ +use std::{ + collections::{HashSet, VecDeque}, + io::Result, +}; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + let mut markers: VecDeque = VecDeque::new(); + let mut completed_at = 0; + for (i, char) in input.to_owned().into_iter().enumerate() { + markers.push_front(char.to_owned()); + + if markers.len() > 4 { + markers.pop_back(); + } + + let set: HashSet = HashSet::from_iter(markers.clone().into_iter()); + if set.len() != 4 { + continue; + } + + completed_at = i + 1; + break; + } + + Ok(completed_at) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + let parse = |input: &str| -> Result { main(&parse_input(&input)) }; + + assert_eq!(parse(&"mjqjpqmgbljsphdztnvjfqwrcgsmlb")?, 7); + assert_eq!(parse(&"bvwbjplbgvbhsrlpgdmjqwftvncz")?, 5); + assert_eq!(parse(&"nppdvjthqldpwncqszvftbrmjlhg")?, 6); + assert_eq!(parse(&"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg")?, 10); + assert_eq!(parse(&"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw")?, 11); + + Ok(()) + } +} diff --git a/day_06/src/part_02.rs b/day_06/src/part_02.rs new file mode 100644 index 0000000..1aa138a --- /dev/null +++ b/day_06/src/part_02.rs @@ -0,0 +1,48 @@ +use std::{ + collections::{HashSet, VecDeque}, + io::Result, +}; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + let mut markers: VecDeque = VecDeque::new(); + let mut completed_at = 0; + for (i, char) in input.to_owned().into_iter().enumerate() { + markers.push_front(char.to_owned()); + + if markers.len() > 14 { + markers.pop_back(); + } + + let set: HashSet = HashSet::from_iter(markers.clone().into_iter()); + if set.len() != 14 { + continue; + } + + completed_at = i + 1; + break; + } + + Ok(completed_at) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + let parse = |input: &str| -> Result { main(&parse_input(&input)) }; + + assert_eq!(parse(&"mjqjpqmgbljsphdztnvjfqwrcgsmlb")?, 19); + assert_eq!(parse(&"bvwbjplbgvbhsrlpgdmjqwftvncz")?, 23); + assert_eq!(parse(&"nppdvjthqldpwncqszvftbrmjlhg")?, 23); + assert_eq!(parse(&"nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg")?, 29); + assert_eq!(parse(&"zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw")?, 26); + + Ok(()) + } +} diff --git a/input/day_06 b/input/day_06 new file mode 100644 index 0000000..534b221 --- /dev/null +++ b/input/day_06 @@ -0,0 +1 @@ +srjsssgppnssqzszmzjmjdmmqwqcwqccslsjswjssgbsgsnggqtqnnjznndtndnhddfldfdsfswsjsjjptjpttlwlccpnpgngcncnscncsnnwrwmwggsgdgssvbsstzsstqqrjqqlsqlsqscschhclhccznzcnnzzqppjbpjjqzzbwbqqzdqdjjqtjtbtgtqgtgdgwwsjjbwjjzssrsvsttgqtgglgplgpllcrcncssbrbgrbrllsqlsltthshrrnddwzdzdbbrppdvdqvvpvdvdsslbbmnbnjjrdrnntzzftzftttrsshpspvvrzzqzwzhzszbzjjwqwvqqglgflfwfrfprpddfvdffhzztwtppwwwztzsznzmnnbvbbtqqdhqdqdfffnjnmnlmljmlmnlnddzbbdgdqdnqqtjjtnthttvwwtrwwwgffqcfchfcfwcczhhgjhghpgpwgwffhghzghhtftntnnmlnmlmddqbbfwbfwbwvvfssrggptpltpllbcbzczzlccjgcgvcczjzvvvdtvdvqdqwdqdvdjvddjfdjjqmmfgfsftfzfwzwzfftmfmmqdqccjqcqwwflwlccpssvzzzbnbjnbjjgtgpttbwbrbwrwvrrwccgrgrvvpjvvznvvhddtdldpldppmlplltdllhqhpqhqqbpptgpgjgqjgjffvfdvdttsrsllhzlzrrdrnrjrddqbbjrrvrsstgsttjqjhjbhhnmndmmnrmnnrggmtgmmhshjjfqjjtqtstpstppggtzzrsrwwqffvzvzvqqggqsqtstszttpvtvvvddcncvncvchhnsnpsnnlmnnmqnqjjfwfccmwmbwbswbswwghhzrrptptbbjbtjjgdjgjrgrwgwlglblbnlnbbwnnpbnbmbtmtvvhjjlwlhhszslldwlwdllhjhghqghhqpplhhtjhhnhqhlhjhmmlhlmmrpptvvcczfcccwgcccpgpspdpccfhchffbjbzzwppfbbstbtltpllcssnnctnnqcqhhclhccvlvlffnjjwbwfbfttwvvdvnnnsrstrtmtfthtzzcvzvhzzpmpfpmmmwggdbdqdbbjnbbdqbbrnnnprrwbbcwbbpjjprjjzzvwwvrrthhqvhvnhhzmzszrszsjssclcjjbhjbhjbhjbjhhzbbzttnrnssrbrjbrjjmsmffdlflfzfbzzmtztdthdhldhlddnccgbbmtbbsbzsbzzcsszpzfffprfpfzfrzzhvhffsmmqtjwgjbzhnmrslrmgfjpqcllcgsjdhrshqtlgmqqtfmswfzwtnrswtzdjzclcfmltqgdhcsgvzrdltgfbtqclpppvbqnbqmlhmdbsjbwbdllzpnrwfhmnlgvdwsdjsznnhqzhwntjvcpzdrfwmwwdrttdvzspmbmqggmlmsvwgcjgpvcmplqwfjgpghnfpbctnfhcgngcbdmzhlpcnjpmczzsgfgrdftrzgvmmpdmgcrpcdrjsgczpfjnwpdjpntdngdjwctvcbsjmfwvtsrlhvpswppmfwrwzsbsgbjvljzqjjldmnqnmwsmqnmhmqhhttppbpqlvdcvdbhmbnjzztjrdjdzlvmbdrghtftwdpcwwblsjbgnzwtpztmtmnrpsvzfzncqmrvhcbqcvqvnlngdcllrqlbhjttnmjmhfhdzmjmplcfdqpmwblzsnmpczwcnggcwdvgnjcrrtmpwwqdqpvtbzpdbfnbfcfllntqjlslcsznjvzvsbntrtzwhcbtdmbmwttvhdvdtvrcmprcrrjlgsqddrsmwsrbtbpjrmlbrnsdrjfhjnqjtgjmhzjbjnprvmhtjcdbztwqmrlfflfmcslshtwmwhgvbdslgjhzjlglhllsdphlzngjfwfrlwpnqfhghnqhzhgrszbcwjvlrtmshntszsqplvfbccjwctgtfmqgqjdlgdbwhgvctqtcgfdwvqdwqzddmsbrpftzpqztgzbnplvhftmgpdthnrdhqbltbrmhpcqsfccmqzwmrbnbgbjspslwpjhdqspssqdtnssmjmvzwwfstgzzjrmfczdlznwqpdhbsjqddvffcgfhfdqdrlwcsgcsszdtpqbbsthpwbhdfzmgmcdggfcwcmzfjnfbzbccjhvwhbwfslnqnrwhgrwtlnmrmncnjtbjbdlmqsczppgbcmsdrwlrpjbgrmnhqqfhhsdhmdmpvvpjrnzsvzctmqhpzcvcjfgtlfvqvnvlnprmgrsvrrvtjfndqfsvqdsfbcwlbglmfhfhcfgqdfmclnzhdtppgzzsqgjqncqrbdhlhdjqwjpmbdnfmgdgwbwmnlngnmrhcgqwzvmbjzdvsspjwwdtpnvpftdlqlzfgtscfczsvbrtrqqpgqlvmrtddqplbzsswbgpdzpqfvqpqbndblghmdhmnctdnjbgglmrlvmrmsfgntfdwvqcvvlvbcnwrctvjqhmnsjqccwltbfqpqpmwsfvmnnfqmlmlcchqcdtbvqwcpptvfwrwtbdrlsgnwpmjgnwlprzqjlwqmtmjglbrzlgfbsghwqdmwhrcmfwdmzmflsbngtgndftdpzsqvgqdsfdhplmfcmwpbtvcdmpghmfwqjvhdhfpmbrqpvnbhlftgdtprlztrgnlcldfpjqjqdfrvqtcnzrtjcgzgsslzghlnfhwwjwzdsmpsczclrfmnqjvfmvsqpntsnnnlrfswqtrppzhqgjzlzvrrbhhfhchhvgztpgctcsgssvttszsrdwzwrbmwmspgqhmmfnzqqdbmnbltdmrsvqgddltwczbbjcmplncspgqgmzrndhttsrbvqbpbvhshfqrpqgmmdbhmmtccjcmntmpqhrvhnfnlqqbctsnfzjbphhqwmztgbhqqlbctbsfcszbggzrlcdhwddtjgtqhppzgjsqcddwjsngjrcdflmgwgfnzhjtcwgbqvpwmpgcpdwvqgswwfzcnjgmdpffmqczmsqgpthpmsjlwnrcbzrfshvwftzllwrmfccmlpjnmpjdfpcvjgjpznllmqjwpcflgqgdljtbbjvjlvhhmtvzfnjfnnwrvtlfdbhqphrjghtmlsrplqscsnvjvqdslsbsfzzrjfmchplzgjgdqvzhfphvsjfvnqlgmjfzhrdlmmvfntnzdvrnwqshsmtjnqmwzgpbbzszrsqcvlzjwnmgjhmfqrbvgmfqpswctmvpfcghvdqgstglmzvpfhzfzvhqqdmvrvrttlpwwhqgddzqlrvvdffqtznvlfgjhhmvbmtzjqnnhqzrtbzpqcwpngrdcndcgzwhzgtfwbmwbrpvvczczhwcqwsqzqbqvqftcswtcbzdbpccjhtwbpnwlwwwqwscptlwshrdbmmdcgrmpnnwgjwzszwwdwctfspbfqvdjqtrflshrqlbfgpnrmbszwjpcdzbggphgplcgvwljprzmtncsvfwqchttndhpnzmtdvtjqtcsddtqvcmztmjgwqvjhflrjjtnvfpjrnlvzvwvrpbhrzslmrqqzqhnzqnvtqppmncddphbwwsjczmphsrlltzndtqjgdlqgpnlfcvwntstmrgcrjzmmllpwldnwmwzpvzctmhszspcvtgnqthszzsmtdnzwtfddfctpjhscbqgwfpqmzpvqrzvtbrdjzrqprdgbpmzzfbgqcvcdtsfrffcpqwtvdwvtcqlcsdrzntgrhrspznndslmnvptlphpdqgbblfhmgbpmmfwqzlhvzshhpzgfjldqclngbcbrmmnqqvqmwdnjsglsggqfgjldqfbsqgtrwmpdffqlcwwlfhlpqfgwtssnjwzhgvtwqzmhmgwzwmcggmpmrzqrcsmflqsrbnzvdmjcdbnscstqrqhvddsbjpzwsvzswqhcqmgzlvfcnzjrrffzphmrvdbhqbrwpsfqvfqwhqhcgfvfsfttzcdsrjgjwcgvhllszmplmvgczqsbfldnbvrnqccbprjjdwhmqpdjjrnfdlhzdvlfmrldjlqclbjrrtjfsflphzdcdpfpr From f0c007c7d93afa59aee1fbb5e6f2991dfe6f3ed5 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Wed, 7 Dec 2022 09:37:01 +0100 Subject: [PATCH 11/25] feat(day_07): somewhat sloppy solution --- README.md | 2 +- day_07/Cargo.toml | 15 + day_07/src/lib.rs | 27 + day_07/src/main.rs | 12 + day_07/src/part_01.rs | 83 +++ day_07/src/part_02.rs | 87 ++++ input/day_07 | 1109 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 1334 insertions(+), 1 deletion(-) create mode 100644 day_07/Cargo.toml create mode 100644 day_07/src/lib.rs create mode 100644 day_07/src/main.rs create mode 100644 day_07/src/part_01.rs create mode 100644 day_07/src/part_02.rs create mode 100644 input/day_07 diff --git a/README.md b/README.md index 28a5863..6770d34 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ - [Day 4](https://github.com/ankjevel/adventofcode/tree/2022/day_04) ⭐️ ⭐️ - [Day 5](https://github.com/ankjevel/adventofcode/tree/2022/day_05) ⭐️ ⭐️ - [Day 6](https://github.com/ankjevel/adventofcode/tree/2022/day_06) ⭐️ ⭐️ -- [Day 7](#) +- [Day 7](https://github.com/ankjevel/adventofcode/tree/2022/day_07) ⭐️ ⭐️ - [Day 8](#) - [Day 9](#) - [Day 10](#) diff --git a/day_07/Cargo.toml b/day_07/Cargo.toml new file mode 100644 index 0000000..8dbc02a --- /dev/null +++ b/day_07/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_07" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_07" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_07/src/lib.rs b/day_07/src/lib.rs new file mode 100644 index 0000000..4949d40 --- /dev/null +++ b/day_07/src/lib.rs @@ -0,0 +1,27 @@ +pub mod part_01; +pub mod part_02; + +pub type Input = Vec; + +pub fn parse_input(input: &str) -> Input { + input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .map(str::to_owned) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + example + "; + + #[test] + fn it_parses_example() { + assert_eq!(parse_input(&EXAMPLE_DATA), vec!["example"]); + } +} diff --git a/day_07/src/main.rs b/day_07/src/main.rs new file mode 100644 index 0000000..6530793 --- /dev/null +++ b/day_07/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_07::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_07")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_07/src/part_01.rs b/day_07/src/part_01.rs new file mode 100644 index 0000000..715cd2a --- /dev/null +++ b/day_07/src/part_01.rs @@ -0,0 +1,83 @@ +use std::{ + collections::{HashMap, VecDeque}, + io::Result, +}; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + let mut path: VecDeque = VecDeque::new(); + let mut dirs: HashMap = HashMap::new(); + + for row in input.into_iter() { + if row.starts_with('$') { + if row.contains("$ cd") { + let next_dir = row.replace("$ cd ", ""); + if next_dir.contains("..") { + path.pop_back(); + } else { + path.push_back(if next_dir.contains('/') { + next_dir + } else { + format!("{}/", next_dir) + }); + } + } + } else { + if !row.starts_with("dir ") { + let n: Vec<&str> = row.split(' ').collect(); + let size = n[0].parse::().unwrap(); + let mut current: Vec = vec![]; + for path in path.clone().into_iter() { + current.push(path); + let path = current.join(""); + *dirs.entry(path).or_insert(0) += size; + } + } + } + } + + Ok(dirs + .into_values() + .filter(|val| val <= &100_000) + .sum::()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + $ cd / + $ ls + dir a + 14848514 b.txt + 8504156 c.dat + dir d + $ cd a + $ ls + dir e + 29116 f + 2557 g + 62596 h.lst + $ cd e + $ ls + 584 i + $ cd .. + $ cd .. + $ cd d + $ ls + 4060174 j + 8033020 d.log + 5626152 d.ext + 7214296 k + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 95437); + Ok(()) + } +} diff --git a/day_07/src/part_02.rs b/day_07/src/part_02.rs new file mode 100644 index 0000000..99c75eb --- /dev/null +++ b/day_07/src/part_02.rs @@ -0,0 +1,87 @@ +use std::{ + collections::{HashMap, VecDeque}, + io::Result, +}; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + let mut path: VecDeque = VecDeque::new(); + let mut dirs: HashMap = HashMap::new(); + + for row in input.into_iter() { + if row.starts_with('$') { + if row.contains("$ cd") { + let next_dir = row.replace("$ cd ", ""); + if next_dir.contains("..") { + path.pop_back(); + } else { + path.push_back(if next_dir.contains('/') { + next_dir + } else { + format!("{}/", next_dir) + }); + } + } + } else { + if !row.starts_with("dir ") { + let n: Vec<&str> = row.split(' ').collect(); + let size = n[0].parse::().unwrap(); + let mut current: Vec = vec![]; + for path in path.clone().into_iter() { + current.push(path); + let path = current.join(""); + *dirs.entry(path).or_insert(0) += size; + } + } + } + } + + let to_remove = 30_000_000 - (70_000_000 - dirs.get("/").unwrap_or(&0)); + + Ok(dirs + .clone() + .into_values() + .filter(|val| val >= &to_remove) + .min() + .unwrap()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + $ cd / + $ ls + dir a + 14848514 b.txt + 8504156 c.dat + dir d + $ cd a + $ ls + dir e + 29116 f + 2557 g + 62596 h.lst + $ cd e + $ ls + 584 i + $ cd .. + $ cd .. + $ cd d + $ ls + 4060174 j + 8033020 d.log + 5626152 d.ext + 7214296 k + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 24933642); + Ok(()) + } +} diff --git a/input/day_07 b/input/day_07 new file mode 100644 index 0000000..2fff684 --- /dev/null +++ b/input/day_07 @@ -0,0 +1,1109 @@ +$ cd / +$ ls +165965 cmwllbzl.jlm +68612 ggb.qgd +dir gwnwqcgq +dir pdlpwdp +211084 qgcn.rbj +dir sbps +179881 sdpjprfb.lsh +318082 tdhgd.lwf +dir wvdlv +$ cd gwnwqcgq +$ ls +dir btddw +310195 cqsblt.jwb +dir ggb +dir hhdfbj +dir hrj +dir mdhln +dir nwbndtgl +dir pjmc +dir rgb +dir sdpjprfb +169518 tbswl.btw +$ cd btddw +$ ls +315327 hjs.dcw +dir pjmc +99361 pmqmgjsw.rqn +$ cd pjmc +$ ls +227980 cfbfmprt +dir hml +310835 mmcrfwdr.sps +170798 rhgmnqz +dir sdpjprfb +178337 vphwlqqw.dlt +dir wnmh +dir zqcnhs +$ cd hml +$ ls +dir fjtwgcw +194693 ggb +175159 ldbhqdbd +dir mzthvdms +90811 qgbrczz.dhh +118942 qvfdwcpn.cmv +227596 rhgmnqz +dir tnvsdr +dir vplhff +$ cd fjtwgcw +$ ls +16046 gfr +277037 jwpzm.vhn +291671 trpvvs.zgh +$ cd .. +$ cd mzthvdms +$ ls +244911 cqsblt.jwb +37587 gplsqzr.nwn +313958 tqrz.wfd +$ cd .. +$ cd tnvsdr +$ ls +185961 qgbrczz.dhh +85515 wjgvlj.qcq +$ cd .. +$ cd vplhff +$ ls +dir trjdm +$ cd trjdm +$ ls +244126 nhv.vgt +795 vlnwhgsc.tzm +$ cd .. +$ cd .. +$ cd .. +$ cd sdpjprfb +$ ls +dir htvgnrrl +$ cd htvgnrrl +$ ls +240529 cfbfmprt +$ cd .. +$ cd .. +$ cd wnmh +$ ls +dir gccwr +322372 qgbrczz.dhh +dir rlhn +184351 tpfzqcs +$ cd gccwr +$ ls +290656 tswtgpd.jsp +$ cd .. +$ cd rlhn +$ ls +208348 gfr +$ cd .. +$ cd .. +$ cd zqcnhs +$ ls +dir dtfrbzgn +dir phhdmp +11336 pjmc.jwv +5056 qgbrczz.dhh +$ cd dtfrbzgn +$ ls +132783 gbvcnv +dir ghbwbc +298563 pjmc +81684 sdpjprfb +$ cd ghbwbc +$ ls +dir zgvwrms +$ cd zgvwrms +$ ls +296919 tlm.rlb +$ cd .. +$ cd .. +$ cd .. +$ cd phhdmp +$ ls +dir ggb +$ cd ggb +$ ls +316842 jrmgt.mqw +196423 qrb.hpd +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd ggb +$ ls +108630 cqsblt.jwb +dir fdmst +216771 fzvcgf +135624 gfr +dir pdwln +dir sdpjprfb +$ cd fdmst +$ ls +dir gcq +20042 ldgpzcbr +dir nnp +dir pjmc +dir qng +dir sdpjprfb +$ cd gcq +$ ls +132662 lvg.jpb +$ cd .. +$ cd nnp +$ ls +229401 css.qfj +17421 pjmc.hgp +$ cd .. +$ cd pjmc +$ ls +317512 pjmc.qph +$ cd .. +$ cd qng +$ ls +241771 cvlnwltp +91504 qgbrczz.dhh +$ cd .. +$ cd sdpjprfb +$ ls +82422 sdpjprfb.ljt +$ cd .. +$ cd .. +$ cd pdwln +$ ls +dir sdpjprfb +$ cd sdpjprfb +$ ls +285618 ltfcmg.chw +$ cd .. +$ cd .. +$ cd sdpjprfb +$ ls +58655 cqsblt.jwb +$ cd .. +$ cd .. +$ cd hhdfbj +$ ls +2937 pslftrf.nqf +$ cd .. +$ cd hrj +$ ls +25769 bcpl.shg +dir dfdtszr +dir ggb +3722 hdwqmgwf +dir nssjjtp +300324 qrvtsrs +100487 rhgmnqz +dir sdpjprfb +dir tmdtrqsl +248216 tswtgpd.jsp +$ cd dfdtszr +$ ls +dir hfmnrlvj +dir plzr +dir zpspcph +$ cd hfmnrlvj +$ ls +237362 cqsblt.jwb +$ cd .. +$ cd plzr +$ ls +295398 bjw.nlg +$ cd .. +$ cd zpspcph +$ ls +87660 pjmc +$ cd .. +$ cd .. +$ cd ggb +$ ls +dir jtpb +dir rsptqbh +$ cd jtpb +$ ls +249820 bfdl.jmv +dir dggzszwn +dir ggb +dir whms +$ cd dggzszwn +$ ls +233228 lrhwrh.mqm +$ cd .. +$ cd ggb +$ ls +dir tcrjcmq +88734 tsbggqvp.fjl +$ cd tcrjcmq +$ ls +36677 wfdtbgf.hft +$ cd .. +$ cd .. +$ cd whms +$ ls +dir ggb +dir pjmc +dir vtgcdprq +$ cd ggb +$ ls +dir sdpjprfb +$ cd sdpjprfb +$ ls +177878 jhbdqn +$ cd .. +$ cd .. +$ cd pjmc +$ ls +35345 swmcmqq.clm +$ cd .. +$ cd vtgcdprq +$ ls +293766 gfr +$ cd .. +$ cd .. +$ cd .. +$ cd rsptqbh +$ ls +dir csvpjd +239634 fszzvprc.phh +240240 tswtgpd.jsp +dir zjvc +$ cd csvpjd +$ ls +218253 rndllvcd +$ cd .. +$ cd zjvc +$ ls +98002 cqsblt.jwb +$ cd .. +$ cd .. +$ cd .. +$ cd nssjjtp +$ ls +63947 tswtgpd.jsp +$ cd .. +$ cd sdpjprfb +$ ls +dir cgczvvbg +30257 flbhswlb.ccd +dir qdqdv +dir vqhps +$ cd cgczvvbg +$ ls +dir bntjhfd +22048 ggb.jrv +142476 qgbrczz.dhh +dir rchlvjb +dir vfrrnf +164431 wjfll.zdw +$ cd bntjhfd +$ ls +217858 hpt.jll +$ cd .. +$ cd rchlvjb +$ ls +208149 qrdrwtfw.dbd +100353 vhs +$ cd .. +$ cd vfrrnf +$ ls +3071 jdz +$ cd .. +$ cd .. +$ cd qdqdv +$ ls +26265 nvtr.ndw +$ cd .. +$ cd vqhps +$ ls +288263 gfr +317973 gfvsbqc +284249 lqpwzz +dir njstt +dir rbhd +185897 vssw +$ cd njstt +$ ls +313561 jgq.mvw +110742 qgbrczz.dhh +$ cd .. +$ cd rbhd +$ ls +11259 cqsblt.jwb +dir gchzg +286309 ltfcmg.chw +dir qmsntzj +dir sdpjprfb +281719 srfgltg.nrz +83632 tswtgpd.jsp +141859 vrv.sll +dir vsg +dir wbqshdm +$ cd gchzg +$ ls +dir phnz +$ cd phnz +$ ls +212607 mdrgcdl.vfp +$ cd .. +$ cd .. +$ cd qmsntzj +$ ls +87958 gfr +$ cd .. +$ cd sdpjprfb +$ ls +110482 ggb.wnf +$ cd .. +$ cd vsg +$ ls +232499 sdpjprfb +$ cd .. +$ cd wbqshdm +$ ls +dir pjmc +$ cd pjmc +$ ls +273630 pjmc +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd tmdtrqsl +$ ls +139366 sqfj.hrg +$ cd .. +$ cd .. +$ cd mdhln +$ ls +dir bftszdgn +dir crhbc +dir ggb +dir jhmvgjrr +dir pjmc +dir sgpnzv +$ cd bftszdgn +$ ls +206539 cplch.dsb +201607 cqsblt.jwb +207839 gdz.plv +dir ggb +72021 ltfcmg.chw +dir nddh +106046 wvf.zvc +$ cd ggb +$ ls +242347 cqsblt.jwb +272603 ltfcmg.chw +273519 nrrz.dfj +5628 pjmc +$ cd .. +$ cd nddh +$ ls +243243 ltfcmg.chw +$ cd .. +$ cd .. +$ cd crhbc +$ ls +dir ccw +dir ggb +120001 ltfcmg.chw +dir pcqcswz +120308 qgbrczz.dhh +dir sscfn +dir vpsj +$ cd ccw +$ ls +dir fgghhg +dir pjmc +$ cd fgghhg +$ ls +238488 czpsmdm +150925 ggb +134050 qgbrczz.dhh +$ cd .. +$ cd pjmc +$ ls +138343 sdpjprfb.bmh +$ cd .. +$ cd .. +$ cd ggb +$ ls +157435 rffmjwpm +dir sdpjprfb +dir tbd +dir zgmffgdz +$ cd sdpjprfb +$ ls +dir jwjpwbdj +$ cd jwjpwbdj +$ ls +88782 tswtgpd.jsp +$ cd .. +$ cd .. +$ cd tbd +$ ls +203640 vclsjppl.jws +$ cd .. +$ cd zgmffgdz +$ ls +203870 ggb +dir jhcrw +$ cd jhcrw +$ ls +70775 gfr +$ cd .. +$ cd .. +$ cd .. +$ cd pcqcswz +$ ls +260246 zlwc.rnh +$ cd .. +$ cd sscfn +$ ls +75162 bjtwwgf +dir cfthmzh +265717 ggb +230679 gsbj.nzd +17708 ltfcmg.chw +dir pdgwcshp +dir ztrpvlbh +$ cd cfthmzh +$ ls +296793 chgws.qfd +187701 pjmc +29681 qgbrczz.dhh +$ cd .. +$ cd pdgwcshp +$ ls +dir cfbfmprt +dir lqbh +dir rrtpqd +$ cd cfbfmprt +$ ls +dir rwflz +$ cd rwflz +$ ls +33792 gtt.qcl +dir jrblfq +dir rldtppt +$ cd jrblfq +$ ls +224257 cqsblt.jwb +$ cd .. +$ cd rldtppt +$ ls +dir nzgg +$ cd nzgg +$ ls +139560 fhbw.vhm +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd lqbh +$ ls +dir mhdbp +dir nqsz +$ cd mhdbp +$ ls +dir ggb +28344 wtc.cfd +$ cd ggb +$ ls +dir hwdtp +220117 rvdpjmgt.szq +$ cd hwdtp +$ ls +288606 cqsblt.jwb +$ cd .. +$ cd .. +$ cd .. +$ cd nqsz +$ ls +197106 dpjfbs.rvc +55173 pjl +$ cd .. +$ cd .. +$ cd rrtpqd +$ ls +178 pjmc +$ cd .. +$ cd .. +$ cd ztrpvlbh +$ ls +201371 bhb.pmw +188328 ddqjnzvw.rdd +206451 tswtgpd.jsp +$ cd .. +$ cd .. +$ cd vpsj +$ ls +5614 cqsblt.jwb +300968 ctmdnwgt.pgj +$ cd .. +$ cd .. +$ cd ggb +$ ls +dir ggb +14512 hzgq.tsb +302375 pjmc.tlj +dir thhgz +282216 zdsbj +dir zrzmb +$ cd ggb +$ ls +180980 brpqch.plw +42195 ltfcmg.chw +322227 qgbrczz.dhh +$ cd .. +$ cd thhgz +$ ls +74145 cfbfmprt.cmp +253851 gfr +272552 gsb +dir pgfqw +276958 tswtgpd.jsp +$ cd pgfqw +$ ls +193634 ggb.zjg +185688 qrml.bvv +$ cd .. +$ cd .. +$ cd zrzmb +$ ls +dir chjmbq +84603 gfr +dir rbms +253620 sbscbqg.jfg +$ cd chjmbq +$ ls +dir svmvlm +$ cd svmvlm +$ ls +251057 nhjwcjj.dgz +$ cd .. +$ cd .. +$ cd rbms +$ ls +72618 tswtgpd.jsp +$ cd .. +$ cd .. +$ cd .. +$ cd jhmvgjrr +$ ls +189022 cqsblt.jwb +172682 djp.npm +dir gdfgtz +147256 ggb +dir ghv +dir pjmc +110715 rhgmnqz +dir sdpjprfb +dir tjlf +183342 tswtgpd.jsp +$ cd gdfgtz +$ ls +268771 cqsblt.jwb +190140 dgrwz +248802 sdpjprfb.dpw +$ cd .. +$ cd ghv +$ ls +dir ggb +dir npqbngg +304352 qzjtnr.qcf +dir sdpjprfb +$ cd ggb +$ ls +285635 ltfcmg.chw +dir vzfdbtg +$ cd vzfdbtg +$ ls +dir pqmb +$ cd pqmb +$ ls +219019 ggb.nmh +$ cd .. +$ cd .. +$ cd .. +$ cd npqbngg +$ ls +dir cfbfmprt +242286 cqsblt.jwb +dir hrqfqpzr +dir nsnfq +dir rggzmfqm +dir wjdnwg +$ cd cfbfmprt +$ ls +34347 cqsblt.jwb +dir gpnzggqb +dir nvdqw +dir qdtcwm +dir ssgg +$ cd gpnzggqb +$ ls +dir pjmc +dir pqbf +dir qjpwm +192404 tswtgpd.jsp +88344 vdb.rzm +dir zjglfpt +$ cd pjmc +$ ls +dir pjmc +142330 psbppvhn +168892 qgbrczz.dhh +18858 vzzc.mtd +135911 zmjhz.tdv +$ cd pjmc +$ ls +197370 mlf +36218 nwq.njv +$ cd .. +$ cd .. +$ cd pqbf +$ ls +233005 gfr +$ cd .. +$ cd qjpwm +$ ls +129132 qqmrm.jrj +6309 tswtgpd.jsp +$ cd .. +$ cd zjglfpt +$ ls +186963 qgbrczz.dhh +$ cd .. +$ cd .. +$ cd nvdqw +$ ls +147955 fcgrqq +224829 gfr +$ cd .. +$ cd qdtcwm +$ ls +36443 ltfcmg.chw +$ cd .. +$ cd ssgg +$ ls +250574 jddfdj +$ cd .. +$ cd .. +$ cd hrqfqpzr +$ ls +143568 pjmc +dir sdpjprfb +dir shcrrpc +$ cd sdpjprfb +$ ls +88050 rhgmnqz +$ cd .. +$ cd shcrrpc +$ ls +dir pjmc +dir pwnnmpm +$ cd pjmc +$ ls +dir wbj +$ cd wbj +$ ls +196127 gfr +$ cd .. +$ cd .. +$ cd pwnnmpm +$ ls +dir cgghrf +dir rhgmnqz +dir vnbvq +$ cd cgghrf +$ ls +108742 lbpfnccl.mtj +$ cd .. +$ cd rhgmnqz +$ ls +89791 qgbrczz.dhh +$ cd .. +$ cd vnbvq +$ ls +48961 ltfcmg.chw +241813 qgbrczz.dhh +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd nsnfq +$ ls +dir tgdqz +$ cd tgdqz +$ ls +dir ggb +$ cd ggb +$ ls +dir rhgmnqz +$ cd rhgmnqz +$ ls +268661 cfbfmprt.tbl +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd rggzmfqm +$ ls +177501 qqn +55857 sdpjprfb.srj +$ cd .. +$ cd wjdnwg +$ ls +147729 lsjvsmsv +242095 pjmc +237172 sdpjprfb.cwf +dir sqh +203791 tswtgpd.jsp +dir zgfz +$ cd sqh +$ ls +44848 cfbfmprt +214010 cqsblt.jwb +175692 gfr +107978 lgsncr +$ cd .. +$ cd zgfz +$ ls +184114 cfbfmprt.gpf +207186 qgbrczz.dhh +$ cd .. +$ cd .. +$ cd .. +$ cd sdpjprfb +$ ls +73238 hfbvz +$ cd .. +$ cd .. +$ cd pjmc +$ ls +210824 cqsblt.jwb +dir pllr +dir tqqjp +$ cd pllr +$ ls +dir fshjdzp +210365 ltfcmg.chw +dir rfbdg +96591 rhgmnqz.wbc +dir rzzwcb +123059 tswtgpd.jsp +dir zzcw +$ cd fshjdzp +$ ls +143719 sdpjprfb +$ cd .. +$ cd rfbdg +$ ls +242103 wllggqm.wcg +$ cd .. +$ cd rzzwcb +$ ls +dir rhgmnqz +$ cd rhgmnqz +$ ls +80525 cqsblt.jwb +$ cd .. +$ cd .. +$ cd zzcw +$ ls +260646 jtgzqh +$ cd .. +$ cd .. +$ cd tqqjp +$ ls +245089 vszrfcc +$ cd .. +$ cd .. +$ cd sdpjprfb +$ ls +7514 cfbfmprt.dvj +$ cd .. +$ cd tjlf +$ ls +dir pjmc +$ cd pjmc +$ ls +230234 cfbfmprt.mqf +197486 gfr +$ cd .. +$ cd .. +$ cd .. +$ cd pjmc +$ ls +19935 fblsvg.btt +dir fgvv +dir ggb +222906 gjl.zrv +84255 jfqqjzbd +dir pjmc +$ cd fgvv +$ ls +94831 dgn +312078 tswtgpd.jsp +132961 vzbl.mnq +$ cd .. +$ cd ggb +$ ls +dir hfcr +310301 msdr.wnh +$ cd hfcr +$ ls +318041 dnp.lcn +dir mjrhdq +dir qwvfg +$ cd mjrhdq +$ ls +18541 ctrrm.ljc +247841 pjmc +$ cd .. +$ cd qwvfg +$ ls +128875 clvgmp.cvb +138276 cqsblt.jwb +26303 tswtgpd.jsp +$ cd .. +$ cd .. +$ cd .. +$ cd pjmc +$ ls +106609 ltfcmg.chw +$ cd .. +$ cd .. +$ cd sgpnzv +$ ls +218397 ltfcmg.chw +233365 sdpjprfb.mfd +$ cd .. +$ cd .. +$ cd nwbndtgl +$ ls +dir fsdvzvvv +198516 sdpjprfb.shs +$ cd fsdvzvvv +$ ls +209527 ggb +dir llwbm +dir lzz +99039 msgsztv.hnq +104248 rhgmnqz +$ cd llwbm +$ ls +187895 ghr.crq +274721 qpr.gnm +dir qrrs +$ cd qrrs +$ ls +260559 mnmhqww +$ cd .. +$ cd .. +$ cd lzz +$ ls +dir tnmwncgl +dir wrjjmhz +$ cd tnmwncgl +$ ls +301449 hqz +$ cd .. +$ cd wrjjmhz +$ ls +145193 mdv +73533 zjwlt.vqf +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd pjmc +$ ls +40049 cqsblt.jwb +169928 gvwcdhvn +314117 qgbrczz.dhh +$ cd .. +$ cd rgb +$ ls +dir bvwn +57953 cqsblt.jwb +240228 fvdpqr.vdz +173344 ltfcmg.chw +dir pjmc +28301 sdpjprfb.qlh +$ cd bvwn +$ ls +dir cfbfmprt +dir fjmbvhh +dir ggb +dir pdgsf +dir sdpjprfb +$ cd cfbfmprt +$ ls +dir vndbzj +234044 zmlrt.zwb +$ cd vndbzj +$ ls +dir cfbfmprt +$ cd cfbfmprt +$ ls +dir ptzpcqh +$ cd ptzpcqh +$ ls +141535 cqsblt.jwb +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd fjmbvhh +$ ls +244287 gfr +146029 rhgmnqz.bsq +$ cd .. +$ cd ggb +$ ls +279627 tswtgpd.jsp +$ cd .. +$ cd pdgsf +$ ls +dir fjdlhn +21454 qncpwnsw.jnc +49920 sdpjprfb.mmp +318538 tswtgpd.jsp +$ cd fjdlhn +$ ls +dir rhgmnqz +$ cd rhgmnqz +$ ls +209357 wggmwlfm +$ cd .. +$ cd .. +$ cd .. +$ cd sdpjprfb +$ ls +39774 rhgmnqz.wrv +$ cd .. +$ cd .. +$ cd pjmc +$ ls +115169 flhtvnq.gzc +147102 ltfcmg.chw +279604 rnn.cpc +$ cd .. +$ cd .. +$ cd sdpjprfb +$ ls +170481 rhgmnqz +48045 szgwfcl.tlh +110279 tnfdmgfl +$ cd .. +$ cd .. +$ cd pdlpwdp +$ ls +dir fwsbsprp +dir gjjjwznz +dir jzpwnpmc +dir qlvfslzp +dir sdpjprfb +59208 tswtgpd.jsp +$ cd fwsbsprp +$ ls +177666 rhczscsq.gmb +$ cd .. +$ cd gjjjwznz +$ ls +163647 cpft.fvj +60316 gfr +$ cd .. +$ cd jzpwnpmc +$ ls +dir cfbfmprt +dir gpd +46041 ltfcmg.chw +$ cd cfbfmprt +$ ls +16870 cqsblt.jwb +dir slj +$ cd slj +$ ls +291893 gdjb.rfv +$ cd .. +$ cd .. +$ cd gpd +$ ls +159168 qgbrczz.dhh +$ cd .. +$ cd .. +$ cd qlvfslzp +$ ls +dir clnms +102746 ggb.tth +35817 pjmc.hsc +dir sdpjprfb +296043 sgf.ssl +dir sqhbbsbj +134188 wbhbmbm.bpz +153495 zdjmnjt.smn +$ cd clnms +$ ls +dir ggb +$ cd ggb +$ ls +305684 rhgmnqz.bht +$ cd .. +$ cd .. +$ cd sdpjprfb +$ ls +29970 ghsjjc +$ cd .. +$ cd sqhbbsbj +$ ls +150243 pjmc.mzd +$ cd .. +$ cd .. +$ cd sdpjprfb +$ ls +dir cfbfmprt +dir qnhl +$ cd cfbfmprt +$ ls +dir ggb +$ cd ggb +$ ls +158203 jtcfv.mvc +dir qnqcb +$ cd qnqcb +$ ls +71245 cqsblt.jwb +$ cd .. +$ cd .. +$ cd .. +$ cd qnhl +$ ls +dir sdpjprfb +$ cd sdpjprfb +$ ls +306685 cqsblt.jwb +$ cd .. +$ cd .. +$ cd .. +$ cd .. +$ cd sbps +$ ls +251286 cqsblt.jwb +dir pjmc +213946 rhgmnqz.nzh +13484 zhzslc.bvp +$ cd pjmc +$ ls +9892 tswtgpd.jsp +249059 wjb.rwq +$ cd .. +$ cd .. +$ cd wvdlv +$ ls +314303 qlbqgp.njq From 8c169b26d21ab618546060caaa41d845a297f0c0 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Wed, 7 Dec 2022 11:27:17 +0100 Subject: [PATCH 12/25] feat(day_07): removed duplicate code --- day_07/src/lib.rs | 34 ++++++++++++++++++++++++++++++++++ day_07/src/part_01.rs | 40 +++------------------------------------- day_07/src/part_02.rs | 40 +++------------------------------------- 3 files changed, 40 insertions(+), 74 deletions(-) diff --git a/day_07/src/lib.rs b/day_07/src/lib.rs index 4949d40..05a8172 100644 --- a/day_07/src/lib.rs +++ b/day_07/src/lib.rs @@ -1,3 +1,5 @@ +use std::collections::{HashMap, VecDeque}; + pub mod part_01; pub mod part_02; @@ -12,6 +14,38 @@ pub fn parse_input(input: &str) -> Input { .collect() } +pub fn get_dirs(input: &Input) -> HashMap { + let mut path: VecDeque = VecDeque::new(); + let mut dirs: HashMap = HashMap::new(); + + for row in input.into_iter() { + if row.starts_with('$') { + if row.contains("$ cd") { + let next_dir = row.replace("$ cd ", ""); + if next_dir.contains("..") { + path.pop_back(); + } else { + path.push_back(if next_dir.contains('/') { + next_dir + } else { + format!("{}/", next_dir) + }); + } + } + } else if !row.starts_with("dir ") { + let n = row.split(' ').collect::>(); + let size = n[0].parse::().unwrap(); + let mut current = vec![]; + for path in path.clone().into_iter() { + current.push(path); + *dirs.entry(current.join("")).or_insert(0) += size; + } + } + } + + dirs +} + #[cfg(test)] mod tests { use super::*; diff --git a/day_07/src/part_01.rs b/day_07/src/part_01.rs index 715cd2a..c869650 100644 --- a/day_07/src/part_01.rs +++ b/day_07/src/part_01.rs @@ -1,43 +1,9 @@ -use std::{ - collections::{HashMap, VecDeque}, - io::Result, -}; +use std::io::Result; -use crate::Input; +use crate::{get_dirs, Input}; pub fn main(input: &Input) -> Result { - let mut path: VecDeque = VecDeque::new(); - let mut dirs: HashMap = HashMap::new(); - - for row in input.into_iter() { - if row.starts_with('$') { - if row.contains("$ cd") { - let next_dir = row.replace("$ cd ", ""); - if next_dir.contains("..") { - path.pop_back(); - } else { - path.push_back(if next_dir.contains('/') { - next_dir - } else { - format!("{}/", next_dir) - }); - } - } - } else { - if !row.starts_with("dir ") { - let n: Vec<&str> = row.split(' ').collect(); - let size = n[0].parse::().unwrap(); - let mut current: Vec = vec![]; - for path in path.clone().into_iter() { - current.push(path); - let path = current.join(""); - *dirs.entry(path).or_insert(0) += size; - } - } - } - } - - Ok(dirs + Ok(get_dirs(&input) .into_values() .filter(|val| val <= &100_000) .sum::()) diff --git a/day_07/src/part_02.rs b/day_07/src/part_02.rs index 99c75eb..1588a8d 100644 --- a/day_07/src/part_02.rs +++ b/day_07/src/part_02.rs @@ -1,46 +1,12 @@ -use std::{ - collections::{HashMap, VecDeque}, - io::Result, -}; +use std::io::Result; -use crate::Input; +use crate::{get_dirs, Input}; pub fn main(input: &Input) -> Result { - let mut path: VecDeque = VecDeque::new(); - let mut dirs: HashMap = HashMap::new(); - - for row in input.into_iter() { - if row.starts_with('$') { - if row.contains("$ cd") { - let next_dir = row.replace("$ cd ", ""); - if next_dir.contains("..") { - path.pop_back(); - } else { - path.push_back(if next_dir.contains('/') { - next_dir - } else { - format!("{}/", next_dir) - }); - } - } - } else { - if !row.starts_with("dir ") { - let n: Vec<&str> = row.split(' ').collect(); - let size = n[0].parse::().unwrap(); - let mut current: Vec = vec![]; - for path in path.clone().into_iter() { - current.push(path); - let path = current.join(""); - *dirs.entry(path).or_insert(0) += size; - } - } - } - } - + let dirs = get_dirs(&input); let to_remove = 30_000_000 - (70_000_000 - dirs.get("/").unwrap_or(&0)); Ok(dirs - .clone() .into_values() .filter(|val| val >= &to_remove) .min() From a8172d3306cb5c75391f91d21b6bff768157afaa Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Thu, 8 Dec 2022 12:53:14 +0100 Subject: [PATCH 13/25] feat(day_08): completed part 1 --- day_08/Cargo.toml | 15 +++++++ day_08/src/lib.rs | 50 ++++++++++++++++++++++ day_08/src/main.rs | 12 ++++++ day_08/src/part_01.rs | 62 +++++++++++++++++++++++++++ day_08/src/part_02.rs | 24 +++++++++++ input/day_08 | 99 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 262 insertions(+) create mode 100644 day_08/Cargo.toml create mode 100644 day_08/src/lib.rs create mode 100644 day_08/src/main.rs create mode 100644 day_08/src/part_01.rs create mode 100644 day_08/src/part_02.rs create mode 100644 input/day_08 diff --git a/day_08/Cargo.toml b/day_08/Cargo.toml new file mode 100644 index 0000000..66b99c2 --- /dev/null +++ b/day_08/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_08" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_08" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_08/src/lib.rs b/day_08/src/lib.rs new file mode 100644 index 0000000..4e3d1a5 --- /dev/null +++ b/day_08/src/lib.rs @@ -0,0 +1,50 @@ +use std::collections::BTreeMap; + +pub mod part_01; +pub mod part_02; + +pub type Input = BTreeMap<(usize, usize), u32>; + +pub fn parse_input(input: &str) -> Input { + input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .enumerate() + .map(|(y, string)| { + string + .chars() + .enumerate() + .map(|(x, c)| ((x, y), c.to_digit(10).unwrap_or(0))) + .collect::>() + }) + .flatten() + .collect() +} + +pub fn max(input: &Input) -> (usize, usize) { + input.into_iter().fold((0, 0), |max, ((x, y), _)| { + ( + if x > &max.0 { x.to_owned() } else { max.0 }, + if y > &max.1 { y.to_owned() } else { max.1 }, + ) + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + 12 + 34 + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + BTreeMap::from([((0, 0), 1), ((1, 0), 2), ((0, 1), 3), ((1, 1), 4)]) + ); + } +} diff --git a/day_08/src/main.rs b/day_08/src/main.rs new file mode 100644 index 0000000..ffd55f5 --- /dev/null +++ b/day_08/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_08::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_08")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_08/src/part_01.rs b/day_08/src/part_01.rs new file mode 100644 index 0000000..32608b3 --- /dev/null +++ b/day_08/src/part_01.rs @@ -0,0 +1,62 @@ +use std::{io::Result, ops::RangeInclusive}; + +use crate::{max, Input}; + +pub fn main(input: &Input) -> Result { + let (max_x, max_y) = max(input); + + let iter_x = |range: RangeInclusive, pos_y: &usize, size: &u32| { + range + .into_iter() + .all(|x| input.get(&(x, *pos_y)).unwrap_or(&9) < &size) + }; + + let iter_y = |range: RangeInclusive, pos_x: &usize, size: &u32| { + range + .into_iter() + .all(|y| input.get(&(*pos_x, y)).unwrap_or(&9) < &size) + }; + + Ok(input + .clone() + .into_iter() + .fold(0, |count, ((pos_x, pos_y), size)| { + if pos_x == 0 || pos_x == max_x || pos_y == 0 || pos_y == max_y { + return count + 1; + } + if iter_x((pos_x + 1)..=max_x, &pos_y, &size) { + return count + 1; + } + if iter_x(0..=(pos_x - 1), &pos_y, &size) { + return count + 1; + } + if iter_y((pos_y + 1)..=max_y, &pos_x, &size) { + return count + 1; + } + if iter_y(0..=(pos_y - 1), &pos_x, &size) { + return count + 1; + } + return count; + })) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + 30373 + 25512 + 65332 + 33549 + 35390 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 21); + Ok(()) + } +} diff --git a/day_08/src/part_02.rs b/day_08/src/part_02.rs new file mode 100644 index 0000000..dd3d5a5 --- /dev/null +++ b/day_08/src/part_02.rs @@ -0,0 +1,24 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(_input: &Input) -> Result<()> { + Ok(()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + example + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, ()); + Ok(()) + } +} diff --git a/input/day_08 b/input/day_08 new file mode 100644 index 0000000..8e85b7d --- /dev/null +++ b/input/day_08 @@ -0,0 +1,99 @@ +012210012213303320303222443113334342210313303454311331225110130030424212142031302213330002110012202 +102111010213210021001022332202430004242253521151233435512412354104014404443413310323011012023201021 +222111003103212333013134434140410034543551441115323331241513231234431132024442112441221030323222201 +211012231012012302200321204210044513144154225545213542132254151144432412133213041300331330203232021 +002201312313222001411332000110432522431232314331333333341453144254551331133403204411302212311202222 +110230032313312322243243202152533544354355433511352253222235253151343332220121001410340302222303310 +101313222321043104033302002124412142331115412425545541414142413325115451211012344223412413001132312 +000110031333220133042312354442315421411124331555452354542212112435551414455113414040333101101113323 +231131102314203003210255344412121351334525335562222234363342225331324441223135530430044214323110010 +001302013334343042232523555451355144155463632254526622543325633542143241412224323141041332210112123 +232133223421212244014135242143544562436253323552332245442342453545311122352325114433041240033123210 +313311013443004432534112223421323522535632525344622566243626643556352132422325121121143043201001112 +312131000031042414412233131132546464466645663456625633662464326243345423442331115145421104131132032 +011313124102221132213332554444454462552663453222636663423523253543344325351111113251500031040140333 +131210242234103445444313116435342654435436424324537553522235434226423435624312521322541433210110300 +202104212221122341315153563325425645423454444673474357636565332462233326223625251414331404301010320 +010242333200311221125541336654235564266376534545434743476476346546644556545321145252442104103430030 +123333033333445552541136546354365266546335747553764544376335347754554432326234113551545230341122122 +111132120023444352452333426232535276677646555634363636373466436473655322366362634415114243210313120 +213200032234314354122644243422227457747776356365644735433734455435673526323365322422555531242442231 +221422333211235525365463423444744353746765464667353567655366755663537575264345266534345331230013324 +033412323511535124544563324333377354535477745733737354753353536336573757542436456241122223533000033 +240304415122421153355563542457436543756544576457865577876334567346736766732362524435122433213011404 +011211402432221454523535324337776747776587685748855567444784577637555433533536663633143332154204440 +423240022155524264343553343746666763475576845467864765764587587677754336346645643264512245534444134 +133414333521342436652322666463456436668687785675678858768577664863663755734764364264263525114242130 +241402355253143246562535745533633575558656588765647785777458888544764747566733233663264213451332133 +032333551141315355366435333675567764455765648777745586456574555878456477447543422633334525124452220 +443022215443326244322465745545476885648844464676774464664455545448774664554746334262332344111154340 +103305251343224345526445765577667564485778867678558957997488484647887746765364343543254345242322403 +110043441441443262567747674335676674655844587575577866678585648565746476554643474632233355434343442 +001044325213325543343734347538774478875456578998996977586879764484468567537574434323663562331543123 +120512432215425546364374473575488687466956956666987685597778996654684857735443354726463462445443221 +304354134424524455546463367846758455678898977855779898575555556744756468487535777333454423344155320 +011353212543542246543767576745688674595889976679665955986577659697778465787765757745642666242411421 +035113413122424246634347354647557557975958889798985567857985768857456555686446556733245245522225141 +025153324533365623646535757766476555597869768568779896597576865777744747767565353456342433231341352 +245544523544266655766436477887688957595955898689898679788685578679586544487665667573264356465251422 +033224433342434445465635878464545765577688878699896876696866556657877488746575367477436555241525221 +315141122265642667755375685566447659789976989866988676997796565888696746776856433447532225622243351 +313314116365233565753337575488658556986998877797986698778699959978579967888578576464625434636312451 +342233333566233676563337864766686879797978677887676977798777685969895746856458447377363344325335322 +433545132565326757334568475578655585957666879898967679868767677796556574467858566457346366663411141 +345324234446263476454766668858898589558667968778997799996866696899667576476558435735432563663332541 +312141353442544546634587484687667669699899878889978787889876998656977896854586536533433532334434521 +421311244624332566556345756478555565896968687987998898897896789888878556656685833563774626536531144 +424222425365265773564457865886659885699879998988998977988768896877988655576765765543662442622645454 +432441323556523363464657758447767865968787678879787879987776788699979988576754636367543325444245154 +123431445555544436575556444769685767679998687877788778778699966787557865544844456347473666445531523 +141345455355664436455478476746966898888798889777798788779789687967978579448686864357535554235412445 +323432535325536337645674444657767665788796688977779898787967796687756757884544657334343235564253135 +155524266324543757346355847785966878888776777978777987878796699896976678584466874354476526234422452 +155113453542247554674774656588686895698799999777899778989667779786879589567555663644655633426355342 +324425534552525433775686547887987786666869898797879878889696899998758576477476534547756426363325444 +321245142432525757435666584569856585696966989787878898787996668696878978556846654476363565345314512 +551524523654624757363567765568975585589787796978979978979997896658879895544684833663556646642244334 +121522443453235435577465888546759769679867986687898889898888767655597875445686737473743335645125321 +121254456654356775475757547554586695776987777877778968976799989889677576677677735543432463624412141 +315112344343554673333478574475899696969998666688769896776687668777587976748675566476662646226255121 +142143445624462657343477675856758699868889977987787669966779887697978885555757655565454666224515442 +034232413435464564355434766688567675659569867678867868676997786787778547566444576466755623434334145 +433254133644634345673646688868756577699669767979788997869877596596987546756753555754764434362342411 +453523241444264455356636664578767556898896696688768979977777589765586745558477344365434245223422542 +304125245622252556736355686646455975868699869688668889667567866589668657854465753477263355354251125 +315141233336436425435755686655767876557668658998888666696755856675677857887673573353344234225143433 +142221555135333525544776565678587568765575558585885689897988658868554567576457556656446554342142345 +333452515435533552356647768446787845656899758895665976775555697958546647854356456633363323254151222 +344245154226666464373466666578555765476887885977776666688699968664547644473634646542623643535115324 +130453141334524632544577733564585445458765598559958679677696776586546565466375534666363444155352423 +314034311325644552263675467548788477478856977777856788986979555884685548374573646344225224423324132 +443124113334245635225654646536458844757458967685869655766894684858667443665437436355654533244451213 +430315431125424333554676647365587855567445477856899969876656878874645737765557644664332641251544403 +033245124123256643265256444735354488454467664748584488686587888568885663446653443333463441421414031 +300130145452213655555257655737563646464886564457844477657565585655577536556477254533465521135322212 +132304552221234236346364447477743567848464675477685485775886475546547347666444653565236445512251302 +340330314521213336632436566555374753447674844446876454847778554547574764673664462654645421434502242 +121444124232153243633223444767675443456644476845644566474556785655455333376232435445343555555542313 +221204445334324345463325435743666444676755675586554587775467764447455474446443635342153521532230041 +433432023331552513536424443244457766436735446475464586555575535644474366735363432542511535543010003 +032241200312124213245452452226334647455667737756675446663633477356455457565644225632353414424130114 +102444132053311412163535553534753534634333436357663565766355454776665764245533435413243435224144400 +024123302224552155342666566235246336465665655666634457367677744553657456352665552433135424243312002 +311331142321221224355246526246556473357543473764353576633343356777344254622222661224121212043301012 +132122023023323354543344245635322326747445745474546537574647374552524662663466433354424314142403130 +302243323343222423342154264623354324646435745333535475464637363442623452625542351441533110034202410 +203314222113402413153513335344264452432253443554553346753553534333333246526254423311554442233333000 +020103121204342211435131256362565655363223625653355544326352524342323465526244435311124113342120211 +002232322230123333221533522425264655455245535436562466322543256236236263653224321242232412211143200 +233112334413432221231114241151223525225546246465652364424433435443662663345545125433230103320432100 +313000003424020140324343523153152644355645263562425566545355355552245413452314553421142141243211223 +203313011330042021445221111514415562552545335255436326646353424363451524225252123000432200020010200 +201300220330103443020432122551342512422456442236525255564524654622552433313111211310124043400310001 +033312220120311243202222252313125514414236234532624552562342253352425325255353141232242222111211032 +203011221000301201412231422352342451452113435145535446312151551345524532555513001023014311113123022 +121021113230041341014134031242325532325435534252442123134323445313341451214204043304321002112120120 +002031331230112330122421141451253231511332232321234152123254342154421311343320113140341230300030301 +001123300300201101244344424301215351355125424322331125121255541444431130023330000344430012000003021 +212022123023002021024004011330024441114255452255343132452423554322214043030000330401030022311221212 +110120102133312201323320402242014144252524113245144224553241354421310224142233233422001111212321010 From d8f57f42c29a541adb8b471f5bc2ad35bd9bb33c Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Thu, 8 Dec 2022 14:34:50 +0100 Subject: [PATCH 14/25] feat(day_08): completed part 2 --- README.md | 2 +- day_08/src/part_02.rs | 60 +++++++++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 6770d34..51c7cb0 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ - [Day 5](https://github.com/ankjevel/adventofcode/tree/2022/day_05) ⭐️ ⭐️ - [Day 6](https://github.com/ankjevel/adventofcode/tree/2022/day_06) ⭐️ ⭐️ - [Day 7](https://github.com/ankjevel/adventofcode/tree/2022/day_07) ⭐️ ⭐️ -- [Day 8](#) +- [Day 8](https://github.com/ankjevel/adventofcode/tree/2022/day_08) ⭐️ ⭐️ - [Day 9](#) - [Day 10](#) - [Day 11](#) diff --git a/day_08/src/part_02.rs b/day_08/src/part_02.rs index dd3d5a5..27c49cb 100644 --- a/day_08/src/part_02.rs +++ b/day_08/src/part_02.rs @@ -1,9 +1,55 @@ use std::io::Result; -use crate::Input; +use crate::{max, Input}; -pub fn main(_input: &Input) -> Result<()> { - Ok(()) +pub fn main(input: &Input) -> Result { + let (max_x, max_y) = max(input); + + let not_valid = |x: &usize, y: &usize, size: &u32| input.get(&(*x, *y)).unwrap_or(&9) >= size; + + let iter_x = |range: Box>, pos_y: &usize, size: &u32| { + let mut n = 0; + for x in range { + n = n + 1; + if not_valid(&x, pos_y, size) { + break; + } + } + n + }; + + let iter_y = |range: Box>, pos_x: &usize, size: &u32| { + let mut n = 0; + for y in range { + n = n + 1; + if not_valid(pos_x, &y, size) { + break; + } + } + n + }; + + Ok(input + .clone() + .into_iter() + .fold(0, |count, ((pos_x, pos_y), size)| { + if pos_x == 0 || pos_x == max_x || pos_y == 0 || pos_y == max_y { + return count; + } + + let right = iter_x(Box::new((pos_x + 1)..=max_x), &pos_y, &size); + let left = iter_x(Box::new((0..=(pos_x - 1)).rev()), &pos_y, &size); + let bottom = iter_y(Box::new((pos_y + 1)..=max_y), &pos_x, &size); + let top = iter_y(Box::new((0..=(pos_y - 1)).rev()), &pos_x, &size); + + let current = left * right * top * bottom; + + if current > count { + current + } else { + count + } + })) } #[cfg(test)] @@ -13,12 +59,16 @@ mod tests { use super::*; const EXAMPLE_DATA: &'static str = " - example + 30373 + 25512 + 65332 + 33549 + 35390 "; #[test] fn it_gets_the_example_correct() -> Result<()> { - assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, ()); + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 8); Ok(()) } } From 201198364b20c40066d72886e6beb6060717c25a Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Fri, 9 Dec 2022 18:28:37 +0100 Subject: [PATCH 15/25] feat(day_09): completed day 9 --- day_09/Cargo.toml | 15 + day_09/src/direction.rs | 7 + day_09/src/knot.rs | 90 ++ day_09/src/lib.rs | 49 + day_09/src/main.rs | 12 + day_09/src/part_01.rs | 40 + day_09/src/part_02.rs | 81 ++ day_09/src/print.rs | 59 ++ input/day_09 | 2000 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 2353 insertions(+) create mode 100644 day_09/Cargo.toml create mode 100644 day_09/src/direction.rs create mode 100644 day_09/src/knot.rs create mode 100644 day_09/src/lib.rs create mode 100644 day_09/src/main.rs create mode 100644 day_09/src/part_01.rs create mode 100644 day_09/src/part_02.rs create mode 100644 day_09/src/print.rs create mode 100644 input/day_09 diff --git a/day_09/Cargo.toml b/day_09/Cargo.toml new file mode 100644 index 0000000..2f96cb8 --- /dev/null +++ b/day_09/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_09" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_09" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_09/src/direction.rs b/day_09/src/direction.rs new file mode 100644 index 0000000..9824b28 --- /dev/null +++ b/day_09/src/direction.rs @@ -0,0 +1,7 @@ +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum Direction { + Left, + Right, + Up, + Down, +} diff --git a/day_09/src/knot.rs b/day_09/src/knot.rs new file mode 100644 index 0000000..1ea3467 --- /dev/null +++ b/day_09/src/knot.rs @@ -0,0 +1,90 @@ +use std::collections::HashSet; + +use crate::direction::Direction::{self, *}; + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Knot { + pub position: (isize, isize), + pub visited: Vec<(isize, isize)>, +} + +impl Knot { + pub fn new() -> Self { + Knot { + position: (0, 0), + visited: vec![], + } + } + + fn next_move(direction: &Direction) -> (Option, Option) { + ( + match direction { + Up | Down => Some(if direction == &Up { -1 } else { 1 }), + _ => None, + }, + match direction { + Left | Right => Some(if direction == &Left { -1 } else { 1 }), + _ => None, + }, + ) + } + + fn store_position(&mut self) { + self.visited.push(self.position.to_owned()); + } + + fn diff(&self, reference: &Knot) -> (isize, isize) { + let horizontal_diff = (self.position.0 - reference.position.0).abs(); + let vertical_diff = (self.position.1 - reference.position.1).abs(); + (horizontal_diff, vertical_diff) + } + + fn should_move(&self, reference: &Knot) -> bool { + let (horizontal_diff, vertical_diff) = self.diff(reference); + horizontal_diff > 1 || vertical_diff > 1 + } + + fn move_towards(&mut self, reference: &Knot) { + let (ref_x, ref_y) = reference.position.to_owned(); + let (pos_x, pos_y) = self.position.to_owned(); + + self.store_position(); + + let (horizontal_diff, vertical_diff) = self.diff(reference); + + let x = if ref_x > pos_x { 1 } else { -1 }; + let y = if ref_y > pos_y { 1 } else { -1 }; + + if horizontal_diff == 0 || vertical_diff == 0 { + if vertical_diff == 0 { + self.position.0 += x; + } else { + self.position.1 += y; + } + } else { + self.position.0 += x; + self.position.1 += y; + } + } + + pub fn goto(&mut self, direction: &Direction) { + let (x, y) = Knot::next_move(direction); + + self.store_position(); + + self.position.0 += x.unwrap_or(0); + self.position.1 += y.unwrap_or(0); + } + + pub fn visited(&self) -> usize { + let hash_set: HashSet<(isize, isize)> = + HashSet::from_iter(self.visited.clone().to_owned().into_iter()); + hash_set.len() + } + + pub fn maybe_move(&mut self, reference: &Knot) { + if self.should_move(reference) { + self.move_towards(reference) + } + } +} diff --git a/day_09/src/lib.rs b/day_09/src/lib.rs new file mode 100644 index 0000000..e54ebc8 --- /dev/null +++ b/day_09/src/lib.rs @@ -0,0 +1,49 @@ +pub mod direction; +pub mod knot; +pub mod part_01; +pub mod part_02; +pub mod print; + +use direction::{Direction, Direction::*}; + +pub type Input = Vec<(Direction, usize)>; + +pub fn parse_input(input: &str) -> Input { + input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .map(str::to_owned) + .map(|row| { + let n: Vec = row.split(" ").into_iter().map(str::to_owned).collect::<_>(); + let direction = match &*n[0] { + "L" => Left, + "R" => Right, + "U" => Up, + _ => Down, + }; + let steps = n[1].parse::().unwrap_or(0); + (direction, steps) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + L 10 + R 2 + U 14 + D 1030 + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + vec![(Left, 10), (Right, 2), (Up, 14), (Down, 1030)] + ); + } +} diff --git a/day_09/src/main.rs b/day_09/src/main.rs new file mode 100644 index 0000000..f7b0858 --- /dev/null +++ b/day_09/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_09::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_09")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_09/src/part_01.rs b/day_09/src/part_01.rs new file mode 100644 index 0000000..f950871 --- /dev/null +++ b/day_09/src/part_01.rs @@ -0,0 +1,40 @@ +use std::io::Result; + +use crate::{knot::Knot, Input}; + +pub fn main(input: &Input) -> Result { + let mut head_tail = (Knot::new(), Knot::new()); + + for (direction, steps) in input.to_owned() { + for _ in 0..steps { + head_tail.0.goto(&direction); + head_tail.1.maybe_move(&head_tail.0); + } + } + + Ok(head_tail.1.visited()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + R 4 + U 4 + L 3 + D 1 + R 4 + D 1 + L 5 + R 2 + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 12); + Ok(()) + } +} diff --git a/day_09/src/part_02.rs b/day_09/src/part_02.rs new file mode 100644 index 0000000..a8f62b8 --- /dev/null +++ b/day_09/src/part_02.rs @@ -0,0 +1,81 @@ +use std::{collections::LinkedList, io::Result}; + +use crate::{knot::Knot, Input}; + +fn handle_tail(head: &mut Knot, tails: &mut LinkedList) { + let mut iter = tails.iter_mut(); + let mut last: Option<&Knot> = None; + loop { + let current = iter.next(); + if current.is_none() { + break; + } + + let current = current.unwrap(); + + if last.is_none() { + current.maybe_move(&head); + } else { + current.maybe_move(last.unwrap()); + }; + + last = Some(current); + } +} + +pub fn main(input: &Input) -> Result { + let mut head = Knot::new(); + let mut tails: LinkedList = LinkedList::from_iter((1..10).map(|_| Knot::new())); + + for (direction, steps) in input.to_owned() { + for _ in 0..steps { + head.goto(&direction); + handle_tail(&mut head, &mut tails); + } + } + + Ok(tails.pop_back().unwrap().visited() + 1) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!( + main(&parse_input( + &" + R 4 + U 4 + L 3 + D 1 + R 4 + D 1 + L 5 + R 2 + " + ))?, + 1 + ); + + assert_eq!( + main(&parse_input( + &" + R 5 + U 8 + L 8 + D 3 + R 17 + D 10 + L 25 + U 20 + " + ))?, + 36 + ); + Ok(()) + } +} diff --git a/day_09/src/print.rs b/day_09/src/print.rs new file mode 100644 index 0000000..aaabd33 --- /dev/null +++ b/day_09/src/print.rs @@ -0,0 +1,59 @@ +use std::collections::{BTreeMap, LinkedList}; + +use crate::knot::Knot; + +fn join<'a>(a: String, b: String) -> String { + let (a, b) = (a.to_owned(), b.to_owned()); + let c = [a, b].concat(); + + c.to_string() +} + +pub fn combine_head_and_tails(head: &Knot, tails: &LinkedList) -> Vec<(isize, isize)> { + let mut points = vec![head.position.clone()]; + let mut tails: Vec<_> = tails + .clone() + .iter_mut() + .map(|tail| tail.position.clone()) + .collect::<_>(); + points.append(&mut tails); + points +} + +pub fn print_grid(points: Vec<(isize, isize)>) { + let board: Vec<((isize, isize), String)> = (-10isize..10isize) + .flat_map(|y| { + (-20isize..20isize) + .map(|x| ((y, x), ".".to_string())) + .collect::>() + }) + .collect::<_>(); + + let mut board: BTreeMap<(isize, isize), String> = BTreeMap::from_iter(board.into_iter()); + for (i, (x, y)) in points.to_owned().into_iter().enumerate() { + let pos = board.get_mut(&(y, x)).unwrap(); + if pos != "." { + continue; + } + *pos = if i == 0 { + "H".to_string() + } else { + i.to_string() + }; + } + + let mut string = "".to_string(); + let mut out = Vec::new(); + let mut current = -5; + for (pos, key) in board { + if pos.0 != current { + out.push(string.to_owned()); + string = "".to_string(); + current = pos.0.to_owned(); + } + string = join(string, key) + } + + // print!("{}c{}\r\n", 27 as char, out.join("\r\n")); + print!("\n{}\r\n", out.join("\r\n")); +} diff --git a/input/day_09 b/input/day_09 new file mode 100644 index 0000000..be008e2 --- /dev/null +++ b/input/day_09 @@ -0,0 +1,2000 @@ +L 2 +D 2 +L 2 +R 2 +L 1 +U 2 +D 1 +U 2 +L 2 +D 2 +L 2 +R 1 +D 2 +L 2 +R 2 +U 1 +R 1 +U 2 +D 2 +R 1 +U 1 +L 2 +D 1 +U 2 +L 2 +R 2 +U 2 +D 2 +L 1 +D 2 +R 2 +L 2 +U 1 +R 2 +D 2 +R 2 +U 2 +D 1 +R 2 +U 2 +L 1 +D 2 +R 1 +U 2 +R 2 +U 1 +L 2 +R 1 +D 1 +L 2 +U 2 +R 1 +L 1 +R 2 +U 1 +D 1 +L 2 +R 1 +D 1 +R 1 +L 2 +U 2 +D 2 +R 2 +L 1 +U 2 +R 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 +U 1 +D 8 +R 13 +D 14 +L 10 +R 4 +D 1 +L 3 +U 13 +R 17 +D 12 +L 4 +U 15 +R 3 +U 19 +D 15 +U 2 +D 7 +U 8 +L 19 +D 17 +U 16 +D 13 +L 3 +R 16 +D 4 +R 17 +L 10 +R 5 +U 10 +L 18 +U 15 +R 11 +D 1 +R 14 +D 7 +R 4 +U 19 +R 14 +U 19 +D 16 +L 14 +R 17 +L 9 +R 16 +L 14 +D 10 +R 1 +U 2 +L 3 +R 12 +D 4 +U 12 +R 16 +L 4 +R 3 +U 9 +R 19 +U 2 +L 13 +D 11 +L 7 +D 11 +U 3 +R 1 +L 8 +R 7 +L 15 +D 9 +U 12 +D 10 +R 9 +U 16 +R 11 +L 11 +R 6 +U 11 +D 9 +L 8 +R 7 +L 9 +R 13 +D 14 +U 11 +R 14 +L 17 +U 9 +L 17 +R 9 +L 10 From ac29cece55853001100996a44d2ce0921efe6380 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Fri, 9 Dec 2022 18:49:22 +0100 Subject: [PATCH 16/25] chore(day_09): forgot readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 51c7cb0..5a4fa88 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ - [Day 6](https://github.com/ankjevel/adventofcode/tree/2022/day_06) ⭐️ ⭐️ - [Day 7](https://github.com/ankjevel/adventofcode/tree/2022/day_07) ⭐️ ⭐️ - [Day 8](https://github.com/ankjevel/adventofcode/tree/2022/day_08) ⭐️ ⭐️ -- [Day 9](#) +- [Day 9](https://github.com/ankjevel/adventofcode/tree/2022/day_09) ⭐️ ⭐️ - [Day 10](#) - [Day 11](#) - [Day 12](#) From 1532953dfd3826680198d9e8589ae51782554896 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Fri, 9 Dec 2022 21:33:10 +0100 Subject: [PATCH 17/25] feat(day_09): print results --- day_09/src/knot.rs | 11 ++++--- day_09/src/lib.rs | 4 +-- day_09/src/part_01.rs | 8 ++--- day_09/src/part_02.rs | 37 ++++++++++------------ day_09/src/print.rs | 72 +++++++++++++++++++++++++++++++------------ 5 files changed, 80 insertions(+), 52 deletions(-) diff --git a/day_09/src/knot.rs b/day_09/src/knot.rs index 1ea3467..335f164 100644 --- a/day_09/src/knot.rs +++ b/day_09/src/knot.rs @@ -2,10 +2,12 @@ use std::collections::HashSet; use crate::direction::Direction::{self, *}; +pub type Position = (isize, isize); + #[derive(Debug, Clone, Eq, PartialEq)] pub struct Knot { - pub position: (isize, isize), - pub visited: Vec<(isize, isize)>, + pub position: Position, + pub visited: Vec, } impl Knot { @@ -33,7 +35,7 @@ impl Knot { self.visited.push(self.position.to_owned()); } - fn diff(&self, reference: &Knot) -> (isize, isize) { + fn diff(&self, reference: &Knot) -> Position { let horizontal_diff = (self.position.0 - reference.position.0).abs(); let vertical_diff = (self.position.1 - reference.position.1).abs(); (horizontal_diff, vertical_diff) @@ -77,8 +79,7 @@ impl Knot { } pub fn visited(&self) -> usize { - let hash_set: HashSet<(isize, isize)> = - HashSet::from_iter(self.visited.clone().to_owned().into_iter()); + let hash_set: HashSet<_> = HashSet::from_iter(self.visited.clone().to_owned().into_iter()); hash_set.len() } diff --git a/day_09/src/lib.rs b/day_09/src/lib.rs index e54ebc8..7d8fb6f 100644 --- a/day_09/src/lib.rs +++ b/day_09/src/lib.rs @@ -15,14 +15,14 @@ pub fn parse_input(input: &str) -> Input { .filter(|string| !string.is_empty()) .map(str::to_owned) .map(|row| { - let n: Vec = row.split(" ").into_iter().map(str::to_owned).collect::<_>(); + let n: Vec<_> = row.split(" ").into_iter().map(str::to_owned).collect::<_>(); let direction = match &*n[0] { "L" => Left, "R" => Right, "U" => Up, _ => Down, }; - let steps = n[1].parse::().unwrap_or(0); + let steps = n[1].parse::<_>().unwrap_or(0); (direction, steps) }) .collect() diff --git a/day_09/src/part_01.rs b/day_09/src/part_01.rs index f950871..751b3b2 100644 --- a/day_09/src/part_01.rs +++ b/day_09/src/part_01.rs @@ -3,16 +3,16 @@ use std::io::Result; use crate::{knot::Knot, Input}; pub fn main(input: &Input) -> Result { - let mut head_tail = (Knot::new(), Knot::new()); + let (mut head, mut tail) = (Knot::new(), Knot::new()); for (direction, steps) in input.to_owned() { for _ in 0..steps { - head_tail.0.goto(&direction); - head_tail.1.maybe_move(&head_tail.0); + head.goto(&direction); + tail.maybe_move(&head); } } - Ok(head_tail.1.visited()) + Ok(tail.visited()) } #[cfg(test)] diff --git a/day_09/src/part_02.rs b/day_09/src/part_02.rs index a8f62b8..7adaf8c 100644 --- a/day_09/src/part_02.rs +++ b/day_09/src/part_02.rs @@ -1,40 +1,35 @@ -use std::{collections::LinkedList, io::Result}; +use std::io::Result; use crate::{knot::Knot, Input}; -fn handle_tail(head: &mut Knot, tails: &mut LinkedList) { - let mut iter = tails.iter_mut(); - let mut last: Option<&Knot> = None; - loop { - let current = iter.next(); - if current.is_none() { - break; - } - - let current = current.unwrap(); - - if last.is_none() { - current.maybe_move(&head); - } else { - current.maybe_move(last.unwrap()); - }; +#[cfg(debug_assertions)] +use crate::print::{combine_head_and_tails, print_grid}; - last = Some(current); +fn handle_tail(head: &mut Knot, tail: &mut Vec) { + let mut last = head; + for current in tail.iter_mut() { + current.maybe_move(last); + last = current; } } pub fn main(input: &Input) -> Result { let mut head = Knot::new(); - let mut tails: LinkedList = LinkedList::from_iter((1..10).map(|_| Knot::new())); + let mut tail = (0..=8).map(|_| Knot::new()).collect::<_>(); for (direction, steps) in input.to_owned() { for _ in 0..steps { head.goto(&direction); - handle_tail(&mut head, &mut tails); + handle_tail(&mut head, &mut tail); } } - Ok(tails.pop_back().unwrap().visited() + 1) + let last = &tail[8]; + + #[cfg(debug_assertions)] + print_grid(&combine_head_and_tails(&head, &tail), &last.visited); + + Ok(last.visited() + 1) } #[cfg(test)] diff --git a/day_09/src/print.rs b/day_09/src/print.rs index aaabd33..3cc91de 100644 --- a/day_09/src/print.rs +++ b/day_09/src/print.rs @@ -1,6 +1,6 @@ -use std::collections::{BTreeMap, LinkedList}; +use std::collections::HashMap; -use crate::knot::Knot; +use crate::knot::{Knot, Position}; fn join<'a>(a: String, b: String) -> String { let (a, b) = (a.to_owned(), b.to_owned()); @@ -9,7 +9,7 @@ fn join<'a>(a: String, b: String) -> String { c.to_string() } -pub fn combine_head_and_tails(head: &Knot, tails: &LinkedList) -> Vec<(isize, isize)> { +pub fn combine_head_and_tails(head: &Knot, tails: &Vec) -> Vec { let mut points = vec![head.position.clone()]; let mut tails: Vec<_> = tails .clone() @@ -20,18 +20,42 @@ pub fn combine_head_and_tails(head: &Knot, tails: &LinkedList) -> Vec<(isi points } -pub fn print_grid(points: Vec<(isize, isize)>) { - let board: Vec<((isize, isize), String)> = (-10isize..10isize) - .flat_map(|y| { - (-20isize..20isize) - .map(|x| ((y, x), ".".to_string())) +fn min_max(points: &Vec) -> (Position, Position) { + let first = points.clone().first().unwrap_or(&(0, 0)).to_owned(); + let (mut min_x, mut max_x, mut min_y, mut max_y) = (first.0, first.0, first.1, first.1); + for (x, y) in points.to_owned() { + if x > max_x { + max_x = x; + } else if x < min_x { + min_x = x + } + + if y > max_y { + max_y = y + } else if y < min_y { + min_y = y + } + } + ((min_x - 2, min_y - 2), (max_x + 2, max_y + 2)) +} + +pub fn print_grid(points: &Vec, visited: &Vec) { + let mut all = points.to_owned(); + all.extend(&visited.to_owned()); + + let ((min_x, min_y), (max_x, max_y)) = min_max(&all); + + let board: Vec<_> = (min_x..=max_x) + .flat_map(|x| { + (min_y..=max_y) + .map(|y| ((x, y), ".".to_string())) .collect::>() }) .collect::<_>(); - let mut board: BTreeMap<(isize, isize), String> = BTreeMap::from_iter(board.into_iter()); + let mut board: HashMap<_, _> = HashMap::from_iter(board.into_iter()); for (i, (x, y)) in points.to_owned().into_iter().enumerate() { - let pos = board.get_mut(&(y, x)).unwrap(); + let pos = board.get_mut(&(x, y)).unwrap(); if pos != "." { continue; } @@ -42,18 +66,26 @@ pub fn print_grid(points: Vec<(isize, isize)>) { }; } - let mut string = "".to_string(); + for (i, (x, y)) in visited.to_owned().into_iter().enumerate() { + let pos = board.get_mut(&(x, y)).unwrap(); + if pos != "." { + continue; + } + *pos = if i == 0 { + "s".to_string() + } else { + "#".to_string() + }; + } + let mut out = Vec::new(); - let mut current = -5; - for (pos, key) in board { - if pos.0 != current { - out.push(string.to_owned()); - string = "".to_string(); - current = pos.0.to_owned(); + for y in min_y..=max_y { + let mut string = "".to_string(); + for x in min_x..=max_x { + string = join(string, board.get(&(x, y)).unwrap().to_owned()) } - string = join(string, key) + out.push(string.to_owned()); } - // print!("{}c{}\r\n", 27 as char, out.join("\r\n")); - print!("\n{}\r\n", out.join("\r\n")); + println!("\r\n{}", out.join("\r\n")); } From e7b1f787445037363f62df40a71b808afbcb462c Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sat, 10 Dec 2022 20:46:33 +0100 Subject: [PATCH 18/25] feat(day_10): completed part 1 --- README.md | 2 +- day_10/Cargo.toml | 15 ++++ day_10/src/cpu.rs | 25 ++++++ day_10/src/instruction.rs | 5 ++ day_10/src/lib.rs | 40 ++++++++++ day_10/src/main.rs | 12 +++ day_10/src/part_01.rs | 33 ++++++++ day_10/src/part_02.rs | 23 ++++++ input/day_10 | 140 ++++++++++++++++++++++++++++++++ input/day_10_extended_example | 146 ++++++++++++++++++++++++++++++++++ 10 files changed, 440 insertions(+), 1 deletion(-) create mode 100644 day_10/Cargo.toml create mode 100644 day_10/src/cpu.rs create mode 100644 day_10/src/instruction.rs create mode 100644 day_10/src/lib.rs create mode 100644 day_10/src/main.rs create mode 100644 day_10/src/part_01.rs create mode 100644 day_10/src/part_02.rs create mode 100644 input/day_10 create mode 100644 input/day_10_extended_example diff --git a/README.md b/README.md index 5a4fa88..e64cb50 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - [Day 7](https://github.com/ankjevel/adventofcode/tree/2022/day_07) ⭐️ ⭐️ - [Day 8](https://github.com/ankjevel/adventofcode/tree/2022/day_08) ⭐️ ⭐️ - [Day 9](https://github.com/ankjevel/adventofcode/tree/2022/day_09) ⭐️ ⭐️ -- [Day 10](#) +- [Day 10](https://github.com/ankjevel/adventofcode/tree/2022/day_10) ⭐️ - [Day 11](#) - [Day 12](#) - [Day 13](#) diff --git a/day_10/Cargo.toml b/day_10/Cargo.toml new file mode 100644 index 0000000..69c932b --- /dev/null +++ b/day_10/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_10" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_10" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_10/src/cpu.rs b/day_10/src/cpu.rs new file mode 100644 index 0000000..97dc5e9 --- /dev/null +++ b/day_10/src/cpu.rs @@ -0,0 +1,25 @@ +use std::collections::HashMap; + +pub struct CPU { + cycles: usize, + strength: i64, + pub stack: HashMap, +} + +impl CPU { + pub fn new() -> Self { + CPU { + cycles: 0, + strength: 1, + stack: HashMap::new(), + } + } + + pub fn store(&mut self, value: Option) { + self.cycles += 1; + self.stack.insert(self.cycles, self.strength); + if let Some(unrapped) = value { + self.strength += unrapped; + } + } +} diff --git a/day_10/src/instruction.rs b/day_10/src/instruction.rs new file mode 100644 index 0000000..39b2d81 --- /dev/null +++ b/day_10/src/instruction.rs @@ -0,0 +1,5 @@ +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum Instruction { + AddX(i64), + Noop, +} diff --git a/day_10/src/lib.rs b/day_10/src/lib.rs new file mode 100644 index 0000000..2a4c144 --- /dev/null +++ b/day_10/src/lib.rs @@ -0,0 +1,40 @@ +use instruction::Instruction::{self, *}; + +pub mod cpu; +pub mod instruction; +pub mod part_01; +pub mod part_02; + +pub type Input = Vec; + +pub fn parse_input(input: &str) -> Input { + input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .map(str::to_owned) + .map(|row| { + let part: Vec<_> = row.split(" ").into_iter().map(str::to_owned).collect::<_>(); + match &*part[0] { + "addx" => AddX(part[1].parse::<_>().unwrap_or(0)), + _ => Noop, + } + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + addx 1 + noop + addx -1 + "; + + #[test] + fn it_parses_example() { + assert_eq!(parse_input(&EXAMPLE_DATA), vec![AddX(1), Noop, AddX(-1)]); + } +} diff --git a/day_10/src/main.rs b/day_10/src/main.rs new file mode 100644 index 0000000..4071efb --- /dev/null +++ b/day_10/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_10::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_10")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_10/src/part_01.rs b/day_10/src/part_01.rs new file mode 100644 index 0000000..c43e992 --- /dev/null +++ b/day_10/src/part_01.rs @@ -0,0 +1,33 @@ +use std::io::Result; + +use crate::{cpu::CPU, instruction::Instruction::*, Input}; + +pub fn main(input: &Input) -> Result { + let mut cpu = CPU::new(); + for instruction in input.to_owned() { + cpu.store(None); + if let AddX(val) = instruction { + cpu.store(Some(val)); + } + } + + Ok(vec![20, 60, 100, 140, 180, 220] + .into_iter() + .map(|index| (index as i64) * cpu.stack.get(&index).unwrap_or(&1).to_owned()) + .sum()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + let input = parse_input(include_str!("../../input/day_10_extended_example")); + assert_eq!(main(&input)?, 13140); + + Ok(()) + } +} diff --git a/day_10/src/part_02.rs b/day_10/src/part_02.rs new file mode 100644 index 0000000..e366484 --- /dev/null +++ b/day_10/src/part_02.rs @@ -0,0 +1,23 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(_input: &Input) -> Result<()> { + Ok(()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + let input = parse_input(include_str!("../../input/day_10_extended_example")); + + let _ = main(&input)?; + + Ok(()) + } +} diff --git a/input/day_10 b/input/day_10 new file mode 100644 index 0000000..5796ef3 --- /dev/null +++ b/input/day_10 @@ -0,0 +1,140 @@ +addx 2 +addx 15 +addx -11 +addx 6 +noop +noop +noop +addx -1 +addx 5 +addx -1 +addx 5 +noop +noop +noop +noop +noop +addx 7 +addx -1 +addx 3 +addx 1 +addx 5 +addx 1 +noop +addx -38 +noop +addx 1 +addx 6 +addx 3 +noop +addx -8 +noop +addx 13 +addx 2 +addx 3 +addx -2 +addx 2 +noop +addx 3 +addx 9 +addx -2 +addx 2 +addx -10 +addx 11 +addx 2 +addx -14 +addx -21 +addx 2 +noop +addx 5 +addx 29 +addx -2 +noop +addx -19 +noop +addx 2 +addx 11 +addx -10 +addx 2 +addx 5 +addx -9 +noop +addx 14 +addx 2 +addx 3 +addx -2 +addx 3 +addx 1 +noop +addx -37 +noop +addx 13 +addx -8 +noop +noop +noop +noop +addx 13 +addx -5 +addx 3 +addx 3 +addx 3 +noop +noop +noop +noop +noop +noop +noop +addx 6 +addx 3 +addx 1 +addx 5 +addx -15 +addx 5 +addx -27 +addx 30 +addx -23 +addx 33 +addx -32 +addx 2 +addx 5 +addx 2 +addx -16 +addx 17 +addx 2 +addx -10 +addx 17 +addx 10 +addx -9 +addx 2 +addx 2 +addx 5 +addx -29 +addx -8 +noop +noop +noop +addx 19 +addx -11 +addx -1 +addx 6 +noop +noop +addx -1 +addx 3 +noop +addx 3 +addx 2 +addx -3 +addx 11 +addx -1 +addx 5 +addx -2 +addx 5 +addx 2 +noop +noop +addx 1 +noop +noop diff --git a/input/day_10_extended_example b/input/day_10_extended_example new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/input/day_10_extended_example @@ -0,0 +1,146 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +noop From d5ecf4ec5b7c7442a3a21009acdec5fa8f9754bb Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sun, 11 Dec 2022 10:49:40 +0100 Subject: [PATCH 19/25] feat(day_10): completed part 2 --- README.md | 2 +- day_10/src/cpu.rs | 17 +++++++++++--- day_10/src/part_01.rs | 9 ++------ day_10/src/part_02.rs | 54 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 65 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index e64cb50..c9733b8 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ - [Day 7](https://github.com/ankjevel/adventofcode/tree/2022/day_07) ⭐️ ⭐️ - [Day 8](https://github.com/ankjevel/adventofcode/tree/2022/day_08) ⭐️ ⭐️ - [Day 9](https://github.com/ankjevel/adventofcode/tree/2022/day_09) ⭐️ ⭐️ -- [Day 10](https://github.com/ankjevel/adventofcode/tree/2022/day_10) ⭐️ +- [Day 10](https://github.com/ankjevel/adventofcode/tree/2022/day_10) ⭐️ ⭐️ - [Day 11](#) - [Day 12](#) - [Day 13](#) diff --git a/day_10/src/cpu.rs b/day_10/src/cpu.rs index 97dc5e9..b8a0e28 100644 --- a/day_10/src/cpu.rs +++ b/day_10/src/cpu.rs @@ -1,9 +1,11 @@ -use std::collections::HashMap; +use std::collections::BTreeMap; + +use crate::{instruction::Instruction::*, Input}; pub struct CPU { cycles: usize, strength: i64, - pub stack: HashMap, + pub stack: BTreeMap, } impl CPU { @@ -11,7 +13,7 @@ impl CPU { CPU { cycles: 0, strength: 1, - stack: HashMap::new(), + stack: BTreeMap::new(), } } @@ -22,4 +24,13 @@ impl CPU { self.strength += unrapped; } } + + pub fn parse(&mut self, instructions: &Input) { + for instruction in instructions.to_owned() { + self.store(None); + if let AddX(val) = instruction { + self.store(Some(val)); + } + } + } } diff --git a/day_10/src/part_01.rs b/day_10/src/part_01.rs index c43e992..05ea6f7 100644 --- a/day_10/src/part_01.rs +++ b/day_10/src/part_01.rs @@ -1,15 +1,10 @@ use std::io::Result; -use crate::{cpu::CPU, instruction::Instruction::*, Input}; +use crate::{cpu::CPU, Input}; pub fn main(input: &Input) -> Result { let mut cpu = CPU::new(); - for instruction in input.to_owned() { - cpu.store(None); - if let AddX(val) = instruction { - cpu.store(Some(val)); - } - } + cpu.parse(input); Ok(vec![20, 60, 100, 140, 180, 220] .into_iter() diff --git a/day_10/src/part_02.rs b/day_10/src/part_02.rs index e366484..162d46e 100644 --- a/day_10/src/part_02.rs +++ b/day_10/src/part_02.rs @@ -1,9 +1,36 @@ -use std::io::Result; +use std::{collections::BTreeMap, io::Result}; -use crate::Input; +use crate::{cpu::CPU, Input}; -pub fn main(_input: &Input) -> Result<()> { - Ok(()) +fn print(crt: &BTreeMap) -> String { + let x = crt.to_owned().into_values().collect::>(); + + let y = x + .chunks(40) + .map(|chunk| chunk.to_owned().into_iter().collect::()) + .collect::>(); + + y.join("\n") +} + +pub fn main(input: &Input) -> Result { + let mut cpu = CPU::new(); + cpu.parse(input); + + let mut crt: BTreeMap = + BTreeMap::from_iter((0..240).map(|n| (n as i64, ".".to_string()))); + + for (position, (_, strength)) in cpu.stack.into_iter().enumerate() { + let position = position as i64; + let horizontal_position = position % 40; + if (strength - horizontal_position).abs() < 2 { + if let Some(pixel) = crt.get_mut(&position) { + *pixel = "#".to_string(); + } + } + } + + Ok(print(&crt)) } #[cfg(test)] @@ -15,8 +42,23 @@ mod tests { #[test] fn it_gets_the_example_correct() -> Result<()> { let input = parse_input(include_str!("../../input/day_10_extended_example")); - - let _ = main(&input)?; + assert_eq!( + main(&input)?, + " + ##..##..##..##..##..##..##..##..##..##.. + ###...###...###...###...###...###...###. + ####....####....####....####....####.... + #####.....#####.....#####.....#####..... + ######......######......######......#### + #######.......#######.......#######..... + " + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .map(str::to_owned) + .collect::>() + .join("\n") + ); Ok(()) } From 2eadc9855dd6df9e392577d447b71e7088708cb5 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sun, 11 Dec 2022 16:42:54 +0100 Subject: [PATCH 20/25] feat(day_11): completed part 1 --- README.md | 2 +- day_11/Cargo.toml | 15 +++++ day_11/src/lib.rs | 106 ++++++++++++++++++++++++++++++++++ day_11/src/main.rs | 12 ++++ day_11/src/monkey.rs | 68 ++++++++++++++++++++++ day_11/src/part_01.rs | 66 +++++++++++++++++++++ day_11/src/part_02.rs | 24 ++++++++ input/day_11 | 55 ++++++++++++++++++ input/day_11_extended_example | 27 +++++++++ 9 files changed, 374 insertions(+), 1 deletion(-) create mode 100644 day_11/Cargo.toml create mode 100644 day_11/src/lib.rs create mode 100644 day_11/src/main.rs create mode 100644 day_11/src/monkey.rs create mode 100644 day_11/src/part_01.rs create mode 100644 day_11/src/part_02.rs create mode 100644 input/day_11 create mode 100644 input/day_11_extended_example diff --git a/README.md b/README.md index c9733b8..4af0c96 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ - [Day 8](https://github.com/ankjevel/adventofcode/tree/2022/day_08) ⭐️ ⭐️ - [Day 9](https://github.com/ankjevel/adventofcode/tree/2022/day_09) ⭐️ ⭐️ - [Day 10](https://github.com/ankjevel/adventofcode/tree/2022/day_10) ⭐️ ⭐️ -- [Day 11](#) +- [Day 11](https://github.com/ankjevel/adventofcode/tree/2022/day_11) ⭐️ - [Day 12](#) - [Day 13](#) - [Day 14](#) diff --git a/day_11/Cargo.toml b/day_11/Cargo.toml new file mode 100644 index 0000000..9e7640f --- /dev/null +++ b/day_11/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_11" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_11" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_11/src/lib.rs b/day_11/src/lib.rs new file mode 100644 index 0000000..1a6aacd --- /dev/null +++ b/day_11/src/lib.rs @@ -0,0 +1,106 @@ +pub mod monkey; +pub mod part_01; +pub mod part_02; + +use std::collections::BTreeMap; + +use monkey::{Monkey, Operation::*}; + +pub type Input = BTreeMap; + +pub fn parse_input(input: &str) -> Input { + input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .map(str::to_owned) + .collect::>() + .chunks(6) + .map(|rows| { + let mut rows = rows.clone().to_owned(); + let mut rows = rows.iter_mut(); + + let index = rows + .next() + .unwrap() + .chars() + .nth(7) + .unwrap() + .to_digit(10) + .unwrap() as isize; + + let splits: Vec<_> = rows + .into_iter() + .map(|row| row.split(": ").nth(1).unwrap()) + .collect::>(); + + ( + index, + Monkey::new( + splits[0] + .split(", ") + .map(|part| part.parse::<_>().unwrap()) + .collect::>(), + { + let (_, operation) = splits[1].split_at(10); + let (operation, value) = operation.split_at(2); + let value = if value.contains("old") { + core::option::Option::None + } else { + Some(value.parse::<_>().unwrap()) + }; + if operation.contains("*") { + Mul(value) + } else { + Add(value) + } + }, + { + let (_, value) = splits[2].split_at(13); + value.parse::<_>().unwrap() + }, + { + let (_, value) = splits[3].split_at(16); + value.parse().unwrap() + }, + { + let (_, value) = splits[4].split_at(16); + value.parse().unwrap() + }, + ), + ) + }) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + + Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + BTreeMap::from_iter(vec![ + (0, Monkey::new(vec![79, 98], Mul(Some(19)), 23, 2, 3)), + (1, Monkey::new(vec![54, 65, 75, 74], Add(Some(6)), 19, 2, 0)), + ]) + ); + } +} diff --git a/day_11/src/main.rs b/day_11/src/main.rs new file mode 100644 index 0000000..539bdf7 --- /dev/null +++ b/day_11/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_11::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_11")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_11/src/monkey.rs b/day_11/src/monkey.rs new file mode 100644 index 0000000..2f0ca1f --- /dev/null +++ b/day_11/src/monkey.rs @@ -0,0 +1,68 @@ +use std::collections::VecDeque; + +#[derive(Debug, Clone, Eq, PartialEq)] +pub enum Operation { + None, + Add(Option), + Mul(Option), +} + +use Operation::*; + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct Monkey { + pub items: VecDeque, + operation: Operation, + divisible_by: i64, + if_true: isize, + if_false: isize, + pub inspected: i64, +} + +impl Monkey { + pub fn new( + items: Vec, + operation: Operation, + divisible_by: i64, + if_true: isize, + if_false: isize, + ) -> Self { + Monkey { + items: VecDeque::from_iter(items.into_iter()), + operation, + divisible_by, + if_true, + if_false, + inspected: 0, + } + } + + pub fn handle_item(&self, item: &i64) -> i64 { + let item = item.to_owned(); + match self.operation { + Add(val) => { + if let Some(val) = val { + item + val + } else { + item + item + } + } + Mul(val) => { + if let Some(val) = val { + item * val + } else { + item * item + } + } + None => item, + } + } + + pub fn throw_to(&self, worry_index: i64) -> isize { + if worry_index % self.divisible_by == 0 { + self.if_true + } else { + self.if_false + } + } +} diff --git a/day_11/src/part_01.rs b/day_11/src/part_01.rs new file mode 100644 index 0000000..ac1dbc7 --- /dev/null +++ b/day_11/src/part_01.rs @@ -0,0 +1,66 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(input: &Input) -> Result { + let mut input = input.to_owned(); + for _round in 0..20 { + let mut index = 0; + 'main: loop { + let mut throw_to_monkeys: Vec<_> = vec![]; + { + if let Some(monkey) = input.get_mut(&index) { + monkey.inspected += monkey.items.len() as i64; + 'inner: loop { + if let Some(item) = monkey.items.pop_front() { + let worry_level = monkey.handle_item(&item.to_owned()) / 3; + let throw_to = monkey.throw_to(worry_level); + throw_to_monkeys.push((throw_to, worry_level)); + } else { + break 'inner; + } + } + } + } + + { + for (index, worry_level) in throw_to_monkeys.clone() { + if let Some(monkey) = input.get_mut(&index) { + monkey.items.push_back(worry_level); + } + } + } + + index += 1; + + if index >= input.len() as isize { + break 'main; + } + } + } + + let mut values = input + .to_owned() + .values() + .map(|m| m.inspected) + .collect::>(); + + values.sort(); + values.reverse(); + + Ok(values.get(0).unwrap() * values.get(1).unwrap()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + let input = parse_input(include_str!("../../input/day_11_extended_example")); + assert_eq!(main(&input)?, 10605); + Ok(()) + } +} diff --git a/day_11/src/part_02.rs b/day_11/src/part_02.rs new file mode 100644 index 0000000..dd3d5a5 --- /dev/null +++ b/day_11/src/part_02.rs @@ -0,0 +1,24 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(_input: &Input) -> Result<()> { + Ok(()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + example + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, ()); + Ok(()) + } +} diff --git a/input/day_11 b/input/day_11 new file mode 100644 index 0000000..51ce92c --- /dev/null +++ b/input/day_11 @@ -0,0 +1,55 @@ +Monkey 0: + Starting items: 77, 69, 76, 77, 50, 58 + Operation: new = old * 11 + Test: divisible by 5 + If true: throw to monkey 1 + If false: throw to monkey 5 + +Monkey 1: + Starting items: 75, 70, 82, 83, 96, 64, 62 + Operation: new = old + 8 + Test: divisible by 17 + If true: throw to monkey 5 + If false: throw to monkey 6 + +Monkey 2: + Starting items: 53 + Operation: new = old * 3 + Test: divisible by 2 + If true: throw to monkey 0 + If false: throw to monkey 7 + +Monkey 3: + Starting items: 85, 64, 93, 64, 99 + Operation: new = old + 4 + Test: divisible by 7 + If true: throw to monkey 7 + If false: throw to monkey 2 + +Monkey 4: + Starting items: 61, 92, 71 + Operation: new = old * old + Test: divisible by 3 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 5: + Starting items: 79, 73, 50, 90 + Operation: new = old + 2 + Test: divisible by 11 + If true: throw to monkey 4 + If false: throw to monkey 6 + +Monkey 6: + Starting items: 50, 89 + Operation: new = old + 3 + Test: divisible by 13 + If true: throw to monkey 4 + If false: throw to monkey 3 + +Monkey 7: + Starting items: 83, 56, 64, 58, 93, 91, 56, 65 + Operation: new = old + 5 + Test: divisible by 19 + If true: throw to monkey 1 + If false: throw to monkey 0 diff --git a/input/day_11_extended_example b/input/day_11_extended_example new file mode 100644 index 0000000..30e09e5 --- /dev/null +++ b/input/day_11_extended_example @@ -0,0 +1,27 @@ +Monkey 0: + Starting items: 79, 98 + Operation: new = old * 19 + Test: divisible by 23 + If true: throw to monkey 2 + If false: throw to monkey 3 + +Monkey 1: + Starting items: 54, 65, 75, 74 + Operation: new = old + 6 + Test: divisible by 19 + If true: throw to monkey 2 + If false: throw to monkey 0 + +Monkey 2: + Starting items: 79, 60, 97 + Operation: new = old * old + Test: divisible by 13 + If true: throw to monkey 1 + If false: throw to monkey 3 + +Monkey 3: + Starting items: 74 + Operation: new = old + 3 + Test: divisible by 17 + If true: throw to monkey 0 + If false: throw to monkey 1 From c85b77d8c6b562faa2426eab9038e8269a4cf18c Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Sun, 11 Dec 2022 18:16:34 +0100 Subject: [PATCH 21/25] feat(day_11): completed part 2 --- README.md | 2 +- day_11/src/evaluate.rs | 56 ++++++++++++++++++++++++++++++++++++++++++ day_11/src/lib.rs | 1 + day_11/src/main.rs | 2 +- day_11/src/monkey.rs | 4 +-- day_11/src/part_01.rs | 49 ++---------------------------------- day_11/src/part_02.rs | 16 ++++++------ 7 files changed, 71 insertions(+), 59 deletions(-) create mode 100644 day_11/src/evaluate.rs diff --git a/README.md b/README.md index 4af0c96..8a62bda 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ - [Day 8](https://github.com/ankjevel/adventofcode/tree/2022/day_08) ⭐️ ⭐️ - [Day 9](https://github.com/ankjevel/adventofcode/tree/2022/day_09) ⭐️ ⭐️ - [Day 10](https://github.com/ankjevel/adventofcode/tree/2022/day_10) ⭐️ ⭐️ -- [Day 11](https://github.com/ankjevel/adventofcode/tree/2022/day_11) ⭐️ +- [Day 11](https://github.com/ankjevel/adventofcode/tree/2022/day_11) ⭐️ ⭐️ - [Day 12](#) - [Day 13](#) - [Day 14](#) diff --git a/day_11/src/evaluate.rs b/day_11/src/evaluate.rs new file mode 100644 index 0000000..ed66531 --- /dev/null +++ b/day_11/src/evaluate.rs @@ -0,0 +1,56 @@ +use crate::Input; + +pub fn evaluate(input: &Input, iterations: i32, division: i64) -> i64 { + let mut input = input.to_owned(); + let modulus = input + .values() + .map(|monkey| monkey.divisible_by) + .product::(); + + for _ in 0..iterations { + let mut index = 0; + 'main: loop { + let mut throw_to_monkeys: Vec<_> = vec![]; + { + if let Some(monkey) = input.get_mut(&index) { + monkey.inspected += monkey.items.len() as i64; + 'inner: loop { + if let Some(item) = monkey.items.pop_front() { + let worry_level = + (monkey.handle_item(&item.to_owned()) % modulus) / division; + let throw_to = monkey.throw_to(worry_level); + throw_to_monkeys.push((throw_to, worry_level)); + } else { + break 'inner; + } + } + } + } + + { + for (index, worry_level) in throw_to_monkeys.clone() { + if let Some(monkey) = input.get_mut(&index) { + monkey.items.push_back(worry_level); + } + } + } + + index += 1; + + if index >= input.len() as isize { + break 'main; + } + } + } + + let mut values = input + .to_owned() + .values() + .map(|m| m.inspected) + .collect::>(); + + values.sort(); + values.reverse(); + + values.get(0).unwrap() * values.get(1).unwrap() +} diff --git a/day_11/src/lib.rs b/day_11/src/lib.rs index 1a6aacd..04ad317 100644 --- a/day_11/src/lib.rs +++ b/day_11/src/lib.rs @@ -1,3 +1,4 @@ +pub mod evaluate; pub mod monkey; pub mod part_01; pub mod part_02; diff --git a/day_11/src/main.rs b/day_11/src/main.rs index 539bdf7..03e7823 100644 --- a/day_11/src/main.rs +++ b/day_11/src/main.rs @@ -6,7 +6,7 @@ fn main() -> Result<()> { let input = parse_input(include_str!("../../input/day_11")); println!("part_01: {:?}", part_01(&input)?); - println!("part_02: {:?}", part_02(&input)?); + println!("part_02: {:?}", part_02(&input, 10000)?); Ok(()) } diff --git a/day_11/src/monkey.rs b/day_11/src/monkey.rs index 2f0ca1f..ba914d7 100644 --- a/day_11/src/monkey.rs +++ b/day_11/src/monkey.rs @@ -12,11 +12,11 @@ use Operation::*; #[derive(Debug, Clone, Eq, PartialEq)] pub struct Monkey { pub items: VecDeque, + pub divisible_by: i64, + pub inspected: i64, operation: Operation, - divisible_by: i64, if_true: isize, if_false: isize, - pub inspected: i64, } impl Monkey { diff --git a/day_11/src/part_01.rs b/day_11/src/part_01.rs index ac1dbc7..5e164dc 100644 --- a/day_11/src/part_01.rs +++ b/day_11/src/part_01.rs @@ -1,54 +1,9 @@ use std::io::Result; -use crate::Input; +use crate::{evaluate::evaluate, Input}; pub fn main(input: &Input) -> Result { - let mut input = input.to_owned(); - for _round in 0..20 { - let mut index = 0; - 'main: loop { - let mut throw_to_monkeys: Vec<_> = vec![]; - { - if let Some(monkey) = input.get_mut(&index) { - monkey.inspected += monkey.items.len() as i64; - 'inner: loop { - if let Some(item) = monkey.items.pop_front() { - let worry_level = monkey.handle_item(&item.to_owned()) / 3; - let throw_to = monkey.throw_to(worry_level); - throw_to_monkeys.push((throw_to, worry_level)); - } else { - break 'inner; - } - } - } - } - - { - for (index, worry_level) in throw_to_monkeys.clone() { - if let Some(monkey) = input.get_mut(&index) { - monkey.items.push_back(worry_level); - } - } - } - - index += 1; - - if index >= input.len() as isize { - break 'main; - } - } - } - - let mut values = input - .to_owned() - .values() - .map(|m| m.inspected) - .collect::>(); - - values.sort(); - values.reverse(); - - Ok(values.get(0).unwrap() * values.get(1).unwrap()) + Ok(evaluate(input, 20, 3)) } #[cfg(test)] diff --git a/day_11/src/part_02.rs b/day_11/src/part_02.rs index dd3d5a5..5fb4f7f 100644 --- a/day_11/src/part_02.rs +++ b/day_11/src/part_02.rs @@ -1,9 +1,9 @@ use std::io::Result; -use crate::Input; +use crate::{evaluate::evaluate, Input}; -pub fn main(_input: &Input) -> Result<()> { - Ok(()) +pub fn main(input: &Input, iterations: i32) -> Result { + Ok(evaluate(input, iterations, 1)) } #[cfg(test)] @@ -12,13 +12,13 @@ mod tests { use super::*; - const EXAMPLE_DATA: &'static str = " - example - "; - #[test] fn it_gets_the_example_correct() -> Result<()> { - assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, ()); + let input = parse_input(include_str!("../../input/day_11_extended_example")); + assert_eq!(main(&input, 1)?, 24); + assert_eq!(main(&input, 20)?, 10197); + assert_eq!(main(&input, 1000)?, 27019168); + assert_eq!(main(&input, 2000)?, 108263829); Ok(()) } } From 4c329ca83cd2f4c2be99d83bd79c5af284f9389d Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Mon, 12 Dec 2022 09:34:17 +0100 Subject: [PATCH 22/25] wip(day_12): can only solve the example --- day_12/Cargo.toml | 15 ++++ day_12/src/lib.rs | 79 +++++++++++++++++++++ day_12/src/main.rs | 12 ++++ day_12/src/part_01.rs | 29 ++++++++ day_12/src/part_02.rs | 24 +++++++ day_12/src/pathfinding.rs | 143 ++++++++++++++++++++++++++++++++++++++ day_12/src/point.rs | 45 ++++++++++++ input/day_12 | 41 +++++++++++ 8 files changed, 388 insertions(+) create mode 100644 day_12/Cargo.toml create mode 100644 day_12/src/lib.rs create mode 100644 day_12/src/main.rs create mode 100644 day_12/src/part_01.rs create mode 100644 day_12/src/part_02.rs create mode 100644 day_12/src/pathfinding.rs create mode 100644 day_12/src/point.rs create mode 100644 input/day_12 diff --git a/day_12/Cargo.toml b/day_12/Cargo.toml new file mode 100644 index 0000000..1e27c32 --- /dev/null +++ b/day_12/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "day_12" +version = "0.1.0" +edition = "2021" +authors = ["Dennis Pettersson "] + +[lib] +doctest = false + +[[bin]] +name = "day_12" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day_12/src/lib.rs b/day_12/src/lib.rs new file mode 100644 index 0000000..2174718 --- /dev/null +++ b/day_12/src/lib.rs @@ -0,0 +1,79 @@ +use std::collections::HashMap; + +use point::Point; + +pub mod part_01; +pub mod part_02; +pub mod pathfinding; +pub mod point; + +type Map = HashMap; + +#[derive(Eq, PartialEq, Debug)] +pub struct Grid { + start: Point, + end: Point, + map: Map, +} + +pub type Input = Grid; + +pub fn parse_input(input: &str) -> Input { + let mut lines = input + .lines() + .map(str::trim) + .filter(|string| !string.is_empty()) + .map(str::to_owned) + .collect::>(); + lines.reverse(); + + let mut start = Point::new(); + let mut end = Point::new(); + let mut map: Map = HashMap::new(); + for (y, row) in lines.into_iter().enumerate() { + for (x, tile) in row.chars().enumerate() { + let point = Point { x, y }; + match tile { + 'S' => { + start = point.to_owned(); + } + 'E' => { + end = point.to_owned(); + } + _ => {} + } + map.insert(point, tile); + } + } + Grid { start, end, map } +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE_DATA: &'static str = " + SE + ab + "; + + #[test] + fn it_parses_example() { + assert_eq!( + parse_input(&EXAMPLE_DATA), + Grid { + start: Point { x: 0, y: 1 }, + end: Point { x: 1, y: 1 }, + map: HashMap::from_iter( + vec![ + (Point { x: 0, y: 1 }, 'S'), + (Point { x: 1, y: 1 }, 'E'), + (Point { x: 0, y: 0 }, 'a'), + (Point { x: 1, y: 0 }, 'b'), + ] + .into_iter() + ) + } + ); + } +} diff --git a/day_12/src/main.rs b/day_12/src/main.rs new file mode 100644 index 0000000..d085f1e --- /dev/null +++ b/day_12/src/main.rs @@ -0,0 +1,12 @@ +use std::io::Result; + +use day_12::{parse_input, part_01::main as part_01, part_02::main as part_02}; + +fn main() -> Result<()> { + let input = parse_input(include_str!("../../input/day_12")); + + println!("part_01: {:?}", part_01(&input)?); + println!("part_02: {:?}", part_02(&input)?); + + Ok(()) +} diff --git a/day_12/src/part_01.rs b/day_12/src/part_01.rs new file mode 100644 index 0000000..82f423a --- /dev/null +++ b/day_12/src/part_01.rs @@ -0,0 +1,29 @@ +use std::io::Result; + +use crate::{pathfinding::find_path, Input}; + +pub fn main(input: &Input) -> Result { + let path = find_path(&input.map, input.start, input.end); + Ok(path.len()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + Sabqponm + abcryxxl + accszExk + acctuvwj + abdefghi + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 31); + Ok(()) + } +} diff --git a/day_12/src/part_02.rs b/day_12/src/part_02.rs new file mode 100644 index 0000000..dd3d5a5 --- /dev/null +++ b/day_12/src/part_02.rs @@ -0,0 +1,24 @@ +use std::io::Result; + +use crate::Input; + +pub fn main(_input: &Input) -> Result<()> { + Ok(()) +} + +#[cfg(test)] +mod tests { + use crate::parse_input; + + use super::*; + + const EXAMPLE_DATA: &'static str = " + example + "; + + #[test] + fn it_gets_the_example_correct() -> Result<()> { + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, ()); + Ok(()) + } +} diff --git a/day_12/src/pathfinding.rs b/day_12/src/pathfinding.rs new file mode 100644 index 0000000..7eac253 --- /dev/null +++ b/day_12/src/pathfinding.rs @@ -0,0 +1,143 @@ +use std::{ + cmp::Ordering, + collections::{BinaryHeap, HashMap}, + usize, +}; + +use crate::{point::Point, Map}; + +#[derive(Eq, PartialEq, Debug)] +struct State { + cost: usize, + point: Point, +} + +impl Ord for State { + fn cmp(&self, other: &State) -> Ordering { + other + .cost + .cmp(&self.cost) + .then_with(|| self.point.cmp(&other.point)) + } +} + +impl PartialOrd for State { + fn partial_cmp(&self, other: &State) -> Option { + Some(self.cmp(other)) + } +} + +fn distance(p1: &Point, p2: &Point) -> f64 { + ((p2.x as f64 - p1.x as f64).powf(2f64) + (p2.y as f64 - p1.y as f64).powf(2f64)).sqrt() +} + +pub fn adjacent(map: &Map, point: &Point) -> Vec { + let mut tiles = Vec::new(); + + let mut vec: Vec<(isize, isize)> = vec![(0, 1), (1, 0)]; + + if point.x > 0 { + vec.push((-1, 0)); + } + + if point.y > 0 { + vec.push((0, -1)); + } + + for (x, y) in vec { + let new_pos = Point { + x: (point.x as isize + x) as usize, + y: (point.y as isize + y) as usize, + }; + if let Some(_) = map.get(&new_pos) { + tiles.push(new_pos.to_owned()); + }; + } + + tiles +} + +pub fn find_path(map: &Map, start: Point, goal: Point) -> Vec { + let can_move = |point: &Point, came_from: &Point| { + let last_tile = map.get(came_from).unwrap_or(&'Z'); + match map.get(&point) { + Some(tile) => { + (last_tile == &'S' && tile == &'a') + || (last_tile == &'z' && tile == &'E') + || tile == last_tile + || (*tile as i32) - (*last_tile as i32) == 1 + } + None => false, + } + }; + + let mut frontier = BinaryHeap::new(); + let mut dist: HashMap = HashMap::new(); + + frontier.push(State { + cost: 0, + point: start, + }); + + let mut came_from = HashMap::new(); + came_from.insert(start, None); + + dist.insert(start.to_owned(), 0); + + while let Some(State { point, cost }) = frontier.pop() { + let is_goal = point == goal; + if is_goal { + break; + } + + let to_point = dist.entry(point.to_owned()).or_insert(usize::MAX); + + if cost > *to_point { + continue; + } + + for edge in adjacent(map, &point) { + if !came_from.contains_key(&edge) && can_move(&edge, &point) { + let next_cost = distance(&goal, &edge) as usize; + + let prev_cost = dist.entry(edge.to_owned()).or_insert(usize::MAX); + + if next_cost > *prev_cost { + continue; + } + + frontier.push(State { + point: edge, + cost: next_cost, + }); + + came_from.insert(edge, Some(point)); + *prev_cost = next_cost; + } + } + } + + let mut current = goal; + let mut path = vec![current]; + + while current != start { + match came_from.get(¤t) { + Some(c) => { + if c.is_none() { + return vec![]; + } + + current = c.unwrap(); + + if current == start { + continue; + } + + path.push(current); + } + _ => return vec![], + } + } + + path +} diff --git a/day_12/src/point.rs b/day_12/src/point.rs new file mode 100644 index 0000000..b0189eb --- /dev/null +++ b/day_12/src/point.rs @@ -0,0 +1,45 @@ +use std::cmp::Ordering::{self, Equal, Greater, Less}; + +#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug, Default)] +pub struct Point { + pub x: usize, + pub y: usize, +} + +impl Point { + pub fn new() -> Self { + Point { x: 0, y: 0 } + } +} + +impl Ord for Point { + fn cmp(&self, other: &Point) -> Ordering { + if self.y == other.y { + if self.x < other.x { + Less + } else if self.x > other.x { + Greater + } else { + Equal + } + } else if self.y < other.y { + Less + } else if self.y > other.y { + Greater + } else { + if self.x < other.x { + Less + } else if self.x > other.x { + Greater + } else { + Equal + } + } + } +} + +impl PartialOrd for Point { + fn partial_cmp(&self, other: &Point) -> Option { + Some(self.cmp(other)) + } +} diff --git a/input/day_12 b/input/day_12 new file mode 100644 index 0000000..1fb4f0a --- /dev/null +++ b/input/day_12 @@ -0,0 +1,41 @@ +abcccccccccccccccccaaccccccccccccaaaaaaaacccccccccccaaaaaccccaaaaaaccaaaaaaaaaaaaaaaaaccccccccccccccccaaacccccaaaaaaaacccaaaccccccccccccccccccccccccccccccccccccccccccccccccccaaaaa +abccccccccccccccccaaacaacccccccccccaaaacccccccccccccaaaaaacccaaaaaaccaaaaaaaaaaaaaaaaaaaacccccccccaaacaaacccccaaaaaaaaaccaaaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa +abcccccccccccccccccaaaaacccccccccccaaaaaccccccccccccaaaaaaccccaaaacccaaaacccaaaaaaaaaaaaacccccccccaaaaaaaaaacccaaaaaaaaccaaaaccccccccccccccccccccccccccccccccccaaacccccccccccaaaaaa +abcccccccccccccccaaaaaacccccccccccaaacaaccaaccccccccaaaaaaccccaaaacccaaaccccaaaaaaaaaaaaaccccccccccaaaaaaaaaccaaaaaacccccaaacccccccccccccccccccccccccccccccccccaaaccccccccccccccaaa +abcccccccccccccccaaaaaaaacccccccccaacccacaaacaacccccccaaccccccaccaccccccccaaaaaaaaaaaaccccccccccccccaaaaaaacccaaaaaaacccccccccccccaacccccccccccccccccccccccccccaaaccccccccccccccaaa +abcccccccccccccccaacaaaaacccccccccccccccccaaaaacccccccccccccccccccccccccccaaaaaaaaaaaaccccccccccccccaaaaaaccccaaccaaacccccccccccaaaaaaccccccccccccccccccccccccccdccccccccccccccccaa +abccaacccccccaaacccaaacaccccccccccaaacccaaaaaaccccccccccccccccccccccccccccaaacccaaaaaacaaaaccccccccaaaaaaaccccccccaaacccccccccccaaaaaacccccccccccccccccllllllcccdddddcccccccccccccc +abaaaacccacccaaccccaacccccccccccccaaacccaaaaaaaacccccccccccccccccaaccccccccacccaaaaccccaaaaccccccccaaacaaaccccccccccccccccccccccaaaaaaccccccccccccccccllllllllldddddddddddccaaccccc +abaaaaccaaaaaaaacccccccccccccccaaaaaaaacaacaaaaacccccccccccccccccaaacccccccaaacaaccccccaaaacccccccccccccaaccccccccccccccccccccccaaaaaccccccccccccccccclllllllllldddddddddeeaaaccccc +abaaaccccaaaaaaaaccccccccaaacccaaaaaaaacccaaacccccccccccccccccaaaaaaaaccccccaaaaacccccccaacccccccccccccccccccccccccccccccccccccccaaaaccccaaaccccccccckllppppplllmmmmmmmdeeeeaaccccc +abaaaacccaaaaaaaaacccccaaaaaaccccaaaaaccccaaccccccccccccccccccaaaaaaaaccccccaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccaaaacccccccckklpppppppplmmmmmmmmmeeeeaccccc +abaaaacccaaaaaaaaacccccaaaaaacccaaaaaaccccccccaacccccccccccccccaaaaaaccccccaaaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccaaaacccccccckkkppppppppqmmmmmmmmmmeeeaacccc +abaaaaaccaaaaaaaaccccccaaaaaacccaaaaaaccccccacaaaacccccccccccccaaaaaaccccccaaaaaaaaccccccccaaaaccccccccaaacccccccccccccccccccccccccccccccaaacccccccckkkpppuuuppqqqqqqqqmmmeeeeacccc +abacccccaaaaaaaccccccccaaaaaccccaccaaaccccccaaaaaacccccacccccccaaaaaaccccccaaaaaacaaaccccccaaaaccccccccaaaacccccccccccccccccccccccccccccccccccccccckkkpppuuuuuuqqqqqqqqqnnneeeccccc +abcccccccaccaaaccccccccaaaaacccccccccccccccccaaaacccaaaacccccccaacaaacccccccccaaacaaaaaccccaaaaccccccccaaaaccccccccccccccccccccccccccccccccccccccckkkkpppuuuuuuuqvvvvqqqnnneeeccccc +abcccccccccccaaacaaccccccccccccccccccccccccccaaaacccaaaaaacccccccccccccccccccccccaaaaaaccccaaacccccccccaaaccccccccccccccccccccccccccccccccccccccckkkkrrpuuuxxxuvvvvvvvqqnnneeeccccc +abcccccccccccccccaacaaaccccccccccccccccccccccaacaacccaaaaacccccccccccccccccccccccaaaaaaccccccccccccccccccccccccccccccccccccccccccccccccccccccccckkkkrrrruuxxxxuvvvvvvvqqnnneeeccccc +abcccccccccccccccaaaaacccccccccccccccccccccccccccaccaaaaacccccaaccccccccccccccccccaaaaaccccccccccccccccaaaccccccccccccccaaacccccccccccccccccccckkkkrrrruuuxxxxyyyyyvvvqqnnneecccccc +abcccccccccccccaaaaaacccccccccccaacaacccccccccccaaaaaaaaacccacaaaacaaccaaccccccccaaacaaccccccccccaaccccaaaaaacccaacaacccaaacacccccccccccccccccjjkkrrrruuuuuxxxyyyyyvvqrqnneffcccccc +abcccccccccccccaaaaaaaacccaaacccaaaaaccccccaacccaaaaaaaaacccaaaaaacaaaaaaccccccccaaacaaacccccccaaaaaacaaaaaaacccaaaaacaaaaaaaaccccccccccccccccjjjrrrtttuuxxxxxyyyyyvvrrnnnfffcccccc +SbccccccccccccccccaaaaacccaaaaccaaaaaaccccaaaaaacaaaaaaaaacccaaaacccaaaaaccccccccaaaaaaacccccccaaaaaaaaaaaaaaccaaaaaccaaaaaaaaccccccccccccccccjjjrrrtttxxxEzzzzyyyvvrrrnnnfffcccccc +abccccccccccaaaccaaccaacccaaaaccaaaaaacccccaaaaacaaaaaaaaacccaaaaccaaaaaacccccccccaaaaaaccccccccaaaacaaaaaaacccaaaaaaccaaaaaacccccccccccccccccjjjrrrtttxxxxxyyyyyyvvrrrnnnfffcccccc +abcccccccccaaaaccaacccccccaaacccaaaaaacccaaaaaaaaaaaaaaaaacccaacacaaaaaaaacccccaaaaaaaacccccccccaaaacccaaaaaaccccaaaacccaaaaacccccccccccccccccjjjrrrtttxxxxxyyyyyyywvrrnnnfffcccccc +abcccccccccaaaacccccccccccccccccccaaaccccaaaaaaaaaaaaaaaaaacccccccaaaaaaaacccccaaaaaaaaaccccccccaccacccaaaaaaccccacccccaaaaaacccccccccccccccccjjjrrrrttttxxxyyyyyyywwrrroooffcccccc +abccccccccccaaaccccccccccccccccccccccccccaaaaaaaaaccaaaaaaaccccccccccaaccccccccaaaaaaaaaaccccccccccccccaacccccccccccccccaaccccccccccccccccccccjjjjqqqqttttxxyywwwwwwwwrrooofffccccc +abcccccccccccccccccccccccccccccccccccccccccaaacacccccaaaaccccccccccccaacccccccccccaaacaaacccccccccccccccccccccccccccccaaacccccccccccccccccccaacjjjjqqqqqttwwwwwwwwwwwrrrooofffccccc +abcccccccccccccccccccccccccccccccccccccccccaaccccccccccaacccccccccccccccccccccccccaaacccccccccccccccccccccccccccccccccaaacccccccccccccccccaaaaaajjjjqqqqttwwwwwwsswwrrrrooofffccccc +abcccccaaaaccccccccccccccaacaaccccccccccccccccccccccccccccccccccccccccccccccccccccaacccccccccccccccccccccccccccccccaaaaaaaaccaaaacccccccccaaaaaacjjjiqqqtttwwwwsssssrrrrooofffccccc +abccccaaaaaccccccccccccccaaaaacccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaaaaccaaaaacccccccccaaaaacciiiiqqttswwwssssssrrroooogffccccc +abccccaaaaaacccccccccccccaaaaaacccaaaccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccaaaaaccaaaaaaccccccccaaaaacccciiiqqqssssssspppooooooogggaccccc +abccccaaaaaaccccccccaacccaaaaaaccccaacccccccccccccccccccccccccccccccccaaaaccccccccccccccccccccccccccccccccccccccccccaaaaaaccaaaaaaccccccccaaaaaccccciiiqqsssssspppppoooooggggaacccc +abccccaaaaaccccccaaaaacccaaaaaacaacaaaaaccccccccccccccccccccaacccccccaaaaacccccccccccaaaccccccccccccccccccccccccccccaaaaaacccaaaaacccccccccacccccccciiiqqqpssspppppgggggggggaaacccc +abccccccaaacccccccaaaaaccccaaaccaaaaaaaacccccccaacccccccaaccaacccccccaaaaaacccccccccaaaacccccccccccaaaaccccccccccccaaccaaacccaaacccccaaacaaaccccccccciiqqppppppphhhggggggggaaaacccc +abccccccccccccccccaaaaaccccccccccaaaaaccccccccaaacaaccccaaaaaacccccccaaaaaaaccccccccaaaacccccccccccaaaacccccccccaaaaaacccccccccccccccaaaaaaaccccccccciiippppppphhhhggggggcaaacccccc +abaacccccccccccccaaaaaccccccccccccaaaaaccccccccaaaaacccccaaaaaaacccccaaaaacaaacccccccaaacccccccccccaaaacccccccccaaaaacccccccccccccccccaaaaaacccccccccciiiippphhhhhhcccccccaaacccccc +abaacccccccccccccccaaacccccccccccaaacaaccccccaaaaaaccccccaaaaaaacccaaccaaacaaaaccaaaccccccccccccccccaaacccccccccaaaaaaacccccccccccccccaaaaaaaacccccccciiihhhhhhhhaaaccccccccccccccc +abaaccccccccccccccccccccccccccccccaacccccccccaaaaaaaacccaaaaaaccaaaaaacccccaaaacaaaaaccccccccccccccccccccccccccaaaaaaaaccccccccccccccaaaaaaaaaccccccccciihhhhhhcaaaacccccccccccccca +abaaccccccccccccccccccccccccccccccccaacccccccaacaaaaacccaaaaaaccaaaaacccccccaaaaaaaacccccccccccccccccccccccccccaaaaaaaacccccccccaaacaaaaaaaaaacccccccccccchhhaccccaacccccccccccccca +abaaccccccccccccccccccccccccccccccccaaaaaaccccccaaccccccccccaaccaaaaaaacccccaaaaaaaccccccccccccccccccccccccaaaccacaaacccccccccccaaaaaaacaaacaaaaaacccccccccaaacccccccccccccccaaaaaa +abccccccccccccccccccccccccccccccccccaaaaaccccccaaccccccccccccccaaaaaaaacaaaaaaaaaaccccccccccccccccccccccccaaaaaaccaaaccccccccccccaaaaaacaaacaaaaaacccccccccaaaccccccccccccccccaaaaa +abccccccccccccccccccccccccccccccccaaaaaaaacccccccccccccccccccccaaaaaaaacaaaaaaaaaaaaacccccccccccccccccccccaaaaaacccccccccccccccaaaaaaaaaaaaaaaaaacccccccccccccccccccccccccccccaaaaa From 6a3a968e85f4ed10b778c13907c06a2f90935b87 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Tue, 13 Dec 2022 08:47:27 +0100 Subject: [PATCH 23/25] wip(day_12): going for BFS --- day_12/src/lib.rs | 4 +- day_12/src/part_01.rs | 4 +- day_12/src/pathfinding.rs | 84 ++++++++++++++++++++------------------- day_12/src/print.rs | 66 ++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 45 deletions(-) create mode 100644 day_12/src/print.rs diff --git a/day_12/src/lib.rs b/day_12/src/lib.rs index 2174718..1dfdb1e 100644 --- a/day_12/src/lib.rs +++ b/day_12/src/lib.rs @@ -6,6 +6,7 @@ pub mod part_01; pub mod part_02; pub mod pathfinding; pub mod point; +pub mod print; type Map = HashMap; @@ -19,13 +20,12 @@ pub struct Grid { pub type Input = Grid; pub fn parse_input(input: &str) -> Input { - let mut lines = input + let lines = input .lines() .map(str::trim) .filter(|string| !string.is_empty()) .map(str::to_owned) .collect::>(); - lines.reverse(); let mut start = Point::new(); let mut end = Point::new(); diff --git a/day_12/src/part_01.rs b/day_12/src/part_01.rs index 82f423a..434f49b 100644 --- a/day_12/src/part_01.rs +++ b/day_12/src/part_01.rs @@ -1,12 +1,12 @@ use std::io::Result; -use crate::{pathfinding::find_path, Input}; +use crate::{pathfinding::find_path, print::print, Input}; pub fn main(input: &Input) -> Result { let path = find_path(&input.map, input.start, input.end); + print(&path, &input.map); Ok(path.len()) } - #[cfg(test)] mod tests { use crate::parse_input; diff --git a/day_12/src/pathfinding.rs b/day_12/src/pathfinding.rs index 7eac253..1be717e 100644 --- a/day_12/src/pathfinding.rs +++ b/day_12/src/pathfinding.rs @@ -28,7 +28,7 @@ impl PartialOrd for State { } fn distance(p1: &Point, p2: &Point) -> f64 { - ((p2.x as f64 - p1.x as f64).powf(2f64) + (p2.y as f64 - p1.y as f64).powf(2f64)).sqrt() + ((p2.x as f64 - p1.x as f64).powf(2.0) + (p2.y as f64 - p1.y as f64).powf(2.0)).sqrt() } pub fn adjacent(map: &Map, point: &Point) -> Vec { @@ -57,63 +57,72 @@ pub fn adjacent(map: &Map, point: &Point) -> Vec { tiles } +fn to_num(input: char) -> i32 { + let n = input as i32 - 65; + if n >= 32 { + n - 32 + } else { + n + 26 + } +} + pub fn find_path(map: &Map, start: Point, goal: Point) -> Vec { let can_move = |point: &Point, came_from: &Point| { - let last_tile = map.get(came_from).unwrap_or(&'Z'); + let last_tile = map.get(came_from).unwrap(); match map.get(&point) { Some(tile) => { + let diff = to_num(*tile) - to_num(*last_tile); (last_tile == &'S' && tile == &'a') || (last_tile == &'z' && tile == &'E') || tile == last_tile - || (*tile as i32) - (*last_tile as i32) == 1 + || diff <= 1 } None => false, } }; let mut frontier = BinaryHeap::new(); - let mut dist: HashMap = HashMap::new(); + // let mut distances = HashMap::new(); + let mut visited = HashMap::new(); frontier.push(State { cost: 0, point: start, }); - let mut came_from = HashMap::new(); - came_from.insert(start, None); - - dist.insert(start.to_owned(), 0); + visited.insert(start, None); + // distances.insert(start.to_owned(), 0); - while let Some(State { point, cost }) = frontier.pop() { - let is_goal = point == goal; - if is_goal { + while let Some(State { point, cost: _ }) = frontier.pop() { + if point == goal { break; } - let to_point = dist.entry(point.to_owned()).or_insert(usize::MAX); - - if cost > *to_point { - continue; - } + // let to_point = distances.entry(point.to_owned()).or_insert(usize::MAX); + // if cost > *to_point { + // continue; + // } for edge in adjacent(map, &point) { - if !came_from.contains_key(&edge) && can_move(&edge, &point) { - let next_cost = distance(&goal, &edge) as usize; + if visited.contains_key(&edge) || !can_move(&edge, &point) { + continue; + } - let prev_cost = dist.entry(edge.to_owned()).or_insert(usize::MAX); + // let next_cost = distance(&goal, &edge) as usize; - if next_cost > *prev_cost { - continue; - } + // let prev_cost = distances.entry(edge.to_owned()).or_insert(usize::MAX); - frontier.push(State { - point: edge, - cost: next_cost, - }); + // if next_cost > *prev_cost { + // continue; + // } - came_from.insert(edge, Some(point)); - *prev_cost = next_cost; - } + // frontier.push(State { + // point: edge, + // cost: next_cost.to_owned(), + // }); + + visited.insert(edge, Some(point)); + // *prev_cost = next_cost; } } @@ -121,19 +130,12 @@ pub fn find_path(map: &Map, start: Point, goal: Point) -> Vec { let mut path = vec![current]; while current != start { - match came_from.get(¤t) { - Some(c) => { - if c.is_none() { - return vec![]; + match visited.get(¤t) { + Some(previous) => { + current = previous.unwrap(); + if current != start { + path.push(current); } - - current = c.unwrap(); - - if current == start { - continue; - } - - path.push(current); } _ => return vec![], } diff --git a/day_12/src/print.rs b/day_12/src/print.rs new file mode 100644 index 0000000..b3d2f34 --- /dev/null +++ b/day_12/src/print.rs @@ -0,0 +1,66 @@ +use std::collections::{BTreeMap, HashMap}; + +use crate::point::Point; + +fn join<'a>(a: String, b: String) -> String { + let (a, b) = (a.to_owned(), b.to_owned()); + let c = [a, b].concat(); + + c.to_string() +} + +fn max(map: &Vec) -> (usize, usize) { + let (x, y) = map.iter().fold((0, 0), |mut acc, point| { + let (x, y) = (point.x, point.y); + if acc.0 < x { + acc.0 = x.to_owned(); + } + if acc.1 < y { + acc.1 = y.to_owned(); + } + acc + }); + + (x, y) +} + +fn grid_as_tree_map( + grid: &HashMap, + max_x: &usize, + max_y: &usize, + path: &Vec, +) -> BTreeMap { + let mut tree_map = BTreeMap::new(); + for y in 0..=(max_y + 1) { + for x in 0..=(max_x + 1) { + let point = Point { x, y }; + let key = grid.get(&point).unwrap_or(&' ').to_owned(); + let key = if path.contains(&point) || key == 'S' { + format!("\x1b[93m{}\x1b[0m", key) + } else { + key.to_string() + }; + tree_map.insert(point, key.to_string()); + } + } + tree_map +} + +pub fn print(path: &Vec, map: &HashMap) { + let (max_x, max_y) = max(&path); + + let mut current = 0; + let mut string = "".to_string(); + let mut out = Vec::new(); + for (point, tile) in grid_as_tree_map(&map, &max_x, &max_y, &path) { + if point.y != current { + out.push(string.to_owned()); + string = "".to_string(); + current = point.y.to_owned(); + } + + string = join(string, tile.to_string()) + } + + println!("{}", out.join("\r\n")); +} From 92378b87bbdc5f6f9c08a7b60594b7ff38c41e08 Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Tue, 13 Dec 2022 22:31:40 +0100 Subject: [PATCH 24/25] feat(day_12): completed part 1 (using BFS) --- day_12/src/lib.rs | 40 +++++++++++-------- day_12/src/pathfinding.rs | 81 ++++----------------------------------- day_12/src/print.rs | 27 +++++++++---- 3 files changed, 49 insertions(+), 99 deletions(-) diff --git a/day_12/src/lib.rs b/day_12/src/lib.rs index 1dfdb1e..7f1d71c 100644 --- a/day_12/src/lib.rs +++ b/day_12/src/lib.rs @@ -8,7 +8,7 @@ pub mod pathfinding; pub mod point; pub mod print; -type Map = HashMap; +pub type Map = HashMap; #[derive(Eq, PartialEq, Debug)] pub struct Grid { @@ -30,19 +30,25 @@ pub fn parse_input(input: &str) -> Input { let mut start = Point::new(); let mut end = Point::new(); let mut map: Map = HashMap::new(); + let a = 'a' as u32; + let z = 'z' as u32; for (y, row) in lines.into_iter().enumerate() { for (x, tile) in row.chars().enumerate() { let point = Point { x, y }; - match tile { - 'S' => { - start = point.to_owned(); - } - 'E' => { - end = point.to_owned(); - } - _ => {} - } - map.insert(point, tile); + map.insert( + point, + match tile { + 'S' => { + start = point.to_owned(); + 0 + } + 'E' => { + end = point.to_owned(); + z - a + 2 + } + _ => (tile as u32) - a + 1, + }, + ); } } Grid { start, end, map } @@ -62,14 +68,14 @@ mod tests { assert_eq!( parse_input(&EXAMPLE_DATA), Grid { - start: Point { x: 0, y: 1 }, - end: Point { x: 1, y: 1 }, + start: Point { x: 0, y: 0 }, + end: Point { x: 1, y: 0 }, map: HashMap::from_iter( vec![ - (Point { x: 0, y: 1 }, 'S'), - (Point { x: 1, y: 1 }, 'E'), - (Point { x: 0, y: 0 }, 'a'), - (Point { x: 1, y: 0 }, 'b'), + (Point { x: 0, y: 0 }, 0), + (Point { x: 0, y: 1 }, 1), + (Point { x: 1, y: 1 }, 2), + (Point { x: 1, y: 0 }, 27), ] .into_iter() ) diff --git a/day_12/src/pathfinding.rs b/day_12/src/pathfinding.rs index 1be717e..94b1602 100644 --- a/day_12/src/pathfinding.rs +++ b/day_12/src/pathfinding.rs @@ -1,36 +1,10 @@ use std::{ - cmp::Ordering, - collections::{BinaryHeap, HashMap}, + collections::{HashMap, VecDeque}, usize, }; use crate::{point::Point, Map}; -#[derive(Eq, PartialEq, Debug)] -struct State { - cost: usize, - point: Point, -} - -impl Ord for State { - fn cmp(&self, other: &State) -> Ordering { - other - .cost - .cmp(&self.cost) - .then_with(|| self.point.cmp(&other.point)) - } -} - -impl PartialOrd for State { - fn partial_cmp(&self, other: &State) -> Option { - Some(self.cmp(other)) - } -} - -fn distance(p1: &Point, p2: &Point) -> f64 { - ((p2.x as f64 - p1.x as f64).powf(2.0) + (p2.y as f64 - p1.y as f64).powf(2.0)).sqrt() -} - pub fn adjacent(map: &Map, point: &Point) -> Vec { let mut tiles = Vec::new(); @@ -57,78 +31,37 @@ pub fn adjacent(map: &Map, point: &Point) -> Vec { tiles } -fn to_num(input: char) -> i32 { - let n = input as i32 - 65; - if n >= 32 { - n - 32 - } else { - n + 26 - } -} - pub fn find_path(map: &Map, start: Point, goal: Point) -> Vec { let can_move = |point: &Point, came_from: &Point| { let last_tile = map.get(came_from).unwrap(); match map.get(&point) { - Some(tile) => { - let diff = to_num(*tile) - to_num(*last_tile); - (last_tile == &'S' && tile == &'a') - || (last_tile == &'z' && tile == &'E') - || tile == last_tile - || diff <= 1 - } + Some(tile) => *tile as i64 - *last_tile as i64 <= 1, None => false, } }; - let mut frontier = BinaryHeap::new(); - // let mut distances = HashMap::new(); + let mut frontier = VecDeque::new(); let mut visited = HashMap::new(); - frontier.push(State { - cost: 0, - point: start, - }); - + frontier.push_front(start); visited.insert(start, None); - // distances.insert(start.to_owned(), 0); - - while let Some(State { point, cost: _ }) = frontier.pop() { + while !frontier.is_empty() { + let point = frontier.pop_front().unwrap(); if point == goal { break; } - // let to_point = distances.entry(point.to_owned()).or_insert(usize::MAX); - // if cost > *to_point { - // continue; - // } - for edge in adjacent(map, &point) { if visited.contains_key(&edge) || !can_move(&edge, &point) { continue; } - - // let next_cost = distance(&goal, &edge) as usize; - - // let prev_cost = distances.entry(edge.to_owned()).or_insert(usize::MAX); - - // if next_cost > *prev_cost { - // continue; - // } - - // frontier.push(State { - // point: edge, - // cost: next_cost.to_owned(), - // }); - + frontier.push_back(edge); visited.insert(edge, Some(point)); - // *prev_cost = next_cost; } } let mut current = goal; let mut path = vec![current]; - while current != start { match visited.get(¤t) { Some(previous) => { diff --git a/day_12/src/print.rs b/day_12/src/print.rs index b3d2f34..f8963ad 100644 --- a/day_12/src/print.rs +++ b/day_12/src/print.rs @@ -1,6 +1,6 @@ -use std::collections::{BTreeMap, HashMap}; +use std::collections::BTreeMap; -use crate::point::Point; +use crate::{point::Point, Map}; fn join<'a>(a: String, b: String) -> String { let (a, b) = (a.to_owned(), b.to_owned()); @@ -25,7 +25,7 @@ fn max(map: &Vec) -> (usize, usize) { } fn grid_as_tree_map( - grid: &HashMap, + grid: &Map, max_x: &usize, max_y: &usize, path: &Vec, @@ -34,19 +34,30 @@ fn grid_as_tree_map( for y in 0..=(max_y + 1) { for x in 0..=(max_x + 1) { let point = Point { x, y }; - let key = grid.get(&point).unwrap_or(&' ').to_owned(); - let key = if path.contains(&point) || key == 'S' { + let key = grid.get(&point).unwrap_or(&(' ' as u32)).to_owned() + 'a' as u32 - 1; + let key = if key < 'a' as u32 { + 'S' as u32 + } else if key > 'z' as u32 { + 'E' as u32 + } else { + key + }; + + let key = char::from_u32(key).unwrap_or(' ').to_string(); + let key = if path.contains(&point) || key == "S" { format!("\x1b[93m{}\x1b[0m", key) + } else if key == "E" { + " ".to_string() } else { - key.to_string() + key }; - tree_map.insert(point, key.to_string()); + tree_map.insert(point, key); } } tree_map } -pub fn print(path: &Vec, map: &HashMap) { +pub fn print(path: &Vec, map: &Map) { let (max_x, max_y) = max(&path); let mut current = 0; From 6147d989126db632074f0bbf74928c48d8f09d9e Mon Sep 17 00:00:00 2001 From: Dennis Pettersson Date: Wed, 14 Dec 2022 08:41:40 +0100 Subject: [PATCH 25/25] feat(day_12): completed part 2 --- day_12/src/lib.rs | 11 ++++++- day_12/src/main.rs | 4 +-- day_12/src/part_01.rs | 6 ++-- day_12/src/part_02.rs | 68 +++++++++++++++++++++++++++++++++++---- day_12/src/pathfinding.rs | 20 ++++++------ day_12/src/print.rs | 13 ++++---- 6 files changed, 95 insertions(+), 27 deletions(-) diff --git a/day_12/src/lib.rs b/day_12/src/lib.rs index 7f1d71c..3a5e6e6 100644 --- a/day_12/src/lib.rs +++ b/day_12/src/lib.rs @@ -10,13 +10,22 @@ pub mod print; pub type Map = HashMap; -#[derive(Eq, PartialEq, Debug)] +#[derive(Debug, Clone, Eq, PartialEq)] pub struct Grid { start: Point, end: Point, map: Map, } +impl Grid { + pub fn can_move(map: &Map, point: &Point, came_from: &Point) -> bool { + match map.get(&point) { + Some(tile) => *tile as i64 - *(map.get(came_from).unwrap()) as i64 <= 1, + None => false, + } + } +} + pub type Input = Grid; pub fn parse_input(input: &str) -> Input { diff --git a/day_12/src/main.rs b/day_12/src/main.rs index d085f1e..aeb21e0 100644 --- a/day_12/src/main.rs +++ b/day_12/src/main.rs @@ -5,8 +5,8 @@ use day_12::{parse_input, part_01::main as part_01, part_02::main as part_02}; fn main() -> Result<()> { let input = parse_input(include_str!("../../input/day_12")); - println!("part_01: {:?}", part_01(&input)?); - println!("part_02: {:?}", part_02(&input)?); + println!("part_01: {:?}", part_01(&input.clone())?); + println!("part_02: {:?}", part_02(&input.clone())?); Ok(()) } diff --git a/day_12/src/part_01.rs b/day_12/src/part_01.rs index 434f49b..0a9c1b2 100644 --- a/day_12/src/part_01.rs +++ b/day_12/src/part_01.rs @@ -1,9 +1,11 @@ use std::io::Result; -use crate::{pathfinding::find_path, print::print, Input}; +use crate::{pathfinding::bfs, print::print, Input}; pub fn main(input: &Input) -> Result { - let path = find_path(&input.map, input.start, input.end); + let path = bfs(&input.map, input.start, input.end, |a, b| { + Input::can_move(&input.map, a, b) + }); print(&path, &input.map); Ok(path.len()) } diff --git a/day_12/src/part_02.rs b/day_12/src/part_02.rs index dd3d5a5..5db7c10 100644 --- a/day_12/src/part_02.rs +++ b/day_12/src/part_02.rs @@ -1,9 +1,61 @@ -use std::io::Result; +use std::{ + collections::{HashMap, VecDeque}, + io::Result, +}; -use crate::Input; +use crate::{ + pathfinding::{adjacent, bfs}, + point::Point, + print::print, + Input, Map, +}; -pub fn main(_input: &Input) -> Result<()> { - Ok(()) +fn find_start(map: &Map, current_start: Point) -> Vec { + let can_move = |point: &Point| match map.get(&point) { + Some(tile) => *tile == 1, + None => false, + }; + let mut frontier = VecDeque::new(); + let mut visited = HashMap::new(); + + frontier.push_front(current_start); + visited.insert(current_start, None); + while !frontier.is_empty() { + let point = frontier.pop_front().unwrap(); + + for edge in adjacent(&map, &point) { + if visited.contains_key(&edge) || !can_move(&edge) { + continue; + } + frontier.push_back(edge); + visited.insert(edge, Some(point)); + } + } + + visited + .clone() + .keys() + .map(Point::to_owned) + .collect::>() +} + +pub fn main(input: &Input) -> Result { + let shortest_route = find_start(&input.map, input.start) + .into_iter() + .map(|start| { + bfs(&input.map, start.to_owned(), input.end, |a, b| { + Input::can_move(&input.map, a, b) + }) + }) + .map(|path| { + println!("\nlen: {}", path.len()); + print(&path, &input.map); + path.len() + }) + .min() + .unwrap_or(0); + + Ok(shortest_route) } #[cfg(test)] @@ -13,12 +65,16 @@ mod tests { use super::*; const EXAMPLE_DATA: &'static str = " - example + Sabqponm + abcryxxl + accszExk + acctuvwj + abdefghi "; #[test] fn it_gets_the_example_correct() -> Result<()> { - assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, ()); + assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 29); Ok(()) } } diff --git a/day_12/src/pathfinding.rs b/day_12/src/pathfinding.rs index 94b1602..a04b80a 100644 --- a/day_12/src/pathfinding.rs +++ b/day_12/src/pathfinding.rs @@ -31,15 +31,12 @@ pub fn adjacent(map: &Map, point: &Point) -> Vec { tiles } -pub fn find_path(map: &Map, start: Point, goal: Point) -> Vec { - let can_move = |point: &Point, came_from: &Point| { - let last_tile = map.get(came_from).unwrap(); - match map.get(&point) { - Some(tile) => *tile as i64 - *last_tile as i64 <= 1, - None => false, - } - }; - +pub fn bfs( + map: &Map, + start: Point, + goal: Point, + can_move: impl Fn(&Point, &Point) -> bool, +) -> Vec { let mut frontier = VecDeque::new(); let mut visited = HashMap::new(); @@ -52,7 +49,10 @@ pub fn find_path(map: &Map, start: Point, goal: Point) -> Vec { } for edge in adjacent(map, &point) { - if visited.contains_key(&edge) || !can_move(&edge, &point) { + if !can_move(&edge, &point) { + continue; + } + if visited.contains_key(&edge) { continue; } frontier.push_back(edge); diff --git a/day_12/src/print.rs b/day_12/src/print.rs index f8963ad..bedb00b 100644 --- a/day_12/src/print.rs +++ b/day_12/src/print.rs @@ -35,22 +35,23 @@ fn grid_as_tree_map( for x in 0..=(max_x + 1) { let point = Point { x, y }; let key = grid.get(&point).unwrap_or(&(' ' as u32)).to_owned() + 'a' as u32 - 1; + let key = if key < 'a' as u32 { - 'S' as u32 - } else if key > 'z' as u32 { - 'E' as u32 + "S".to_string() + } else if key == 123 { + "E".to_string() } else { - key + char::from_u32(key).unwrap_or(' ').to_string() }; - let key = char::from_u32(key).unwrap_or(' ').to_string(); - let key = if path.contains(&point) || key == "S" { + let key = if path.contains(&point) { format!("\x1b[93m{}\x1b[0m", key) } else if key == "E" { " ".to_string() } else { key }; + tree_map.insert(point, key); } }