Skip to content

Instantly share code, notes, and snippets.

@plvhx
Created January 22, 2021 14:01
Show Gist options
  • Save plvhx/28a6117dfb05d3c584174305dbcb6c86 to your computer and use it in GitHub Desktop.
Save plvhx/28a6117dfb05d3c584174305dbcb6c86 to your computer and use it in GitHub Desktop.
advent of code day 4 part 1
const fs = require('fs')
const splitted = fs.readFileSync('./input.txt', 'utf-8').split(String.fromCharCode(0x0a).repeat(2))
function normalize(splt) {
for (let i = 0; i < splt.length; i++) {
splt[i] = splt[i].split(/\s+/).map((el) => { return el.split(':')[0] })
}
return splt
}
function solve(splt) {
const mandatory = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid']
let numValid = 0
let valid = false
for (let i = 0; i < splt.length; i++) {
for (let j = 0; j < mandatory.length; j++) {
valid = splt[i].some((x) => x === mandatory[j])
if (!valid) {
break
}
}
if (valid) {
numValid++
}
valid = false
}
return numValid
}
console.log(solve(normalize(splitted)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment