From 5519716c0c4adaa16b885881536ed106a4008252 Mon Sep 17 00:00:00 2001 From: dalan <863286+dalanmiller@users.noreply.github.com> Date: Mon, 11 Dec 2023 13:08:05 +1100 Subject: [PATCH] 2023 Day 5 --- 2023/day5/Cargo.lock | 25 ++++++ 2023/day5/Cargo.toml | 9 ++ 2023/day5/src/input | 204 ++++++++++++++++++++++++++++++++++++++++++ 2023/day5/src/main.rs | 183 +++++++++++++++++++++++++++++++++++++ 4 files changed, 421 insertions(+) create mode 100644 2023/day5/Cargo.lock create mode 100644 2023/day5/Cargo.toml create mode 100644 2023/day5/src/input create mode 100644 2023/day5/src/main.rs diff --git a/2023/day5/Cargo.lock b/2023/day5/Cargo.lock new file mode 100644 index 0000000..16f293d --- /dev/null +++ b/2023/day5/Cargo.lock @@ -0,0 +1,25 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day5" +version = "0.1.0" +dependencies = [ + "itertools", +] + +[[package]] +name = "either" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" + +[[package]] +name = "itertools" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +dependencies = [ + "either", +] diff --git a/2023/day5/Cargo.toml b/2023/day5/Cargo.toml new file mode 100644 index 0000000..d8782ea --- /dev/null +++ b/2023/day5/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day5" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +itertools = "0.12.0" diff --git a/2023/day5/src/input b/2023/day5/src/input new file mode 100644 index 0000000..c733b8b --- /dev/null +++ b/2023/day5/src/input @@ -0,0 +1,204 @@ +seeds: 3169137700 271717609 3522125441 23376095 1233948799 811833837 280549587 703867355 166086528 44766996 2326968141 69162222 2698492851 14603069 2755327667 348999531 2600461189 92332846 1054656969 169099767 + +seed-to-soil map: +404519244 291909660 232498813 +1320269465 1130502191 53088254 +3383428636 3839037267 267181273 +0 667785713 35892165 +1373357719 524408473 98619504 +2581158120 2457172776 306553547 +80649901 971363049 35482565 +2330985014 3113014199 250173106 +637018057 1006845614 54766591 +1010187739 1061612205 68889986 +851189485 132911406 158998254 +718278079 0 132911406 +1079077725 730171309 241191740 +2887711667 2392582028 64590748 +3321831622 2330985014 61597014 +3650609909 4106218540 188748756 +2952302415 3363187305 20241331 +368073252 1183590445 36445992 +3839358665 3383428636 455608631 +35892165 623027977 44757736 +2972543746 2763726323 349287876 +691784648 703677878 26493431 +116132466 1220036437 251940786 + +soil-to-fertilizer map: +1392321439 1575423106 1147080465 +261871322 2722503571 622657242 +884528564 768981668 507792875 +3152000765 0 514557996 +2539401904 3607032135 247383303 +2786785207 3854415438 110791886 +2897577093 514557996 254423672 +3666558761 1276774543 298648563 +0 3345160813 261871322 + +fertilizer-to-water map: +385494683 1437201590 374714893 +3894452006 2756513635 124929297 +1389571244 1299917000 137284590 +3267934068 2881442932 9378230 +4140946106 2602492445 154021190 +760209576 585965354 629361668 +1948388381 1832044460 70164711 +170741915 176668131 214752768 +3570093835 2531499423 70993022 +2647843344 3421511423 399466167 +1832044460 3124261283 116343921 +3458218517 2890821162 111875318 +1721400289 1215327022 84589978 +1526855834 391420899 194544455 +4019381303 3002696480 121564803 +3641086857 4041602147 253365149 +2018553092 1902209171 629290252 +3047309511 3820977590 220624557 +1805990267 170741915 5926216 +3277312298 3240605204 180906219 + +water-to-light map: +3186854896 2955619881 216294813 +1320085115 3394507929 25016085 +1300936985 1818141535 19148130 +3620767971 560782805 39686383 +2121550779 2395592136 95582251 +3594740149 4260270958 26027822 +1138228493 3171914694 34259798 +823736132 600469188 55727065 +3049715340 2803112532 137139556 +0 461804882 98977923 +2217133030 1416346841 282474073 +1225320767 1742525317 75616218 +3481284141 3206174492 113456008 +1172488291 656196253 52832476 +1980983454 2662545207 140567325 +182051501 2121838778 15960124 +1788572787 256566375 2332800 +881662118 0 256566375 +3769058535 899106596 517240245 +157917514 3851650114 24133987 +564440564 1937530266 107354197 +2527210819 4090588051 169682907 +1790905587 709028729 190077867 +2499607103 3824046398 27603716 +1585667080 258899175 202905707 +671794761 3419524014 51700770 +3403149709 2491174387 62766639 +228474519 2044884463 76954315 +3660454354 2553941026 108604181 +305428834 2137798902 257793234 +879463197 3875784101 2198921 +2696893726 3471224784 352821614 +1345101200 3319630500 74877429 +98977923 4031648460 58939591 +1541962677 1698820914 43704403 +3465916348 2940252088 15367793 +1419978629 3909664412 121984048 +723495531 1837289665 100240601 +563222068 3908445916 1218496 +198011625 3877983022 30462894 + +light-to-temperature map: +2138957924 2867100637 394375633 +1108145028 3261476270 27971395 +2732973434 712143665 259385506 +642453123 1589178663 51870752 +316125718 164593015 67732689 +0 145842404 18750611 +1861017768 2758655274 29719920 +2130609825 2858752538 8348099 +828190830 2187066385 73864838 +18750611 0 126160197 +144910808 376698573 26842041 +383858407 126160197 19682207 +2533333557 3337386080 199639877 +1507332395 1950588856 215367909 +3619117542 3537025957 51848058 +1740303803 1763491162 21707209 +3793407347 4140330531 154636765 +477829952 3588874015 164623171 +1722700304 1432603163 17603499 +1890737688 493381148 218762517 +4255326359 1549537726 39640937 +1408001331 1450206662 99331064 +2992358940 2260931223 497724051 +429891537 3289447665 47938415 +3670965600 1641049415 122441747 +171752849 232325704 144372869 +3504169105 3753497186 114948437 +2109500205 2165956765 21109620 +4113434597 1290711401 141891762 +3490082991 1070535927 14086114 +1136116423 3868445623 271884908 +757813486 2788375194 70377344 +3948044112 1785198371 165390485 +694323875 429891537 63489611 +1762011012 971529171 99006756 +902055668 1084622041 206089360 + +temperature-to-humidity map: +480269915 557576190 100117705 +2489408207 1585236324 410693151 +2900101358 422045351 135530839 +666327594 779006907 28457451 +1945409298 807464358 543998909 +96132998 2440771002 3096292 +1295929224 2746460169 2283706 +1298212930 2022055443 99310345 +3547441134 3503466915 344864128 +1659646920 325230280 96815071 +4125576596 3928494543 169390700 +3035632197 1351463267 233773057 +1397523275 2592671813 109157546 +1032938960 2748743875 109076238 +1756461991 2276586008 92935317 +3968917823 4097885243 156658773 +595077917 2369521325 71249677 +1593864657 3027700045 65782263 +850005265 657693895 121313012 +1506680821 2505487977 87183836 +1186646008 0 109283216 +1849397308 3389340328 96011990 +3496945571 4254544016 40423280 +3898826606 3858403326 70091217 +99229290 3189615306 199725022 +468834244 1995929475 11435671 +3269405254 109283216 215947064 +580387620 2007365146 14690297 +0 3093482308 96132998 +971318277 2443867294 61620683 +3892305262 3496945571 6521344 +298954312 2857820113 169879932 +1142015198 2701829359 44630810 +694785045 2121365788 155220220 +3537368851 3848331043 10072283 + +humidity-to-location map: +3490144003 1623866227 218040905 +1709610578 1620839197 3027030 +105449249 586389428 113279526 +1899604338 2167886292 348178199 +1712637608 2678624215 186966730 +218728775 0 245776251 +2472992580 923734334 143670388 +2616662968 3670169885 15297294 +2247782537 1395629154 225210043 +0 480940179 105449249 +4113852846 3959729057 159909096 +3322784653 1067404722 167359350 +923734334 4119638153 175329143 +1534964496 2516064491 162559724 +2631960262 3496028440 140849733 +2862695906 1234764072 97988355 +1697524220 3636878173 12086358 +2985646048 1332752427 62876727 +1099063477 2970241510 435901019 +4009202281 2865590945 104650565 +2960684261 2142924505 24961787 +2772809995 3406142529 89885911 +4273761942 3648964531 21205354 +3708184908 1841907132 301017373 +464505026 245776251 235163928 +3048522775 3685467179 274261878 diff --git a/2023/day5/src/main.rs b/2023/day5/src/main.rs new file mode 100644 index 0000000..0be3b22 --- /dev/null +++ b/2023/day5/src/main.rs @@ -0,0 +1,183 @@ +use std::fs::read_to_string; +use itertools::Itertools; + +struct Rule { + name: Option, + source_start: isize, + source_end: isize, + dest_start: isize, + dest_end: isize, +} + +impl Rule { + fn convert(&self, n: isize) -> isize { + // if n == 53 { + // println!("ohno") + // } + return self.dest_start + (n - self.source_start); + } + + fn contains(&self, n: isize) -> bool { + return self.source_start <= n && n <= self.source_end + } +} + +fn read_input() -> (Vec, [Vec; 7]) { + +// let lines: Vec = r#"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"#.lines().map(String::from).collect(); + + let lines: Vec = read_to_string("src/input") + .unwrap() + .lines() + .map(String::from) + .collect(); + + // let split = lines.split("$\n"); + let seeds: Vec = lines[0][7..] + .split(" ") + .map(|n| n.parse::().unwrap()) + .collect(); + + let (sts, stf, ftw, wtl, ltt, tth, htl): ( + Vec, + Vec, + Vec, + Vec, + Vec, + Vec, + Vec, + ) = ( + Vec::new(), + Vec::new(), + Vec::new(), + Vec::new(), + Vec::new(), + Vec::new(), + Vec::new(), + ); + let mut rule_sets = [sts, stf, ftw, wtl, ltt, tth, htl]; + let mut i: usize = 0; + let mut name: Option = None; + for line in lines[1..].iter() { + match line.as_str() { + line if line.contains("map") => { + i += 1; + name = Some(line.to_string()); + }, + "\n" | "" => continue, + _ => { + let items: Vec = line + .split(" ") + .map(|n| n.parse::().unwrap()) + .collect(); + let (dest, source, range) = (items[0], items[1], items[2]); + rule_sets[i - 1].push( + Rule { name: name.clone(), source_start: source, source_end: (source+range -1), dest_start: (dest), dest_end: (dest + range -1)} + ); + } + } + } + + (seeds, rule_sets) +} + +fn main() { + let (seeds, rule_sets) = read_input(); + part_one(&seeds, &rule_sets); + part_two(&seeds, &rule_sets); +} + +fn part_one(seeds: &Vec, rule_sets: &[Vec; 7]) { + let mut min = *seeds.iter().max().unwrap(); + for seed in seeds.iter() { + let mut tmp = *seed; + for rule_set in rule_sets.iter() { + for rule in rule_set.iter() { + if rule.contains(tmp) { + tmp = rule.convert(tmp); + break + } + } + } + + min = std::cmp::min(min, tmp); + } + + println!("Answer: {:?}", min); +} + +fn part_two(seeds: &Vec, rule_sets: &[Vec; 7]) { + + let seed_ranges = seeds.chunks(2).map(|c| c[0]..(c[0]+c[1])).collect_vec(); + + let mut index: usize = 0; + let mut answer: Option = None; + + while answer.is_none() { + // Our tmp variable that we are using to convert backwards + // through all the maps + let mut tmp: usize = index; + // println!("Starting with: {:?}", tmp); + for rule_set in rule_sets.iter().rev() { + for rule in rule_set { + // println!("{:?}", rule.name); + // We want to ensure that the current tmp variable is within + // the destination range + if rule.dest_start as usize <= tmp && tmp <= rule.dest_end as usize { + // This operations takes the output and reverses it into the input for the given + // rule + tmp = rule.source_start as usize + tmp - rule.dest_start as usize; + break; + } + } + } + // println!("Ending with: {:?}", tmp); + + for seed_range in seed_ranges.iter() { + if seed_range.start as usize <= tmp && tmp <= seed_range.end as usize { + // println!("Answer found: {:?}", index); + answer = Some(index); + break + } + } + // if seed_ranges.iter().any(|sr| sr.start as usize <= location && location <= sr.end as usize ) { + // answer = Some(index); + // }; + index+=1; + } + + println!("Answer: {:?}", answer.unwrap()); + +}