diff --git a/index.ts b/index.ts index e809564..3ffc6b4 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,3 @@ -import {runDayEight} from "./src/day_eight"; +import {runDayNine} from "./src/day_nine"; -runDayEight(); \ No newline at end of file +runDayNine(); \ No newline at end of file diff --git a/src/day_nine.test.ts b/src/day_nine.test.ts new file mode 100644 index 0000000..a9e7dc7 --- /dev/null +++ b/src/day_nine.test.ts @@ -0,0 +1,35 @@ +import {Oasis} from "./day_nine"; + +describe('Day Nine', () => { + const input = `0 3 6 9 12 15 +1 3 6 10 15 21 +10 13 16 21 30 45`; + + it.each([ + [[0, 3, 6, 9, 12, 15], 18], + [[1, 3, 6, 10, 15, 21], 28], + [[10, 13, 16, 21, 30, 45], 68] + ])('It should get the next value in the sequence', (sequence, expected) => { + const oasis = new Oasis(input); + expect(oasis.getNextValueInSequence(sequence)).toEqual(expected); + }); + + it('should get the total of the next values in each sequence', () => { + const oasis = new Oasis(input); + expect(oasis.getTotalOfNextValuesInSequences()).toEqual(114); + }); + + it.each([ + [[0, 3, 6, 9, 12, 15], -3], + [[1, 3, 6, 10, 15, 21], 0], + [[10, 13, 16, 21, 30, 45], 5] + ])('It should get the previous value in the sequence', (sequence, expected) => { + const oasis = new Oasis(input); + expect(oasis.getPreviousValueInSequence(sequence)).toEqual(expected); + }); + + it('should get the total of the previous values in each sequence', () => { + const oasis = new Oasis(input); + expect(oasis.getTotalOfPreviousValuesInSequences()).toEqual(2); + }); +}); \ No newline at end of file diff --git a/src/day_nine.ts b/src/day_nine.ts new file mode 100644 index 0000000..7553bba --- /dev/null +++ b/src/day_nine.ts @@ -0,0 +1,44 @@ +import fs from "fs"; + +export class Oasis { + private readonly sequences: number[][]; + + constructor(input: string) { + this.sequences = input.split('\n').map(line => line.split(' ').map(Number)); + } + + public getNextValueInSequence(seq: number[]): number { + if (new Set(seq).size === 1) return seq[0]; + const differences = seq.reduce((acc: number[], curr, i, arr) => { + if (i === 0) return acc; + acc.push(curr - arr[i - 1]); + return acc; + }, []); + return this.getNextValueInSequence(differences) + seq[seq.length - 1]; + } + + public getPreviousValueInSequence(seq: number[]): number { + if (new Set(seq).size === 1) return seq[0]; + const differences = seq.reduce((acc: number[], curr, i, arr) => { + if (i === 0) return acc; + acc.push(curr - arr[i - 1]); + return acc; + }, []); + return seq[0] - this.getPreviousValueInSequence(differences); + } + + public getTotalOfNextValuesInSequences(): number { + return this.sequences.reduce((acc, curr) => acc + this.getNextValueInSequence(curr), 0); + } + + public getTotalOfPreviousValuesInSequences(): number { + return this.sequences.reduce((acc, curr) => acc + this.getPreviousValueInSequence(curr), 0); + } +} + +export const runDayNine = () => { + const input = fs.readFileSync('./inputs/day_nine_input.txt', 'utf-8').trimEnd(); + const oasis = new Oasis(input); + console.log(oasis.getTotalOfNextValuesInSequences()); + console.log(oasis.getTotalOfPreviousValuesInSequences()); +} \ No newline at end of file