2020-12-04 18:23:18 +00:00
|
|
|
const fs = require('fs');
|
2020-12-05 10:35:52 +00:00
|
|
|
const file = fs.readFileSync(__dirname + '/input.txt').toString('utf-8');
|
2020-12-04 18:23:18 +00:00
|
|
|
const passports = file.split('\n\n');
|
|
|
|
|
|
|
|
const fieldNames = {
|
|
|
|
byr: 'Birth year',
|
|
|
|
iyr: 'Issue year',
|
|
|
|
eyr: 'Expiration year',
|
|
|
|
hgt: 'Height',
|
|
|
|
hcl: 'Hair color',
|
|
|
|
ecl: 'Eye color',
|
|
|
|
pid: 'Passport ID',
|
|
|
|
cid: 'Country ID'
|
|
|
|
}, passportFields = Object.keys(fieldNames);
|
|
|
|
|
|
|
|
const bypassed = [ 'cid' ]; // These are ignored
|
|
|
|
|
|
|
|
let invalid = 0;
|
|
|
|
let invalidStats = {}
|
|
|
|
passportFields.forEach(field => invalidStats[field] = 0);
|
|
|
|
|
|
|
|
passports.forEach(passport => {
|
|
|
|
passport = passport
|
|
|
|
.replace(/\n/g, ' ')
|
|
|
|
.split(' ');
|
|
|
|
|
|
|
|
let inv = false;
|
|
|
|
passportFields.forEach(field => {
|
|
|
|
let h = passport.filter(h => h.split(':')[0] == field);
|
|
|
|
if (h?.[0]) h = h[0].split(':');
|
|
|
|
if (!h[1] && bypassed.indexOf(field) == -1) {
|
|
|
|
inv = true;
|
|
|
|
invalidStats[field] += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (inv) invalid++;
|
|
|
|
});
|
|
|
|
|
|
|
|
let statsStr = ``;
|
|
|
|
for (const [key, value] of Object.entries(invalidStats))
|
|
|
|
statsStr += `${fieldNames[key]}: ${value}\n`;
|
|
|
|
|
|
|
|
console.log(statsStr);
|
|
|
|
console.log(`Invalid passports: ${invalid}`);
|
|
|
|
console.log(`Valid passports: ${passports.length - invalid}`);
|
|
|
|
console.log(`Total passports: ${passports.length}`);
|