summaryrefslogtreecommitdiffstats
path: root/day4/day4-2.js
blob: d9b23b873f64a02776cfcc6c04f6831d2a1693bf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)