Skip to content

Commit

Permalink
Day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
kzlsakal committed Dec 15, 2020
1 parent 2117191 commit 777475c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
34 changes: 34 additions & 0 deletions challenges/2020/15.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const solvePuzzle = (input, endTurn = 30000000) => {
input = input.split(',').map((n, i) => [Number(n), i + 1]);
const memo = new Map(input);

let turn = input.length;
let lastNum = Number(input[input.length - 1]);

while (turn < endTurn) {
const age = memo.get(lastNum);

memo.set(lastNum, turn);

lastNum = age ? turn - age : 0;

turn += 1;
}
return lastNum;
};

// eslint-disable-next-line no-undef
test('Puzzle Result', (done) => {
const fs = require('fs');
const path = require('path');
const currentDay = path.basename(__filename).split('.')[0];

fs.readFile(`${__dirname}/inputs/${currentDay}.txt`, (err, input) => {
if (err) {
throw err;
}
const result = solvePuzzle(input.toString());
console.log('\x1b[1m\x1b[31m%s\x1b[0m', 'Result:', result);
done();
});
});
1 change: 1 addition & 0 deletions challenges/2020/inputs/15.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7,14,0,17,11,1,2

0 comments on commit 777475c

Please sign in to comment.