Skip to content

Instantly share code, notes, and snippets.

@abrwn
Created December 4, 2020 14:24
Show Gist options
  • Save abrwn/eaf8acb7272c3ae1661d1faa35c0ca1a to your computer and use it in GitHub Desktop.
Save abrwn/eaf8acb7272c3ae1661d1faa35c0ca1a to your computer and use it in GitHub Desktop.
const d4p2 = data => {
const getObj = passport => passport
.split(/[^a-z0-9:#]/)
.reduce((acc, str) => {
const splitString = str.split(':');
acc[splitString[0]] = splitString[1];
return acc;
}, {});
const validate = ({
byr,
iyr,
eyr,
hgt,
hcl,
ecl,
pid,
}) => {
const heightUnit = hgt?.slice(hgt.length - 2);
const heightValue = hgt?.slice(0, hgt.length - 2);
return byr >= 1920 && byr < 2003 &&
iyr >= 2010 && iyr < 2021 &&
eyr >= 2020 && eyr < 2031 &&
(heightUnit === 'cm' && heightValue >= 150 && heightValue < 194 || heightUnit === 'in' && heightValue >= 59 && heightValue < 77) &&
/^#([a-f0-9]{6})$/.test(hcl) &&
['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'].includes(ecl) &&
/^[0-9]{9}$/.test(pid);
}
return data
.split('\n\n')
.map(getObj)
.map(validate)
.filter(Boolean)
.length;
};
d4p2(document.body.innerText)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment