diff --git a/Cargo.toml b/Cargo.toml index 10e978a..7a0d741 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,15 @@ [workspace] resolver = "2" -members = ["day01", "day02", "day03", "day04"] +members = ["day01", "day02", "day03", "day04", "day05"] [workspace.package] authors = ["David Oberacker "] edition = "2021" description = "Solution for this days Advent of Code exercise" publish = false + +[profile.release] +#incremental = true +#debug = true +#lto = "fat" + diff --git a/day04/Cargo.toml b/day04/Cargo.toml index 6e424cd..55bf847 100644 --- a/day04/Cargo.toml +++ b/day04/Cargo.toml @@ -15,3 +15,4 @@ name = "day04_part1" name = "day04_part2" [dependencies] + diff --git a/day04/src/bin/day04_part1.rs b/day04/src/bin/day04_part1.rs index 3cabe2e..3cb4140 100644 --- a/day04/src/bin/day04_part1.rs +++ b/day04/src/bin/day04_part1.rs @@ -1,6 +1,7 @@ use std::collections::HashSet; use std::ops::Sub; use std::str::FromStr; +use std::time::Instant; struct Card { winning_numbers: HashSet, @@ -37,10 +38,13 @@ fn calculate_result(cards: Vec) -> u64 { fn main() { + let now = Instant::now(); let input = include_str!("../../resources/input_01.txt"); let cards = parse_input(input); let res = calculate_result(cards); println!("{}", res); + let elapsed = now.elapsed(); + println!("Elapsed: {:?}", elapsed); } #[cfg(test)] @@ -54,4 +58,4 @@ mod tests { let res = calculate_result(cards); assert_eq!(res, 13); } -} \ No newline at end of file +} diff --git a/day04/src/bin/day04_part2.rs b/day04/src/bin/day04_part2.rs index 5eda267..db3ac87 100644 --- a/day04/src/bin/day04_part2.rs +++ b/day04/src/bin/day04_part2.rs @@ -48,7 +48,6 @@ fn calculate_result(cards: Vec) -> u64 { } fn main() { - let input = include_str!("../../resources/input_01.txt"); let cards = parse_input(input); let res = calculate_result(cards); diff --git a/day05/Cargo.toml b/day05/Cargo.toml new file mode 100644 index 0000000..580ee12 --- /dev/null +++ b/day05/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "day05" +version = "0.1.0" +authors.workspace = true +edition.workspace = true +description.workspace = true +publish.workspace = true + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1.10.2" diff --git a/day05/resources/input01.txt b/day05/resources/input01.txt new file mode 100644 index 0000000..b2a4615 --- /dev/null +++ b/day05/resources/input01.txt @@ -0,0 +1,213 @@ +seeds: 202517468 131640971 1553776977 241828580 1435322022 100369067 2019100043 153706556 460203450 84630899 3766866638 114261107 1809826083 153144153 2797169753 177517156 2494032210 235157184 856311572 542740109 + +seed-to-soil map: +1393363309 644938450 159685707 +2025282601 1844060172 19312202 +1233103806 1026919253 32871092 +1086566452 1933428941 86530991 +1265974898 0 21589659 +1357621124 1636167265 35742185 +2343571960 2665606060 81121142 +1585337376 809179011 202497192 +3151050390 3039622538 54531851 +2059837853 804624157 4554854 +169037772 124717914 59280146 +228317918 183998060 248114943 +2646529073 2343571960 51673623 +1173097443 1360585007 60006363 +2000660015 1203115155 24622586 +1059486394 1176035097 27080058 +3129081851 4185259485 17367169 +3599437884 3098755759 211817367 +2810085327 3883695720 116314513 +2424693102 4015066563 32329632 +3398847262 2507128214 141172870 +1787834568 432113003 212825447 +1553049016 1603878905 32288360 +3111414816 4202626654 17667035 +4015961437 3493485543 279005859 +3584381554 4000010233 15056330 +609280127 1840486939 3573233 +0 1603418622 460283 +3302297495 3310573126 79244791 +3811255251 4047396195 137863290 +3381542286 2648301084 17304976 +3280255848 3389817917 22041647 +840113387 21589659 103128255 +3949118541 3816852824 66842896 +3205582241 4220293689 74673607 +657286135 1420591370 182827252 +1287564557 1863372374 70056567 +460283 1671909450 168577489 +2926399840 2746727202 103388997 +3146449020 3094154389 4601370 +943241642 1059790345 116244752 +476432861 1227737741 132847266 +612853360 2019959932 44432775 +2698202696 2395245583 111882631 +3029788837 3411859564 81625979 +2044594803 1011676203 15243050 +2457022734 2850116199 189506339 +3540020132 3772491402 44361422 + +soil-to-fertilizer map: +1845319553 827629590 305617985 +3122295925 2644420892 346256096 +1459294850 681645131 145984459 +1609507353 0 58999651 +255693782 1322254706 15503402 +1136906676 1310560683 7032394 +609209731 1833691163 45329504 +271197184 2213414186 148369535 +3483324631 2990676988 343929863 +3943098203 3619829050 148418709 +2945015193 3803447520 177280732 +504622935 1337758108 104586796 +2644420892 3334606851 81771815 +2909815432 3768247759 35199761 +3468873015 4096571961 14451616 +3827254494 3980728252 115843709 +1044649784 1218303791 92256892 +3468552021 4111023577 320994 +1605279309 677417087 4228044 +1668507004 58999651 176812549 +978403972 1317593077 4661629 +212737043 1879020667 42956739 +916089003 1655081947 62314969 +0 1442344904 212737043 +1228536146 2361783721 230758704 +419566719 1133247575 85056216 +1143939070 1717396916 84597076 +2726192707 4111344571 183622725 +983065601 2151830003 61584183 +2150937538 235812200 441604887 +884391832 1801993992 31697171 +654539235 1921977406 229852597 +4091516912 3416378666 203450384 + +fertilizer-to-water map: +2549847515 3576009818 718957478 +0 241538153 477666033 +2425421388 2487425840 6333278 +2431754666 2369332991 118092849 +4172623904 3453666426 122343392 +2050888028 0 241538153 +2369332991 2493759118 56088397 +477666033 719204186 1573221995 +3268804993 2587418451 866247975 +4135052968 2549847515 37570936 + +water-to-light map: +0 614660468 46162263 +992982309 3320291957 519425172 +2148695908 4242883656 34662742 +2183358650 992982309 1749887545 +622053693 575891430 38769038 +1973119806 3839717129 175576102 +3950667093 3281596434 38695523 +46162263 0 575891430 +1512407481 4015293231 227590425 +1739997906 3048474534 233121900 +3933246195 4277546398 17420898 +3989362616 2742869854 305604680 + +light-to-temperature map: +3926915598 4278168812 16798484 +1868013910 2147559018 140836186 +750719301 1001446770 132766166 +0 591148217 159571084 +2757723179 3756680674 111319765 +3526572182 1656447494 400343416 +159571084 0 569934147 +2869042944 3868000439 358532427 +2008850096 3039560686 189896094 +2649579616 3734175051 22505623 +2270874649 2588070691 164667420 +4008144721 2752738111 286822575 +2435542069 2374033144 214037547 +2672085239 2288395204 85637940 +3450693140 2056790910 18639649 +3469332789 3452574549 57239393 +883485467 750719301 250727469 +3943714082 3509813942 64430639 +3227575371 3229456780 223117769 +1708083440 3574244581 159930470 +2198746190 2075430559 72128459 +729505231 569934147 21214070 +1656447494 4226532866 51635946 + +temperature-to-humidity map: +2530950430 2986195732 64296956 +3097031068 3050492688 225336526 +2595247386 2262922844 63415061 +394235114 386308291 573314459 +159338027 199058685 71729011 +2107189180 2969998741 16196991 +231067038 0 22309581 +266735072 959622750 109765613 +1941982137 2514902112 165207043 +3525862760 2680109155 81512917 +3809165514 2049071587 78022870 +3887188384 2459869958 55032154 +61551861 270787696 97786166 +4083271930 3575006611 18595319 +993228240 162831557 10548461 +967549573 173380018 25678667 +376500685 368573862 17734429 +2877832272 3856947724 19626311 +3607375677 4093177459 201789837 +2519444451 4007134226 11505979 +2658662447 4082384303 10793156 +3322367594 3945539199 61595027 +253376619 149473104 13358453 +0 87921243 61551861 +2961202681 2127094457 135828387 +3942220538 3468929261 33142375 +2669455603 2761622072 208376669 +4197950728 3371912693 97016568 +4101867249 3275829214 96083479 +2446763340 1976390476 72681111 +2313231287 2326337905 133532053 +2897458583 4018640205 63744098 +2123386171 3667102608 189845116 +1003776701 22309581 65611662 +3975362913 3593601930 73500678 +4048863591 1941982137 34408339 +3452927785 3502071636 72934975 +3383962621 3876574035 68965164 + +humidity-to-location map: +0 853712401 14149303 +2655090225 1087300934 303915897 +2027272660 3174210041 18998832 +1525779414 1936221923 38337972 +4147713982 3193208873 142508118 +2959006122 2143904256 380930882 +1087300934 1765319513 65896883 +1352738345 4121926227 173041069 +1290854129 4060042011 61884216 +3931908769 4005664051 54377960 +4091732209 2524835138 55981773 +653782902 95274560 214078802 +477505648 85866717 9407843 +2632545935 1543458967 22544290 +123251703 309353362 97540905 +3762564408 1974559895 169344361 +3487433944 3409639319 23582322 +318179985 430129950 159325663 +1216931801 3335716991 12046317 +1153197817 2580816911 63733984 +14149303 406894267 23235683 +2206646140 3433221641 320894268 +3986286729 1566003257 105445480 +37384986 0 85866717 +2112775364 1671448737 93870776 +2046271492 3107706169 66503872 +3511016266 3754115909 251548142 +1228978118 3347763308 61876011 +1564117386 2644550895 463155274 +3339937004 1391216831 147496940 +486913491 589455613 166869411 +4290222100 1538713771 4745196 +220792608 756325024 97387377 +2527540408 1831216396 105005527 diff --git a/day05/resources/test_input01.txt b/day05/resources/test_input01.txt new file mode 100644 index 0000000..bd902a4 --- /dev/null +++ b/day05/resources/test_input01.txt @@ -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/day05/src/main.rs b/day05/src/main.rs new file mode 100644 index 0000000..63f5f2a --- /dev/null +++ b/day05/src/main.rs @@ -0,0 +1,117 @@ +use std::collections::HashMap; +use std::str::FromStr; +use std::time::Instant; +use regex::Regex; + +#[derive(Default, Debug,)] +struct Almanac { + seeds: Vec, + seeds_to_soil: HashMap, + soil_to_fertilizer: HashMap, + fertilizer_to_water: HashMap, + water_to_light: HashMap, + light_to_temperature: HashMap, + temperature_to_humidity: HashMap, + humidity_to_location: HashMap +} + +fn create_mapping(map: &mut HashMap, start_x: u64, start_y: u64, len: u64) { + (start_x..start_x+len).zip(start_y..start_y+len).for_each(|(from ,to) | { + map.insert(from, to); + }); +} + +fn parse_mapping_lines(mapping_lines: &[& str]) -> HashMap> { + let heading_re = Regex::new(r"^(?P[\w-]+)\s+map:$").unwrap(); + let mut result = HashMap::new(); + for mapping in mapping_lines { + let mut lines = mapping.lines(); + let header = lines.next().unwrap(); + + let heading_matches = heading_re.captures(header).unwrap(); + let mapping_name: String = heading_matches["map_name"].to_string(); + + let mut line_mappings = Vec::new(); + for line in lines { + let mut line_elems = line.split_ascii_whitespace(); + let x = u64::from_str(line_elems.next().unwrap()).unwrap(); + let y = u64::from_str(line_elems.next().unwrap()).unwrap(); + let len = u64::from_str(line_elems.next().unwrap()).unwrap(); + + line_mappings.push((x, y, len)); + } + result.insert(mapping_name, line_mappings); + } + result +} + +fn parse_mapping(map: &mut HashMap, encoded_mappings: Vec<(u64, u64, u64)>) { + for (to, from, len) in encoded_mappings { + create_mapping(map, from, to, len); + } +} +fn parse_seeds(seed_config: &str) -> Vec { + seed_config.split_terminator(":").nth(1).unwrap().split_ascii_whitespace().map(|x| u64::from_str(x).unwrap()).collect() +} + +fn get_location_from_seed(almanac: &Almanac, seed: &u64) -> u64 { + let soil = almanac.seeds_to_soil.get(seed).unwrap_or(seed); + let fertilizer = almanac.soil_to_fertilizer.get(soil).unwrap_or(soil); + let water = almanac.fertilizer_to_water.get(fertilizer).unwrap_or(fertilizer); + let light = almanac.water_to_light.get(water).unwrap_or(water); + let temperature = almanac.light_to_temperature.get(light).unwrap_or(light); + let humidity = almanac.temperature_to_humidity.get(temperature).unwrap_or(temperature); + let location = almanac.humidity_to_location.get(humidity).unwrap_or(humidity); + *location +} + +fn find_lowest_location(almanac: &Almanac) -> u64 { + almanac.seeds.iter().map(|x| get_location_from_seed(&almanac, x)).min().unwrap() +} + +fn create_almanac(input: Vec<&str>) -> Almanac{ + let mut almanac = Almanac::default(); + let seeds_vec = parse_seeds(input[0]); + + almanac.seeds = seeds_vec; + + let mappings = parse_mapping_lines(&input[1..]); + + for (mapping_name, mapping) in mappings { + match mapping_name.as_str() { + "seed-to-soil" => parse_mapping(&mut almanac.seeds_to_soil, mapping), + "soil-to-fertilizer" => parse_mapping(&mut almanac.soil_to_fertilizer, mapping), + "fertilizer-to-water" => parse_mapping(&mut almanac.fertilizer_to_water, mapping), + "water-to-light" => parse_mapping(&mut almanac.water_to_light, mapping), + "light-to-temperature" => parse_mapping(&mut almanac.light_to_temperature, mapping), + "temperature-to-humidity" => parse_mapping(&mut almanac.temperature_to_humidity, mapping), + "humidity-to-location" => parse_mapping(&mut almanac.humidity_to_location, mapping), + &_ => {eprintln!("Invalid mapping type!")} + } + } + almanac +} + +fn main() { + let now = Instant::now(); + let input: Vec<&str> = include_str!("../resources/input01.txt").split_terminator("\n\n").collect(); + let almanac = create_almanac(input); + let res = find_lowest_location(&almanac); + println!("Result: {}", res); + let elapsed = now.elapsed(); + println!("Elapsed: {:?}", elapsed); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_input01() { + + let input: Vec<&str> = include_str!("../resources/test_input01.txt").split_terminator("\n\n").collect(); + let almanac = create_almanac(input); + let res = find_lowest_location(&almanac); + assert_eq!(res, 35); + } +} \ No newline at end of file