-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
213 lines (194 loc) · 7.26 KB
/
lib.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
201
202
203
204
205
206
207
208
209
210
211
212
use std::fs;
use std::rc::Rc;
use rustc_hash::FxHashSet;
use pathfinding::matrix::Matrix;
pub fn part1(input_file: &str) -> usize {
let maze = Rc::new(parse_input(input_file));
let start = Pos::new(1,0,Rc::clone(&maze));
let end = Pos::new((maze.columns as isize) - 2, (maze.rows as isize) - 1, Rc::clone(&maze));
find_longest_path(start, end, true)
}
// Brute force works eventually so why not...
//⠀⠀⠀⠀⢀⣤⡀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⣿⠉⢻⠟⢹⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⢀⣿⡄⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣤⣄⣠⣤⣄⠀⠀⠀⠀
//⠀⠀⣰⡿⠋⠀⣀⣀⠈⣿⠂⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣇⠘⠋⠀⣿⠇⠀⠀⠀
//⠀⣠⡟⠀⢀⣾⠟⠻⠿⠿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣀⣀⣀⡀⠀⠀⣾⠋⢀⣀⠈⠻⢶⣄⠀⠀
//⢠⣿⠁⣰⡿⠁⠀⣀⣤⣶⣶⡶⢶⣤⣄⡀⢀⣠⠴⠚⠉⠉⠉⠉⠉⠙⢶⡄⠛⠒⠛⠙⢳⣦⡀⠹⣆⠀
//⢸⡇⢠⣿⣠⣴⣿⡟⢉⣠⠤⠶⠶⠾⠯⣿⣿⣧⣀⣤⣶⣾⣿⡿⠿⠛⠋⢙⣛⡛⠳⣄⡀⠙⣷⡀⢹⡆
//⢸⠀⢸⣿⣿⣿⣿⠞⠉⠀⠀⠀⠀⣀⣤⣤⠬⠉⠛⠻⠿⠟⠉⢀⣠⢞⣭⣤⣤⣍⠙⠺⢷⡀⢸⡇⠀⣿
//⢸⠀⢸⣿⣿⡟⠀⠀⠀⢀⣠⠞⣫⢗⣫⢽⣶⣤⣀⠉⠛⣶⠖⠛⠀⣾⡷⣾⠋⣻⡆⠀⠀⡇⣼⠇⠀⣿
//⢸⠀⠀⣿⣿⡇⢠⡤⠔⣋⡤⠞⠁⢸⣷⣾⣯⣹⣿⡆⢀⣏⠀⠈⠈⣿⣷⣼⣿⠿⠷⣴⡞⠀⣿⠀⠀⣿
//⢸⠀⠀⢿⣿⡇⠀⠀⠘⠻⠤⣀⡀⠸⣿⣯⣿⣿⡿⠷⠚⠉⠛⠛⠛⠛⠉⠉⠀⣠⡾⠛⣦⢸⡏⠀⠀⣿
//⢸⠀⠀⢸⣿⡇⠀⣠⠶⠶⠶⠶⠿⣿⣭⣭⣁⣀⣠⣤⣤⣤⣤⣤⣤⡶⠶⠛⠋⢁⣀⣴⠟⣽⠇⠀⠀⣿
//⢸⠀⠀⢸⣿⡇⢾⣅⠀⠀⠶⠶⢦⣤⣤⣀⣉⣉⣉⣉⣁⣡⣤⣤⣴⡶⠶⠶⠚⠉⢉⡿⣠⠟⠀⠀⣰⡟
//⢸⡀⠀⠀⢿⣇⠀⠈⠛⠳⠶⠤⠤⢤⣀⣉⣉⣉⣉⣉⣉⣁⣀⣠⣤⡤⠤⠤⠶⠞⢻⡟⠃⠀⠀⣰⠟⠀
//⢸⣧⠀⠀⠘⣿⣦⣄⡀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠉⠉⠉⠁⠀⠀⠀⠀⠀⣠⣤⣶⣿⣧⣀⣴⠟⠃⠀⠀
// ⢻⣆⠀⠀⠈⢻⣿⣿⣷⣶⣤⣄⣀⣀⣀⣠⣤⣶⣶⣶⣶⣶⣶⣶⣿⣿⣿⣿⣿⣿⣟⡉⠀⠀⠀⠀⠀
// ⠀⢻⣦⡄⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⠀⠀⠀⠀
// ⠀⢀⣿⣿⣿⣶⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡧⠀
pub fn part2(input_file: &str) -> usize {
let maze = Rc::new(parse_input(input_file));
let start = Pos::new(1,0,Rc::clone(&maze));
let end = Pos::new((maze.columns as isize) - 2, (maze.rows as isize) - 1, Rc::clone(&maze));
find_longest_path(start, end, false)
}
fn dfs(curr: &Pos, hist: &mut FxHashSet<(isize,isize)>, cost: usize, end: &Pos, costs: &mut Vec<usize>, slopes: bool) {
if curr == end {
costs.push(cost);
return;
} else if hist.contains(&curr.as_coords()) {
return;
}
hist.insert(curr.as_coords());
for next in curr.successors(slopes) {
if !hist.contains(&next.as_coords()) {
dfs(&next, hist, cost+1, end, costs, slopes);
}
}
hist.remove(&curr.as_coords());
}
fn find_longest_path(start: Pos, end: Pos, slopes: bool) -> usize {
let mut costs = Vec::new();
let mut hist = FxHashSet::default();
dfs(&start, &mut hist, 0, &end, &mut costs, slopes);
*costs.iter().max().unwrap()
}
#[derive(Copy,Clone,Debug,Eq,Hash,PartialEq)]
enum Piece {
Path,
Rock,
North,
South,
East,
West,
}
#[derive(Clone, Hash)]
struct Pos{
x: isize,
y: isize,
m: Rc<Matrix<Piece>>,
}
impl Pos {
fn new(x: isize, y: isize, m: Rc<Matrix<Piece>>) -> Self {
Self { x, y, m }
}
fn as_coords(&self) -> (isize,isize) {
(self.x,self.y)
}
fn successors(&self, slopes: bool) -> Vec<Pos> {
let mut next: Vec<Pos> = vec![];
match self.north(slopes) {
Some(x) => next.push(x),
_ => (),
}
match self.south(slopes) {
Some(x) => next.push(x),
_ => (),
}
match self.east(slopes) {
Some(x) => next.push(x),
_ => (),
}
match self.west(slopes) {
Some(x) => next.push(x),
_ => (),
}
next
}
fn north(&self, slopes: bool) -> Option<Self> {
match self.m.get(((self.y - 1) as usize, self.x as usize)) {
Some(Piece::Path) => (),
Some(Piece::North) => (),
Some(x) => {
match x {
Piece::Rock => return None,
_ => if slopes{ return None; },
}
}
_ => return None,
}
Some(Self::new(self.x,self.y-1,Rc::clone(&self.m)))
}
fn south(&self, slopes: bool) -> Option<Self> {
match self.m.get(((self.y + 1) as usize, self.x as usize)) {
Some(Piece::Path) => (),
Some(Piece::South) => (),
Some(x) => {
match x {
Piece::Rock => return None,
_ => if slopes{ return None; },
}
}
_ => return None,
}
Some(Self::new(self.x,self.y+1,Rc::clone(&self.m)))
}
fn east(&self, slopes: bool) -> Option<Self> {
match self.m.get((self.y as usize, (self.x + 1) as usize)) {
Some(Piece::Path) => (),
Some(Piece::East) => (),
Some(x) => {
match x {
Piece::Rock => return None,
_ => if slopes{ return None; },
}
}
_ => return None,
}
Some(Self::new(self.x+1,self.y,Rc::clone(&self.m)))
}
fn west(&self, slopes: bool) -> Option<Self> {
match self.m.get((self.y as usize, (self.x - 1) as usize)) {
Some(Piece::Path) => (),
Some(Piece::West) => (),
Some(x) => {
match x {
Piece::Rock => return None,
_ => if slopes{ return None; },
}
}
_ => return None,
}
Some(Self::new(self.x-1,self.y,Rc::clone(&self.m)))
}
}
impl PartialEq for Pos {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}
impl Eq for Pos {}
fn parse_input(input_file: &str) -> Matrix<Piece> {
let input = fs::read_to_string(input_file)
.expect("Unable to open input data");
let input = input.trim();
Matrix::from_rows(
input.lines()
.map(|l|{
l.chars().map(|c|{
match c {
'.' => Piece::Path,
'#' => Piece::Rock,
'^' => Piece::North,
'v' => Piece::South,
'>' => Piece::East,
'<' => Piece::West,
_ => unreachable!(),
}
})
})
).unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test1() {
assert_eq!(part1("data/test.txt"), 94);
}
#[test]
fn test2() {
assert_eq!(part2("data/test.txt"), 154);
}
}