Created
January 22, 2021 14:01
-
-
Save plvhx/28a6117dfb05d3c584174305dbcb6c86 to your computer and use it in GitHub Desktop.
advent of code day 4 part 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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