-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
143 lines (128 loc) · 3.38 KB
/
mod.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
136
137
138
139
140
141
142
143
use rayon::prelude::*;
use std::collections::HashSet;
#[derive(Debug, PartialEq, Hash, Eq, Clone)]
enum Direction {
U,
R,
D,
L,
}
#[derive(Debug, PartialEq, Hash, Eq, Clone)]
struct Step {
x: i32,
y: i32,
dir: Direction,
}
fn next(
matrix: &[Vec<u8>],
step: Step,
bounds: &(i32, i32),
distinct_fields: &mut Option<&mut HashSet<(i32, i32)>>,
loop_check_pos: (i32, i32),
cap: usize,
) -> u64 {
let mut step = step;
let mut cache = HashSet::with_capacity(cap);
loop {
if let Some(ref mut df) = distinct_fields {
// walk
df.insert((step.x, step.y));
} else {
// loop detection
if cache.contains(&step) {
// exit immediately when encountering a loop
return 1;
}
cache.insert(step.clone());
}
// next pos
let next_pos = match step.dir {
Direction::U => (step.x, step.y - 1),
Direction::R => (step.x + 1, step.y),
Direction::D => (step.x, step.y + 1),
Direction::L => (step.x - 1, step.y),
};
// bounds checks
if next_pos.0 < 0 || next_pos.1 < 0 || next_pos.0 > bounds.0 || next_pos.1 > bounds.1 {
return 0;
}
step = if matrix[next_pos.1 as usize][next_pos.0 as usize] == b'#'
|| next_pos == loop_check_pos
{
// we ran into roadblock, turn right, but do not yet walk
Step {
x: step.x,
y: step.y,
dir: match step.dir {
Direction::U => Direction::R,
Direction::R => Direction::D,
Direction::D => Direction::L,
Direction::L => Direction::U,
},
}
} else {
// step in the previous direction
Step {
x: next_pos.0,
y: next_pos.1,
dir: step.dir,
}
};
}
}
fn find_start(matrix: &[Vec<u8>], bounds: &(i32, i32)) -> Step {
for y in 0..bounds.1 + 1 {
for x in 0..bounds.0 + 1 {
if matrix[y as usize][x as usize] == b'^' {
return Step {
x,
y,
dir: Direction::U,
};
}
}
}
panic!("Start not found")
}
pub fn solve(input: String) -> Vec<u64> {
let (matrix, bounds) = aoc::parse_matrix(input);
let mut distinct_fields = HashSet::new();
let start = find_start(&matrix, &bounds);
next(
&matrix,
start.clone(),
&bounds,
&mut Some(&mut distinct_fields),
(-1, -1),
(bounds.0 * bounds.1) as usize,
);
let cap = distinct_fields.len();
let loop_cnt: u64 = distinct_fields
.clone()
.par_iter()
// Skip the start point
.filter(|(x, y)| start.x != *x || start.y != *y)
.map(|(x, y)| next(&matrix, start.clone(), &bounds, &mut None, (*x, *y), cap))
.sum();
vec![distinct_fields.len() as u64, loop_cnt]
}
#[test]
fn test() {
assert_eq!(
solve(
"....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#..."
.to_string()
),
[41, 6]
);
assert_eq!(solve(aoc::input(6)), [4602, 1703]);
}