From 916e52783aa0bd7d29f3444f9e67323d058cb7a9 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 5 Dec 2023 07:25:39 +0000 Subject: [PATCH] Day 5 Part 1 complete --- Cargo.toml | 1 + src/input/day05 | 239 +++++++++++++++++++++++++++++++++++++++++ src/input/day05_test1 | 33 ++++++ src/input/day05_test2 | 33 ++++++ src/input/day06 | 0 src/input/day06_test1 | 0 src/input/day06_test2 | 0 src/main.rs | 2 +- src/solutions/day05.rs | 37 ++++++- src/solutions/day06.rs | 2 +- 10 files changed, 341 insertions(+), 6 deletions(-) create mode 100644 src/input/day06 create mode 100644 src/input/day06_test1 create mode 100644 src/input/day06_test2 diff --git a/Cargo.toml b/Cargo.toml index 164caa7..6cc7d0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,6 @@ edition = "2021" [dependencies] fancy-regex = "0.12.0" +itertools = "0.12.0" strum = "0.25.0" strum_macros = "0.25.3" diff --git a/src/input/day05 b/src/input/day05 index e69de29..d4fe61f 100644 --- a/src/input/day05 +++ b/src/input/day05 @@ -0,0 +1,239 @@ +seeds: 91926764 235794528 3279509610 325625103 2781720183 218217413 1315129829 102999617 3995609239 143268116 358337926 185836835 1543999077 241888600 1795811745 806228439 2616560939 56204204 869828854 224520829 + +seed-to-soil map: +2076625497 3385713231 258448094 +933162806 1124446801 128749435 +744984268 625015359 188178538 +551924411 211563684 96582495 +2664738738 2072311374 297142807 +1406760174 813193897 14102104 +0 1274151473 331435427 +403777493 976299883 148146918 +1987639580 2783535044 88985917 +4074594902 3332858447 52854784 +1484055780 86124597 125439087 +331435427 371339681 72342066 +1146768491 1605586900 152911849 +3283022369 2992105862 70998045 +648506906 528537997 96477362 +1609494867 827296001 149003882 +3685297224 3111036616 121638972 +1420862278 308146179 63193502 +2961881545 3644161325 321140824 +1061912241 443681747 56957522 +4127449686 2872520961 119584901 +3806936196 2497318144 139794743 +3585114365 3232675588 100182859 +1118869763 500639269 27898728 +2335073591 3965302149 329665147 +3984585308 2407308550 90009594 +1320635577 0 86124597 +1841217423 2637112887 146422157 +4247034587 3063103907 47932709 +1299680340 1253196236 20955237 +3354020414 1841217423 231093951 +3946730939 2369454181 37854369 + +soil-to-fertilizer map: +3384134166 1995234330 176305237 +1155226403 855999534 411447090 +2208613005 3459130785 737289315 +3115190977 2188772380 56379264 +3046023331 2245151644 69167646 +3287350250 3362346869 96783916 +0 688893976 167105558 +199962926 1300303992 576627555 +1566673493 0 380943110 +1995234330 2314319290 213378675 +776590481 380943110 307950866 +3560439403 2527697965 734527893 +2945902320 3262225858 100121011 +1084541347 1876931547 70685056 +3229639031 4196420100 40478406 +3270117437 2171539567 17232813 +167105558 1267446624 32857368 +3171570241 4236898506 58068790 + +fertilizer-to-water map: +2261570026 3454758517 88568015 +1802864872 1796719466 196521844 +1029796290 924285105 94936250 +1502956048 1019221355 34573577 +1675836952 4234138393 33837263 +383249040 0 273576862 +1124732540 457794332 53985776 +2350138041 2091507351 48912742 +1674136750 1795019264 1700202 +3859927050 2781536522 163983633 +90982234 273576862 129799649 +1537529625 511780108 260859 +2488789087 1993241310 98266041 +2196492990 3543326532 49319261 +2254009624 2694827028 7560402 +2587055128 3111811340 69748037 +2002799677 4168500219 65638174 +840643879 512040967 108011574 +1390364751 688875858 8559931 +1398924682 721632529 104031366 +656825902 620052541 68823317 +1709674215 3189756750 73094246 +2068437851 1671843595 123175669 +73501861 906804732 17480373 +2656803165 3853597757 225164158 +3592079857 3612742204 240855553 +2191613520 2945520155 4879470 +2881967323 2950399625 161411715 +0 1085334645 69517503 +725649219 697435789 24196740 +2245812251 3181559377 8197373 +220781883 1375323327 162467157 +3270779999 2140420093 321299858 +2399050783 4078761915 89738304 +4023910683 2702387430 79149092 +69517503 1053794932 3984358 +1782768461 3592645793 20096411 +1308391575 1057779290 27555355 +749845959 1154852148 61727890 +1671843595 2692533873 2293155 +1335946930 403376511 54417821 +3832935410 4267975656 26991640 +3043379038 2461719951 227400961 +948655453 825663895 81140837 +4103059775 3262850996 191907521 +1999386716 2689120912 3412961 +1178718316 1216580038 129673259 +811573849 1346253297 29070030 + +water-to-light map: +12536522 803922375 381092756 +1034093555 2274122448 375903462 +4046347183 3727145496 30218294 +1644177017 1468743601 210972620 +2033158562 0 65899273 +3918690677 4044943223 48984895 +3252369528 2650025910 46811980 +3846317697 4159436488 72372980 +825337225 701230416 92775221 +708443948 3110054761 116893277 +4281152040 4231809468 13815256 +4076565477 3945094008 99849215 +467053271 262914333 56912139 +457136533 794005637 9916738 +4176414692 3757363790 104737348 +1960807084 319826472 72351478 +3983208893 4096298198 63138290 +2099057835 2696837890 179036871 +3400708401 4093928118 2370080 +393629278 2210615193 63507255 +2278094706 1723463860 487151333 +3351365829 4245624724 49342572 +523965410 65899273 184478538 +1855149637 595572969 105657447 +2765246039 1185015131 283728470 +961860085 3226948038 72233470 +3967675572 3711612175 15533321 +3486071351 3351365829 360246346 +3403078481 3862101138 82992870 +918112446 1679716221 43747639 +1409997017 2875874761 234180000 +0 250377811 12536522 +3048974509 392177950 203395019 + +light-to-temperature map: +3663115998 2273256818 16619810 +1779396221 3140211126 35421643 +1671064478 2289876628 5675989 +2459496143 2164474153 65282060 +1676740467 3228344832 68171740 +3108268812 3396614149 34367621 +0 1247024027 117995309 +3142636433 2302123739 520479565 +3914906966 3296516572 48028032 +1451315146 20271696 29756177 +3962934998 1685507004 235533336 +3679735808 3872604190 235171158 +201522431 0 20271696 +2606764356 2257434699 15822119 +1095940165 930151315 235007724 +2092197835 2032400258 15603728 +1814817864 4107775348 187191948 +2107801563 3430981770 351694580 +221794127 50027873 874146038 +2524778203 2082488000 81986153 +4205039456 3811944039 60660151 +2622586475 1921040340 111359918 +3051554215 3344544604 52069545 +1605883623 1648004635 37502369 +3103623760 1643359583 4645052 +1412812877 1442569054 38502269 +2039485772 3175632769 52712063 +123972713 1365019336 77549718 +1744912207 2048003986 34484014 +4198468334 2295552617 6571122 +2733946393 2822603304 317607822 +117995309 924173911 5977404 +1330947889 1165159039 81864988 +2002009812 1605883623 37475960 +4265699607 3782676350 29267689 +1643385992 2229756213 27678486 + +temperature-to-humidity map: +1358631653 987873070 147949589 +2413964705 2097965649 70341317 +816457527 972386794 15486276 +2080824412 2168306966 250363611 +958855341 2055074721 42890928 +1948315905 1968747613 86327108 +1810667112 2669632213 137648793 +2484306022 857156868 115229926 +1001746269 1523646763 356885384 +1641013697 687503453 169653415 +2698691839 0 56146061 +4138641394 2975929973 156325902 +831943803 2418670577 38696072 +3039761928 4068116224 226851072 +870639875 1880532147 88215466 +2975929973 3132255875 63831955 +3839900820 3497181952 298740574 +3567707122 3795922526 272193698 +3266613000 3203991608 293190344 +83162889 56146061 361318942 +1506581242 553070998 134432455 +2754837900 500627892 52443106 +2034643013 1135822659 46181399 +786124536 2556522540 30332991 +2599535948 2457366649 99155891 +3559803344 3196087830 7903778 +0 417465003 83162889 +444481831 1182004058 341642705 +2331188023 2586855531 82776682 + +humidity-to-location map: +1050996268 1532072704 311260544 +3180071790 2724566227 289337188 +4221284312 3899164493 35309413 +3807278034 2656088631 16719113 +1362256812 2203016881 453071750 +338253566 1843333248 359683633 +697937199 1009396889 40111814 +2970507299 3589172780 195028044 +2338906915 3808103383 1357317 +1879134746 3052743004 5325923 +1815328562 346829408 63806184 +3899164493 3972847477 185700981 +2340264232 0 83356697 +2610350026 3013903415 38839589 +0 671143323 338253566 +2423620929 3402443683 186729097 +738049013 1049508703 312947255 +1884460669 561071833 110071490 +3714686766 254238140 92591268 +3469408978 1362455958 169616746 +2799625856 83356697 170881443 +3639025724 3784200824 23902559 +4084865474 4158548458 136418838 +1994532159 3058068927 344374756 +4256593725 3934473906 38373571 +3165535343 3809460700 14536447 +2649189615 410635592 150436241 +3662928283 2672807744 51758483 \ No newline at end of file diff --git a/src/input/day05_test1 b/src/input/day05_test1 index e69de29..bd902a4 100644 --- a/src/input/day05_test1 +++ b/src/input/day05_test1 @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 \ No newline at end of file diff --git a/src/input/day05_test2 b/src/input/day05_test2 index e69de29..bd902a4 100644 --- a/src/input/day05_test2 +++ b/src/input/day05_test2 @@ -0,0 +1,33 @@ +seeds: 79 14 55 13 + +seed-to-soil map: +50 98 2 +52 50 48 + +soil-to-fertilizer map: +0 15 37 +37 52 2 +39 0 15 + +fertilizer-to-water map: +49 53 8 +0 11 42 +42 0 7 +57 7 4 + +water-to-light map: +88 18 7 +18 25 70 + +light-to-temperature map: +45 77 23 +81 45 19 +68 64 13 + +temperature-to-humidity map: +0 69 1 +1 0 69 + +humidity-to-location map: +60 56 37 +56 93 4 \ No newline at end of file diff --git a/src/input/day06 b/src/input/day06 new file mode 100644 index 0000000..e69de29 diff --git a/src/input/day06_test1 b/src/input/day06_test1 new file mode 100644 index 0000000..e69de29 diff --git a/src/input/day06_test2 b/src/input/day06_test2 new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index 318ca4b..c15e369 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ mod solutions; mod utils; -use std::{error::Error, sync::{Arc, Mutex}}; +use std::error::Error; use solutions::*; diff --git a/src/solutions/day05.rs b/src/solutions/day05.rs index 5371019..f20b2e7 100644 --- a/src/solutions/day05.rs +++ b/src/solutions/day05.rs @@ -1,13 +1,23 @@ use super::Solution; +use itertools::Itertools; pub struct Day05 {} impl Solution for Day05 { fn part1( &self, - _input: &mut Vec, + input: &mut Vec, ) -> Result, Box> { - Ok(Box::new("Ready")) + let (seeds, maps) = self.get_mappings(input)?; + let locations = maps.iter().fold(seeds, |seeds, mappings| + seeds.iter().map(|&seed| + mappings.iter() + .find(|&&(_, src, range)| (src..src+range).contains(&seed)) + .map(|(dst, src, _)| dst + seed - src) + .unwrap_or(seed) + ).collect() + ); + Ok(Box::new(*locations.iter().min().unwrap())) } fn part2( @@ -22,7 +32,26 @@ impl Solution for Day05 { } } -impl Day05 {} +impl Day05 { + fn get_mappings(&self, input: &Vec) -> Result<(Vec, Vec>), Box> { + let seeds: Vec = input[0].split_whitespace() + .skip(1) + .map(|s| s.parse().unwrap()) + .collect::>(); + + let maps: Vec> = input[2..input.len()].join("\n") + .split("\n\n").map(|s| + s.split('\n').skip(1).map(|l| + l.split_whitespace() + .map(|s| s.parse().unwrap()) + .collect_tuple() + .unwrap() + ).collect::>() + ).collect::>(); + + Ok((seeds, maps)) + } +} /// Test from puzzle input #[cfg(test)] @@ -44,7 +73,7 @@ mod test { .unwrap() .to_string(); - assert_eq!(answer, "Ready"); + assert_eq!(answer, "35"); } #[test] diff --git a/src/solutions/day06.rs b/src/solutions/day06.rs index 9bcb1a0..a874f45 100644 --- a/src/solutions/day06.rs +++ b/src/solutions/day06.rs @@ -18,7 +18,7 @@ impl Solution for Day06 { } fn get_day(&self) -> u8 { - 5 + 6 } }