From abd869ff057904456f1c15164f6b76dbf682d17a Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Thu, 14 Dec 2023 15:27:41 -0500 Subject: [PATCH] day 5 part 1 complete --- day_5/data/input | 197 ++++++++++++++++++++++++++++++++++++++++++++++ day_5/src/main.rs | 144 ++++++++++++++++++++++++--------- 2 files changed, 304 insertions(+), 37 deletions(-) create mode 100644 day_5/data/input diff --git a/day_5/data/input b/day_5/data/input new file mode 100644 index 0000000..6808607 --- /dev/null +++ b/day_5/data/input @@ -0,0 +1,197 @@ +seeds: 1972667147 405592018 1450194064 27782252 348350443 61862174 3911195009 181169206 626861593 138786487 2886966111 275299008 825403564 478003391 514585599 6102091 2526020300 15491453 3211013652 546191739 + +seed-to-soil map: +325190047 421798005 78544109 +4034765382 1473940091 137996533 +403734156 658668780 288666603 +2574766003 2624114227 17352982 +1931650757 2203381572 98211987 +4263596455 2843660329 31370841 +1614547845 2641467209 55121215 +3441604278 2032673361 170708211 +692400759 563703672 94965108 +2992851755 3824700930 114550818 +3957953582 2540115966 24844899 +0 500342114 59804107 +4172761915 2577017231 47096996 +2029862744 3548344768 52256082 +2620304610 2875031170 99567251 +3982798481 3939251748 51966901 +2325101894 1616388089 203150170 +3612312489 1611936624 4451465 +787365867 0 77461445 +1341614907 4162572181 132395115 +2542801978 3468402968 31964025 +223387052 947335383 59156225 +2297805420 1819538259 27296474 +2082118826 3991218649 171353532 +3374211778 3040047171 67392500 +2592118985 1341614907 28185625 +2253472358 2495782904 44333062 +4219858911 3780963386 43737544 +59804107 560146221 3557451 +3107402573 3600600850 64973479 +1669669060 3115086052 235712356 +2719871861 1369800532 104139559 +3172376052 2301593559 194189345 +2980795389 2564960865 12056366 +1905381416 3350798408 26269341 +63361558 261772511 160025494 +2824011420 3377067749 91335219 +3366565397 3107439671 7646381 +282543277 77461445 42646770 +3713793353 2696588424 147071905 +2528252064 1877585624 14549914 +3927202691 1846834733 30750891 +3665815578 3500366993 47977775 +1474010022 1892135538 140537823 +3616763954 3665574329 49051624 +2915346639 2974598421 65448750 +864827312 120108215 141664296 +3860865258 3714625953 66337433 + +soil-to-fertilizer map: +3835605444 4098164436 1662218 +682286299 0 63480553 +396476124 2072802434 285810175 +1644893571 655614677 162631098 +3625179075 4099826654 40627721 +1431211762 1859120625 213681809 +2853687843 4140454375 103601386 +1390165578 2358612609 41046184 +2405285827 3959200676 138963760 +900243562 1478767251 170015757 +3727732722 3084190064 107872722 +893948759 1084467374 6294803 +210337617 818245775 186138507 +1807524669 63480553 592134124 +825849944 1090762177 68098815 +3837267662 3192062786 457699634 +0 1648783008 210337617 +3665806796 2405285827 61925926 +1070259319 1158860992 319906259 +745766852 1004384282 80083092 +2957289229 4244055761 50911535 +2544249587 3649762420 309438256 +3008200764 2467211753 616978311 + +fertilizer-to-water map: +1169336944 3024036226 46676171 +1263157944 1445876546 148868263 +2080390054 2683279801 65621604 +949040795 1266013203 61140343 +2146011658 2793621589 110265098 +525510412 2618124082 65155719 +2977122301 3122211455 179751286 +3885825304 713560214 409141992 +1081017627 4124851079 88319317 +590666131 2039951828 38597255 +3596454634 1981772613 58179215 +2256276756 2078549083 315165891 +1639631621 1122702206 143310997 +2858399301 1327153546 118723000 +3654633849 3301962741 72039868 +749412925 3549292249 161128796 +1412026207 541983534 151190298 +1054901322 4268850991 26116305 +1829368778 3374002609 175289640 +1216013115 2570979253 47144829 +2806900243 3070712397 51499058 +1563216505 3760425124 76415116 +1782942618 3710421045 29953038 +629263386 2903886687 120149539 +2060339013 3740374083 20051041 +3156873587 3836840240 52553243 +2693038006 4010988842 113862237 +3726673717 2411827666 159151587 +2004658418 4213170396 55680595 +1812895656 525510412 16473122 +1010181138 2748901405 44720184 +910541721 693173832 20386382 +930928103 2393714974 18112692 +3209426830 1594744809 387027804 +2571442647 3889393483 121595359 + +water-to-light map: +555269773 2142838063 230952411 +2879443939 2889030006 80512763 +192641991 2686257040 106620606 +786222184 967781117 56662479 +3467110983 4162792381 132174915 +1955778230 0 505352386 +2461130616 1138855522 99691153 +1436321461 1403321335 440861327 +2625559119 1024443596 104825859 +1255310011 2792877646 96152360 +1877182788 697994377 78595442 +3732975066 3467110983 374194949 +842884663 2373790474 56459390 +299262597 2430249864 256007176 +3599285898 3841305932 133689168 +4107170015 3974995100 187797281 +0 505352386 192641991 +1090535351 1238546675 164774660 +2959956702 1129269455 9586067 +1351462371 1844182662 84859090 +2730384978 1929041752 149058961 +899344053 776589819 191191298 +2560821769 2078100713 64737350 + +light-to-temperature map: +4103141199 3912772142 105835099 +1994281004 833968687 112844016 +4208976298 1124841590 85990998 +3756966983 4018607241 111390720 +3868357703 1907239336 234783496 +2368591640 1210832588 293667703 +882426579 2320467318 1111854425 +3064998388 717457244 33489309 +3578938096 946812703 178028887 +2107125020 750946553 83022134 +3157760738 3432321743 421177358 +717457244 4129997961 164969335 +2190147154 2142022832 178444486 +3098487697 3853499101 59273041 +2662259343 1504500291 402739045 + +temperature-to-humidity map: +0 1820412620 129662806 +613828383 2855382935 55943394 +4004726464 3519717349 290240832 +769767991 99996214 1720416406 +3126066249 3043992795 475724554 +2490184397 2434241003 421141932 +129662806 1950075426 484165577 +689805485 0 79962506 +669771777 79962506 20033708 +3601790803 3809958181 402935661 +3043992795 4212893842 82073454 + +humidity-to-location map: +1305211417 3371927062 89487200 +947159122 0 358052295 +324330151 2021970861 8067408 +332397559 654359706 174171341 +506568900 3311893445 60033617 +11065691 828531047 45586147 +3556729147 369117986 26689998 +3583419145 395807984 258551722 +2356886904 984938593 400606547 +1394698617 874117194 110821399 +566602517 3946624826 164852367 +2998901322 2301963630 256425441 +0 358052295 11065691 +3331964991 3087129289 34908052 +1505520016 2106676497 195287133 +56651838 2819450976 267678313 +3366873043 3293025783 18867662 +3385740705 3122037341 170988442 +2281795782 2799796942 19654034 +2301449816 1966533773 55437088 +3992934054 3677118500 118543139 +3255326763 2030038269 76638228 +3849868384 3803559156 143065670 +2757493451 2558389071 241407871 +3841970867 3795661639 7897517 +1700807149 1385545140 580988633 +731454884 3461414262 215704238 diff --git a/day_5/src/main.rs b/day_5/src/main.rs index 090930e..e0ff566 100644 --- a/day_5/src/main.rs +++ b/day_5/src/main.rs @@ -1,30 +1,50 @@ -use std::{io::prelude::*, fs::File, path::Path, io, collections::HashMap }; +use std::{io::prelude::*, fs::File, path::Path, collections::HashMap }; +///////////////////////////////////////////////////////////////////// +// DATA STRUCTS +///////////////////////////////////////////////////////////////////// + #[derive(Clone, Debug)] struct SubMap { - destination_start: i32, - source_start: i32, - length: i32, + destination_start: i64, + source_start: i64, + length: i64, - map: HashMap + // map: HashMap } impl SubMap { fn new() -> SubMap { - SubMap { destination_start: 0, source_start: 0, length: 0, map: HashMap::new() } + SubMap { destination_start: 0, source_start: 0, length: 0 } + } + + // fn map_values(&mut self) + // { + // for i in 0..self.length + // { + // self.map.insert(self.source_start + i, self.destination_start + i); + // } + // } + + fn contains_value(&self, val: i64) -> bool + { + val >= self.source_start && val <= self.source_start + self.length } - fn map_values(&mut self) + fn map_value(&self, val: i64) -> i64 { - for i in 0..self.length + let distance = val - self.source_start; + if distance < 0 { - self.map.insert(self.source_start + i, self.destination_start + i); + return -1; } + + return self.destination_start + distance; } } @@ -42,13 +62,13 @@ impl Map Map { name: name.to_string(), sub_maps: Vec::new() } } - fn map_input(&self, input: i32) -> i32 + fn map_input(&self, input: i64) -> i64 { for sub_map in &self.sub_maps { - if sub_map.map.contains_key(&input) + if sub_map.contains_value(input) { - return sub_map.map[&input]; + return sub_map.map_value(input); } } @@ -61,26 +81,68 @@ impl Map ///////////////////////////////////////////////////////////////////// fn main() { - let input = load_data("data/test_input"); + let input = load_data("data/input"); let (seeds, maps) = parse_input(&input); - println!("SEEDS: {:#?}, MAPS: {:#?}", seeds, maps); + // TEST STUFF + // println!("SEEDS: {:#?}, MAPS: {:#?}", seeds, maps); + + // let test_1 = maps[0].map_input(99); + // let test_2 = maps[0].map_input(53); + // let test_3 = maps[0].map_input(47); + + // println!("=========TESTS:\n\tseed-to-soil {} maps to {}\n\n\tseed-to-soil {} maps to {}\n\n\tseed-to-soil {} maps to {}\n", + // 99, test_1, 53, test_2, 47, test_3); + // + + let location = find_closest_location(&seeds, &maps); + println!("Closest location: {}", location); +} + +fn find_closest_location(seeds: &Vec, maps: &Vec) -> i64 +{ + // let mut best_seed = 0; + let mut closest_location = i64::MAX; + + for seed in seeds + { + println!("Checking seed: {}", seed); + let location = find_seed_location(*seed, maps); + + if location < closest_location + { + closest_location = location; + // best_seed = *seed; + } + } + + closest_location +} + +fn find_seed_location(seed: i64, maps: &Vec) -> i64 +{ - let test_1 = maps["seed-to-soil"].map_input(99); - let test_2 = maps["seed-to-soil"].map_input(53); - let test_3 = maps["seed-to-soil"].map_input(47); + let mut current_value = seed; + for map in maps + { + println!("Mapping {}...", map.name); + current_value = map.map_input(current_value); + } - println!("=========TESTS:\n\tseed-to-soil {} maps to {}\n\n\tseed-to-soil {} maps to {}\n\n\tseed-to-soil {} maps to {}\n", - 99, test_1, 53, test_2, 47, test_3); + current_value } -fn parse_input(input: &str) -> (Vec, HashMap) // String is the map name +///////////////////////////////////////////////////////////////////// +// INPUT PARSING +///////////////////////////////////////////////////////////////////// +fn parse_input(input: &str) -> (Vec, Vec) // String is the map name { - let mut seeds: Vec = Vec::new(); + let mut seeds: Vec = Vec::new(); - let mut maps: HashMap = HashMap::new(); + let mut maps: Vec = Vec::new(); let mut current_map = ""; - for (idx, line) in input.to_ascii_lowercase().split("\n").into_iter().enumerate() + let mut sub_map_idx = 0; + for (_, line) in input.to_ascii_lowercase().split("\n").into_iter().enumerate() { // stupid windows dumb line ending non-sense if line.starts_with("\r") @@ -91,9 +153,10 @@ fn parse_input(input: &str) -> (Vec, HashMap) // String is the // SEEDS if line.contains("seeds:") { + println!("Parseing seed input..."); for val in line.trim_start_matches("seeds:").trim().split(" ") { - let num = val.parse::().expect(&format!("Failed to parse seed value: {}", val)); + let num = val.parse::().expect(&format!("Failed to parse seed value: {}", val)); seeds.push(num); } @@ -104,15 +167,19 @@ fn parse_input(input: &str) -> (Vec, HashMap) // String is the // NEW MAP if line.contains("map:") { + sub_map_idx = 0; current_map = line.trim().trim_end_matches("map:").trim(); + println!("Parsing map: {}", current_map); let map = Map::new(¤t_map); - maps.insert(current_map.to_string(), map.clone()); + // maps.insert(current_map.to_string(), map.clone()); + maps.push(map.clone()); continue; } // NEW SUB MAP let mut sub_map = SubMap::new(); let vals: Vec<&str> = line.trim().split(" ").collect(); + println!("Parsing sub map: {}", sub_map_idx); if vals.len() < 3 { @@ -120,18 +187,21 @@ fn parse_input(input: &str) -> (Vec, HashMap) // String is the continue; } - sub_map.destination_start = vals[0].parse::().expect(&format!("Failed to parse dest value: {}", vals[0])); - sub_map.source_start = vals[1].parse::().expect(&format!("Failed to parse source value: {}", vals[1])); - sub_map.length = vals[2].parse::().expect(&format!("Failed to parse length value: {}", vals[2])); - sub_map.map_values(); - if let Some(map) = maps.get_mut(current_map) - { - map.sub_maps.push(sub_map.clone()); - } - else - { - println!("========CURRENT MAP NOT FOUND IN MAP: {}========", current_map); - } + sub_map.destination_start = vals[0].parse::().expect(&format!("Failed to parse dest value: {}", vals[0])); + sub_map.source_start = vals[1].parse::().expect(&format!("Failed to parse source value: {}", vals[1])); + sub_map.length = vals[2].parse::().expect(&format!("Failed to parse length value: {}", vals[2])); + //sub_map.map_values(); + let idx = maps.len() - 1; + maps[idx].sub_maps.push(sub_map.clone()); + sub_map_idx += 1; + // if let Some(map) = maps.get_mut(current_map) + // { + // map.sub_maps.push(sub_map.clone()); + // } + // else + // { + // println!("========CURRENT MAP NOT FOUND IN MAP: {}========", current_map); + // } } (seeds, maps)