1- import gleam/dict
21import gleam/int
32import gleam/io
43import gleam/list
54import gleam/result
6- import gleam/set
75import gleam/string
86import internal/aoc_utils
7+ import internal/disjoint_set
98
109pub fn main ( ) {
1110 let filename = "inputs/day08.txt"
@@ -36,12 +35,11 @@ pub fn solve_p1(lines: List(String), connections: Int) -> Result(String, String)
3635 |> list . take ( connections )
3736 |> list . map ( fn ( t ) { t . 0 } )
3837
39- wire ( dict . new ( ) , closest_boxes )
40- |> dict . to_list
41- |> list . map ( fn ( dtup ) { dtup . 1 } )
42- |> set . from_list
43- |> set . to_list
44- |> list . map ( set . size )
38+ let circuits = wire ( disjoint_set . from_list ( boxes ) , closest_boxes )
39+
40+ disjoint_set . setlist ( circuits )
41+ |> list . map ( disjoint_set . size ( circuits , _) )
42+ |> result . values
4543 |> list . sort ( int . compare )
4644 |> list . reverse
4745 |> list . take ( 3 )
@@ -63,7 +61,11 @@ pub fn solve_p2(lines: List(String)) -> Result(String, String) {
6361 |> list . sort ( fn ( v1 , v2 ) { int . compare ( v1 . 1 , v2 . 1 ) } )
6462 |> list . map ( fn ( t ) { t . 0 } )
6563
66- build_circuit_sized ( list . length ( boxes ) , dict . new ( ) , closest_boxes )
64+ build_circuit_sized (
65+ list . length ( boxes ) ,
66+ disjoint_set . from_list ( boxes ) ,
67+ closest_boxes ,
68+ )
6769 |> fn ( v ) { { v . 0 } . x * { v . 1 } . x }
6870 |> int . to_string
6971 |> Ok
@@ -90,21 +92,12 @@ fn parse_line(line: String) -> Junction {
9092// When I combine two boxes I have to update the circuit of all involed
9193// junctions.
9294fn wire (
93- circuit_table : dict . Dict ( Junction , set . Set ( Junction ) ) ,
95+ circuit_table : disjoint_set . DisjointSet ( Junction ) ,
9496 connections : List ( # ( Junction , Junction ) ) ,
95- ) -> dict . Dict ( Junction , set . Set ( Junction ) ) {
97+ ) -> disjoint_set . DisjointSet ( Junction ) {
9698 list . fold ( connections , circuit_table , fn ( acc , conn ) {
97- // Get the circuit for a box, if it's not part of a circuit yet
98- // just provide a set containing only itself
99- let s1 = dict . get ( acc , conn . 0 ) |> result . unwrap ( set . from_list ( [ conn . 0 ] ) )
100- let s2 = dict . get ( acc , conn . 1 ) |> result . unwrap ( set . from_list ( [ conn . 1 ] ) )
101-
102- // new combined circuit
103- let combined = set . union ( s1 , s2 )
104-
105- set . fold ( combined , acc , fn ( acc_inner , jb ) {
106- dict . insert ( acc_inner , jb , combined )
107- } )
99+ let assert Ok ( new_table ) = disjoint_set . union ( acc , conn . 0 , conn . 1 )
100+ new_table
108101 } )
109102}
110103
@@ -113,33 +106,17 @@ fn wire(
113106// connection that achieved that size.
114107fn build_circuit_sized (
115108 size : Int ,
116- circuit_table : dict . Dict ( Junction , set . Set ( Junction ) ) ,
109+ circuit_table : disjoint_set . DisjointSet ( Junction ) ,
117110 connections : List ( # ( Junction , Junction ) ) ,
118111) -> # ( Junction , Junction ) {
119112 case connections {
120113 [ ] -> panic as "unable to build requested circuit size"
121114 [ first , .. rest ] -> {
122- // Get the circuit for a box, if it's not part of a circuit yet
123- // just provide a set containing only itself
124- let s1 =
125- dict . get ( circuit_table , first . 0 )
126- |> result . unwrap ( set . from_list ( [ first . 0 ] ) )
127- let s2 =
128- dict . get ( circuit_table , first . 1 )
129- |> result . unwrap ( set . from_list ( [ first . 1 ] ) )
130-
131- // new combined circuit
132- let combined = set . union ( s1 , s2 )
133-
134- case set . size ( combined ) {
135- s if s == size -> first
136- _ -> {
137- let new_table =
138- set . fold ( combined , circuit_table , fn ( acc , jb ) {
139- dict . insert ( acc , jb , combined )
140- } )
141- build_circuit_sized ( size , new_table , rest )
142- }
115+ let assert Ok ( new_table ) =
116+ disjoint_set . union ( circuit_table , first . 0 , first . 1 )
117+ case disjoint_set . size ( new_table , first . 0 ) {
118+ Ok ( s ) if s == size -> first
119+ _ -> build_circuit_sized ( size , new_table , rest )
143120 }
144121 }
145122 }
0 commit comments