1
1
use crate :: solutions:: Solution ;
2
+ use crate :: utils:: graphs:: graph:: Graph ;
2
3
use itertools:: Itertools ;
3
- use std:: collections:: HashMap ;
4
4
5
5
pub struct Day23 ;
6
6
7
7
impl Solution for Day23 {
8
8
fn part_one ( & self , input : & str ) -> String {
9
- let mut neighbours : HashMap < & str , Vec < & str > > = HashMap :: new ( ) ;
9
+ let graph = self . parse ( input ) ;
10
10
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 ( )
24
13
. iter ( )
25
14
. 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 ) ;
28
17
29
18
a_neighbours
30
19
. iter ( )
@@ -34,6 +23,7 @@ impl Solution for Day23 {
34
23
set. sort ( ) ;
35
24
set
36
25
} )
26
+ . collect :: < Vec < [ & str ; 3 ] > > ( )
37
27
} )
38
28
. unique ( )
39
29
. filter ( |set| set. iter ( ) . any ( |c| c. starts_with ( "t" ) ) )
@@ -46,6 +36,20 @@ impl Solution for Day23 {
46
36
}
47
37
}
48
38
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
+
49
53
#[ cfg( test) ]
50
54
mod tests {
51
55
use crate :: solutions:: year2024:: day23:: Day23 ;
0 commit comments