-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd16.rs
More file actions
191 lines (157 loc) Β· 5.75 KB
/
d16.rs
File metadata and controls
191 lines (157 loc) Β· 5.75 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::{collections::HashSet, iter::zip, sync::mpsc::Sender, thread};
use crate::{Day, y2023::Direction};
pub struct Day16 {}
impl Day for Day16 {
fn year(&self) -> u16 {
2023
}
fn day(&self) -> u8 {
16
}
fn part_one(&self) -> String {
let contraption = self
.read_default_input()
.lines()
.map(|line| line.chars().collect::<Vec<char>>())
.collect::<Vec<Vec<char>>>();
let mut split_tracking: HashSet<(i32, i32)> = HashSet::new();
let path = trace_path(&contraption, (-1, 0), Direction::East, &mut split_tracking);
path.len().to_string()
}
fn part_two(&self) -> String {
let contraption = self
.read_default_input()
.lines()
.map(|line| line.chars().collect::<Vec<char>>())
.collect::<Vec<Vec<char>>>();
let mut max_path = 0;
let (tx, rx) = std::sync::mpsc::channel::<usize>();
// Left starting
calculate_max_direction(tx.clone(), contraption.clone(), Direction::East);
// Top starting
calculate_max_direction(tx.clone(), contraption.clone(), Direction::South);
// Right starting
calculate_max_direction(tx.clone(), contraption.clone(), Direction::West);
// Bottom starting
calculate_max_direction(tx.clone(), contraption.clone(), Direction::North);
drop(tx);
while let Ok(val) = rx.recv() {
if val > max_path {
max_path = val;
}
}
max_path.to_string()
}
}
fn trace_path(
contraption: &Vec<Vec<char>>,
starting_pos: (i32, i32),
direction: Direction,
split_tracking: &mut HashSet<(i32, i32)>,
) -> HashSet<(i32, i32)> {
let mut path: HashSet<(i32, i32)> = HashSet::new();
let mut direction = direction;
let mut current_pos = starting_pos;
loop {
let (next_x, next_y) = (
current_pos.0 + direction.modifier().0,
current_pos.1 + direction.modifier().1,
);
if next_y >= contraption.len() as i32 || next_y < 0 {
break;
}
if next_x >= contraption[0].len() as i32 || next_x < 0 {
break;
}
path.insert((next_x, next_y));
current_pos = (next_x, next_y);
let value = contraption[current_pos.1 as usize][current_pos.0 as usize];
match (value, direction) {
('.', _) => (),
('-', Direction::North | Direction::South) => {
match split_tracking.contains(¤t_pos) {
true => break,
false => split_tracking.insert(current_pos),
};
let west_path =
trace_path(contraption, current_pos, Direction::West, split_tracking);
let east_path =
trace_path(contraption, current_pos, Direction::East, split_tracking);
path.extend(west_path);
path.extend(east_path);
break;
}
('|', Direction::East | Direction::West) => {
match split_tracking.contains(¤t_pos) {
true => break,
false => split_tracking.insert(current_pos),
};
let north_path =
trace_path(contraption, current_pos, Direction::North, split_tracking);
let south_path =
trace_path(contraption, current_pos, Direction::South, split_tracking);
path.extend(north_path);
path.extend(south_path);
break;
}
('-' | '|', _) => (),
('/', Direction::North) => direction = Direction::East,
('/', Direction::East) => direction = Direction::North,
('/', Direction::South) => direction = Direction::West,
('/', Direction::West) => direction = Direction::South,
('\\', Direction::North) => direction = Direction::West,
('\\', Direction::East) => direction = Direction::South,
('\\', Direction::South) => direction = Direction::East,
('\\', Direction::West) => direction = Direction::North,
_ => unreachable!(),
}
}
path
}
fn calculate_max_direction(
sender: Sender<usize>,
contraption: Vec<Vec<char>>,
direction: Direction,
) {
thread::spawn(move || {
let positions = starting_positions(&contraption, direction);
let mut max_path = 0;
positions.iter().for_each(|position| {
let mut split_tracking: HashSet<(i32, i32)> = HashSet::new();
let path = trace_path(
&contraption,
(position.0, position.1),
direction,
&mut split_tracking,
);
if path.len() > max_path {
max_path = path.len();
}
});
sender.send(max_path).unwrap();
});
}
fn starting_positions(contraption: &[Vec<char>], direction: Direction) -> Vec<(i32, i32)> {
match direction {
Direction::North => zip(
0_i32..contraption[0].len() as i32,
vec![contraption.len() as i32; contraption.len()],
)
.collect::<Vec<_>>(),
Direction::East => zip(
vec![-1_i32; contraption[0].len()],
0_i32..contraption.len() as i32,
)
.collect::<Vec<_>>(),
Direction::South => zip(
0_i32..contraption[0].len() as i32,
vec![-1; contraption.len()],
)
.collect::<Vec<_>>(),
Direction::West => zip(
vec![contraption[0].len() as i32; contraption[0].len()],
0_i32..contraption.len() as i32,
)
.collect::<Vec<_>>(),
}
}