@@ -41,15 +41,7 @@ pub fn solve_p1(lines: List(String), connections: Int) -> Result(String, String)
4141 |> list . take ( connections )
4242 |> list . map ( fn ( t ) { t . 0 } )
4343
44- // This table has junction boxes as keys and the circuits they make
45- // as a set of junction boxes as a value. They all start with just
46- // themselves.
47- let circuit_table =
48- boxes
49- |> list . map ( fn ( box ) { # ( box , set . new ( ) |> set . insert ( box ) ) } )
50- |> dict . from_list
51-
52- wire ( circuit_table , closest_boxes )
44+ wire ( dict . new ( ) , closest_boxes )
5345 |> dict . to_list
5446 |> list . map ( fn ( dtup ) { dtup . 1 } )
5547 |> list . filter ( fn ( circuit ) { set . size ( circuit ) > 1 } )
@@ -82,15 +74,7 @@ pub fn solve_p2(lines: List(String)) -> Result(String, String) {
8274 |> list . sort ( fn ( v1 , v2 ) { int . compare ( v1 . 1 , v2 . 1 ) } )
8375 |> list . map ( fn ( t ) { t . 0 } )
8476
85- // This table has junction boxes as keys and the circuits they make
86- // as a set of junction boxes as a value. They all start with just
87- // themselves.
88- let circuit_table =
89- boxes
90- |> list . map ( fn ( box ) { # ( box , set . new ( ) |> set . insert ( box ) ) } )
91- |> dict . from_list
92-
93- build_circuit_sized ( dict . size ( circuit_table ) , circuit_table , closest_boxes )
77+ build_circuit_sized ( list . length ( boxes ) , dict . new ( ) , closest_boxes )
9478 |> fn ( v ) { { v . 0 } . x * { v . 1 } . x }
9579 |> int . to_string
9680 |> Ok
@@ -121,9 +105,12 @@ fn wire(
121105 connections : List ( # ( Junction , Junction ) ) ,
122106) -> dict . Dict ( Junction , set . Set ( Junction ) ) {
123107 list . fold ( connections , circuit_table , fn ( acc , conn ) {
124- let assert Ok ( s1 ) = dict . get ( acc , conn . 0 )
125- let assert Ok ( s2 ) = dict . get ( acc , conn . 1 )
108+ // Get the circuit for a box, if it's not part of a circuit yet
109+ // just provide a set containing only itself
110+ let s1 = dict . get ( acc , conn . 0 ) |> result . unwrap ( set . from_list ( [ conn . 0 ] ) )
111+ let s2 = dict . get ( acc , conn . 1 ) |> result . unwrap ( set . from_list ( [ conn . 1 ] ) )
126112
113+ // new combined circuit
127114 let combined = set . union ( s1 , s2 )
128115
129116 set . fold ( combined , acc , fn ( acc_inner , jb ) {
@@ -143,9 +130,16 @@ fn build_circuit_sized(
143130 case connections {
144131 [ ] -> panic as "unable to build requested circuit size"
145132 [ first , .. rest ] -> {
146- let assert Ok ( s1 ) = dict . get ( circuit_table , first . 0 )
147- let assert Ok ( s2 ) = dict . get ( circuit_table , first . 1 )
148-
133+ // Get the circuit for a box, if it's not part of a circuit yet
134+ // just provide a set containing only itself
135+ let s1 =
136+ dict . get ( circuit_table , first . 0 )
137+ |> result . unwrap ( set . from_list ( [ first . 0 ] ) )
138+ let s2 =
139+ dict . get ( circuit_table , first . 1 )
140+ |> result . unwrap ( set . from_list ( [ first . 1 ] ) )
141+
142+ // new combined circuit
149143 let combined = set . union ( s1 , s2 )
150144
151145 case set . size ( combined ) {
0 commit comments