-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
267 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Part 1 | ||
const solvePuzzle1 = (input) => { | ||
const histories = parseSequences(input); | ||
return addValues(histories, extrapolateForwards); | ||
}; | ||
|
||
// Part 2 | ||
const solvePuzzle2 = (input) => { | ||
const histories = parseSequences(input); | ||
return addValues(histories, extrapolateBackwards); | ||
}; | ||
|
||
// Helpers | ||
|
||
const addValues = (histories, extraPolate) => { | ||
let sum = 0; | ||
|
||
histories.forEach((history) => { | ||
const sequences = iterate(history); | ||
sum += extraPolate(sequences); | ||
}); | ||
|
||
return sum; | ||
}; | ||
|
||
const getLast = (arr) => arr[arr.length - 1]; | ||
const parseSequences = (input) => input.split('\n').map((l) => l.split(' ').map((n) => Number(n))); | ||
|
||
const iterate = (history) => { | ||
const currentSequences = [history]; | ||
|
||
while (getLast(currentSequences).some((n) => n !== 0)) { | ||
const newSeq = []; | ||
const lastSeq = getLast(currentSequences); | ||
|
||
lastSeq.forEach((n, idx) => { | ||
idx && newSeq.push(n - lastSeq[idx - 1]); | ||
}); | ||
|
||
currentSequences.push(newSeq); | ||
} | ||
|
||
return currentSequences; | ||
}; | ||
|
||
const extrapolateBackwards = (sequences) => { | ||
getLast(sequences).unshift(0); | ||
|
||
for (let i = sequences.length - 2; i >= 0; i--) { | ||
sequences[i].unshift(sequences[i][0] - sequences[i + 1][0]); | ||
} | ||
|
||
return sequences[0][0]; | ||
}; | ||
|
||
const extrapolateForwards = (sequences) => { | ||
getLast(sequences).push(0); | ||
|
||
for (let i = sequences.length - 2; i >= 0; i--) { | ||
sequences[i].push(getLast(sequences[i]) + getLast(sequences[i + 1])); | ||
} | ||
|
||
return getLast(sequences[0]); | ||
}; | ||
|
||
require(__dirname + '/../../utils/test.js').test(__filename, __dirname, solvePuzzle1, '1'); | ||
require(__dirname + '/../../utils/test.js').test(__filename, __dirname, solvePuzzle2, '2'); |
Oops, something went wrong.