-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_05.rs
162 lines (128 loc) Β· 3.12 KB
/
day_05.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use common::{solution, Answer};
use rayon::{iter::ParallelIterator, slice::ParallelSlice};
solution!("If You Give A Seed A Fertilizer", 5);
fn part_a(input: &str) -> Answer {
let seeds = parse(input);
let mut min = u32::MAX;
for mut seed in seeds.seeds {
for map in &seeds.maps {
seed = map.map(seed);
}
min = min.min(seed);
}
min.into()
}
fn part_b(input: &str) -> Answer {
let seeds = parse(input);
// eh its fast enough
// ~1min on my machine
seeds
.seeds
.par_chunks_exact(2)
.map(|seed| {
let mut min = u32::MAX;
for mut seed in seed[0]..=seed[0] + seed[1] {
for map in &seeds.maps {
seed = map.map(seed);
}
min = min.min(seed);
}
min
})
.min()
.unwrap()
.into()
}
#[derive(Debug)]
struct Map {
ranges: Vec<Range>,
}
#[derive(Debug, Clone)]
struct Range {
end: u32,
start: u32,
length: u32,
}
struct ParseResult {
maps: Vec<Map>,
seeds: Vec<u32>,
}
fn parse(input: &str) -> ParseResult {
let mut maps = Vec::new();
let mut sections = input.split("\n\n");
let seeds = sections
.next()
.unwrap()
.split_whitespace()
.skip(1)
.map(|s| s.parse().unwrap())
.collect();
for section in sections.filter(|x| !x.is_empty()) {
let lines = section.lines();
let mut ranges = Vec::new();
for line in lines.skip(1) {
let mut parts = line.split_whitespace();
let end = parts.next().unwrap().parse().unwrap();
let start = parts.next().unwrap().parse().unwrap();
let length = parts.next().unwrap().parse().unwrap();
ranges.push(Range { start, end, length });
}
maps.push(Map { ranges });
}
ParseResult { maps, seeds }
}
impl Range {
fn start_contains(&self, value: u32) -> bool {
self.start <= value && value < self.start.wrapping_add(self.length)
}
}
impl Map {
fn map(&self, value: u32) -> u32 {
for range in &self.ranges {
if range.start_contains(value) {
return range.end.wrapping_add(value).wrapping_sub(range.start);
}
}
value
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {"
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 part_a() {
assert_eq!(super::part_a(CASE), 35.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 46.into());
}
}