-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
69 lines (61 loc) · 2.01 KB
/
main.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
const fs = require("fs");
let path = process.argv[1].replace("/main.js", "");
let buffer = fs.readFileSync(`${path}/input.txt`);
let rows = buffer.toString().trimEnd()
.split("\n\n").join("\n=====\n") // replace two line break's with ====='s
.split("\n").join(" ") // replace all line-breaks with space's
.split(" ===== ").join("\n") // replace ====='s with line breaks.
.split("\n");
const requiredFields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"];
let validate = (key, value) => {
switch (key) {
case "byr":
return /(19[2-8][0-9]|199[0-9]|200[0-2])/.test(value);
case "iyr":
return /(201[0-9]|2020)/.test(value);
case "eyr":
return /(202[0-9]|2030)/.test(value);
case "hgt":
return /^(1[5-8][0-9]|19[0-3])cm$|^(59|6[0-9]|7[0-6])in$/.test(value);
case "hcl":
return /^#[0-9a-f]{6}$/.test(value);
case "ecl":
return /^(amb|blu|brn|gry|grn|hzl|oth)$/.test(value);
case "pid":
return /^\d{9}$/.test(value);
case "cid":
return true;
}
};
let validsCounter = (puzzleNumber) => {
let validCount = rows.length;
rows.forEach(passport => {
let passportKeyValues = passport.split(" ");
let passportKeys = [];
let valid = true;
passportKeyValues.forEach(passportField => {
let [key, value] = passportField.split(":");
passportKeys.push(key);
// Check that all required fields are valid
if (puzzleNumber === 2) {
if (valid && !validate(key, value)) {
valid = false;
}
}
});
// Check that all required fields are present
for (let i = 0; i < requiredFields.length; i++) {
if (!passportKeys.includes(requiredFields[i])) {
valid = false;
break;
}
}
// Reduce valid passports count if not valid
if (!valid) {
validCount--;
}
});
return validCount;
};
console.log(`Valid passports: ${validsCounter(1)} out of ${rows.length} (part 1)`);
console.log(`Valid passports: ${validsCounter(2)} out of ${rows.length} (part 2)`);