day 5
This commit is contained in:
parent
31b15680b9
commit
b35073f5b5
7
day5/Cargo.lock
generated
Normal file
7
day5/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
8
day5/Cargo.toml
Normal file
8
day5/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[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]
|
239
day5/input.txt
Normal file
239
day5/input.txt
Normal file
@ -0,0 +1,239 @@
|
||||
seeds: 929142010 467769747 2497466808 210166838 3768123711 33216796 1609270159 86969850 199555506 378609832 1840685500 314009711 1740069852 36868255 2161129344 170490105 2869967743 265455365 3984276455 31190888
|
||||
|
||||
seed-to-soil map:
|
||||
3788621315 24578909 268976974
|
||||
3633843608 2672619957 154777707
|
||||
1562003446 2827397664 767899879
|
||||
2618130896 293555883 1015712712
|
||||
178572254 3595297543 462300746
|
||||
640873000 1553961386 921130446
|
||||
2373438105 1435027522 118933864
|
||||
2492371969 1309268595 125758927
|
||||
2329903325 2629085177 43534780
|
||||
24578909 2475091832 153993345
|
||||
|
||||
soil-to-fertilizer map:
|
||||
3438795585 3489901885 246180709
|
||||
2207634271 2882741084 20404178
|
||||
2921657379 3093830975 5578539
|
||||
3684976294 494116589 47356386
|
||||
3924272359 3740035076 65483093
|
||||
501906244 2178419272 30310459
|
||||
1234683323 1988217660 166809869
|
||||
1901474422 3380297447 5929907
|
||||
1561485292 902794130 43191607
|
||||
3740035076 4064176085 35875061
|
||||
1756130945 2337296126 107889556
|
||||
1631134020 2212299201 124996925
|
||||
3775910137 4100051146 148362222
|
||||
1907404329 355795024 25976339
|
||||
98608320 1132323727 78740039
|
||||
177348359 323563608 17306027
|
||||
3181146272 2546033985 176714999
|
||||
1135867625 395300891 98815698
|
||||
1604676899 381771363 13529528
|
||||
1062398411 3348792067 31505380
|
||||
1933380668 1211063766 135031541
|
||||
4152704827 4248413368 14047124
|
||||
2231607919 3386227354 49301269
|
||||
2068412209 1349845221 139222062
|
||||
1618206427 0 12927593
|
||||
4166751951 3935960740 128215345
|
||||
4055852131 3871614848 64345892
|
||||
1093903791 340869635 14925389
|
||||
2280909188 93861907 229701701
|
||||
252523691 3099409514 249382553
|
||||
194654386 640081295 34477562
|
||||
2228038449 2208729731 3569470
|
||||
2927235918 674558857 8851379
|
||||
3989755452 3805518169 66096679
|
||||
3732332680 1346095307 3749914
|
||||
229131948 2155027529 23391743
|
||||
1108829180 945985737 27038445
|
||||
2510610889 683410236 219383894
|
||||
2729994783 973024182 159299545
|
||||
2936087297 2903145262 190685713
|
||||
2889294328 1489067283 32363051
|
||||
4120198023 4262460492 32506804
|
||||
633065006 1558884255 429333405
|
||||
3126773010 3435528623 54373262
|
||||
0 541472975 98608320
|
||||
3357861271 12927593 80934314
|
||||
532216703 2445185682 100848303
|
||||
1401493192 2722748984 159992100
|
||||
1864020501 1521430334 37453921
|
||||
|
||||
fertilizer-to-water map:
|
||||
1947541026 454827174 151424508
|
||||
2368626375 2974348911 49282632
|
||||
1180361458 1899400011 39609082
|
||||
680762316 1808139816 91260195
|
||||
3146089823 3660401583 124406340
|
||||
1750346896 411180537 43646637
|
||||
812988815 228261758 177656250
|
||||
1318675555 1023120195 274976204
|
||||
1593651759 866425058 156695137
|
||||
3971733879 2669799085 75517075
|
||||
3655228757 4176552157 118415139
|
||||
3441507492 3915100392 170047342
|
||||
3888713098 2412300298 83020781
|
||||
552417940 99917382 128344376
|
||||
1799256062 1298096399 110644443
|
||||
0 825619231 40805827
|
||||
123867918 1939009093 176811732
|
||||
3835793763 4085147734 52919335
|
||||
300679650 1712674949 32370741
|
||||
772022511 1598457235 40966304
|
||||
1219970540 1639423539 73251410
|
||||
3297826708 3784807923 40826594
|
||||
1793993533 405918008 5262529
|
||||
3056623948 3825634517 89465875
|
||||
3773643896 3023631543 62149867
|
||||
1909900505 1770499295 37640521
|
||||
40805827 16855291 83062091
|
||||
2417909007 2745316160 229032751
|
||||
3611554834 2368626375 43673923
|
||||
2646941758 4138067069 38485088
|
||||
4047250954 2598175269 71623816
|
||||
3338653302 2495321079 102854190
|
||||
2685426846 3279231811 71633749
|
||||
4118874770 3085781410 165769241
|
||||
3028942788 3251550651 27681160
|
||||
333050391 606251682 219367549
|
||||
2757060595 3388519390 271882193
|
||||
1293221950 1745045690 25453605
|
||||
3270496163 3350865560 27330545
|
||||
4284644011 3378196105 10323285
|
||||
990645065 1408740842 189716393
|
||||
2098965534 0 16855291
|
||||
|
||||
water-to-light map:
|
||||
1069022530 222696536 26137468
|
||||
509774651 1001927432 264687540
|
||||
2799496570 4178737505 91960917
|
||||
2622746937 2920297784 87218886
|
||||
998047684 72317843 70974846
|
||||
20975330 248834004 16650836
|
||||
1516625244 2418736444 445891562
|
||||
867141089 265484840 130906595
|
||||
1452450963 1912658188 64174281
|
||||
2891457487 2382365765 36370679
|
||||
837778702 42955456 29362387
|
||||
3824593525 1874523410 38134778
|
||||
87390794 857485915 142625628
|
||||
3874640725 2075355481 18387003
|
||||
774462191 529657635 63316511
|
||||
3607044420 4131300557 47436948
|
||||
2288390345 3657176164 11501412
|
||||
3042932184 1976832469 64291113
|
||||
2947540744 4035909117 95391440
|
||||
87126518 42691180 264276
|
||||
1425451487 3765808292 26999476
|
||||
3893027728 3579750125 77426039
|
||||
2203098143 3680516090 85292202
|
||||
3992272144 3274151418 290856638
|
||||
2927828166 4033389105 2520012
|
||||
278763375 464023676 18702895
|
||||
3114215141 1527053163 335557825
|
||||
3654481368 3014508514 170112157
|
||||
2930348178 3565008056 14742069
|
||||
230016422 482726571 46931064
|
||||
3970453767 4270698422 21818377
|
||||
4283128782 3668677576 11838514
|
||||
1223923792 0 42691180
|
||||
3505442744 1425451487 101601676
|
||||
2709965823 3184620671 89530747
|
||||
1174563845 737650286 49359947
|
||||
3862728303 1862610988 11912422
|
||||
37626166 787010233 49500352
|
||||
3449772966 2864628006 55669778
|
||||
297466270 592974146 144676140
|
||||
2334123656 2093742484 288623281
|
||||
2945090247 4292516799 2450497
|
||||
1095159998 143292689 79403847
|
||||
3107223297 3007516670 6991844
|
||||
0 836510585 20975330
|
||||
2299891757 2041123582 34231899
|
||||
276947486 1000111543 1815889
|
||||
442142410 396391435 67632241
|
||||
1962516806 3792807768 240581337
|
||||
|
||||
light-to-temperature map:
|
||||
3884798542 3179257010 5536030
|
||||
2221366309 1722332134 176062455
|
||||
550850442 2239976316 812984276
|
||||
1811481549 3100760963 78496047
|
||||
2640090257 457193060 313876206
|
||||
2495814208 2197536573 42439743
|
||||
3995825312 1898394589 299141984
|
||||
3754849534 3407613625 129949008
|
||||
3890334572 3302122885 105490740
|
||||
1889977596 771069266 255516805
|
||||
3300733900 3052960592 47800371
|
||||
3497312635 4294682795 284501
|
||||
3348534271 3830987352 148778364
|
||||
3181367714 3537562633 118078810
|
||||
2397428764 3655641443 98385444
|
||||
2563129792 3754026887 11234150
|
||||
3497597136 3279737798 22385087
|
||||
2538253951 1249809462 24875841
|
||||
2953966463 4067281544 227401251
|
||||
2145494401 3979765716 75871908
|
||||
1363834718 1274685303 447646831
|
||||
457193060 3184793040 93657382
|
||||
3519982223 4055637624 11643920
|
||||
2574363942 3765261037 65726315
|
||||
3299446524 3278450422 1287376
|
||||
3531626143 1026586071 223223391
|
||||
|
||||
temperature-to-humidity map:
|
||||
1042511941 613297634 21380371
|
||||
1338560988 2467432579 195807105
|
||||
1689790100 2663239684 5147838
|
||||
1534368093 1255646654 123554085
|
||||
1657922178 472280060 31867922
|
||||
1063892312 634678005 22101508
|
||||
3020373353 2743804882 227587056
|
||||
3354622010 3207836814 51442173
|
||||
279315000 1414949840 172148078
|
||||
2333443661 0 36016786
|
||||
2656069043 859373847 9250148
|
||||
81520987 1873230878 37317266
|
||||
2452902712 868623995 174257482
|
||||
2665319191 1042881477 3068331
|
||||
1085993820 504147982 109149652
|
||||
1875725935 36016786 328594859
|
||||
3247960409 4062312657 86516416
|
||||
2743804882 3833392893 10707972
|
||||
2204320794 2254867447 129122867
|
||||
1302811887 1379200739 35749101
|
||||
3930773544 4267797728 7024383
|
||||
4056766582 3259278987 238200714
|
||||
1195143472 364611645 107668415
|
||||
0 656779513 81520987
|
||||
2369460447 2383990314 83442265
|
||||
3334476825 4274822111 20145185
|
||||
451463078 1910548144 344319303
|
||||
811683287 1642402224 230828654
|
||||
2754512854 3160188107 47648707
|
||||
2802161561 3844100865 218211792
|
||||
3741977375 2971391938 188796169
|
||||
2627160194 1226737805 28908849
|
||||
3406064183 3497479701 245403322
|
||||
795782381 843472941 15900906
|
||||
174142559 738300500 105172441
|
||||
1694937938 1045949808 180787997
|
||||
3651467505 3742883023 90509870
|
||||
3937797927 4148829073 118968655
|
||||
118838253 1587097918 55304306
|
||||
|
||||
humidity-to-location map:
|
||||
561172837 465568195 974822499
|
||||
3386805071 4164489434 100929125
|
||||
1899981360 1440390694 188334950
|
||||
459590666 0 101582171
|
||||
0 1735512469 459590666
|
||||
3517282933 3386805071 777684363
|
||||
2088316310 1628725644 106786825
|
||||
3487734196 4265418559 29548737
|
||||
1535995336 101582171 363986024
|
116
day5/src/main.rs
Normal file
116
day5/src/main.rs
Normal file
@ -0,0 +1,116 @@
|
||||
use std::fs::read_to_string;
|
||||
use std::cmp;
|
||||
use std::time::Instant;
|
||||
|
||||
fn get_numbers_in_line(str: &str) -> Vec<u32> {
|
||||
return str.split_whitespace().filter_map(|substr| substr.parse::<u32>().ok()).collect();
|
||||
}
|
||||
|
||||
fn process(seeds: Vec<u32>, mut maps: Vec<Vec<(u32, u32, u32)>>) -> (u32, u32) {
|
||||
for map in maps.iter_mut() {
|
||||
map.sort_unstable_by(|&(a, _, _), &(b, _, _)| a.partial_cmp(&b).unwrap());
|
||||
}
|
||||
// Task 1 - mapping a single number through all mappings
|
||||
let min1 = seeds.iter().map(
|
||||
|seed| {
|
||||
let mut n = *seed;
|
||||
for map in &maps {
|
||||
for &(src, dst, len) in map {
|
||||
if src > n {
|
||||
break;
|
||||
}
|
||||
if n - src < len {
|
||||
n = n - src + dst;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
n
|
||||
}
|
||||
).min().unwrap();
|
||||
|
||||
// Task 2 - mapping whole ranges through all mappings
|
||||
// Array of ranges that will be mapped for task 2 initialized with pairs from seeds
|
||||
let mut processed_ranges: Vec<_> = (0..seeds.len()).step_by(2).map(|i| (seeds[i], seeds[i + 1])).collect();
|
||||
let mut swap_ranges: Vec<(u32, u32)> = Vec::new();
|
||||
// Map the processed_ranged through all mappings
|
||||
for map in &maps {
|
||||
swap_ranges.clear();
|
||||
for &(working_start, working_len) in &processed_ranges {
|
||||
// Applying one or more mapping ranges to the processed range
|
||||
let (mut temp_start, mut temp_len) = (working_start, working_len);
|
||||
for &(src, dst, len) in map {
|
||||
// Working range starts before mapping range, means it's an unmapped segment
|
||||
if temp_start < src {
|
||||
let out_len = cmp::min(temp_len, src - temp_start);
|
||||
swap_ranges.push((temp_start, out_len));
|
||||
if out_len == temp_len {
|
||||
break;
|
||||
}
|
||||
temp_start = src;
|
||||
}
|
||||
// Working range is sure to not begin before mapping range
|
||||
let start_diff = temp_start - src;
|
||||
// Mapping range is so short that it doesn't reach the working range, skip
|
||||
if len <= start_diff {
|
||||
continue;
|
||||
}
|
||||
let out_len = cmp::min(temp_len, len - start_diff);
|
||||
swap_ranges.push((dst + start_diff, out_len));
|
||||
if out_len == temp_len {
|
||||
break;
|
||||
}
|
||||
// Consume the beginning of the working range
|
||||
temp_start += out_len;
|
||||
temp_len -= out_len;
|
||||
}
|
||||
}
|
||||
std::mem::swap(&mut processed_ranges, &mut swap_ranges);
|
||||
}
|
||||
// Find the range with lowest starting value, length is guaranteed to be nonzero
|
||||
let min2 = processed_ranges.iter().map(|&(start, _)| start).min().unwrap();
|
||||
(min1, min2)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let time_start = Instant::now();
|
||||
// Set of inputs
|
||||
let mut seeds: Vec<u32> = Vec::new();
|
||||
// Mappings of ranges, ordered in order to easily find the lowest relevant range
|
||||
// Order: (src, dst, len)
|
||||
let mut maps: Vec<Vec<(u32, u32, u32)>> = Vec::new();
|
||||
maps.push(Vec::new());
|
||||
let mut did_read_seeds: bool = false;
|
||||
for line in read_to_string("input.txt").unwrap().lines() {
|
||||
if !did_read_seeds {
|
||||
seeds = get_numbers_in_line(line);
|
||||
did_read_seeds = true;
|
||||
} else {
|
||||
let numbers = get_numbers_in_line(line);
|
||||
if numbers.len() == 3 {
|
||||
maps.last_mut().unwrap().push((numbers[1],numbers[0], numbers[2]));
|
||||
} else {
|
||||
if !maps.last().unwrap().is_empty() {
|
||||
maps.push(Vec::new());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if maps.last().unwrap().is_empty() {
|
||||
maps.pop();
|
||||
}
|
||||
let time_start_no_io = Instant::now();
|
||||
|
||||
// for _ in 0..1000 {
|
||||
// let (_, _) = process(seeds.clone(), maps.clone());
|
||||
// }
|
||||
let (min1, min2) = process(seeds, maps);
|
||||
|
||||
// Measurements
|
||||
let elapsed = time_start.elapsed().as_nanos();
|
||||
let elapsed_no_io = time_start_no_io.elapsed().as_nanos();
|
||||
println!("Time: {}ns", elapsed);
|
||||
println!("Time without file i/o: {}ns", elapsed_no_io);
|
||||
println!("Min location1: {}", min1);
|
||||
println!("Min location2: {}", min2);
|
||||
}
|
Loading…
Reference in New Issue
Block a user