-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathb.rs
79 lines (67 loc) · 1.86 KB
/
b.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
// Rust vectors return an Option when retrieving an element, unlike Go that will panic
// if accessing an out of bounds element. No need to perform if based lookups
const NEIGHBOURS: [(isize, isize); 4] = [(0, -1), (0, 1), (-1, 0), (1, 0)];
pub fn run() -> usize {
let input = include_str!("./input.txt");
return solve(input)
}
fn solve(input: &str) -> usize {
let mut grid = input
.as_bytes()
.split(|&b| b == b'\n')
.map(|l| l.to_vec())
.collect::<Vec<_>>();
let mut basins = vec![];
for y in 0..grid.len() {
for x in 0..grid[0].len() {
if grid[y][x] < b'9' {
basins.push(search_basin(&mut grid, x, y));
}
}
}
// Sort the slice and take the largest 3 values
basins.sort_unstable();
return basins.iter()
.rev()
.take(3)
.product::<usize>()
}
fn search_basin(grid: &mut Vec<Vec<u8>>, x: usize, y: usize) -> usize {
// Recursively search all neighbours, marking each visited point with a 9
// to ensure it is not traversed again
grid[y][x] = b'9';
NEIGHBOURS.iter()
.map(|(xx, yy)| ((x as isize + xx) as usize, (y as isize + yy) as usize))
.fold(1, |acc, (x, y)| {
match grid
.get(y)
.and_then(|l| l.get(x))
.map(|&n| n < b'9') {
Some(true) => acc + search_basin(grid, x, y),
_ => acc,
}
})
}
#[cfg(test)]
mod tests {
extern crate test;
use test::Bencher;
use super::*;
#[test]
fn test_example() {
let input = r"2199943210
3987894921
9856789892
8767896789
9899965678";
assert_eq!(solve(input), 1134);
}
#[test]
fn test_puzzle() {
assert_eq!(run(), 1045660);
}
#[bench]
fn bench_run(b: &mut Bencher) {
b.iter(|| run())
}
}