-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_16.rs
145 lines (124 loc) Β· 3.86 KB
/
day_16.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
use std::collections::HashSet;
use aoc_lib::{direction::cardinal::Direction, matrix::Grid};
use common::{solution, Answer};
use nd_vec::{vector, Vec2};
solution!("The Floor Will Be Lava", 16);
type Pos = Vec2<isize>;
fn part_a(input: &str) -> Answer {
lazer(&parse(input), vector!(-1, 0), Direction::Right).into()
}
fn part_b(input: &str) -> Answer {
let tiles = parse(input);
let mut max = 0;
let size = tiles.size.num_cast::<isize>().unwrap();
for y in 0..size.y() {
max = max.max(lazer(&tiles, vector!(-1, y), Direction::Right));
max = max.max(lazer(&tiles, vector!(size.x(), y), Direction::Left));
}
for x in 0..size.x() {
max = max.max(lazer(&tiles, vector!(x, -1), Direction::Down));
max = max.max(lazer(&tiles, vector!(x, size.y()), Direction::Up));
}
max.into()
}
fn parse(input: &str) -> Grid<Tile> {
Grid::parse(input, Tile::from_char)
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum Tile {
Empty, // .
Horizontal, // -
Vertical, // |
SlantLeft, // \
SlantRight, // /
}
fn lazer(cavern: &Grid<Tile>, start: Pos, direction: Direction) -> usize {
fn _lazer(cavern: &Grid<Tile>, visited: &mut HashSet<Pos>, mut pos: Pos, mut dir: Direction) {
loop {
pos = dir.advance(pos);
if !cavern.contains(pos) {
break;
}
let new = visited.insert(pos);
let tile = cavern[pos];
if tile == Tile::Empty || tile.matching_dir(dir) {
continue;
}
match tile {
Tile::SlantLeft => {
dir = match dir {
Direction::Up => Direction::Left,
Direction::Down => Direction::Right,
Direction::Left => Direction::Up,
Direction::Right => Direction::Down,
}
}
Tile::SlantRight => {
dir = match dir {
Direction::Up => Direction::Right,
Direction::Down => Direction::Left,
Direction::Left => Direction::Down,
Direction::Right => Direction::Up,
}
}
Tile::Horizontal if new => {
_lazer(cavern, visited, pos, Direction::Left);
_lazer(cavern, visited, pos, Direction::Right);
break;
}
Tile::Vertical if new => {
_lazer(cavern, visited, pos, Direction::Up);
_lazer(cavern, visited, pos, Direction::Down);
break;
}
_ => break,
};
}
}
let mut visited = HashSet::new();
_lazer(cavern, &mut visited, start, direction);
visited.len()
}
impl Tile {
fn from_char(c: char) -> Self {
match c {
'.' => Self::Empty,
'-' => Self::Horizontal,
'|' => Self::Vertical,
'/' => Self::SlantRight,
'\\' => Self::SlantLeft,
_ => panic!("Invalid tile: {}", c),
}
}
fn matching_dir(&self, direction: Direction) -> bool {
matches!(
(self, direction),
(Self::Horizontal, Direction::Left | Direction::Right)
| (Self::Vertical, Direction::Up | Direction::Down)
)
}
}
#[cfg(test)]
mod test {
use indoc::indoc;
const CASE: &str = indoc! {r"
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....
"};
#[test]
fn part_a() {
assert_eq!(super::part_a(CASE), 46.into());
}
#[test]
fn part_b() {
assert_eq!(super::part_b(CASE), 51.into());
}
}