-
Notifications
You must be signed in to change notification settings - Fork 0
/
04.js
40 lines (38 loc) · 971 Bytes
/
04.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
/**
* @param {string} d
*/
const part1 = async d => {
const data = d.split('\n\n').map(e => e.split(/[ \n]+/)).map(e => e.reduce((p, c) => {
c = c.split(':');
p[c[0]] = c[1];
return p;
}, {})).filter(e => {
return (e.byr && e.iyr && e.eyr && e.hgt && e.hcl && e.ecl && e.pid);
});
return data.length;
};
/**
* @param {string} d
*/
const part2 = async d => {
const data = d.split('\n\n').map(e => e.split(/[ \n]+/)).map(e => e.reduce((p, c) => {
c = c.split(':');
p[c[0]] = c[1];
return p;
}, {})).filter(e => {
return (
e.byr && e.byr >= 1920 && e.byr <= 2002 &&
e.iyr && e.iyr >= 2010 && e.iyr <= 2020 &&
e.eyr && e.eyr >= 2020 && e.eyr <= 2030 &&
e.hgt && /(?:(?:59|6[0-9]|7[1-6])in|1(?:[5-8][0-9]|9[0-3])cm)/.test(e.hgt) &&
e.hcl && /#[0-9a-f]{6}/.test(e.hcl) &&
e.ecl && /amb|blu|brn|gry|grn|hzl|oth/.test(e.ecl) &&
e.pid && /\d{9}/.test(e.pid)
);
});
return data.length;
};
module.exports = {
part1,
part2
};