File tree Expand file tree Collapse file tree 5 files changed +173
-0
lines changed Expand file tree Collapse file tree 5 files changed +173
-0
lines changed Original file line number Diff line number Diff line change 1+ 28
2+ 33
3+ 18
4+ 42
5+ 31
6+ 14
7+ 46
8+ 20
9+ 48
10+ 47
11+ 24
12+ 23
13+ 49
14+ 45
15+ 19
16+ 38
17+ 39
18+ 11
19+ 1
20+ 32
21+ 25
22+ 35
23+ 8
24+ 17
25+ 7
26+ 9
27+ 4
28+ 2
29+ 34
30+ 10
31+ 3
Original file line number Diff line number Diff line change 1+ 56
2+ 139
3+ 42
4+ 28
5+ 3
6+ 87
7+ 142
8+ 57
9+ 147
10+ 6
11+ 117
12+ 95
13+ 2
14+ 112
15+ 107
16+ 54
17+ 146
18+ 104
19+ 40
20+ 26
21+ 136
22+ 127
23+ 111
24+ 47
25+ 8
26+ 24
27+ 13
28+ 92
29+ 18
30+ 130
31+ 141
32+ 37
33+ 81
34+ 148
35+ 31
36+ 62
37+ 50
38+ 80
39+ 91
40+ 33
41+ 77
42+ 1
43+ 96
44+ 100
45+ 9
46+ 120
47+ 27
48+ 97
49+ 60
50+ 102
51+ 25
52+ 83
53+ 55
54+ 118
55+ 19
56+ 113
57+ 49
58+ 133
59+ 14
60+ 119
61+ 88
62+ 124
63+ 110
64+ 145
65+ 65
66+ 21
67+ 7
68+ 74
69+ 72
70+ 61
71+ 103
72+ 20
73+ 41
74+ 53
75+ 32
76+ 44
77+ 10
78+ 34
79+ 121
80+ 114
81+ 67
82+ 69
83+ 66
84+ 82
85+ 101
86+ 68
87+ 84
88+ 48
89+ 73
90+ 17
91+ 43
92+ 140
Original file line number Diff line number Diff line change 1+ fn parse_input ( input : & str ) -> Vec < i32 > {
2+ input. trim ( ) . lines ( ) . map ( |s| s. parse ( ) . unwrap ( ) ) . collect ( )
3+ }
4+
5+ pub fn part_one ( input : & str ) -> usize {
6+ let mut input = parse_input ( input) ;
7+ input. push ( 0 ) ;
8+ input. sort_unstable ( ) ;
9+ input. push ( input. last ( ) . unwrap ( ) + 3 ) ;
10+ let diffs: Vec < i32 > = input. windows ( 2 ) . map ( |v| v[ 1 ] - v[ 0 ] ) . collect ( ) ;
11+ let c1 = diffs. iter ( ) . filter ( |& v| * v == 1 ) . count ( ) ;
12+ let c3 = diffs. iter ( ) . filter ( |& v| * v == 3 ) . count ( ) ;
13+ c1 * c3
14+ }
15+
16+ pub fn part_two ( input : & str ) -> usize {
17+ let mut input = parse_input ( input) ;
18+ input. push ( 0 ) ;
19+ input. sort_unstable ( ) ;
20+ input. push ( input. last ( ) . unwrap ( ) + 3 ) ;
21+
22+ let n = input. len ( ) ;
23+ let mut dp = vec ! [ 0 ; n] ;
24+ dp[ 0 ] = 1 ;
25+ for i in 1 ..n {
26+ for j in ( 0 ..i) . rev ( ) {
27+ if input[ i] - input[ j] <= 3 {
28+ dp[ i] += dp[ j] ;
29+ } else {
30+ break ;
31+ }
32+ }
33+ }
34+ dp[ n - 1 ]
35+ }
36+
37+ #[ cfg( test) ]
38+ mod tests {
39+ use super :: * ;
40+ use crate :: read_example;
41+
42+ #[ test]
43+ fn example ( ) {
44+ let input = read_example ( 10 ) ;
45+ assert_eq ! ( part_one( & input) , 220 ) ;
46+ assert_eq ! ( part_two( & input) , 19208 ) ;
47+ }
48+ }
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ pub mod day06;
99pub mod day07;
1010pub mod day08;
1111pub mod day09;
12+ pub mod day10;
1213
1314pub fn read_as_string ( day : u8 , filename : & str ) -> String {
1415 let filename = format ! ( "inputs/{:02}-{}.txt" , day, filename) ;
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ fn main() {
2525 puzzle!( day07, "Handy Haversacks" ) ,
2626 puzzle!( day08, "Handheld Halting" ) ,
2727 puzzle!( day09, "Encoding Error" ) ,
28+ puzzle!( day10, "Adapter Array" ) ,
2829 ] ;
2930
3031 let filename = match env:: args ( ) . find ( |a| a == "--example" ) {
You can’t perform that action at this time.
0 commit comments