-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
112 lines (102 loc) · 2.64 KB
/
mod.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
use aoc_runner_derive::{aoc, aoc_generator};
#[allow(unused)]
use itertools::Itertools;
use pathfinding::{
matrix::Matrix,
prelude::{bfs_reach, yen},
};
type Num = usize;
type Output = Num;
type Input = Matrix<Num>;
#[aoc_generator(day10)]
pub fn input_generator(input: &str) -> Input {
Matrix::from_rows(
input
.lines()
.map(|line| line.chars().map(|n| n.to_digit(10).unwrap() as Num)),
)
.unwrap()
}
#[aoc(day10, part1)]
pub fn solve_part1(input: &Input) -> Output {
input
.iter()
.enumerate()
.flat_map(|(r, row)| {
row.iter()
.enumerate()
.filter(|(_, n)| **n == 0)
.map(move |(c, _)| (r, c))
.collect::<Vec<(usize, usize)>>()
})
.map(|trailhead| {
bfs_reach(trailhead, |cur| {
input
.neighbours(*cur, false)
.filter(|neigh| *input.get(*neigh).unwrap() == *input.get(*cur).unwrap() + 1)
.collect::<Vec<_>>()
})
.filter(|step| *input.get(*step).unwrap() == 9)
.count()
})
.sum()
}
#[aoc(day10, part2)]
pub fn solve_part2(input: &Input) -> Output {
input
.iter()
.enumerate()
.flat_map(|(r, row)| {
row.iter()
.enumerate()
.filter(|(_, n)| **n == 0)
.map(move |(c, _)| (r, c))
.collect::<Vec<(usize, usize)>>()
})
.map(|trailhead| {
yen(
&trailhead,
|cur| {
input
.neighbours(*cur, false)
.filter(|neigh| {
*input.get(*neigh).unwrap() == *input.get(*cur).unwrap() + 1
})
.map(|neigh| (neigh, 1))
.collect::<Vec<_>>()
},
|step| *input.get(*step).unwrap() == 9,
1000,
)
.len()
})
.sum()
}
pub fn part1(input: &str) -> impl std::fmt::Display {
solve_part1(&input_generator(input))
}
pub fn part2(input: &str) -> impl std::fmt::Display {
solve_part2(&input_generator(input))
}
#[cfg(test)]
mod tests {
use super::*;
fn sample() -> &'static str {
"89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732"
}
#[test]
fn samples_part1() {
assert_eq!(36, solve_part1(&input_generator(sample())));
}
#[test]
fn samples_part2() {
assert_eq!(81, solve_part2(&input_generator(sample())));
}
}