Created
December 4, 2020 13:58
-
-
Save isaacplmann/b2c70c63eb59c7962257e93275b11057 to your computer and use it in GitHub Desktop.
This file contains 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 validators = { | |
byr: (v) => Number(v) > 1919 && Number(v) < 2003, | |
iyr: (v) => Number(v) > 2009 && Number(v) < 2021, | |
eyr: (v) => Number(v) > 2019 && Number(v) < 2031, | |
hgt: (v) => | |
(v.endsWith('cm') && | |
Number(v.slice(0, -2)) > 149 && | |
Number(v.slice(0, -2)) < 194) || | |
(v.endsWith('in') && | |
Number(v.slice(0, -2)) > 58 && | |
Number(v.slice(0, -2)) < 77), | |
hcl: (v) => v.match(/^#[0-9a-f]{6}$/), | |
ecl: (v) => ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'].includes(v), | |
pid: (v) => v.match(/^[0-9]{9}$/), | |
}; | |
const numValid = input.split('\n\n').filter((rawPass) => { | |
const passport = Object.fromEntries( | |
rawPass.split(/[ \n]/).map((item) => item.split(':')) | |
); | |
return Object.keys(validators).every( | |
(key) => passport[key] && validators[key](passport[key]) | |
); | |
}).length; | |
console.log(numValid); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment