93 lines
1.9 KiB
JavaScript
93 lines
1.9 KiB
JavaScript
const { Glib } = require('../../lib');
|
|
|
|
const REQUIRED_FIELDS = [
|
|
'byr',
|
|
'iyr',
|
|
'eyr',
|
|
'hgt',
|
|
'hcl',
|
|
'ecl',
|
|
'pid',
|
|
// 'cid',
|
|
];
|
|
|
|
const VALIDATORS = [
|
|
({ byr }) => {
|
|
if (byr.length !== 4) {
|
|
return false;
|
|
}
|
|
const p = parseInt(byr, 10);
|
|
if (byr !== p.toString()) {
|
|
return false;
|
|
}
|
|
return p >= 1920 && p <= 2002;
|
|
},
|
|
({ iyr }) => {
|
|
if (iyr.length !== 4) {
|
|
return false;
|
|
}
|
|
const p = parseInt(iyr, 10);
|
|
if (iyr !== p.toString()) {
|
|
return false;
|
|
}
|
|
return p >= 2010 && p <= 2020;
|
|
},
|
|
({ eyr }) => {
|
|
if (eyr.length !== 4) {
|
|
return false;
|
|
}
|
|
const p = parseInt(eyr, 10);
|
|
if (eyr !== p.toString()) {
|
|
return false;
|
|
}
|
|
return p >= 2020 && p <= 2030;
|
|
},
|
|
({ hgt }) => {
|
|
const p = parseInt(hgt, 10);
|
|
if (hgt.endsWith('cm')) {
|
|
if (hgt !== p.toString() + 'cm') {
|
|
return false;
|
|
}
|
|
return p >= 150 && p <= 193;
|
|
} else if (hgt.endsWith('in')) {
|
|
if (hgt !== p.toString() + 'in') {
|
|
return false;
|
|
}
|
|
return p >= 59 && p <= 76;
|
|
}
|
|
return false;
|
|
},
|
|
({ hcl }) => {
|
|
return hcl.replace(/#[0-9a-f]{6}/, '') === '';
|
|
},
|
|
({ ecl }) => {
|
|
// console.log(`${hcl} ${hcl.replace(/#[0-9a-f]{6}/, '')}`);
|
|
return ecl.replace(/(amb|blu|brn|gry|grn|hzl|oth)/, '') === '';
|
|
},
|
|
({ pid }) => {
|
|
return pid.replace(/\d{9}/, '') === '';
|
|
},
|
|
];
|
|
|
|
const parse = (input) =>
|
|
Glib.fromSplit(input, '\n\n').map((l) =>
|
|
Object.fromEntries(
|
|
Glib.fromSplit(l.trim().replace(/\s+/g, ' '), ' ').map((pair) =>
|
|
pair.split(':'),
|
|
),
|
|
),
|
|
);
|
|
|
|
module.exports = {
|
|
'1': (input) =>
|
|
parse(input).filter((i) =>
|
|
REQUIRED_FIELDS.glib.every((field) => i.hasOwnProperty(field)),
|
|
).length,
|
|
'2': (input) =>
|
|
parse(input)
|
|
.filter((i) =>
|
|
REQUIRED_FIELDS.glib.every((field) => i.hasOwnProperty(field)),
|
|
)
|
|
.filter((i) => VALIDATORS.glib.every((vf) => vf(i))).length,
|
|
};
|