mirror of
https://github.com/seigler/advent-of-code-2020
synced 2025-07-27 08:16:08 +00:00
Day 6
This commit is contained in:
parent
ffdc47f77c
commit
b34569db85
3 changed files with 2239 additions and 0 deletions
2161
solutions/day6/input.txt
Normal file
2161
solutions/day6/input.txt
Normal file
File diff suppressed because it is too large
Load diff
35
solutions/day6/solution.js
Normal file
35
solutions/day6/solution.js
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
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('\n\n')
|
||||||
|
|
||||||
|
await solveForFirstStar(input)
|
||||||
|
await solveForSecondStar(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
function yesses (group) {
|
||||||
|
return group.replace(/\s+/gm, '').split('').reduce((acc, cur) => acc.includes(cur) ? acc : [...acc, cur], [])
|
||||||
|
}
|
||||||
|
|
||||||
|
async function solveForFirstStar (input) {
|
||||||
|
let solution = 0
|
||||||
|
for (const group of input) {
|
||||||
|
solution += yesses(group).length
|
||||||
|
}
|
||||||
|
// report('Input:', input);
|
||||||
|
report('Solution 1:', solution)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function solveForSecondStar (input) {
|
||||||
|
let solution = 0
|
||||||
|
for (const group of input) {
|
||||||
|
const numAnswers = group.split('\n').length
|
||||||
|
solution += yesses(group).filter(yes => group.split(yes).length === numAnswers + 1).length
|
||||||
|
}
|
||||||
|
report('Solution 2:', solution)
|
||||||
|
}
|
||||||
|
|
||||||
|
run()
|
43
solutions/day6/viewer.html
Normal file
43
solutions/day6/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