@@ -2,22 +2,20 @@ use crate::solutions::Solution;
22use crate :: utils:: point3d:: Point3D ;
33use itertools:: Itertools ;
44
5- const INPUT_CONNECTIONS : u64 = 1_000 ;
5+ const INPUT_CONNECTIONS : usize = 1_000 ;
66
77type Pair = ( Point3D , Point3D ) ;
88
99pub struct Day08 {
10- connections : u64 ,
10+ connections : usize ,
1111}
1212
1313impl Solution for Day08 {
1414 fn part_one ( & self , input : & str ) -> String {
1515 let junction_boxes = self . parse ( input) ;
16- let closest = self . closest ( junction_boxes) ;
17-
1816 let mut circuits: Vec < Vec < Point3D > > = Vec :: new ( ) ;
1917
20- for pair in closest {
18+ for pair in self . closest_limited ( junction_boxes ) {
2119 let left_circuit = circuits
2220 . iter ( )
2321 . position ( |circuit| circuit. contains ( & pair. 0 ) ) ;
@@ -40,9 +38,7 @@ impl Solution for Day08 {
4038 ( None , Some ( right) ) => circuits[ right] . push ( pair. 0 ) ,
4139 ( Some ( left) , None ) => circuits[ left] . push ( pair. 1 ) ,
4240 ( None , None ) => {
43- let new_circuit = vec ! [ pair. 0 , pair. 1 ] ;
44-
45- circuits. push ( new_circuit) ;
41+ circuits. push ( vec ! [ pair. 0 , pair. 1 ] ) ;
4642 }
4743 }
4844 }
@@ -67,7 +63,11 @@ impl Day08 {
6763 input. lines ( ) . map ( |line| line. parse ( ) . unwrap ( ) ) . collect ( )
6864 }
6965
70- fn closest ( & self , boxes : Vec < Point3D > ) -> Vec < Pair > {
66+ fn closest_limited ( & self , boxes : Vec < Point3D > ) -> impl Iterator < Item = Pair > {
67+ self . closest_all ( boxes) . take ( self . connections )
68+ }
69+
70+ fn closest_all ( & self , boxes : Vec < Point3D > ) -> impl Iterator < Item = Pair > {
7171 let mut calculated: Vec < ( f64 , Pair ) > = Vec :: new ( ) ;
7272 for i in 0 ..boxes. len ( ) {
7373 for j in i + 1 ..boxes. len ( ) {
@@ -77,11 +77,7 @@ impl Day08 {
7777
7878 calculated. sort_by ( |a, b| a. 0 . partial_cmp ( & b. 0 ) . unwrap ( ) ) ;
7979
80- calculated
81- . iter ( )
82- . take ( self . connections as usize )
83- . map ( |x| x. 1 )
84- . collect ( )
80+ calculated. into_iter ( ) . map ( |x| x. 1 )
8581 }
8682}
8783
@@ -97,7 +93,8 @@ impl Default for Day08 {
9793mod tests {
9894 use crate :: solutions:: year2025:: day08:: Day08 ;
9995 use crate :: solutions:: Solution ;
100- const TEST_CONNECTIONS : u64 = 10 ;
96+
97+ const TEST_CONNECTIONS : usize = 10 ;
10198
10299 impl Day08 {
103100 fn new_for_tests ( ) -> Self {
0 commit comments