diff options
author | 2020-12-07 23:03:50 +0100 | |
---|---|---|
committer | 2020-12-07 23:03:50 +0100 | |
commit | ca03c82b68cea23752d78dc42aa5ab8c2ecb7ec7 (patch) | |
tree | 23d2be6d35c13f293c9257395072c1f4bdab421c /day4/day4-2.js | |
parent | 3700a9063725b001267730d6586d9cf905476cbb (diff) | |
download | advent_of_code_2020-ca03c82b68cea23752d78dc42aa5ab8c2ecb7ec7.tar.gz advent_of_code_2020-ca03c82b68cea23752d78dc42aa5ab8c2ecb7ec7.tar.bz2 advent_of_code_2020-ca03c82b68cea23752d78dc42aa5ab8c2ecb7ec7.zip |
day4
Diffstat (limited to 'day4/day4-2.js')
-rw-r--r-- | day4/day4-2.js | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/day4/day4-2.js b/day4/day4-2.js new file mode 100644 index 0000000..d9b23b8 --- /dev/null +++ b/day4/day4-2.js @@ -0,0 +1,41 @@ +const data = require('./data') +const rules = (o) => { + switch(o[0]) { + case 'byr': + return parseInt(o[1]) >= 1920 && parseInt(o[1]) <= 2002 ? o[0] : false + case 'iyr': + return parseInt(o[1]) >= 2010 && parseInt(o[1]) <= 2020 ? o[0] : false + case 'eyr': + return parseInt(o[1]) >= 2020 && parseInt(o[1]) <= 2030 ? o[0] : false + case 'hgt': + if (o[1].indexOf('cm') > -1) { + const h = parseInt(o[1].replace('cm','')) + if (h >= 150 && h <= 193) return o[0] + } else if (o[1].indexOf('in') > -1) { + const h = parseInt(o[1].replace('in','')) + if (h >= 59 && h <= 76) return o[0] + } + return false + case 'hcl': + return /#([a-f]|[A-F]|[0-9]){6}\b/.test(o[1]) ? o[0] : false + case 'ecl': + return ['amb','blu','brn','gry','grn','hzl','oth'].includes(o[1]) ? o[0] : false + case 'pid': + return /^\d{9}$/.test(o[1]) ? o[0] : false + default: + return false + } +} + +const req = ['byr','iyr','eyr','hgt','hcl','ecl','pid'] +const valid = data + .replace(/\n\r/g, "\n") + .split(/\n{2,}/g) + .map(s => s.replace(/\n/g,"").split(' ')) + .map(a => a.filter(Boolean)) + .map(a => a.map(s => s.split(':'))) + .map(o => req.every(r => o.map(rules).includes(r))) + .filter(v => v === true) + .length + +console.log(valid) |