mirror of
https://github.com/seigler/advent-of-code-2020
synced 2025-07-27 08:16:08 +00:00
Day 2 solved
This commit is contained in:
parent
93aea4f013
commit
6eeaabc649
3 changed files with 1093 additions and 0 deletions
1000
solutions/day2/input.txt
Normal file
1000
solutions/day2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
50
solutions/day2/solution.js
Normal file
50
solutions/day2/solution.js
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
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)
|
||||||
|
|
||||||
|
function mapInput (line) {
|
||||||
|
const regex = /(?<num1>[0-9]+)-(?<num2>[0-9]+) (?<letter>[a-z]): (?<password>.+)$/
|
||||||
|
return line.match(regex).groups
|
||||||
|
}
|
||||||
|
|
||||||
|
function XOR (a, b) {
|
||||||
|
return a ? !b : !!b
|
||||||
|
}
|
||||||
|
|
||||||
|
async function run () {
|
||||||
|
const input = (await read(fromHere('input.txt'), 'utf8')).trim().split('\n').map(mapInput)
|
||||||
|
|
||||||
|
await solveForFirstStar(input)
|
||||||
|
await solveForSecondStar(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function solveForFirstStar (input) {
|
||||||
|
const solution = input.filter(
|
||||||
|
({ num1, num2, letter, password }) => {
|
||||||
|
let count = 0
|
||||||
|
for (let i = 0; i < password.length; i++) {
|
||||||
|
if (password.charAt(i) === letter) {
|
||||||
|
count++
|
||||||
|
if (count > num2) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count >= num1
|
||||||
|
}
|
||||||
|
).length
|
||||||
|
report('Input:', input)
|
||||||
|
report('Solution 1:', solution)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function solveForSecondStar (input) {
|
||||||
|
const solution = input.filter(
|
||||||
|
({ num1, num2, letter, password }) => {
|
||||||
|
return XOR(password.charAt(num1 - 1) === letter, password.charAt(num2 - 1) === letter)
|
||||||
|
}
|
||||||
|
).length
|
||||||
|
report('Solution 2:', solution)
|
||||||
|
}
|
||||||
|
|
||||||
|
run()
|
43
solutions/day2/viewer.html
Normal file
43
solutions/day2/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