|
@@ -1095,6 +1095,8 @@ iyr:1930 eyr:2024
|
|
|
|
|
|
ecl:oth hcl:#602927 eyr:2025 iyr:2013 hgt:151cm byr:1992 pid:812583062`
|
|
|
|
|
|
+const lodash = require('lodash')
|
|
|
+
|
|
|
const mandatory = [
|
|
|
"byr",
|
|
|
"iyr",
|
|
@@ -1105,20 +1107,98 @@ const mandatory = [
|
|
|
"pid"
|
|
|
]
|
|
|
|
|
|
+const eyeColour = [
|
|
|
+ "amb", "blu", "brn", "gry", "grn", "hzl", "oth"
|
|
|
+]
|
|
|
+
|
|
|
let dataSet = data.split("\n\n");
|
|
|
|
|
|
-let invalid = []
|
|
|
+let valid = []
|
|
|
+
|
|
|
+let valid2 = []
|
|
|
+
|
|
|
+function splitLines(line) {
|
|
|
+ line = line.split(/\s+/)
|
|
|
+ const newMap = line.map(pairItems)
|
|
|
+ return newMap
|
|
|
+}
|
|
|
+
|
|
|
+function pairItems(line) {
|
|
|
+ return line.split(':')
|
|
|
+}
|
|
|
+
|
|
|
+async function part2(validArray) {
|
|
|
+ const map = validArray.map(splitLines);
|
|
|
+ map.forEach(element => {
|
|
|
+ let byr = lodash.find(element, function (el) { return el[0] == mandatory[0]; });
|
|
|
+ let iyr = lodash.find(element, function (el) { return el[0] == mandatory[1]; });
|
|
|
+ let eyr = lodash.find(element, function (el) { return el[0] == mandatory[2]; });
|
|
|
+ let hgt = lodash.find(element, function (el) { return el[0] == mandatory[3]; });
|
|
|
+ let hcl = lodash.find(element, function (el) { return el[0] == mandatory[4]; });
|
|
|
+ let ecl = lodash.find(element, function (el) { return el[0] == mandatory[5]; });
|
|
|
+ let pid = lodash.find(element, function (el) { return el[0] == mandatory[6]; });
|
|
|
+
|
|
|
+ //Birth Year
|
|
|
+ if (byr[1] >= 1920 && byr[1] <= 2002) {
|
|
|
+ //Issue Year
|
|
|
+ if (iyr[1] >= 2010 && iyr[1] <= 2020) {
|
|
|
+ //Expriation Year
|
|
|
+ if (eyr[1] >= 2020 && eyr[1] <= 2030) {
|
|
|
+ //Hair Colour
|
|
|
+ if (hcl[1].match(/^#[0-9a-f]{6}/)) {
|
|
|
+ //Eye Colour
|
|
|
+ if (eyeColour.includes(ecl[1])) {
|
|
|
+ //passport ID
|
|
|
+ if (pid[1].match(/\d{9}/g) && pid[1].length == 9) {
|
|
|
+ //Height
|
|
|
+ // Height in CM
|
|
|
+ if (hgt[1].includes('cm')) {
|
|
|
+ let check = hgt[1].split('cm')
|
|
|
+ if (parseInt(check[0]) >= 150 && parseInt(check[0]) <= 193) {
|
|
|
+ valid2.push(element)
|
|
|
+ }
|
|
|
+ // Height in In
|
|
|
+ } else if (hgt[1].includes('in')) {
|
|
|
+ let check = hgt[1].split('in')
|
|
|
+ if (parseInt(check[0]) >= 59 && parseInt(check[0]) <= 76) {
|
|
|
+ valid2.push(element)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
-dataSet.forEach(data => {
|
|
|
- mandatory.forEach(field => {
|
|
|
- if (data.includes(field)) {
|
|
|
- } else {
|
|
|
- invalid.push(data)
|
|
|
+ });
|
|
|
+ valid2 = [...new Set(valid2)];
|
|
|
+
|
|
|
+ console.log('Part 2 Valid= ' + valid2.length)
|
|
|
+}
|
|
|
+async function part1(dataSet) {
|
|
|
+ dataSet.forEach(async data => {
|
|
|
+ // Check string for mandatory items.
|
|
|
+ if (data.includes(mandatory[0])) {
|
|
|
+ if (data.includes(mandatory[1])) {
|
|
|
+ if (data.includes(mandatory[2])) {
|
|
|
+ if (data.includes(mandatory[3])) {
|
|
|
+ if (data.includes(mandatory[4])) {
|
|
|
+ if (data.includes(mandatory[5])) {
|
|
|
+ if (data.includes(mandatory[6])) {
|
|
|
+ valid.push(data)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
-});
|
|
|
+ valid = [...new Set(valid)];
|
|
|
|
|
|
-invalid = [...new Set(invalid)];
|
|
|
+ console.log("Part 1 Valid= " + valid.length)
|
|
|
+ await part2(valid)
|
|
|
+}
|
|
|
|
|
|
-let answer = dataSet.length - invalid.length
|
|
|
-console.log("Valid=" + answer)
|
|
|
+part1(dataSet)
|