-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.rs
273 lines (244 loc) · 7.83 KB
/
day10.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
use std::collections::HashMap;
// I really wanted to stick with usize, but oh well!
type Point = (i32, i32);
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Tile {
BendNE,
BendNW,
BendSE,
BendSW,
PipeEW,
PipeNS,
Start,
}
impl Tile {
pub fn contains_char(self, c: char) -> bool {
let name = match self {
Tile::BendNE => "NE",
Tile::BendNW => "NW",
Tile::BendSE => "SE",
Tile::BendSW => "SW",
Tile::PipeEW => "EW",
Tile::PipeNS => "NS",
Tile::Start => "",
};
name.contains(c)
}
}
#[derive(Debug)]
pub enum Direction {
North,
East,
South,
West,
Stop,
}
impl Direction {
pub fn from(self, loc: &Point) -> Point {
match self {
Direction::North => (loc.0, loc.1 - 1),
Direction::East => (loc.0 + 1, loc.1),
Direction::South => (loc.0, loc.1 + 1),
Direction::West => (loc.0 - 1, loc.1),
Direction::Stop => (loc.0, loc.1),
}
}
}
trait Area {
fn shoestring(&self) -> usize;
}
impl Area for Vec<Point> {
fn shoestring(&self) -> usize {
(self
.windows(2)
.fold(0, |acc, matrix|
acc + (matrix[0].0 * matrix[1].1) - (matrix[1].0 * matrix[0].1)
) / 2) as usize
}
}
trait Interior {
fn picks(&self, boundary: usize) -> usize;
}
impl Interior for usize {
fn picks(&self, boundary: usize) -> usize {
self + 1 - boundary/2
}
}
fn find_next(map: &HashMap<Point, Tile>, current: Point, last: Option<Direction>) -> Point {
let mut next = None;
if last.is_none() {
let check = &[
(current.0, current.1 - 1, 'S'),
(current.0 + 1, current.1, 'W'),
(current.0, current.1 + 1, 'N'),
(current.0 - 1, current.1, 'E'),
];
for &(col, row, dir) in check {
if let Some(tile) = map.get(&(col, row)) {
if tile.contains_char(dir) {
next = Some((col, row));
break;
}
}
}
} else {
next = match map.get(¤t).unwrap() {
Tile::BendNE => {
match last.unwrap() {
Direction::South => Some(Direction::East.from(¤t)),
Direction::West => Some(Direction::North.from(¤t)),
_ => panic!("Invalid move."),
}
},
Tile::BendNW => {
match last.unwrap() {
Direction::South => Some(Direction::West.from(¤t)),
Direction::East => Some(Direction::North.from(¤t)),
_ => panic!("Invalid move."),
}
},
Tile::BendSE => {
match last.unwrap() {
Direction::North => Some(Direction::East.from(¤t)),
Direction::West => Some(Direction::South.from(¤t)),
_ => panic!("Invalid move."),
}
},
Tile::BendSW => {
match last.unwrap() {
Direction::North => Some(Direction::West.from(¤t)),
Direction::East => Some(Direction::South.from(¤t)),
_ => panic!("Invalid move."),
}
},
Tile::PipeEW => {
match last.unwrap() {
Direction::East => Some(Direction::East.from(¤t)),
Direction::West => Some(Direction::West.from(¤t)),
_ => panic!("Invalid move."),
}
},
Tile::PipeNS => {
match last.unwrap() {
Direction::North => Some(Direction::North.from(¤t)),
Direction::South => Some(Direction::South.from(¤t)),
_ => panic!("Invalid move."),
}
},
Tile::Start => Some(Direction::Stop.from(¤t)),
};
}
next.unwrap()
}
#[aoc_generator(day10)]
pub fn input_generator(input: &str) -> (HashMap<Point, Tile>, Point) {
input
.lines()
.enumerate()
.fold((HashMap::new(), (0, 0)), |(mut map, mut start), (row, line)| {
line.trim()
.chars()
.enumerate()
.for_each(|(col, char)| {
let tile = match char {
'L' => Tile::BendNE,
'J' => Tile::BendNW,
'F' => Tile::BendSE,
'7' => Tile::BendSW,
'-' => Tile::PipeEW,
'|' => Tile::PipeNS,
'S' => {
start = (col as i32, row as i32);
Tile::Start
},
_ => return,
};
map.insert((col as i32, row as i32), tile);
});
(map, start)
})
}
#[aoc(day10, part1)]
pub fn solve_part1(input: &(HashMap<Point, Tile>, Point)) -> usize {
let mut last_move = None;
let start = input.1;
let mut current = start;
let mut visited: Vec<Point> = Vec::new();
loop {
let next = find_next(&input.0, current, last_move);
visited.push(next);
let delta = (next.0 - current.0, next.1 - current.1);
last_move = match delta {
(0, -1) => Some(Direction::North),
(1, 0) => Some(Direction::East),
(0, 1) => Some(Direction::South),
(-1, 0) => Some(Direction::West),
(0, 0) => break,
_ => panic!("Invalid move {:?}.", delta)
};
current = next;
}
visited.len() / 2
}
#[aoc(day10, part2)]
pub fn solve_part2(input: &(HashMap<Point, Tile>, Point)) -> usize {
let mut last_move = None;
let start = input.1;
let mut current = start;
// let mut vertices: Vec<Point> = vec![start];
// let mut visited: Vec<Point> = Vec::new();
let mut visited: Vec<Point> = vec![start];
loop {
let next = find_next(&input.0, current, last_move);
// Apparently, calculating the area using all +13k vertices is still faster than using an optimised
// vertex table of only the start point and bends.
// if let Some(&tile) = input.0.get(&next) {
// if !(tile == Tile::PipeEW || tile == Tile::PipeNS) {
// vertices.push(next);
// }
// }
visited.push(next);
let delta = (next.0 - current.0, next.1 - current.1);
last_move = match delta {
(0, -1) => Some(Direction::North),
(1, 0) => Some(Direction::East),
(0, 1) => Some(Direction::South),
(-1, 0) => Some(Direction::West),
(0, 0) => break,
_ => panic!("Invalid move {:?}.", delta)
};
current = next;
}
// vertices.shoestring().picks(visited.len())
visited.shoestring().picks(visited.len() - 1)
}
#[cfg(test)]
mod tests {
use super::*;
const TEST1: &str = ".....
.S-7.
.|.|.
.L-J.
.....";
const TEST2: &str = "..F7.
.FJ|.
SJ.L7
|F--J
LJ...";
#[test]
fn part1_test1() {
assert_eq!(solve_part1(&input_generator(TEST1)), 4);
}
#[test]
fn part1_test2() {
assert_eq!(solve_part1(&input_generator(TEST2)), 8);
}
#[test]
fn part2_test1() {
assert_eq!(solve_part2(&input_generator(TEST1)), 1);
}
#[test]
fn part2_test2() {
assert_eq!(solve_part2(&input_generator(TEST2)), 1);
}
}