-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-16.rs
129 lines (114 loc) · 3.45 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
use aoc_2023::{aoc, str_block, Dir, Error};
const INPUT: &str = include_str!("day-16.txt");
#[allow(dead_code)]
const INPUT_EX: &str = str_block! {r"
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....
"};
aoc! {
struct Day16 {
map: Vec<Vec<u8>>,
}
self(input = INPUT) {
Ok(Self { map: input.lines().map(|line| line.as_bytes().to_owned()).collect() })
}
1 part1 usize {
Ok(self.trace_beam(0, 0, Dir::E)?)
}
2 part2 usize {
let mut max = 0;
for y in 0..self.height() {
for x in 0..self.width() {
if x == 0 {
max = max.max(self.trace_beam(x, y, Dir::E)?);
}
if x == self.width() - 1 {
max = max.max(self.trace_beam(x, y, Dir::W)?);
}
if y == 0 {
max = max.max(self.trace_beam(x, y, Dir::S)?);
}
if y == self.height() - 1 {
max = max.max(self.trace_beam(x, y, Dir::N)?);
}
}
}
Ok(max)
}
INPUT_EX { 1 part1 = 46, 2 part2 = 51 }
INPUT { 1 part1 = 7543, 2 part2 = 8231 }
}
impl Day16 {
fn width(&self) -> usize {
self.map[0].len()
}
fn height(&self) -> usize {
self.map.len()
}
fn trace_beam(&self, x: usize, y: usize, dir: Dir) -> Result<usize, Error> {
let mut bmap = self.map.clone();
for row in bmap.iter_mut() {
for c in row.iter_mut() {
*c = 0;
}
}
let mut beams = vec![((x, y), dir)];
let mut energized = 0;
while let Some(((x, y), dir)) = beams.pop() {
if bmap[y][x] & dir.bits() == 0 {
if bmap[y][x] == 0 {
energized += 1;
}
bmap[y][x] |= dir.bits();
let mut push = |dir: Dir| {
if let Some((x, y)) = dir.mov(x, y, self.width(), self.height()) {
beams.push(((x, y), dir));
}
};
match self.map[y][x] {
b'.' => push(dir),
b'/' => push(match dir {
Dir::N => Dir::E,
Dir::E => Dir::N,
Dir::S => Dir::W,
Dir::W => Dir::S,
_ => unreachable!(),
}),
b'\\' => push(match dir {
Dir::N => Dir::W,
Dir::E => Dir::S,
Dir::S => Dir::E,
Dir::W => Dir::N,
_ => unreachable!(),
}),
b'|' => {
if matches!(dir, Dir::E | Dir::W) {
push(Dir::N);
push(Dir::S);
} else {
push(dir);
}
}
b'-' => {
if matches!(dir, Dir::N | Dir::S) {
push(Dir::E);
push(Dir::W);
} else {
push(dir);
}
}
_ => panic!("unknown tile"),
}
}
}
Ok(energized)
}
}