mirror of
https://github.com/seigler/advent-of-code-2020
synced 2025-07-27 08:16:08 +00:00
day 4 solved
This commit is contained in:
parent
9db2a69230
commit
407eaf921e
3 changed files with 1239 additions and 0 deletions
1121
solutions/day4/input.txt
Normal file
1121
solutions/day4/input.txt
Normal file
File diff suppressed because it is too large
Load diff
75
solutions/day4/solution.js
Normal file
75
solutions/day4/solution.js
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
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').map(
|
||||||
|
line => line.replace(/\n/g, ' ').split(' ').reduce((acc, cur) => {
|
||||||
|
const [key, value] = cur.split(':')
|
||||||
|
return { ...acc, [key]: value }
|
||||||
|
}, {})
|
||||||
|
)
|
||||||
|
|
||||||
|
await solveForFirstStar(input)
|
||||||
|
await solveForSecondStar(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
const required = [
|
||||||
|
'byr',
|
||||||
|
'iyr',
|
||||||
|
'eyr',
|
||||||
|
'hgt',
|
||||||
|
'hcl',
|
||||||
|
'ecl',
|
||||||
|
'pid'
|
||||||
|
]
|
||||||
|
|
||||||
|
const between = (a, b, c) => {
|
||||||
|
return a <= b && b <= c
|
||||||
|
}
|
||||||
|
|
||||||
|
const validators = {
|
||||||
|
byr: x => between(1920, parseInt(x), 2002),
|
||||||
|
iyr: x => between(2010, parseInt(x), 2020),
|
||||||
|
eyr: x => between(2020, parseInt(x), 2030),
|
||||||
|
hgt: x => {
|
||||||
|
const parsed = x.match(/(\d+)(cm|in)/)
|
||||||
|
if (!parsed) return false
|
||||||
|
const [, height, units] = parsed
|
||||||
|
if (units === 'cm') {
|
||||||
|
return between(150, parseInt(height), 193)
|
||||||
|
} else if (units === 'in') {
|
||||||
|
return between(59, parseInt(height), 76)
|
||||||
|
} else return false
|
||||||
|
},
|
||||||
|
hcl: x => /^#[0-9a-f]{6}$/.test(x),
|
||||||
|
ecl: x => ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'].includes(x),
|
||||||
|
pid: x => /^[0-9]{9}$/.test(x)
|
||||||
|
}
|
||||||
|
|
||||||
|
function isValid (line) {
|
||||||
|
for (const key of required) {
|
||||||
|
console.log(Object.keys(line), key)
|
||||||
|
if (!Object.keys(line).includes(key)) { return false }
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
async function solveForFirstStar (input) {
|
||||||
|
const solution = input.filter(isValid).length
|
||||||
|
report('Input:', input)
|
||||||
|
report('Solution 1:', solution)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function solveForSecondStar (input) {
|
||||||
|
const solution = input.filter(doc => {
|
||||||
|
for (const [key, validator] of Object.entries(validators)) {
|
||||||
|
if (!validator(doc[key])) return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}).length
|
||||||
|
report('Solution 2:', solution)
|
||||||
|
}
|
||||||
|
|
||||||
|
run()
|
43
solutions/day4/viewer.html
Normal file
43
solutions/day4/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