File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ 3 4
2+ 4 3
3+ 2 5
4+ 1 3
5+ 3 9
6+ 3 3
Original file line number Diff line number Diff line change 1+ advent_of_code:: solution!( 1 ) ;
2+
3+ pub fn part_one ( input : & str ) -> Option < u32 > {
4+ // read input into two lists
5+ let ( mut a, mut b) : ( Vec < u32 > , Vec < u32 > ) = input
6+ . lines ( )
7+ . filter_map ( |line| {
8+ let mut parts = line. split_whitespace ( ) ;
9+ let first = parts. next ( ) ?. parse :: < u32 > ( ) . ok ( ) ?;
10+ let second = parts. next ( ) ?. parse :: < u32 > ( ) . ok ( ) ?;
11+ Some ( ( first, second) )
12+ } )
13+ . unzip ( ) ;
14+
15+ a. sort ( ) ;
16+ b. sort ( ) ;
17+
18+ let sum_diff: u32 = a
19+ . iter ( )
20+ . zip ( b. iter ( ) )
21+ . map ( |( x, y) | if x > y { x - y } else { y - x } )
22+ . sum ( ) ;
23+
24+ Some ( sum_diff)
25+ }
26+
27+ pub fn part_two ( input : & str ) -> Option < u32 > {
28+ None
29+ }
30+
31+ #[ cfg( test) ]
32+ mod tests {
33+ use super :: * ;
34+
35+ #[ test]
36+ fn test_part_one ( ) {
37+ let result = part_one ( & advent_of_code:: template:: read_file ( "examples" , DAY ) ) ;
38+ assert_eq ! ( result, Some ( 11 ) ) ;
39+ }
40+
41+ #[ test]
42+ fn test_part_two ( ) {
43+ let result = part_two ( & advent_of_code:: template:: read_file ( "examples" , DAY ) ) ;
44+ assert_eq ! ( result, None ) ;
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments