generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.rs
70 lines (53 loc) · 1.69 KB
/
10.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
advent_of_code::solution!(10);
use advent_of_code::majcn::grid::*;
use advent_of_code::maneatingape::grid::*;
use advent_of_code::maneatingape::hash::*;
use advent_of_code::maneatingape::point::*;
fn parse_data(input: &str) -> Grid<u8> {
Grid::parse(input)
}
fn part_x(grid: Grid<u8>) -> FastMap<(Point, Point), u32> {
let mut result = FastMap::new();
let mut paths = vec![];
for start_position in grid.points().filter(|&p| grid[p] == b'0') {
paths.push((b'0', start_position));
while let Some((height, location)) = paths.pop() {
if height == b'9' {
*result.entry((start_position, location)).or_insert(0) += 1;
continue;
}
for next_location in ORTHOGONAL.map(|o| location + o) {
if grid.contains(next_location) && grid[next_location] == height + 1 {
paths.push((height + 1, next_location));
}
}
}
}
result
}
pub fn part_one(input: &str) -> Option<u32> {
let grid = parse_data(input);
let result = part_x(grid).len() as u32;
Some(result)
}
pub fn part_two(input: &str) -> Option<u32> {
let grid = parse_data(input);
let result = part_x(grid).values().sum();
Some(result)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let input = advent_of_code::template::read_file("examples", DAY);
let result = part_one(&input);
assert_eq!(result, Some(36));
}
#[test]
fn test_part_two() {
let input = advent_of_code::template::read_file("examples", DAY);
let result = part_two(&input);
assert_eq!(result, Some(81));
}
}