mirror of
https://github.com/seigler/advent-of-code-2020
synced 2025-07-27 00:06:09 +00:00
Day 15 (late, lol)
This commit is contained in:
parent
d94ca60a36
commit
bb39322b2f
4 changed files with 95 additions and 8 deletions
|
@ -11,11 +11,11 @@ async function run () {
|
||||||
await solveForSecondStar(input)
|
await solveForSecondStar(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
function draw (input) {
|
// function draw (input) {
|
||||||
for (const row of input) {
|
// for (const row of input) {
|
||||||
console.log(row.join(''))
|
// console.log(row.join(''))
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
function neighbors (input, row, col) {
|
function neighbors (input, row, col) {
|
||||||
let total = 0
|
let total = 0
|
||||||
|
@ -48,20 +48,19 @@ function step (input) {
|
||||||
function stabilize (input) {
|
function stabilize (input) {
|
||||||
// console.log('\nInput\n--------')
|
// console.log('\nInput\n--------')
|
||||||
// draw(input)
|
// draw(input)
|
||||||
let rounds = 1; let old; let fresh = input
|
let old; let fresh = input
|
||||||
do {
|
do {
|
||||||
old = fresh
|
old = fresh
|
||||||
fresh = step(old)
|
fresh = step(old)
|
||||||
// console.log(`\nRound ${rounds}\n--------`)
|
// console.log(`\nRound ${rounds}\n--------`)
|
||||||
// draw(fresh)
|
// draw(fresh)
|
||||||
rounds++
|
|
||||||
} while (JSON.stringify(old) !== JSON.stringify(fresh))
|
} while (JSON.stringify(old) !== JSON.stringify(fresh))
|
||||||
return fresh
|
return fresh
|
||||||
}
|
}
|
||||||
|
|
||||||
async function solveForFirstStar (input) {
|
async function solveForFirstStar (input) {
|
||||||
const stable = stabilize(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)
|
report('Solution 1:', filledSeats)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1
solutions/day15/input.txt
Normal file
1
solutions/day15/input.txt
Normal file
|
@ -0,0 +1 @@
|
||||||
|
9,12,1,4,17,0,18
|
44
solutions/day15/solution.js
Normal file
44
solutions/day15/solution.js
Normal 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()
|
43
solutions/day15/viewer.html
Normal file
43
solutions/day15/viewer.html
Normal 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>
|
Loading…
Add table
Add a link
Reference in a new issue