-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.rs
135 lines (130 loc) · 4.12 KB
/
day9.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
pub fn part1<'a, I, S>(lines: I) -> u32
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let lines: Vec<Vec<char>> = lines
.into_iter()
.map(|line| line.as_ref().chars().collect())
.collect();
lines
.iter()
.enumerate()
.map(|(i, line)| -> u32 {
line.iter()
.enumerate()
.filter_map(|(j, c)| -> Option<u32> {
if [
j.checked_sub(1).and_then(|j| line.get(j)),
j.checked_add(1).and_then(|j| line.get(j)),
i.checked_sub(1)
.and_then(|i| lines.get(i))
.and_then(|line| line.get(j)),
i.checked_add(1)
.and_then(|i| lines.get(i))
.and_then(|line| line.get(j)),
]
.into_iter()
.all(|d| d.map_or(true, |d| c < d))
{
c.to_digit(10).map(|c| c + 1)
} else {
None
}
})
.sum()
})
.sum()
}
pub fn part2<'a, I, S>(lines: I) -> usize
where
I: IntoIterator<Item = &'a S>,
S: AsRef<str> + 'a,
{
let mut visited: Vec<Vec<bool>> = lines
.into_iter()
.map(|line| {
line.as_ref()
.chars()
.map(|c| c.to_digit(10).map_or(true, |d| d >= 9))
.collect()
})
.collect();
let mut basins = Vec::<usize>::new();
for i in 0..visited.len() {
for j in 0..visited[i].len() {
if visited[i][j] {
continue;
}
visited[i][j] = true;
let mut basin = 0;
let mut stack = vec![(i, j)];
while let Some((i, j)) = stack.pop() {
basin += 1;
if let Some((i, visited)) = i.checked_sub(1).and_then(|i| {
visited
.get_mut(i)
.and_then(|row| row.get_mut(j))
.filter(|visited| !**visited)
.map(|visited| (i, visited))
}) {
*visited = true;
stack.push((i, j));
}
if let Some((i, visited)) = i.checked_add(1).and_then(|i| {
visited
.get_mut(i)
.and_then(|row| row.get_mut(j))
.filter(|visited| !**visited)
.map(|visited| (i, visited))
}) {
*visited = true;
stack.push((i, j));
}
if let Some((j, visited)) = j.checked_sub(1).and_then(|j| {
visited
.get_mut(i)
.and_then(|row| row.get_mut(j))
.filter(|visited| !**visited)
.map(|visited| (j, visited))
}) {
*visited = true;
stack.push((i, j));
}
if let Some((j, visited)) = j.checked_add(1).and_then(|j| {
visited
.get_mut(i)
.and_then(|row| row.get_mut(j))
.filter(|visited| !**visited)
.map(|visited| (j, visited))
}) {
*visited = true;
stack.push((i, j));
}
}
basins.push(basin);
}
}
basins.sort_unstable();
basins[basins.len().saturating_sub(3)..].iter().product()
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
static EXAMPLE: &[&str] = &[
"2199943210",
"3987894921",
"9856789892",
"8767896789",
"9899965678",
];
#[test]
fn part1_examples() {
assert_eq!(15, part1(EXAMPLE));
}
#[test]
fn part2_examples() {
assert_eq!(1134, part2(EXAMPLE));
}
}