Created
January 23, 2021 13:41
-
-
Save plvhx/802dfb24ac3150b0e47968de6b231535 to your computer and use it in GitHub Desktop.
advent of code day 4 part 2
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) { | |
let tmp = Object.create({}) | |
for (let i = 0; i < splt.length; i++) { | |
splt[i].split(/\s+/).forEach((el) => { | |
let _splt = el.split(':'); | |
tmp[_splt[0]] = _splt[1] | |
}) | |
splt[i] = tmp | |
tmp = Object.create({}) | |
} | |
return splt | |
} | |
function validate(ps) { | |
let result = false | |
result = ps.byr === undefined | |
? false | |
: (/^[0-9]+$/.test(ps.byr) && (Number.parseInt(ps.byr) >= 1920) && (Number.parseInt(ps.byr) <= 2002) | |
? true | |
: false) | |
if (!result) { | |
return false | |
} | |
result = ps.iyr === undefined | |
? false | |
: (/^[0-9]+$/.test(ps.iyr) && (Number.parseInt(ps.iyr) >= 2010) && (Number.parseInt(ps.iyr) <= 2020) | |
? true | |
: false) | |
if (!result) { | |
return false | |
} | |
result = ps.eyr === undefined | |
? false | |
: (/^[0-9]+$/.test(ps.eyr) && (Number.parseInt(ps.eyr) >= 2020) && (Number.parseInt(ps.eyr) <= 2030) | |
? true | |
: false) | |
if (!result) { | |
return false | |
} | |
result = ps.hgt === undefined | |
? false | |
: (!((x) => { | |
if (!/^(?:[0-9]+(?:cm|in))$/.test(x)) { | |
return false | |
} | |
let size = x.slice(0, x.length - 2) | |
let measurement = x.slice(x.length - 2) | |
if ((measurement === 'cm' && (Number.parseInt(size) >= 150) && (Number.parseInt(size) <= 193)) || | |
(measurement === 'in' && (Number.parseInt(size) >= 59) && (Number.parseInt(size) <= 76))) { | |
return true | |
} | |
return false | |
})(ps.hgt) ? false : true) | |
if (!result) { | |
return false | |
} | |
result = ps.hcl === undefined | |
? false | |
: /^(?:\#[0-9a-f]{6})$/.test(ps.hcl) | |
if (!result) { | |
return false | |
} | |
result = ps.ecl === undefined | |
? false | |
: /^(?:amb|blu|brn|gry|grn|hzl|oth)$/.test(ps.ecl) | |
if (!result) { | |
return false | |
} | |
result = ps.pid === undefined | |
? false | |
: /^[0-9]{9}$/.test(ps.pid) | |
if (!result) { | |
return false | |
} | |
return result | |
} | |
function solve(splt) { | |
let numValid = 0 | |
let valid = true | |
splt.forEach((el) => { | |
valid = validate(el) | |
if (valid) { | |
numValid++ | |
} | |
}) | |
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