Day Nine
This commit is contained in:
parent
b4052d956f
commit
376c7a4532
4
index.ts
4
index.ts
|
@ -1,3 +1,3 @@
|
|||
import {runDayEight} from "./src/day_eight";
|
||||
import {runDayNine} from "./src/day_nine";
|
||||
|
||||
runDayEight();
|
||||
runDayNine();
|
|
@ -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);
|
||||
});
|
||||
});
|
|
@ -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());
|
||||
}
|
Loading…
Reference in New Issue