Day 15 (late, lol)

This commit is contained in:
Joshua Seigler 2020-12-16 16:47:00 +00:00
parent d94ca60a36
commit bb39322b2f
4 changed files with 95 additions and 8 deletions

View file

@ -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)
}

View file

@ -0,0 +1 @@
9,12,1,4,17,0,18

View file

@ -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()

View file

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<title>Solution Viewer</title>
<style>
html, body { font-family: sans-serif; }
pre { border-radius: 0.5em; padding: 0.5em; background: #eee; }
</style>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body>
<div id="viewer">
<h1>Solution Viewer ({{ solutionTitle }})</h1>
<p>For interesting problems; this page can be used as a dynamic viewer.</p>
<h3><a href="./input.txt">input.txt</a></h3>
<pre><code>{{ inputText }}</code></pre>
<h3><a href="./solution.js">solution.js</a></h3>
<pre><code>{{ solutionText }}</code></pre>
</div>
<script>
const app = new Vue({
el: '#viewer',
data: () => {
return {
solutionText: '[Loading]',
inputText: '[Loading]'
}
},
computed: {
solutionTitle() {
const parts = (document.location + '').split('/')
return parts.reverse()[1]
}
},
async mounted () {
this.solutionText = (await axios.get('./solution.js')).data
this.inputText = (await axios.get('./input.txt')).data
}
})
</script>
</body>
</html>