-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05.rs
145 lines (127 loc) · 3.68 KB
/
day05.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use rayon::prelude::*;
// Vec<usize> is just a list of all seed numbers
// Vec<Vec<Map>> is a vector of all maps in the order of seed2dirt, dirt2shit, shit2aqua, aqua2lux, lux2temp, temp2damp, damp2loc
// Would Vec<Vec<Vec<usize>>> be taking the piss just a little?
// On second thought, let's take the piss.
#[aoc_generator(day5)]
pub fn input_generator(input: &str) -> (Vec<usize>, Vec<Vec<Vec<usize>>>) {
input
.split_once("\n\n")
.map(|(first, second)|
(first
.split(':')
.last()
.unwrap()
.split_whitespace()
.map(|seed| seed.parse().unwrap())
.collect(),
second
.split("\n\n")
.map(|maps| {
maps.lines()
.skip(1)
.map(|map| {
map
.split_whitespace()
.map(|num| num.parse().unwrap())
.collect()
})
.collect()
})
.collect()))
.unwrap()
}
#[aoc(day5, part1)]
pub fn solve_part1(input: &(Vec<usize>, Vec<Vec<Vec<usize>>>)) -> usize {
let seeds = &input.0;
let cats = &input.1;
let locs: Vec<usize> = seeds
.iter()
.map(|&seed| {
let mut result = seed;
for maps in cats {
for map in maps {
if result >= map[1] && result < map[1] + map[2] {
result = map[0] + result - map[1];
break; // Second test case revealed a dirty, dirty trap
}
}
}
result
})
.collect();
*locs.iter().min().unwrap()
}
#[aoc(day5, part2)]
pub fn solve_part2(input: &(Vec<usize>, Vec<Vec<Vec<usize>>>)) -> usize {
// Run out of memory doing it this way.
// Maybe I'll try it on my desktop, but I'll split it for my laptop.
// let seeds: Vec<usize> = input.0
// .as_slice()
// .chunks_exact(2)
// .flat_map(|ch| (ch[0]..ch[0]+ch[1]))
// .collect();
let seeds = &input.0;
let cats = &input.1;
seeds
.chunks_exact(2)
.map(|ch| {
let batch = (ch[0]..ch[0] + ch[1]).collect::<Vec<usize>>();
let locs: Vec<usize> = batch
.par_iter()
.map(|&seed| {
let mut result = seed;
for maps in cats {
for map in maps {
if result >= map[1] && result < map[1] + map[2] {
result = map[0] + result - map[1];
break;
}
}
}
result
})
.collect();
*locs.iter().min().unwrap()
})
.min()
.unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
const TEST: &str = "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";
#[test]
fn part1() {
assert_eq!(solve_part1(&input_generator(TEST)), 35);
}
#[test]
fn part2() {
assert_eq!(solve_part2(&input_generator(TEST)), 46);
}
}