Skip to content

Commit 6cbda10

Browse files
committed
refactor(08/2025): simplify and extract common code from first and second part
1 parent 61b37ab commit 6cbda10

File tree

1 file changed

+39
-51
lines changed

1 file changed

+39
-51
lines changed

src/solutions/year2025/day08.rs

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,7 @@ impl Solution for Day08 {
1616
let mut circuits: Vec<Vec<Point3D>> = Vec::new();
1717

1818
for pair in self.closest_limited(&junction_boxes) {
19-
let left_circuit = circuits
20-
.iter()
21-
.position(|circuit| circuit.contains(&pair.0));
22-
23-
let right_circuit = circuits
24-
.iter()
25-
.position(|circuit| circuit.contains(&pair.1));
26-
27-
match (left_circuit, right_circuit) {
28-
(Some(left), Some(right)) => {
29-
if left == right {
30-
continue;
31-
}
32-
33-
for in_circuit in circuits[right].clone() {
34-
circuits[left].push(in_circuit);
35-
}
36-
circuits.remove(right);
37-
}
38-
(None, Some(right)) => circuits[right].push(pair.0),
39-
(Some(left), None) => circuits[left].push(pair.1),
40-
(None, None) => {
41-
circuits.push(vec![pair.0, pair.1]);
42-
}
43-
}
19+
self.assign_pair_to_circuit(&pair, &mut circuits);
4420
}
4521

4622
circuits
@@ -58,33 +34,9 @@ impl Solution for Day08 {
5834
let mut circuits: Vec<Vec<Point3D>> = Vec::new();
5935

6036
for pair in self.closest_all(&junction_boxes) {
61-
let left_circuit = circuits
62-
.iter()
63-
.position(|circuit| circuit.contains(&pair.0));
64-
65-
let right_circuit = circuits
66-
.iter()
67-
.position(|circuit| circuit.contains(&pair.1));
68-
69-
match (left_circuit, right_circuit) {
70-
(Some(left), Some(right)) => {
71-
if left == right {
72-
continue;
73-
}
74-
75-
for in_circuit in circuits[right].clone() {
76-
circuits[left].push(in_circuit);
77-
}
78-
circuits.remove(right);
79-
}
80-
(None, Some(right)) => circuits[right].push(pair.0),
81-
(Some(left), None) => circuits[left].push(pair.1),
82-
(None, None) => {
83-
circuits.push(vec![pair.0, pair.1]);
84-
}
85-
}
37+
self.assign_pair_to_circuit(&pair, &mut circuits);
8638

87-
if circuits.len() == 1 && circuits.first().unwrap().len() == junction_boxes.len() {
39+
if self.are_all_boxes_connected_in_one_circuit(&circuits, &junction_boxes) {
8840
return (pair.0.x * pair.1.x).to_string();
8941
}
9042
}
@@ -114,6 +66,42 @@ impl Day08 {
11466

11567
calculated.into_iter().map(|x| x.1)
11668
}
69+
70+
fn assign_pair_to_circuit(&self, pair: &Pair, circuits: &mut Vec<Vec<Point3D>>) {
71+
let left_circuit = circuits
72+
.iter()
73+
.position(|circuit| circuit.contains(&pair.0));
74+
75+
let right_circuit = circuits
76+
.iter()
77+
.position(|circuit| circuit.contains(&pair.1));
78+
79+
match (left_circuit, right_circuit) {
80+
(Some(left), Some(right)) => {
81+
if left == right {
82+
return;
83+
}
84+
85+
for box_in_circuit in circuits[right].clone() {
86+
circuits[left].push(box_in_circuit);
87+
}
88+
circuits.remove(right);
89+
}
90+
(None, Some(right)) => circuits[right].push(pair.0),
91+
(Some(left), None) => circuits[left].push(pair.1),
92+
(None, None) => {
93+
circuits.push(vec![pair.0, pair.1]);
94+
}
95+
}
96+
}
97+
98+
fn are_all_boxes_connected_in_one_circuit(
99+
&self,
100+
circuits: &[Vec<Point3D>],
101+
junction_boxes: &[Point3D],
102+
) -> bool {
103+
circuits.len() == 1 && circuits.first().unwrap().len() == junction_boxes.len()
104+
}
117105
}
118106

119107
impl Default for Day08 {

0 commit comments

Comments
 (0)