Part 05
This commit is contained in:
166
src/bin/part05.rs
Normal file
166
src/bin/part05.rs
Normal file
@@ -0,0 +1,166 @@
|
||||
use std::time;
|
||||
|
||||
use rayon::prelude::*;
|
||||
|
||||
struct Almanac {
|
||||
seeds: Vec<u32>,
|
||||
seed_to_soil: Vec<Mapping>,
|
||||
soil_to_fertilizer: Vec<Mapping>,
|
||||
fertilizer_to_water: Vec<Mapping>,
|
||||
water_to_light:Vec<Mapping>,
|
||||
light_to_temperature: Vec<Mapping>,
|
||||
temperature_to_humidity: Vec<Mapping>,
|
||||
humidity_to_location: Vec<Mapping>,
|
||||
}
|
||||
|
||||
impl Almanac {
|
||||
fn new() -> Almanac {
|
||||
Almanac {
|
||||
seeds: Vec::new(),
|
||||
seed_to_soil: Vec::new(),
|
||||
soil_to_fertilizer: Vec::new(),
|
||||
fertilizer_to_water: Vec::new(),
|
||||
water_to_light: Vec::new(),
|
||||
light_to_temperature: Vec::new(),
|
||||
temperature_to_humidity: Vec::new(),
|
||||
humidity_to_location: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_soil(&self, seed: u32) -> u32 {
|
||||
map(seed, &self.seed_to_soil)
|
||||
}
|
||||
|
||||
fn get_fertilizer(&self, seed: u32) -> u32 {
|
||||
map(self.get_soil(seed), &self.soil_to_fertilizer)
|
||||
}
|
||||
|
||||
fn get_water(&self, seed: u32) -> u32 {
|
||||
map(self.get_fertilizer(seed), &self.fertilizer_to_water)
|
||||
}
|
||||
|
||||
fn get_light(&self, seed: u32) -> u32 {
|
||||
map(self.get_water(seed), &self.water_to_light)
|
||||
}
|
||||
|
||||
fn get_temperature(&self, seed: u32) -> u32 {
|
||||
map(self.get_light(seed), &self.light_to_temperature)
|
||||
}
|
||||
|
||||
fn get_humidity(&self, seed: u32) -> u32 {
|
||||
map(self.get_temperature(seed), &self.temperature_to_humidity)
|
||||
}
|
||||
|
||||
fn get_location(&self, seed: u32) -> u32 {
|
||||
map(self.get_humidity(seed), &self.humidity_to_location)
|
||||
}
|
||||
}
|
||||
|
||||
struct Mapping {
|
||||
dst_range_start: u64,
|
||||
src_range_start: u64,
|
||||
range_length: u64
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let s = time::Instant::now();
|
||||
|
||||
let almanac = load_input();
|
||||
let mut locations: Vec<u32> = Vec::new();
|
||||
|
||||
// for seed in &almanac.seeds {
|
||||
// let location = almanac.get_location(*seed);
|
||||
// locations.push(location);
|
||||
|
||||
// println!("Seed: {}", seed);
|
||||
// println!("Location: {}", location);
|
||||
// println!();
|
||||
// }
|
||||
|
||||
// println!("Lowest location #1: {}", locations.iter().min().unwrap());
|
||||
// locations.clear();
|
||||
|
||||
let mut seeds = almanac.seeds.iter();
|
||||
loop {
|
||||
let seed_start = seeds.next().unwrap_or(&0);
|
||||
if *seed_start == 0 {
|
||||
break;
|
||||
}
|
||||
let seed_end = seed_start + seeds.next().unwrap();
|
||||
let seed_count = seed_end - seed_start;
|
||||
|
||||
let s1 = time::Instant::now();
|
||||
|
||||
let locs = (*seed_start..seed_end).into_par_iter().map(|seed| {
|
||||
almanac.get_location(seed)
|
||||
});
|
||||
locations.push(locs.min().unwrap());
|
||||
|
||||
let d1 = time::Instant::now().duration_since(s1);
|
||||
println!("Parsed {} seeds in {:?} ({} seeds/s)", seed_count, d1, seed_count as f64 / d1.as_secs_f64());
|
||||
}
|
||||
|
||||
let d = time::Instant::now().duration_since(s);
|
||||
println!("Parsed in {:?}", d);
|
||||
|
||||
println!("Lowest location #2: {}", locations.iter().min().unwrap());
|
||||
}
|
||||
|
||||
fn load_input() -> Almanac {
|
||||
let mut almanac = Almanac::new();
|
||||
|
||||
let data = std::fs::read_to_string("src/bin/part05.txt")
|
||||
.expect("Could not read file");
|
||||
let mut lines = data.lines();
|
||||
|
||||
almanac.seeds = lines.next().unwrap().split(": ").last().unwrap().split_whitespace().map(|s| s.parse::<u32>().unwrap()).collect::<Vec<_>>();
|
||||
almanac.seed_to_soil = parse_map(&mut lines);
|
||||
almanac.soil_to_fertilizer = parse_map(&mut lines);
|
||||
almanac.fertilizer_to_water = parse_map(&mut lines);
|
||||
almanac.water_to_light = parse_map(&mut lines);
|
||||
almanac.light_to_temperature = parse_map(&mut lines);
|
||||
almanac.temperature_to_humidity = parse_map(&mut lines);
|
||||
almanac.humidity_to_location = parse_map(&mut lines);
|
||||
|
||||
almanac
|
||||
}
|
||||
|
||||
fn parse_map(line_iter: &mut dyn Iterator<Item=&str>) -> Vec<Mapping> {
|
||||
let mut mappings = Vec::new();
|
||||
|
||||
line_iter.nth(0);
|
||||
|
||||
loop {
|
||||
let line = line_iter.next().unwrap_or("");
|
||||
if line.is_empty() {
|
||||
break;
|
||||
} else if line.ends_with(":") {
|
||||
continue;
|
||||
}
|
||||
let mut raw_mapping = line.split_whitespace();
|
||||
mappings.push(Mapping {
|
||||
dst_range_start: raw_mapping.next().unwrap().parse::<u64>().unwrap(),
|
||||
src_range_start: raw_mapping.next().unwrap().parse::<u64>().unwrap(),
|
||||
range_length: raw_mapping.next().unwrap().parse::<u64>().unwrap()
|
||||
});
|
||||
}
|
||||
|
||||
mappings.sort_by(|a, b| b.src_range_start.cmp(&a.src_range_start));
|
||||
|
||||
mappings
|
||||
}
|
||||
|
||||
fn map(src: u32, mappings: &Vec<Mapping>) -> u32 {
|
||||
let src = src as u64;
|
||||
let mut dst = src;
|
||||
|
||||
let mapping = mappings.iter().find(|mapping| {
|
||||
src >= mapping.src_range_start && src < mapping.src_range_start + mapping.range_length
|
||||
});
|
||||
|
||||
if let Some(mapping) = mapping {
|
||||
dst = (mapping.dst_range_start + src) - mapping.src_range_start;
|
||||
}
|
||||
|
||||
dst as u32
|
||||
}
|
||||
33
src/bin/part05.sample
Normal file
33
src/bin/part05.sample
Normal file
@@ -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
|
||||
213
src/bin/part05.txt
Normal file
213
src/bin/part05.txt
Normal file
@@ -0,0 +1,213 @@
|
||||
seeds: 3139431799 50198205 3647185634 110151761 2478641666 139825503 498892555 8913570 961540761 489996751 568452082 100080382 907727477 42158689 1617552130 312026427 342640189 97088268 2049289560 336766062
|
||||
|
||||
seed-to-soil map:
|
||||
1615836342 1401909974 23067952
|
||||
785532007 269485885 88937774
|
||||
3019002892 2773729385 10470414
|
||||
4202163101 2747292152 26437233
|
||||
3183210415 4217634159 77333137
|
||||
2847460091 3211730218 136699600
|
||||
2455891790 3791729773 70553041
|
||||
3260543552 2581343101 165949051
|
||||
3840286095 2849853212 361877006
|
||||
4228600334 2361239030 66366962
|
||||
1594559581 1077839137 21276761
|
||||
380069408 165017790 44262617
|
||||
3598718222 1894384162 241567873
|
||||
0 1424977926 190757551
|
||||
1894384162 2810496375 39356837
|
||||
424332025 606264721 196539291
|
||||
3521487829 2221977524 77230393
|
||||
742681934 69797365 36566707
|
||||
1638904294 1615735477 139190145
|
||||
1335949488 0 69797365
|
||||
779248641 802804012 6283366
|
||||
2638766896 4008940964 208693195
|
||||
250963029 1142644585 70452661
|
||||
1933740999 3470280789 321448984
|
||||
190757551 209280407 60205478
|
||||
1778094439 1099115898 43528687
|
||||
2255189983 3348429818 121850971
|
||||
1000500225 809087378 268751759
|
||||
1269251984 1754925622 66697504
|
||||
874469781 358423659 126030444
|
||||
2526444831 2135952035 86025489
|
||||
2439072067 3992121241 16819723
|
||||
3426492603 3897126015 94995226
|
||||
1405746853 1213097246 188812728
|
||||
321415690 106364072 58653718
|
||||
2984159691 3862282814 34843201
|
||||
2377040954 2299207917 62031113
|
||||
3029473306 2427605992 153737109
|
||||
2612470320 2784199799 26296576
|
||||
620871316 484454103 121810618
|
||||
|
||||
soil-to-fertilizer map:
|
||||
4245401761 2352458099 28057201
|
||||
2099789767 3998256334 14950546
|
||||
3446056574 2749719529 135349925
|
||||
890092371 1379309857 42097049
|
||||
953714890 896502554 10335567
|
||||
3115342240 2380515300 218129381
|
||||
3333471621 3885671381 112584953
|
||||
663999152 0 226093219
|
||||
1873325002 727305635 169196919
|
||||
2042521921 1328150912 51158945
|
||||
3581406499 4034715214 260252082
|
||||
500989478 564295961 163009674
|
||||
4273458962 4013206880 21508334
|
||||
3992733429 2099789767 164092891
|
||||
4156826320 2263882658 88575441
|
||||
3841658581 2598644681 151074848
|
||||
1094703999 226093219 21270249
|
||||
1708571521 399542480 164753481
|
||||
964050457 247363468 130653542
|
||||
1236290130 1421406906 171284482
|
||||
0 1592691388 500989478
|
||||
932189420 378017010 21525470
|
||||
2114740313 2885069454 1000601927
|
||||
1115974248 1207835030 120315882
|
||||
1407574612 906838121 300996909
|
||||
|
||||
fertilizer-to-water map:
|
||||
3217858280 3761663130 355893932
|
||||
2319366035 2401839275 72374872
|
||||
1962726423 909927230 105011330
|
||||
2115307878 441322644 204058157
|
||||
2095064202 1824085445 20243676
|
||||
110580631 329763915 34129824
|
||||
2573484127 2701101998 225220022
|
||||
1780224111 2342656863 59182412
|
||||
1717605398 1571533532 62618713
|
||||
3589165621 3062909078 75538793
|
||||
842280446 1871096488 471560375
|
||||
409726333 0 243114563
|
||||
397401582 760576019 12324751
|
||||
0 667815878 92760141
|
||||
3990305711 2926322020 5981819
|
||||
251664023 1741105813 32320840
|
||||
2072629125 645380801 22435077
|
||||
92760141 363893739 17820490
|
||||
2798704149 3138447871 156185660
|
||||
3664704414 2573484127 127617871
|
||||
1839406523 381714229 59608415
|
||||
283984863 1844329121 26767367
|
||||
1313840821 1176023584 27579684
|
||||
2391740907 1790738543 33346902
|
||||
3929650615 4234312200 60655096
|
||||
3996287530 3557602002 204061128
|
||||
1402088224 772900770 137026460
|
||||
1911435456 2474214147 51290967
|
||||
1539114684 1393042818 178490714
|
||||
3573752212 4117557062 15413409
|
||||
1341420505 1014938560 60667719
|
||||
3792322285 4132970471 6723091
|
||||
4200348658 4139693562 80082634
|
||||
652840896 1203603268 189439550
|
||||
310752230 243114563 86649352
|
||||
2425087809 1075606279 100417305
|
||||
1899014938 1773426653 12420518
|
||||
2067737753 1785847171 4891372
|
||||
3799045376 2932303839 130605239
|
||||
2954889809 3294633531 262968471
|
||||
4280431292 4219776196 14536004
|
||||
144710455 1634152245 106953568
|
||||
|
||||
water-to-light map:
|
||||
1071107509 759231097 26724064
|
||||
1293599454 642189614 64567949
|
||||
3147690498 1633749175 71376364
|
||||
3487999223 4080968704 155844998
|
||||
1700873635 2097781236 292450760
|
||||
1146121952 1950303734 147477502
|
||||
2864027062 1470573702 75076585
|
||||
2507471274 3021419800 175807670
|
||||
2939103647 922829268 123372939
|
||||
2826742681 2551285626 37284381
|
||||
719212681 1821763210 100196810
|
||||
4236813702 4292227071 2740225
|
||||
3643844221 3837685117 243283587
|
||||
209103905 785955161 52302440
|
||||
100332146 752700417 6530680
|
||||
1478192138 3229160565 10342236
|
||||
1358167403 3239502801 94242072
|
||||
3219066862 706757563 45942854
|
||||
3062476586 1358475140 29661777
|
||||
819409491 2390231996 26561329
|
||||
4195136152 3511184798 41677550
|
||||
1488534374 1046202207 35444248
|
||||
3887127808 3552862348 284822769
|
||||
2188804057 1705125539 116637671
|
||||
0 309410320 100332146
|
||||
1117778238 1921960020 28343714
|
||||
261406345 3333744873 74978759
|
||||
2100705169 1545650287 88098888
|
||||
3450098653 3473284228 37900570
|
||||
2742067948 1273800407 84674733
|
||||
2305441728 107380774 202029546
|
||||
3265009716 1130086491 143713916
|
||||
1993324395 0 107380774
|
||||
1523978622 465294601 176895013
|
||||
106862826 2588570007 102241079
|
||||
2683278944 864040264 58789004
|
||||
4239553927 4236813702 55413369
|
||||
1097831573 1081646455 19946665
|
||||
524293914 1101593120 28493371
|
||||
336385104 2833510990 187908810
|
||||
552787285 3197227470 31933095
|
||||
845970820 2690811086 142699904
|
||||
988670724 1388136917 82436785
|
||||
1452409475 838257601 25782663
|
||||
3092138363 409742466 55552135
|
||||
584720380 2416793325 134492301
|
||||
4171950577 3450098653 23185575
|
||||
|
||||
light-to-temperature map:
|
||||
2906633798 3843376160 451591136
|
||||
1332454428 1190958320 69004583
|
||||
1837712164 0 353313230
|
||||
494809338 353313230 376619264
|
||||
871428602 729932494 461025826
|
||||
1401459011 1754772241 373416033
|
||||
3976747173 3375456648 91164221
|
||||
3495346659 3466620869 376755291
|
||||
0 1259962903 494809338
|
||||
2608238358 2459541635 298395440
|
||||
3907558614 2984992977 69188559
|
||||
3872101950 2286963246 35456664
|
||||
4067911394 2757937075 227055902
|
||||
3358224934 2322419910 137121725
|
||||
2286963246 3054181536 321275112
|
||||
1774875044 2128188274 62837120
|
||||
|
||||
temperature-to-humidity map:
|
||||
3966168141 3406025946 214996780
|
||||
4181164921 3292223571 113802375
|
||||
1493139015 1471031672 367564898
|
||||
1423475871 1838596570 69663144
|
||||
0 479293006 226560784
|
||||
2500785470 2859072453 433151118
|
||||
3197453551 2500785470 96923792
|
||||
758446483 1237739489 233292183
|
||||
991738666 0 278789291
|
||||
3555740534 3884539689 410427607
|
||||
3294377343 2597709262 261363191
|
||||
226560784 705853790 531885699
|
||||
1860703913 305742584 20602508
|
||||
2933936588 3621022726 263516963
|
||||
1881306421 278789291 26953293
|
||||
1270527957 326345092 152947914
|
||||
|
||||
humidity-to-location map:
|
||||
848612454 2250862530 61410922
|
||||
910023376 3689675651 35197452
|
||||
3724873103 3865027106 240221283
|
||||
483883727 3324946924 364728727
|
||||
0 1766978803 483883727
|
||||
1957894300 561533 922927950
|
||||
945220828 2590144784 734802140
|
||||
2880822250 0 561533
|
||||
3447014853 1489120553 277858250
|
||||
2881383783 923489483 565631070
|
||||
3965094386 3724873103 140154003
|
||||
1680022968 2312273452 277871332
|
||||
Reference in New Issue
Block a user