This repository was archived by the owner on Dec 25, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +14
-11
lines changed Expand file tree Collapse file tree 1 file changed +14
-11
lines changed Original file line number Diff line number Diff line change @@ -6,16 +6,19 @@ fn read_input(input: &str) -> (Vec<i32>, Vec<i32>) {
6
6
input
7
7
. trim ( )
8
8
. lines ( )
9
+ . map ( |line| {
10
+ let ( left, right) = line. trim ( ) . split_once ( " " ) . unwrap_or_else ( || {
11
+ panic ! ( "Expect two values per line with three spaces in between, but {line} is not" )
12
+ } ) ;
13
+
14
+ (
15
+ left. parse ( ) . expect ( "not a number" ) ,
16
+ right. parse ( ) . expect ( "not a number" ) ,
17
+ )
18
+ } )
9
19
. fold ( ( vec ! [ ] , vec ! [ ] ) , |mut acc, line| {
10
- let values = line. split_whitespace ( ) . collect :: < Vec < _ > > ( ) ;
11
-
12
- if values. len ( ) != 2 {
13
- panic ! ( "Expect two values per line, but {line} is not" ) ;
14
- }
15
-
16
- acc. 0 . push ( values[ 0 ] . parse ( ) . expect ( "not a number" ) ) ;
17
- acc. 1 . push ( values[ 1 ] . parse ( ) . expect ( "not a number" ) ) ;
18
-
20
+ acc. 0 . push ( line. 0 ) ;
21
+ acc. 1 . push ( line. 1 ) ;
19
22
acc
20
23
} )
21
24
}
@@ -29,7 +32,7 @@ fn p1(input: &str) -> String {
29
32
left_list
30
33
. into_iter ( )
31
34
. zip ( right_list)
32
- . map ( |( left, right) | left . max ( right ) - left . min ( right ) )
35
+ . map ( |( left, right) | ( left - right ) . abs ( ) )
33
36
. sum :: < i32 > ( )
34
37
. to_string ( )
35
38
}
@@ -45,7 +48,7 @@ fn p2(input: &str) -> String {
45
48
46
49
left_list
47
50
. into_iter ( )
48
- . map ( |left_number| left_number * ( right_map. get ( & left_number) . copied ( ) . unwrap_or_default ( ) ) )
51
+ . map ( |left_number| left_number * ( * right_map. get ( & left_number) . unwrap_or ( & 0 ) ) )
49
52
. sum :: < i32 > ( )
50
53
. to_string ( )
51
54
}
You can’t perform that action at this time.
0 commit comments