-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday16.rs
147 lines (135 loc) · 5.7 KB
/
day16.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
use rayon::iter::ParallelIterator;
use std::collections::HashSet;
use std::fs;
use rayon::iter::IntoParallelRefIterator;
pub(crate) fn day16() {
println!("{}", part_a(fs::read_to_string("input/2023/day16/input.txt").unwrap()));
println!("{}", part_b(fs::read_to_string("input/2023/day16/input.txt").unwrap()));
}
fn part_a(input: String) -> usize {
let matrix: Vec<Vec<char>> = input.lines().map(|line| {
line.chars().collect()
}).collect();
calc_beam(&matrix, 0, 0, 90)
}
fn part_b(input: String) -> usize {
let matrix: Vec<Vec<char>> = input.lines().map(|line| {
line.chars().collect()
}).collect();
let mut starts: Vec<(usize, usize, usize)> = vec!();
for r in 0..matrix.len() {
starts.push((r, 0, 90));
starts.push((r, matrix[r].len() - 1, 270));
}
for c in 0..matrix.len() {
starts.push((0, c, 180));
starts.push((matrix.len() - 1, c, 0));
}
starts.par_iter().map(|(sr, sc, sdir)| {
calc_beam(&matrix, *sr, *sc, *sdir)
}).max().unwrap()
}
fn calc_beam(matrix: &Vec<Vec<char>>, sr: usize, sc: usize, sdir: usize) -> usize {
let mut energized: HashSet<(usize, usize)> = HashSet::new();
let mut seen: HashSet<(usize, usize, usize)> = HashSet::new();
let mut beams = vec!();
beams.push((sr, sc, sdir));
for _i in 0..1000 {
let mut new_beams: Vec<(usize, usize, usize)> = vec!();
for (r, c, dir) in &beams {
energized.insert((*r, *c));
seen.insert((*r, *c, *dir));
match dir {
90 => {
let curr_c = matrix[*r][*c];
match curr_c {
'.' => { if *c < (matrix[*r].len() - 1) { new_beams.push((*r, c + 1, 90)) } }
'-' => { if *c < (matrix[*r].len() - 1) { new_beams.push((*r, c + 1, 90)) } }
'|' => {
if *r > 0 { new_beams.push((r - 1, *c, 0)) }
if *r < matrix.len() - 1 { new_beams.push((r + 1, *c, 180)) }
}
'/' => {
if *r > 0 { new_beams.push((r - 1, *c, 0)) }
}
'\\' => {
if *r < (matrix.len() - 1) { new_beams.push((r + 1, *c, 180)) }
}
_ => panic!("Unknown {}", c)
}
}
270 => {
let curr_c = matrix[*r][*c];
match curr_c {
'.' => { if *c > 0 { new_beams.push((*r, c - 1, 270)) } }
'-' => { if *c > 0 { new_beams.push((*r, c - 1, 270)) } }
'|' => {
if *r > 0 { new_beams.push((r - 1, *c, 0)) }
if *r < matrix.len() - 1 { new_beams.push((r + 1, *c, 180)) }
}
'/' => {
if *r < matrix.len() - 1 { new_beams.push((r + 1, *c, 180)) }
}
'\\' => {
if *r > 0 { new_beams.push((r - 1, *c, 0)) }
}
_ => panic!("Unknown {}", c)
}
}
0 => {
let curr_c = matrix[*r][*c];
match curr_c {
'.' => { if *r > 0 { new_beams.push((r - 1, *c, 0)) } }
'-' => {
if *c > 0 { new_beams.push((*r, c - 1, 270)) }
if *c < matrix[*r].len() - 1 { new_beams.push((*r, c + 1, 90)) }
}
'|' => { if *r > 0 { new_beams.push((r - 1, *c, 0)) } }
'/' => {
if *c < matrix[*r].len() - 1 { new_beams.push((*r, c + 1, 90)) }
}
'\\' => {
if *c > 0 { new_beams.push((*r, c - 1, 270)) }
}
_ => panic!("Unknown {}", c)
}
}
180 => {
let curr_c = matrix[*r][*c];
match curr_c {
'.' => { if *r < matrix[*r].len() - 1 { new_beams.push((r + 1, *c, 180)) } }
'-' => {
if *c > 0 { new_beams.push((*r, c - 1, 270)) }
if *c < matrix[*r].len() - 1 { new_beams.push((*r, c + 1, 90)) }
}
'|' => { if *r < matrix[*r].len() - 1 { new_beams.push((r + 1, *c, 180)) } }
'/' => { if *c > 0 { new_beams.push((*r, c - 1, 270)) } }
'\\' => { if *c < matrix[*r].len() - 1 { new_beams.push((*r, c + 1, 90)) } }
_ => panic!("Unknown {}", c)
}
}
_ => panic!("unknown dir {}", dir)
}
}
beams = new_beams.iter()
.filter(|(r, c, dir)| !seen.contains(&(*r, *c, *dir)))
.map(|x| *x)
.collect()
}
energized.len()
}
#[cfg(test)]
mod day16_tests {
use std::fs;
use crate::y2023::day16::{part_a, part_b};
#[test]
fn test_works() {
assert_eq!(46, part_a(fs::read_to_string("input/2023/day16/test.txt").unwrap()));
assert_eq!(51, part_b(fs::read_to_string("input/2023/day16/test.txt").unwrap()));
}
#[test]
fn input_works() {
assert_eq!(6978, part_a(fs::read_to_string("input/2023/day16/input.txt").unwrap()));
assert_eq!(7315, part_b(fs::read_to_string("input/2023/day16/input.txt").unwrap()));
}
}