-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.rs
30 lines (26 loc) · 909 Bytes
/
main.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
const NEXT: [(isize, isize); 4] = [(0, -1), (0, 1), (-1, 0), (1, 0)];
pub fn main() {
let mut map = include_bytes!("../input.txt")
.split(|&b| b == b'\n')
.map(|l| l.to_vec())
.collect::<Vec<_>>();
let mut basins = vec![];
for y in 0..map.len() {
for x in 0..map[0].len() {
(map[y][x] < b'9').then(|| basins.push(basin(&mut map, x, y)));
}
}
basins.sort_unstable();
println!("{}", basins.iter().rev().take(3).product::<usize>());
}
fn basin(map: &mut Vec<Vec<u8>>, x: usize, y: usize) -> usize {
map[y][x] = b'9';
NEXT.iter()
.map(|(xx, yy)| ((x as isize + xx) as usize, (y as isize + yy) as usize))
.fold(1, |acc, (x, y)| {
match map.get(y).and_then(|l| l.get(x)).map(|&n| n < b'9') {
Some(true) => acc + basin(map, x, y),
_ => acc,
}
})
}