-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_10.rs
208 lines (193 loc) · 5.22 KB
/
day_10.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::collections::VecDeque;
type Grid = Vec<Vec<Option<u32>>>;
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq)]
struct Coord {
x: usize,
y: usize,
height: Option<u32>,
}
#[derive(Debug)]
enum NextTrail {
Init(Coord),
PartialTrail(Vec<Coord>),
CompletedTrail(Vec<Coord>),
}
fn get_zeros_loc(g: &Grid) -> Vec<Coord> {
g.iter()
.enumerate()
.flat_map(|(r_idx, row)| {
row.iter().enumerate().filter_map(move |(c_idx, height)| {
if let Some(0) = height {
return Some(Coord {
x: c_idx,
y: r_idx,
height: *height,
});
}
None
})
})
.collect()
}
fn get_height(g: &Grid, c: (usize, usize)) -> Option<u32> {
let (x, y) = c;
*g.get(y)?.get(x)?
}
fn parse_input(s: &str) -> Grid {
s.lines()
.map(|line| line.chars().map(|c| c.to_digit(10)).collect())
.collect()
}
fn get_next_trails(g: &Grid, c: NextTrail) -> Vec<NextTrail> {
let mut trail;
let mut coord;
match c {
NextTrail::Init(init_coord) => {
trail = vec![];
coord = init_coord;
}
NextTrail::PartialTrail(part_trail) => {
coord = part_trail.last().unwrap().to_owned();
trail = part_trail;
}
NextTrail::CompletedTrail(comp_trail) => unreachable!(),
};
let up = (coord.x, coord.y.saturating_sub(1));
let down = (coord.x, coord.y + 1);
let left = (coord.x.saturating_sub(1), coord.y);
let right = (coord.x + 1, coord.y);
[
(get_height(g, up), up),
(get_height(g, down), down),
(get_height(g, left), left),
(get_height(g, right), right),
]
.into_iter()
.filter(|(_, next_coord)| (coord.x, coord.y) != (next_coord.0, next_coord.1))
.filter(|(next_height, _)| next_height == &coord.height.map(|h| h + 1))
.map(move |(next_height, next_coord)| {
let next_coord = Coord {
x: next_coord.0,
y: next_coord.1,
height: next_height,
};
let mut trail = trail.clone();
trail.push(next_coord);
if let Some(9) = next_height {
NextTrail::CompletedTrail(trail)
} else {
NextTrail::PartialTrail(trail)
}
})
.collect()
}
// Returns a list of all completed trails.
fn recurse_trails(g: &Grid, trails: Vec<NextTrail>) -> Vec<Vec<Coord>> {
let mut completed = vec![];
for trail in trails {
match trail {
NextTrail::Init(coord) => {
let next_trails = get_next_trails(g, NextTrail::Init(coord));
completed.extend(recurse_trails(g, next_trails));
}
NextTrail::PartialTrail(trails) => {
let next_trails = get_next_trails(g, NextTrail::PartialTrail(trails));
completed.extend(recurse_trails(g, next_trails));
}
NextTrail::CompletedTrail(vec) => completed.push(vec),
}
}
completed
}
fn get_trails_score(t: &[Vec<Coord>]) -> usize {
let mut t = t.to_owned();
t.sort_by(|t1, t2| t1.last().unwrap().cmp(t2.last().unwrap()));
t.dedup_by(|t1, t2| t1.last().unwrap() == t2.last().unwrap());
t.len()
}
fn part_2_solution(s: &str) -> usize {
let grid = parse_input(s);
let zeros = get_zeros_loc(&grid);
let mut output = 0;
for zero in zeros {
let next = get_next_trails(&grid, NextTrail::Init(zero));
let next_completed = recurse_trails(&grid, next);
output += next_completed.len();
}
output
}
fn part_1_solution(s: &str) -> usize {
let grid = parse_input(s);
let zeros = get_zeros_loc(&grid);
let mut output = 0;
for zero in zeros {
let next = get_next_trails(&grid, NextTrail::Init(zero));
let next_completed = recurse_trails(&grid, next);
output += get_trails_score(&next_completed);
}
output
}
pub(crate) fn part_1(input: String) {
println!("Total trailhead scores: {}", part_1_solution(&input));
}
pub(crate) fn part_2(input: String) {
println!("Total trailhead scores: {}", part_2_solution(&input));
}
#[cfg(test)]
mod tests {
use crate::day_10::{parse_input, part_1_solution, part_2_solution};
const TEST_1: &str = "...0...
...1...
...2...
6543456
7.....7
8.....8
9.....9";
const TEST_2: &str = "..90..9
...1.98
...2..7
6543456
765.987
876....
987....";
const TEST_3: &str = "10..9..
2...8..
3...7..
4567654
...8..3
...9..2
.....01";
const TEST_4: &str = "89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732";
#[test]
fn test_part_1_1() {
let output = part_1_solution(TEST_1);
assert_eq!(output, 2);
}
#[test]
fn test_part_1_2() {
let output = part_1_solution(TEST_2);
assert_eq!(output, 4);
}
#[test]
fn test_part_1_3() {
let output = part_1_solution(TEST_3);
assert_eq!(output, 3);
}
#[test]
fn test_part_1_4() {
let output = part_1_solution(TEST_4);
assert_eq!(output, 36);
}
#[test]
fn test_part_2_4() {
let output = part_2_solution(TEST_4);
assert_eq!(output, 81);
}
}