-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
200 lines (182 loc) · 4.21 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
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
use pathfinding::prelude::yen;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
enum Direction {
U,
R,
D,
L,
}
#[derive(Debug, Clone, Copy)]
struct Step {
x: i32,
y: i32,
dir: Direction,
}
// we only want x and y to be considered in eq / hash
impl PartialEq for Step {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}
impl Eq for Step {}
impl Hash for Step {
fn hash<H: Hasher>(&self, state: &mut H) {
self.x.hash(state);
self.y.hash(state);
}
}
impl Step {
fn step(&self, dir: Direction) -> Step {
match dir {
Direction::L => Step {
x: self.x - 1,
y: self.y,
dir: Direction::L,
},
Direction::R => Step {
x: self.x + 1,
y: self.y,
dir: Direction::R,
},
Direction::U => Step {
x: self.x,
y: self.y - 1,
dir: Direction::U,
},
Direction::D => Step {
x: self.x,
y: self.y + 1,
dir: Direction::D,
},
}
}
}
fn add(v: &mut Vec<(Step, u32)>, m: &[Vec<u8>], b: (i32, i32), p: Step, val: u32) {
if p.x < 0 || p.y < 0 || p.x > b.0 || p.y > b.1 {
return;
}
if m[p.y as usize][p.x as usize] == b'#' {
// cannot walk into wall
return;
}
v.push((p, val));
}
fn options(v: &mut Vec<(Step, u32)>, m: &[Vec<u8>], b: (i32, i32), p: Step) {
add(v, m, b, p.step(p.dir), 1);
match p.dir {
Direction::L | Direction::R => {
add(v, m, b, p.step(Direction::U), 1001);
add(v, m, b, p.step(Direction::D), 1001);
}
Direction::U | Direction::D => {
add(v, m, b, p.step(Direction::L), 1001);
add(v, m, b, p.step(Direction::R), 1001);
}
}
}
pub fn solve(input: String) -> Vec<u64> {
let (matrix, bounds) = aoc::parse_matrix(input);
let mut start = Step {
x: 0,
y: 0,
dir: Direction::D,
};
let mut end = Step {
x: 0,
y: 0,
dir: Direction::D,
};
for y in 1..bounds.1 {
for x in 1..bounds.0 {
if matrix[y as usize][x as usize] == b'S' {
start = Step {
x,
y,
dir: Direction::R,
};
} else if matrix[y as usize][x as usize] == b'E' {
end = Step {
x,
y,
dir: Direction::D,
};
}
}
}
let shortest_paths = yen(
&start,
|&p| {
let mut v: Vec<(Step, u32)> = Vec::new();
options(&mut v, &matrix, bounds, p);
v
},
|&p| p == end,
// guess 10 paths, seems okay :shrug:
10,
);
let mut score = 0;
let mut tiles = HashSet::new();
for (path, path_score) in shortest_paths {
if score == 0 {
// first
score = path_score;
} else if score < path_score {
// no longer shortest
break;
}
for s in path {
tiles.insert(s);
}
}
vec![score as u64, tiles.len() as u64]
}
#[test]
fn test() {
assert_eq!(
solve(
"###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############"
.to_string()
),
vec![7036, 45]
);
assert_eq!(
solve(
"#################
#...#...#...#..E#
#.#.#.#.#.#.#.#.#
#.#.#.#...#...#.#
#.#.#.#.###.#.#.#
#...#.#.#.....#.#
#.#.#.#.#.#####.#
#.#...#.#.#.....#
#.#.#####.#.###.#
#.#.#.......#...#
#.#.###.#####.###
#.#.#...#.....#.#
#.#.#.#####.###.#
#.#.#.........#.#
#.#.#.#########.#
#S#.............#
#################"
.to_string()
),
vec![11048, 64]
);
assert_eq!(solve(aoc::input(16)), [108504, 538]);
}