File tree 1 file changed +25
-2
lines changed
1 file changed +25
-2
lines changed Original file line number Diff line number Diff line change 1
1
advent_of_code:: solution!( 1 ) ;
2
2
3
+ use std:: collections:: HashMap ;
4
+
3
5
pub fn part_one ( input : & str ) -> Option < u32 > {
4
6
// read input into two lists
5
7
let ( mut a, mut b) : ( Vec < u32 > , Vec < u32 > ) = input
@@ -25,7 +27,28 @@ pub fn part_one(input: &str) -> Option<u32> {
25
27
}
26
28
27
29
pub fn part_two ( input : & str ) -> Option < u32 > {
28
- None
30
+ // read input into two lists
31
+ let ( a, b) : ( Vec < u32 > , Vec < u32 > ) = input
32
+ . lines ( )
33
+ . filter_map ( |line| {
34
+ let mut parts = line. split_whitespace ( ) ;
35
+ let first = parts. next ( ) ?. parse :: < u32 > ( ) . ok ( ) ?;
36
+ let second = parts. next ( ) ?. parse :: < u32 > ( ) . ok ( ) ?;
37
+ Some ( ( first, second) )
38
+ } )
39
+ . unzip ( ) ;
40
+
41
+ let b_freq = b. into_iter ( ) . fold ( HashMap :: new ( ) , |mut map, val| {
42
+ * map. entry ( val) . or_insert ( 0 ) += 1 ;
43
+ map
44
+ } ) ;
45
+
46
+ let similarity_score: u32 = a
47
+ . iter ( )
48
+ . map ( |x| x * b_freq. get ( x) . unwrap_or ( & 0 ) )
49
+ . sum ( ) ;
50
+
51
+ Some ( similarity_score)
29
52
}
30
53
31
54
#[ cfg( test) ]
@@ -41,6 +64,6 @@ mod tests {
41
64
#[ test]
42
65
fn test_part_two ( ) {
43
66
let result = part_two ( & advent_of_code:: template:: read_file ( "examples" , DAY ) ) ;
44
- assert_eq ! ( result, None ) ;
67
+ assert_eq ! ( result, Some ( 31 ) ) ;
45
68
}
46
69
}
You can’t perform that action at this time.
0 commit comments