Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/2020/day10/input
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
8
131
91
35
47
116
105
121
56
62
94
72
13
82
156
102
12
59
31
138
46
120
7
127
126
111
2
123
22
69
18
157
75
149
88
81
23
98
132
1
63
142
37
133
61
112
122
128
155
145
139
66
42
134
24
60
9
28
17
29
101
148
96
68
25
19
6
67
113
55
40
135
97
79
48
159
14
43
86
36
41
85
87
119
30
108
80
152
158
151
32
78
150
95
3
52
49
14 changes: 14 additions & 0 deletions src/2020/day10/part1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const part1 = (input: string) => {
const joltage = [0, ...input.split('\n')].map(Number).sort((a, b) => a - b);

let oneDiff = 0;
let threeDiff = 1; // 3 higher than the highest-rated adapter.

joltage.forEach((current, i) => {
const diff = joltage[i + 1] - current;
if (diff === 1) oneDiff++;
else if (diff === 3) threeDiff++;
});

return oneDiff * threeDiff;
};
24 changes: 24 additions & 0 deletions src/2020/day10/part2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const countDistinctWays = (input: number[]) => {
const lastIndex = input.length - 1;
const distinctWays = input.reduce(
(distinctWays, adapter, i) => {
// Compare current adapter with the three next neighbours and
// check if the joltage difference less or equal to three.
for (let j = i + 1; j < i + 4; j++) {
if (input[j] - adapter <= 3) {
distinctWays[j] += distinctWays[i];
}
}
return distinctWays;
},
[1, ...Array(lastIndex).fill(0)] // First is always valid
);
return distinctWays[lastIndex];
};

export const part2 = (input: string) => {
const adapters = [0, ...input.split('\n')].map(Number).sort((a, b) => a - b);
adapters.push(adapters[adapters.length - 1] + 3); // 3 higher than the highest-rated adapter.

return countDistinctWays(adapters);
};
63 changes: 63 additions & 0 deletions src/2020/day10/test.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {readInput} from '../../utils';

import {part1} from './part1';
import {part2} from './part2';

describe('Advent of Code 2020 - Day 10', () => {
let input: string;
let testInput: string;

beforeAll(async () => {
input = await readInput(__dirname + '/input');
testInput = await readInput(__dirname + '/test_input');
});

describe('part 1', () => {
it('should output 7*5', () => {
const input = `16
10
15
5
1
11
7
19
6
12
4`;
expect(part1(input)).toBe(7 * 5);
});
it('should output 22*10 ', () => {
expect(part1(testInput)).toBe(22 * 10);
});

it('should output 2112 from input', () => {
expect(part1(input)).toBe(2112);
});
});

describe('part 2', () => {
it('should output 8', () => {
const input = `16
10
15
5
1
11
7
19
6
12
4`;
expect(part2(input)).toBe(8);
});

it('should output 19208 ', () => {
expect(part2(testInput)).toBe(19208);
});

it('should output 3022415986688 from input', () => {
expect(part2(input)).toBe(3022415986688);
});
});
});
31 changes: 31 additions & 0 deletions src/2020/day10/test_input
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
28
33
18
42
31
14
46
20
48
47
24
23
49
45
19
38
39
11
1
32
25
35
8
17
7
9
4
2
34
10
3