Created
December 5, 2020 22:15
-
-
Save zaceno/6358d84d8ab02d6890da1ae0c6be002d to your computer and use it in GitHub Desktop.
advent of code, day4 part 2
This file contains hidden or 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
/* | |
Apparently this script produces too high number of valid passports. Unclear why | |
*/ | |
const required = ['byr','iyr','eyr','hgt','hcl','ecl','pid'] | |
const byrValid = x => { | |
if (!x.match(/^\d{4}$/)) return false | |
if (+x > 2002) return false | |
if (+x < 1920) return false | |
return true | |
} | |
const iyrValid = x => { | |
if (!x.match(/^\d{4}$/)) return false | |
if (+x > 2020) return false | |
if (+x < 2010) return false | |
return true | |
} | |
const eyrValid = x => { | |
if (!x.match(/^\d{4}$/)) return false | |
if (+x > 2030) return false | |
if (+x < 2020) return false | |
return true | |
} | |
const hgtValid = x => { | |
let m = x.match(/^(\d+)(cm|in)$/) | |
if (!m) return false | |
if (m[2] === 'cm') { | |
if (+m[1] < 150) return false | |
if (+m[1] > 193) return false | |
} | |
if (m[2] === 'in') { | |
if (+m[1] < 59) return false | |
if (+m[1] > 76) return false | |
} | |
return true | |
} | |
const hclValid = x => { | |
if (!x.match(/^#[a-f0-9]{6}$/)) return false | |
return true | |
} | |
const eclValid = x => ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth'].includes(x) | |
const pidValid = x => !!x.match(/^\d{9}$/) | |
const validatePassport = p => { | |
for (let i in required) { | |
if (!p._fields.includes(required[i])) { | |
p._reason = 'missing required field: ' + required[i] | |
return false | |
} | |
} | |
if (!byrValid(p.byr)) { | |
p._reason = 'byr invalid: ' + p.byr | |
return false | |
} | |
if (!iyrValid(p.iyr)) { | |
p._reason = 'iyr invalid: ' + p.iyr | |
return false | |
} | |
if (!eyrValid(p.eyr)) { | |
p._reason = 'eyr invalid: ' + p.eyr | |
return false | |
} | |
if (!hclValid(p.hcl)) { | |
p._reason= 'hcl invalid: ' + p.hcl | |
return false | |
} | |
if (!eclValid(p.ecl)) { | |
p._reason = 'ecl invalid: ' + p.ecl | |
return false | |
} | |
if (!pidValid(p.pid)) { | |
p._reason = 'pid invalid: ' + p.pid | |
return false | |
} | |
return true | |
} | |
const fs = require('fs') | |
const inp = fs.readFileSync('./input.txt', 'utf-8') | |
const passports = inp | |
.split('\n\n') | |
.map(p => p | |
.trim() | |
.split(/[\n ]+/) | |
.map(x => x.split(':')) | |
.reduce((o, [k, v]) => { | |
o._fields = o._fields || [] | |
o._fields.push(k) | |
o[k] = v | |
return o | |
}, {}) | |
) | |
const valid = passports.filter(p => validatePassport(p)) | |
const invalid = passports.filter(p => !validatePassport(p)) | |
invalid.forEach(p => console.log(p._reason)) | |
console.log('VALID', valid.length, 'INVALID', invalid.length, 'TOTAL', passports.length) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment