diff --git a/src/2020/day10/input b/src/2020/day10/input new file mode 100644 index 0000000..4e0b54a --- /dev/null +++ b/src/2020/day10/input @@ -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 \ No newline at end of file diff --git a/src/2020/day10/part1.ts b/src/2020/day10/part1.ts new file mode 100644 index 0000000..4cdc45d --- /dev/null +++ b/src/2020/day10/part1.ts @@ -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; +}; diff --git a/src/2020/day10/part2.ts b/src/2020/day10/part2.ts new file mode 100644 index 0000000..685e3dc --- /dev/null +++ b/src/2020/day10/part2.ts @@ -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); +}; diff --git a/src/2020/day10/test.spec.ts b/src/2020/day10/test.spec.ts new file mode 100644 index 0000000..d364fce --- /dev/null +++ b/src/2020/day10/test.spec.ts @@ -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); + }); + }); +}); diff --git a/src/2020/day10/test_input b/src/2020/day10/test_input new file mode 100644 index 0000000..be5c492 --- /dev/null +++ b/src/2020/day10/test_input @@ -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 \ No newline at end of file