-
Notifications
You must be signed in to change notification settings - Fork 3
/
day_16.rs
154 lines (131 loc) Β· 4.07 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
146
147
148
149
150
151
152
153
154
use std::collections::HashSet;
use aoc_lib::{direction::Direction, matrix::Matrix};
use common::{Answer, Solution};
use nd_vec::{vector, Vec2};
type Pos = Vec2<isize>;
pub struct Day16;
impl Solution for Day16 {
fn name(&self) -> &'static str {
"The Floor Will Be Lava"
}
fn part_a(&self, input: &str) -> Answer {
lazer(&parse(input), vector!(-1, 0), Direction::Right).into()
}
fn part_b(&self, 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) -> Matrix<Tile> {
Matrix::new_chars(input, Tile::from_char)
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum Tile {
Empty, // .
Horizontal, // -
Vertical, // |
SlantLeft, // \
SlantRight, // /
}
fn lazer(cavern: &Matrix<Tile>, start: Pos, direction: Direction) -> usize {
fn _lazer(cavern: &Matrix<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 common::Solution;
use indoc::indoc;
use super::Day16;
const CASE: &str = indoc! {r"
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....
"};
#[test]
fn part_a() {
assert_eq!(Day16.part_a(CASE), 46.into());
}
#[test]
fn part_b() {
assert_eq!(Day16.part_b(CASE), 51.into());
}
}