Skip to content

Instantly share code, notes, and snippets.

@plvhx
Created January 20, 2021 16:04
Show Gist options
  • Select an option

  • Save plvhx/b20b0529895e7795cb6fd4193e68cff5 to your computer and use it in GitHub Desktop.

Select an option

Save plvhx/b20b0529895e7795cb6fd4193e68cff5 to your computer and use it in GitHub Desktop.
advent of code 2020 day 2 part 2
const fs = require('fs')
const splitted = fs.readFileSync('./input.txt', 'utf-8').split(String.fromCharCode(0x0a))
function buildDictionary(splt) {
let dict = []
splt.forEach((el, i) => {
if (!el.length) {
return
}
let parts = el.split(String.fromCharCode(0x20))
let minMaxPair = parts[0].split('-')
dict.push({
pivot: parts[1][0],
min: Number.parseInt(minMaxPair[0]),
max: Number.parseInt(minMaxPair[1]),
target: parts[2]
})
})
return dict
}
function solve(dict) {
let numMatches = 0
dict.forEach((el) => {
if ((el.max - 1) > (el.target.length - 1)) {
return
}
if (el.target.charAt(el.min - 1) === el.pivot && el.target.charAt(el.max - 1) === el.pivot) {
return
}
if (el.target.charAt(el.min - 1) !== el.pivot && el.target.charAt(el.max - 1) !== el.pivot) {
return
}
numMatches++
})
return numMatches
}
console.log(solve(buildDictionary(splitted)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment