This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
16.rs
173 lines (149 loc) · 5.23 KB
/
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::collections::HashSet;
use advent_of_code::helper::{
array_2d::Array2D,
carte::{Direction, Pos},
};
advent_of_code::solution!(16);
/* -------------------------------------------------------------------------- */
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Beam {
position: Pos,
direction: Direction,
}
fn parse_input(input: &str) -> Array2D<u8> {
Array2D::from_iter(input.lines().map(|line| line.bytes()))
}
fn process(grid: &Array2D<u8>, start: Beam) -> usize {
let mut beams = vec![start];
let mut energized = HashSet::new();
energized.insert(start);
while !beams.is_empty() {
beams = beams
.drain(..)
.flat_map(
|Beam {
position,
direction,
}| {
let new_beam = |direction| {
position.move_to(direction).map(|position| Beam {
position,
direction,
})
};
let mirror = grid.get(position.x, position.y).copied().unwrap();
match mirror {
b'|' => match direction {
Direction::Up | Direction::Down => [new_beam(direction), None],
Direction::Right | Direction::Left => {
[new_beam(Direction::Up), new_beam(Direction::Down)]
}
},
b'-' => match direction {
Direction::Right | Direction::Left => [new_beam(direction), None],
Direction::Up | Direction::Down => {
[new_beam(Direction::Left), new_beam(Direction::Right)]
}
},
b'\\' => match direction {
Direction::Right => [new_beam(Direction::Down), None],
Direction::Left => [new_beam(Direction::Up), None],
Direction::Up => [new_beam(Direction::Left), None],
Direction::Down => [new_beam(Direction::Right), None],
},
b'/' => match direction {
Direction::Right => [new_beam(Direction::Up), None],
Direction::Left => [new_beam(Direction::Down), None],
Direction::Up => [new_beam(Direction::Right), None],
Direction::Down => [new_beam(Direction::Left), None],
},
b'.' => [new_beam(direction), None],
_ => unreachable!(),
}
},
)
.flatten()
.filter(|beam| {
grid.get(beam.position.x, beam.position.y).is_ok() && energized.insert(*beam)
})
.collect();
}
energized
.into_iter()
.map(|beam| beam.position)
.collect::<HashSet<_>>()
.len()
}
/* -------------------------------------------------------------------------- */
pub fn part_one(input: &str) -> Option<usize> {
let grid = parse_input(input);
let result = process(
&grid,
Beam {
position: Pos { x: 0, y: 0 },
direction: Direction::Right,
},
);
Some(result)
}
/* -------------------------------------------------------------------------- */
pub fn part_two(input: &str) -> Option<usize> {
let grid = parse_input(input);
let width = grid.width();
let height = grid.height();
let result = (0..width)
.flat_map(|x| {
[
process(
&grid,
Beam {
position: Pos { x, y: 0 },
direction: Direction::Down,
},
),
process(
&grid,
Beam {
position: Pos { x, y: height - 1 },
direction: Direction::Up,
},
),
]
})
.chain((0..height).flat_map(|y| {
[
process(
&grid,
Beam {
position: Pos { x: 0, y },
direction: Direction::Right,
},
),
process(
&grid,
Beam {
position: Pos { x: width - 1, y },
direction: Direction::Left,
},
),
]
}))
.max()
.unwrap();
Some(result)
}
/* -------------------------------------------------------------------------- */
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(46));
}
#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(51));
}
}