File tree Expand file tree Collapse file tree 5 files changed +229
-0
lines changed Expand file tree Collapse file tree 5 files changed +229
-0
lines changed Original file line number Diff line number Diff line change 1+ 8
2+ 131
3+ 91
4+ 35
5+ 47
6+ 116
7+ 105
8+ 121
9+ 56
10+ 62
11+ 94
12+ 72
13+ 13
14+ 82
15+ 156
16+ 102
17+ 12
18+ 59
19+ 31
20+ 138
21+ 46
22+ 120
23+ 7
24+ 127
25+ 126
26+ 111
27+ 2
28+ 123
29+ 22
30+ 69
31+ 18
32+ 157
33+ 75
34+ 149
35+ 88
36+ 81
37+ 23
38+ 98
39+ 132
40+ 1
41+ 63
42+ 142
43+ 37
44+ 133
45+ 61
46+ 112
47+ 122
48+ 128
49+ 155
50+ 145
51+ 139
52+ 66
53+ 42
54+ 134
55+ 24
56+ 60
57+ 9
58+ 28
59+ 17
60+ 29
61+ 101
62+ 148
63+ 96
64+ 68
65+ 25
66+ 19
67+ 6
68+ 67
69+ 113
70+ 55
71+ 40
72+ 135
73+ 97
74+ 79
75+ 48
76+ 159
77+ 14
78+ 43
79+ 86
80+ 36
81+ 41
82+ 85
83+ 87
84+ 119
85+ 30
86+ 108
87+ 80
88+ 152
89+ 158
90+ 151
91+ 32
92+ 78
93+ 150
94+ 95
95+ 3
96+ 52
97+ 49
Original file line number Diff line number Diff line change 1+ export const part1 = ( input : string ) => {
2+ const joltage = [ 0 , ...input . split ( '\n' ) ] . map ( Number ) . sort ( ( a , b ) => a - b ) ;
3+
4+ let oneDiff = 0 ;
5+ let threeDiff = 1 ; // 3 higher than the highest-rated adapter.
6+
7+ joltage . forEach ( ( current , i ) => {
8+ const diff = joltage [ i + 1 ] - current ;
9+ if ( diff === 1 ) oneDiff ++ ;
10+ else if ( diff === 3 ) threeDiff ++ ;
11+ } ) ;
12+
13+ return oneDiff * threeDiff ;
14+ } ;
Original file line number Diff line number Diff line change 1+ const countDistinctWays = ( input : number [ ] ) => {
2+ const lastIndex = input . length - 1 ;
3+ const distinctWays = input . reduce (
4+ ( distinctWays , adapter , i ) => {
5+ // Compare current adapter with the three next neighbours and
6+ // check if the joltage difference less or equal to three.
7+ for ( let j = i + 1 ; j < i + 4 ; j ++ ) {
8+ if ( input [ j ] - adapter <= 3 ) {
9+ distinctWays [ j ] += distinctWays [ i ] ;
10+ }
11+ }
12+ return distinctWays ;
13+ } ,
14+ [ 1 , ...Array ( lastIndex ) . fill ( 0 ) ] // First is always valid
15+ ) ;
16+ return distinctWays [ lastIndex ] ;
17+ } ;
18+
19+ export const part2 = ( input : string ) => {
20+ const adapters = [ 0 , ...input . split ( '\n' ) ] . map ( Number ) . sort ( ( a , b ) => a - b ) ;
21+ adapters . push ( adapters [ adapters . length - 1 ] + 3 ) ; // 3 higher than the highest-rated adapter.
22+
23+ return countDistinctWays ( adapters ) ;
24+ } ;
Original file line number Diff line number Diff line change 1+ import { readInput } from '../../utils' ;
2+
3+ import { part1 } from './part1' ;
4+ import { part2 } from './part2' ;
5+
6+ describe ( 'Advent of Code 2020 - Day 10' , ( ) => {
7+ let input : string ;
8+ let testInput : string ;
9+
10+ beforeAll ( async ( ) => {
11+ input = await readInput ( __dirname + '/input' ) ;
12+ testInput = await readInput ( __dirname + '/test_input' ) ;
13+ } ) ;
14+
15+ describe ( 'part 1' , ( ) => {
16+ it ( 'should output 7*5' , ( ) => {
17+ const input = `16
18+ 10
19+ 15
20+ 5
21+ 1
22+ 11
23+ 7
24+ 19
25+ 6
26+ 12
27+ 4` ;
28+ expect ( part1 ( input ) ) . toBe ( 7 * 5 ) ;
29+ } ) ;
30+ it ( 'should output 22*10 ' , ( ) => {
31+ expect ( part1 ( testInput ) ) . toBe ( 22 * 10 ) ;
32+ } ) ;
33+
34+ it ( 'should output 2112 from input' , ( ) => {
35+ expect ( part1 ( input ) ) . toBe ( 2112 ) ;
36+ } ) ;
37+ } ) ;
38+
39+ describe ( 'part 2' , ( ) => {
40+ it ( 'should output 8' , ( ) => {
41+ const input = `16
42+ 10
43+ 15
44+ 5
45+ 1
46+ 11
47+ 7
48+ 19
49+ 6
50+ 12
51+ 4` ;
52+ expect ( part2 ( input ) ) . toBe ( 8 ) ;
53+ } ) ;
54+
55+ it ( 'should output 19208 ' , ( ) => {
56+ expect ( part2 ( testInput ) ) . toBe ( 19208 ) ;
57+ } ) ;
58+
59+ it ( 'should output 3022415986688 from input' , ( ) => {
60+ expect ( part2 ( input ) ) . toBe ( 3022415986688 ) ;
61+ } ) ;
62+ } ) ;
63+ } ) ;
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
You can’t perform that action at this time.
0 commit comments