File tree 3 files changed +72
-3
lines changed
3 files changed +72
-3
lines changed Original file line number Diff line number Diff line change 1
- use std:: cmp:: Ordering ;
2
-
3
1
use util:: input_lines;
4
2
5
3
#[ derive( Debug , Clone ) ]
Original file line number Diff line number Diff line change @@ -6,3 +6,4 @@ edition = "2021"
6
6
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7
7
8
8
[dependencies ]
9
+ util = { path = " ../util" }
Original file line number Diff line number Diff line change
1
+ use util:: input_lines;
2
+
3
+ #[ derive( Debug ) ]
4
+ struct Race {
5
+ time : u64 ,
6
+ distance : u64 ,
7
+ }
8
+
9
+ impl Race {
10
+ fn ways_to_beat_record ( & self ) -> u64 {
11
+ let mut ways = 0 ;
12
+ for press in 0 ..=self . time {
13
+ if ( self . time - press) * press > self . distance {
14
+ ways += 1 ;
15
+ }
16
+ }
17
+ ways
18
+ }
19
+ }
20
+
1
21
fn main ( ) {
2
- println ! ( "Hello, world!" ) ;
22
+ let lines = input_lines ( ) ;
23
+ let races = lines[ 0 ]
24
+ . split_once ( ":" )
25
+ . unwrap ( )
26
+ . 1
27
+ . trim ( )
28
+ . split_whitespace ( )
29
+ . map ( |x| x. parse ( ) . unwrap ( ) )
30
+ . zip (
31
+ lines[ 1 ]
32
+ . split_once ( ":" )
33
+ . unwrap ( )
34
+ . 1
35
+ . trim ( )
36
+ . split_whitespace ( )
37
+ . map ( |x| x. parse ( ) . unwrap ( ) ) ,
38
+ )
39
+ . map ( |( time, distance) | Race { time, distance } )
40
+ . collect :: < Vec < _ > > ( ) ;
41
+ // println!("{:?}", races);
42
+ let mut product_of_ways = 1 ;
43
+ for race in & races {
44
+ product_of_ways *= race. ways_to_beat_record ( ) ;
45
+ // println!("{:?} {}", race, race.ways_to_beat_record());
46
+ }
47
+ println ! ( "Part 1: {}" , product_of_ways) ;
48
+
49
+ let race = Race {
50
+ time : lines[ 0 ]
51
+ . split_once ( ":" )
52
+ . unwrap ( )
53
+ . 1
54
+ . trim ( )
55
+ . split_whitespace ( )
56
+ . collect :: < Vec < _ > > ( )
57
+ . join ( "" )
58
+ . parse ( )
59
+ . unwrap ( ) ,
60
+ distance : lines[ 1 ]
61
+ . split_once ( ":" )
62
+ . unwrap ( )
63
+ . 1
64
+ . trim ( )
65
+ . split_whitespace ( )
66
+ . collect :: < Vec < _ > > ( )
67
+ . join ( "" )
68
+ . parse ( )
69
+ . unwrap ( ) ,
70
+ } ;
71
+ // println!("{:?}", race);
72
+ println ! ( "Part 2: {}" , race. ways_to_beat_record( ) ) ;
3
73
}
You can’t perform that action at this time.
0 commit comments