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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::line_ending,
combinator::{map, value},
multi::{many1, separated_list1},
sequence::{delimited, tuple},
IResult,
};
use std::{collections::BTreeSet, fs};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let input = fs::read_to_string("inputs/day_24.txt")?;
let blizzard_map = BlizzardMap::parser(&input).unwrap().1;
let start = Point { x: 0, y: 0 };
let end = Point {
x: blizzard_map.width - 1,
y: blizzard_map.height - 1,
};
let there_the_first_time =
dbg!(blizzard_map.find_shortest_path_through(start.clone(), end.clone(), 1));
let back_again = dbg!(blizzard_map.find_shortest_path_through(
end.clone(),
start.clone(),
there_the_first_time + 1
));
dbg!(blizzard_map.find_shortest_path_through(start, end, back_again + 1));
Ok(())
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct BlizzardMap {
width: usize,
height: usize,
blizzards: Vec<Blizzard>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
struct Blizzard {
start: Point,
direction: Direction,
}
#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Point {
y: usize,
x: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum Direction {
North,
South,
West,
East,
}
impl BlizzardMap {
fn parser(input: &str) -> IResult<&str, Self> {
map(
delimited(
tuple((many1(tag("#")), tag("."), many1(tag("#")), line_ending)),
separated_list1(
line_ending,
delimited(tag("#"), many1(Direction::parser), tag("#")),
),
tuple((line_ending, many1(tag("#")), tag("."), many1(tag("#")))),
),
|blizzard_directions| BlizzardMap {
width: blizzard_directions[0].len(),
height: blizzard_directions.len(),
blizzards: blizzard_directions
.into_iter()
.enumerate()
.flat_map(|(y, row)| {
row.into_iter()
.enumerate()
.filter_map(move |(x, direction)| {
direction.map(|direction| Blizzard {
start: Point { x, y },
direction,
})
})
})
.collect(),
},
)(input)
}
fn find_shortest_path_through(
&self,
start: Point,
end: Point,
mut current_round: usize,
) -> usize {
// Not sure if "visited" makes sense here because of the time
// delay. Same position at different times is different. Maybe
// makes sense if I keep time in too.
let mut frontier = BTreeSet::new();
frontier.insert(start);
while !frontier.contains(&end) {
current_round += 1;
let last_frontier = std::mem::take(&mut frontier);
let blizzards = self.blizzards_at_time(current_round);
for frontier_state in last_frontier {
let mut possible_moves = vec![frontier_state.clone()];
if frontier_state.x > 0 {
possible_moves.push(Point {
x: frontier_state.x - 1,
y: frontier_state.y,
});
}
if frontier_state.x < self.width - 1 {
possible_moves.push(Point {
x: frontier_state.x + 1,
y: frontier_state.y,
});
}
if frontier_state.y > 0 {
possible_moves.push(Point {
x: frontier_state.x,
y: frontier_state.y - 1,
});
}
if frontier_state.y < self.height - 1 {
possible_moves.push(Point {
x: frontier_state.x,
y: frontier_state.y + 1,
});
}
for next in possible_moves {
if !blizzards.contains(&next) {
frontier.insert(next);
}
}
}
}
current_round + 1 // extra minute to leave
}
fn blizzards_at_time(&self, t: usize) -> BTreeSet<Point> {
self.blizzards
.iter()
.map(|blizzard| match blizzard.direction {
Direction::North => {
let t = t % self.height;
Point {
y: if blizzard.start.y > t {
blizzard.start.y - t
} else {
blizzard.start.y + self.height - t
},
..blizzard.start
}
}
Direction::South => Point {
y: (blizzard.start.y + t) % self.height,
..blizzard.start
},
Direction::West => {
let t = t % self.width;
Point {
x: if blizzard.start.x > t {
blizzard.start.x - t
} else {
blizzard.start.x + self.width - t
},
..blizzard.start
}
}
Direction::East => Point {
x: (blizzard.start.x + t) % self.width,
..blizzard.start
},
})
.collect()
}
}
impl Direction {
fn parser(input: &str) -> IResult<&str, Option<Self>> {
alt((
value(None, tag(".")),
value(Some(Direction::South), tag("v")),
value(Some(Direction::West), tag("<")),
value(Some(Direction::East), tag(">")),
value(Some(Direction::North), tag("^")),
))(input)
}
}
|