11use crate :: solutions:: Solution ;
2+ use std:: collections:: { HashMap , HashSet } ;
23
34pub struct Day23 ;
45
56impl Solution for Day23 {
6- fn part_one ( & self , _input : & str ) -> String {
7- String :: from ( "0" )
7+ fn part_one ( & self , input : & str ) -> String {
8+ let mut neighbours: HashMap < & str , Vec < & str > > = HashMap :: new ( ) ;
9+
10+ let connections: Vec < ( & str , & str ) > = input
11+ . lines ( )
12+ . map ( |line| {
13+ let ( a, b) = line. split_once ( '-' ) . unwrap ( ) ;
14+
15+ neighbours. entry ( a) . or_default ( ) . push ( b) ;
16+ neighbours. entry ( b) . or_default ( ) . push ( a) ;
17+
18+ ( a, b)
19+ } )
20+ . collect ( ) ;
21+
22+ let mut sets: HashSet < [ & str ; 3 ] > = HashSet :: new ( ) ;
23+
24+ connections. iter ( ) . for_each ( |( a, b) | {
25+ let a_neighbours = neighbours. get ( a) . unwrap ( ) ;
26+ let b_neighbours = neighbours. get ( b) . unwrap ( ) ;
27+
28+ let intersection: Vec < _ > = a_neighbours
29+ . iter ( )
30+ . filter ( |x| b_neighbours. contains ( x) )
31+ . collect ( ) ;
32+
33+ intersection. iter ( ) . for_each ( |c| {
34+ let mut set = [ * a, * b, * c] ;
35+ set. sort ( ) ;
36+ sets. insert ( set) ;
37+ } ) ;
38+ } ) ;
39+
40+ sets. iter ( )
41+ . filter ( |set| set. iter ( ) . any ( |c| c. starts_with ( "t" ) ) )
42+ . count ( )
43+ . to_string ( )
844 }
945
1046 fn part_two ( & self , _input : & str ) -> String {
@@ -17,10 +53,41 @@ mod tests {
1753 use crate :: solutions:: year2024:: day23:: Day23 ;
1854 use crate :: solutions:: Solution ;
1955
20- const EXAMPLE : & str = r#""# ;
56+ const EXAMPLE : & str = r#"kh-tc
57+ qp-kh
58+ de-cg
59+ ka-co
60+ yn-aq
61+ qp-ub
62+ cg-tb
63+ vc-aq
64+ tb-ka
65+ wh-tc
66+ yn-cg
67+ kh-ub
68+ ta-co
69+ de-co
70+ tc-td
71+ tb-wq
72+ wh-td
73+ ta-ka
74+ td-qp
75+ aq-cg
76+ wq-ub
77+ ub-vc
78+ de-ta
79+ wq-aq
80+ wq-vc
81+ wh-yn
82+ ka-de
83+ kh-ta
84+ co-tc
85+ wh-qp
86+ tb-vc
87+ td-yn"# ;
2188
2289 #[ test]
2390 fn part_one_example ( ) {
24- assert_eq ! ( "0 " , Day23 . part_one( EXAMPLE ) ) ;
91+ assert_eq ! ( "7 " , Day23 . part_one( EXAMPLE ) ) ;
2592 }
2693}
0 commit comments