46 lines
1.3 KiB
JavaScript
46 lines
1.3 KiB
JavaScript
const fs = require('fs');
|
|
const file = fs.readFileSync(__dirname + '/input.txt').toString('utf-8');
|
|
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}`); |