Created
December 4, 2020 09:16
-
-
Save koox00/a4fb45a3e337fb1832f22277db557719 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
var ar = document.getElementsByTagName('pre')[0].textContent.split('\n\n').filter(Boolean) | |
function transform(str) { | |
const a = str.replace(/\s/g, ' ').split(' ') | |
return a.reduce((t, n) => { | |
const [key, val] = n.split(':'); | |
t[key] = val; | |
return t; | |
}, {}) | |
} | |
function validateHeight(s) { | |
const h = parseInt(s); | |
switch(true) { | |
case /cm/.test(s): | |
return h >= 150 && h <= 193; | |
case /in/.test(s): | |
return h >= 59 && h <= 76; | |
}; | |
} | |
function validateColor(s) { | |
if (!s) return false | |
const [,color] = s.split('#'); | |
if (!color) return false; | |
return ( | |
color.length === 6 && | |
!/[^A-Za-z0-9]+/.test(color) | |
) | |
} | |
function validateEcl(s) { | |
return ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'].includes(s); | |
} | |
function validateId(n) { | |
if (!n) return false; | |
return n.length === 9 && Number(n) | |
} | |
function validateRange(n, down, up) { | |
const i = Number(n); | |
return i >= down && i <= up; | |
} | |
function validate(pass) { | |
const { | |
byr, | |
iyr, | |
eyr, | |
hgt, | |
hcl, | |
ecl, | |
pid, | |
cid, | |
} = pass; | |
return ( | |
validateRange(byr, 1920, 2002) && | |
validateRange(iyr, 2010, 2020) && | |
validateRange(eyr, 2020, 2030) && | |
validateHeight(hgt) && | |
validateColor(hcl) && | |
validateEcl(ecl) && | |
validateId(pid) | |
) | |
} | |
ar.filter(Boolean).map(transform).filter(validate).length |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment