diff --git a/challenges/2020/15.js b/challenges/2020/15.js new file mode 100644 index 0000000..60c7c28 --- /dev/null +++ b/challenges/2020/15.js @@ -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(); + }); +}); diff --git a/challenges/2020/inputs/15.txt b/challenges/2020/inputs/15.txt new file mode 100644 index 0000000..079cebb --- /dev/null +++ b/challenges/2020/inputs/15.txt @@ -0,0 +1 @@ +7,14,0,17,11,1,2 \ No newline at end of file