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
196
197
198
199
200
201
202
203
204
|
// This code is more complicated than it should be. The reason is that
// for part 1 I decided to ignore the start and finish position, and not
// to treat them as other cells. This way I could get away with not
// considering wall cells '#' at all.
// But then for part 2 it was more convenient to include them, so I
// added an enum Position {} that treats start and finish as special
// cases. This is quite messy.
use std::collections::{VecDeque, HashSet};
#[derive(PartialEq, Debug)]
pub enum Direction { Up, Down, Left, Right, None }
impl Direction {
pub fn from_byte(b: u8) -> Direction {
match b {
b'^' => Direction::Up,
b'v' => Direction::Down,
b'>' => Direction::Right,
b'<' => Direction::Left,
b'.' => Direction::None,
x => panic!("unexpected char '{}'", x as char)
}
}
}
#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug)]
pub enum Position {
Start,
Finish,
Other { i: usize, j: usize }
}
impl Position {
pub fn dist(&self, other: Position) -> usize {
match (*self, other) {
(Position::Start, Position::Start) |
(Position::Finish, Position::Finish) =>
0,
(Position::Start, Position::Finish) |
(Position::Finish, Position::Start) =>
usize::MAX, // Not technically correct, but it works
(Position::Start | Position::Finish, Position::Other { i, j }) |
(Position::Other { i, j }, Position::Start | Position::Finish) =>
i + j,
(Position::Other { i: i1, j: j1 },
Position::Other { i: i2, j: j2 }) => {
let i = (i1 as i64 - i2 as i64).abs() as usize;
let j = (j1 as i64 - j2 as i64).abs() as usize;
i + j
}
}
}
}
pub struct Map {
pub tiles: Vec<Vec<Direction>>,
pub imax: usize,
pub jmax: usize
}
#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug)]
struct Node {
p: Position,
t: usize
}
impl Map {
fn submod(i: usize, t: usize, m: usize) -> usize {
let maybeneg = ((i as i64) - (t as i64)) % m as i64;
(maybeneg as usize + m) % m
}
pub fn is_busy(&self, p: Position, t: usize) -> bool {
match p {
Position::Start | Position::Finish => false,
Position::Other { i, j } => {
let iup = Self::submod(i, t, self.imax);
let idown = (i + t) % self.imax;
let jleft = Map::submod(j, t, self.jmax);
let jright = (j + t) % self.jmax;
self.tiles[iup][j] == Direction::Down ||
self.tiles[idown][j] == Direction::Up ||
self.tiles[i][jleft] == Direction::Right ||
self.tiles[i][jright] == Direction::Left
}
}
}
#[allow(dead_code)]
pub fn print(&self, t: usize) {
print!("#.");
for _ in 0..self.jmax { print!("#"); }
println!();
for i in 0..self.imax {
print!("#");
for j in 0..self.jmax {
let p = Position::Other { i, j };
print!("{}", if self.is_busy(p, t) { '*' } else { '.' });
}
println!("#");
}
for _ in 0..self.jmax { print!("#"); }
print!(".#");
println!();
}
fn mv(&self, p: Position, d: Direction) -> Option<Position> {
if d == Direction::None { return Some(p); }
match p {
Position::Start => if d == Direction::Down {
Some(Position::Other { i: 0, j: 0 })
} else { None },
Position::Finish => if d == Direction::Up {
Some(Position::Other { i: self.imax-1, j: self.jmax-1 })
} else { None },
Position::Other { i, j } => match d {
Direction::Up => if i > 0 {
Some(Position::Other { i: i-1, j })
} else {
if i == 0 && j == 0 {
Some(Position::Start)
} else { None }
},
Direction::Down => if i < self.imax-1 {
Some(Position::Other { i: i+1, j })
} else {
if i == self.imax-1 && j == self.jmax-1 {
Some(Position::Finish)
} else { None }
},
Direction::Left => if j > 0 {
Some(Position::Other { i, j: j-1 })
} else { None },
Direction::Right => if j < self.jmax-1 {
Some(Position::Other { i, j: j+1 })
} else { None },
Direction::None => panic!("stupid rust")
}
}
}
fn maybe_push(&self, x: Node, d: Direction, v: &mut HashSet<Node>) {
if let Some(p) = self.mv(x.p, d) {
if !self.is_busy(p, x.t+1) {
v.insert(Node { p, t: x.t+1 });
}
}
}
pub fn shortest_path(
&self,
start: Position,
finish: Position,
t0: usize
) -> usize {
let mut vnext = HashSet::<Node>::new();
vnext.insert(Node { p: start, t: t0 });
while !vnext.is_empty() {
let mut v = vnext.clone().into_iter().collect::<Vec<_>>();
// Heuristic: explore first the positions closer to the target
v.sort_by(|a, b| a.p.dist(finish).cmp(&(b.p.dist(finish))));
let mut v = VecDeque::<Node>::from(v);
vnext.clear();
while !v.is_empty() {
let x = v.pop_front().unwrap();
if x.p == finish { return x.t; }
self.maybe_push(x, Direction::Up, &mut vnext);
self.maybe_push(x, Direction::Down, &mut vnext);
self.maybe_push(x, Direction::Left, &mut vnext);
self.maybe_push(x, Direction::Right, &mut vnext);
if !self.is_busy(x.p, x.t+1) {
self.maybe_push(x, Direction::None, &mut vnext);
}
}
}
panic!("queue is empty");
}
}
pub fn read_map_from_stdin() -> Map {
let mut tiles = vec![];
let mut line = String::new();
while std::io::stdin().read_line(&mut line).unwrap() > 0 {
let bytes = line.as_bytes();
if bytes[2] == b'#' {
line.clear();
continue;
}
let i = tiles.len();
tiles.push(vec![]);
for j in 1..bytes.len()-2 {
tiles[i].push(Direction::from_byte(bytes[j]));
}
line.clear();
}
let imax = tiles.len();
let jmax = tiles[0].len();
Map { tiles, imax, jmax }
}
|