11use crate :: solutions:: Solution ;
2+ use crate :: utils:: graphs:: graph:: Graph ;
23use itertools:: Itertools ;
3- use std:: collections:: HashMap ;
44
55pub struct Day23 ;
66
77impl Solution for Day23 {
88 fn part_one ( & self , input : & str ) -> String {
9- let mut neighbours : HashMap < & str , Vec < & str > > = HashMap :: new ( ) ;
9+ let graph = self . parse ( input ) ;
1010
11- let connections: Vec < ( & str , & str ) > = input
12- . lines ( )
13- . map ( |line| {
14- let ( a, b) = line. split_once ( '-' ) . unwrap ( ) ;
15-
16- neighbours. entry ( a) . or_default ( ) . push ( b) ;
17- neighbours. entry ( b) . or_default ( ) . push ( a) ;
18-
19- ( a, b)
20- } )
21- . collect ( ) ;
22-
23- connections
11+ graph
12+ . edges ( )
2413 . iter ( )
2514 . flat_map ( |( a, b) | {
26- let a_neighbours = neighbours . get ( a ) . unwrap ( ) ;
27- let b_neighbours = neighbours . get ( b ) . unwrap ( ) ;
15+ let a_neighbours = graph . neighbours ( a ) ;
16+ let b_neighbours = graph . neighbours ( b ) ;
2817
2918 a_neighbours
3019 . iter ( )
@@ -34,6 +23,7 @@ impl Solution for Day23 {
3423 set. sort ( ) ;
3524 set
3625 } )
26+ . collect :: < Vec < [ & str ; 3 ] > > ( )
3727 } )
3828 . unique ( )
3929 . filter ( |set| set. iter ( ) . any ( |c| c. starts_with ( "t" ) ) )
@@ -46,6 +36,20 @@ impl Solution for Day23 {
4636 }
4737}
4838
39+ impl Day23 {
40+ fn parse < ' a > ( & self , input : & ' a str ) -> Graph < & ' a str > {
41+ let mut graph: Graph < & str > = Graph :: undirected ( ) ;
42+
43+ input. lines ( ) . for_each ( |line| {
44+ let ( a, b) = line. split_once ( '-' ) . unwrap ( ) ;
45+
46+ graph. add_edge ( a, b) ;
47+ } ) ;
48+
49+ graph
50+ }
51+ }
52+
4953#[ cfg( test) ]
5054mod tests {
5155 use crate :: solutions:: year2024:: day23:: Day23 ;
0 commit comments