-
Notifications
You must be signed in to change notification settings - Fork 0
/
day6.rs
147 lines (134 loc) · 3.79 KB
/
day6.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
144
145
146
147
use if_chain::if_chain;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use std::collections::BTreeSet;
use std::str::FromStr;
#[derive(Clone, Debug)]
struct Data {
initial_pos: (usize, usize),
max_bounds: (usize, usize),
obstacles: BTreeSet<(usize, usize)>,
}
impl FromStr for Data {
type Err = ();
fn from_str(value: &str) -> Result<Self, Self::Err> {
let mut initial_pos = None;
let mut max_bounds = (0, 0);
let mut obstacles = BTreeSet::new();
for (y, line) in value.lines().enumerate() {
max_bounds = (y + 1, max_bounds.1.max(line.len()));
for (x, b) in line.bytes().enumerate() {
match b {
b'^' => match initial_pos {
None => initial_pos = Some((y, x)),
Some(_) => return Err(()),
},
b'#' => {
obstacles.insert((y, x));
}
_ => {}
}
}
}
Ok(Data {
initial_pos: initial_pos.ok_or(())?,
max_bounds,
obstacles,
})
}
}
#[derive(Clone)]
struct Visitor<'a> {
state: Option<((usize, usize), (isize, isize))>,
max_bounds: (usize, usize),
obstacles: &'a BTreeSet<(usize, usize)>,
}
impl Iterator for Visitor<'_> {
type Item = ((usize, usize), (isize, isize));
fn next(&mut self) -> Option<Self::Item> {
match &mut self.state {
Some((pos, dir)) => {
let state = (*pos, *dir);
if_chain! {
if let Some(y) = pos.0.checked_add_signed(dir.0);
if y < self.max_bounds.0;
if let Some(x) = pos.1.checked_add_signed(dir.1);
if x < self.max_bounds.1;
then {
if self.obstacles.contains(&(y, x)) {
*dir = (dir.1, -dir.0);
} else {
*pos = (y, x);
}
}
else {
self.state = None;
}
}
Some(state)
}
None => None,
}
}
}
impl Data {
fn iter(&self) -> Visitor {
Visitor {
state: Some((self.initial_pos, (-1, 0))),
max_bounds: self.max_bounds,
obstacles: &self.obstacles,
}
}
}
pub fn part1(data: &str) -> Option<usize> {
Some(
data.parse::<Data>()
.ok()?
.iter()
.map(|(pos, _)| pos)
.collect::<BTreeSet<_>>()
.len(),
)
}
pub fn part2(data: &str) -> Option<usize> {
let data = data.parse::<Data>().ok()?;
let mut candidates = data.iter().map(|(pos, _)| pos).collect::<BTreeSet<_>>();
candidates.remove(&data.initial_pos);
Some(
candidates
.into_par_iter()
.filter(|candidate| {
let mut data = data.clone();
assert!(data.obstacles.insert(*candidate));
let mut visited = BTreeSet::new();
let ok = data.iter().any(|state| !visited.insert(state));
ok
})
.count(),
)
}
#[cfg(test)]
mod tests {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
static EXAMPLE: &str = indoc! {"
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
"};
#[test]
fn part1_examples() {
assert_eq!(Some(41), part1(EXAMPLE));
}
#[test]
fn part2_examples() {
assert_eq!(Some(6), part2(EXAMPLE));
}
}