diff --git a/solutions/day11/solution.js b/solutions/day11/solution.js index 61e79c7..9f2dddc 100644 --- a/solutions/day11/solution.js +++ b/solutions/day11/solution.js @@ -11,11 +11,11 @@ async function run () { await solveForSecondStar(input) } -function draw (input) { - for (const row of input) { - console.log(row.join('')) - } -} +// function draw (input) { +// for (const row of input) { +// console.log(row.join('')) +// } +// } function neighbors (input, row, col) { let total = 0 @@ -48,20 +48,19 @@ function step (input) { function stabilize (input) { // console.log('\nInput\n--------') // draw(input) - let rounds = 1; let old; let fresh = input + let old; let fresh = input do { old = fresh fresh = step(old) // console.log(`\nRound ${rounds}\n--------`) // draw(fresh) - rounds++ } while (JSON.stringify(old) !== JSON.stringify(fresh)) return fresh } async function solveForFirstStar (input) { const stable = stabilize(input) - const filledSeats = stable.flat(2).filter(x => x == '#').length + const filledSeats = stable.flat(2).filter(x => x === '#').length report('Solution 1:', filledSeats) } diff --git a/solutions/day15/input.txt b/solutions/day15/input.txt new file mode 100644 index 0000000..0c5df98 --- /dev/null +++ b/solutions/day15/input.txt @@ -0,0 +1 @@ +9,12,1,4,17,0,18 \ No newline at end of file diff --git a/solutions/day15/solution.js b/solutions/day15/solution.js new file mode 100644 index 0000000..630c5c9 --- /dev/null +++ b/solutions/day15/solution.js @@ -0,0 +1,44 @@ +const path = require('path') +const { read, position } = require('promise-path') +const fromHere = position(__dirname) +const report = (...messages) => console.log(`[${require(fromHere('../../package.json')).logName} / ${__dirname.split(path.sep).pop()}]`, ...messages) + +async function run () { + const input = (await read(fromHere('input.txt'), 'utf8')).trim().split(',').map(x => 1 * x) + + await solveForFirstStar(input) + await solveForSecondStar(input) +} + +async function solveForFirstStar (input) { + const seq = [...input] + const next = () => { + for (let i = 1; i < seq.length; i++) { + if (seq[seq.length - 1 - i] === seq[seq.length - 1]) { + return i + } + } + return 0 + } + while (seq.length < 2020) { + seq.push(next()) + } + + const solution = seq[2019] + // report('Input:', input); + report('Solution 1:', solution) +} + +async function solveForSecondStar (input) { + const lastSeenIndex = new Map(input.slice(0, -1).map((n, i) => [n, i])) + let last = input[input.length - 1] + for (let i = input.length; i < 30000000; i++) { + const next = i - 1 - lastSeenIndex.get(last) || 0 + lastSeenIndex.set(last, i - 1) + last = next + } + + report('Solution 2:', last) +} + +run() diff --git a/solutions/day15/viewer.html b/solutions/day15/viewer.html new file mode 100644 index 0000000..d583fae --- /dev/null +++ b/solutions/day15/viewer.html @@ -0,0 +1,43 @@ + + + + Solution Viewer + + + + + +
+

Solution Viewer ({{ solutionTitle }})

+

For interesting problems; this page can be used as a dynamic viewer.

+

input.txt

+
{{ inputText }}
+

solution.js

+
{{ solutionText }}
+
+ + + \ No newline at end of file