-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay4.js
87 lines (78 loc) · 2.06 KB
/
Day4.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"use strict"
//https://adventofcode.com/2020/day/4
//load input
const fs = require('fs')
const encoded = fs.readFileSync("/Users/hakizu/Downloads/message.txt", 'utf-8')
const input = encoded.split('\n\n')
//Part1
const passports = input.map(it => parseData(it))
const validPassports = passports.filter(it => {
const amountOfKeys = Object.keys(it).length
return it.cid === undefined ? amountOfKeys === 7 :
amountOfKeys === 8
})
console.log(validPassports.length)
function parseData (string) {
const segments = string.split(/\s/)
const entries = segments.map(it => {
const [key, value] = it.split(':')
return [key, value]
})
const passports = Object.fromEntries(entries)
return passports
}
//Part2
const answer = validPassports.filter(it => {
if (!validateRange(it.byr, 1920, 2002)) {
return
}
if (!validateRange(it.iyr, 2010, 2020)) {
return
}
if (!validateRange(it.eyr, 2020, 2030)) {
return
}
if (!validateHeight(it.hgt, [150, 193], [59, 76])) {
return
}
if (!validateHairColor(it.hcl)) {
return
}
if (!validateEyeColor(it.ecl)) {
return
}
if (!validatePID(it.pid)) {
return
}
return it
})
console.log(answer.length);
function validateRange(arg, min, max) {
const value = parseInt(arg)
if (typeof value !== 'number') {
return false
}
return min <= value && value <= max
}
function validateHeight(arg, cm, inches) {
const type = arg.slice(-2)
if (type === 'cm') {
return validateRange(arg, cm[0], cm[1])
}
if (type === 'in') {
return validateRange(arg, inches[0], inches[1])
}
return
}
function validateHairColor(arg) {
const pattern = /^#([0-9a-f]{6})$/
return arg.match(pattern) !== null
}
function validateEyeColor(arg) {
const selection = ['amb', 'blu', 'brn', 'gry', 'grn', 'hzl', 'oth']
return selection.some(it => it === arg.slice(-3))
}
function validatePID(arg) {
const pattern = /^([0-9]{9})$/
return arg.match(pattern) !== null
}