advent-of-code-2020/solutions/day2/solution.js
2020-12-02 21:30:58 +00:00

61 lines
1.5 KiB
JavaScript

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.reduce(
(acc, { 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 acc
}
}
}
return acc + (
count >= num1
? 1
: 0
)
}, 0
)
report('Input:', input)
report('Solution 1:', solution)
}
async function solveForSecondStar (input) {
const solution = input.reduce(
(acc, { num1, num2, letter, password }) => {
return acc + (
XOR(
password.charAt(num1 - 1) === letter,
password.charAt(num2 - 1) === letter
)
? 1
: 0
)
}, 0
)
report('Solution 2:', solution)
}
run()