-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha.rs
60 lines (51 loc) · 1.38 KB
/
a.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
// 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 grid = input
.as_bytes()
.split(|&b| b == b'\n')
.collect::<Vec<_>>();
let mut total = 0;
for (y, line) in grid.iter().enumerate() {
for (x, location) in line.iter().enumerate() {
if NEIGHBOURS.iter().all(|&(xx, yy)| {
grid
.get((y as isize + yy) as usize)
.and_then(|l| l.get((x as isize + xx) as usize))
.map(|n| location < n)
.unwrap_or(true)
}) {
total += (location - b'0') as usize + 1
}
}
}
return total
}
#[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), 15);
}
#[test]
fn test_puzzle() {
assert_eq!(run(), 480);
}
#[bench]
fn bench_run(b: &mut Bencher) {
b.iter(|| run())
}
}