karl.hudgell 3 年之前
父节点
当前提交
47f2d9fed1
共有 4 个文件被更改,包括 112 次插入17 次删除
  1. 2 2
      .vscode/launch.json
  2. 90 10
      Day 4/part1_2.js
  3. 18 4
      package-lock.json
  4. 2 1
      package.json

+ 2 - 2
.vscode/launch.json

@@ -61,11 +61,11 @@
         {
             "type": "node",
             "request": "launch",
-            "name": "Day4-Part1",
+            "name": "Day4-Part1&2",
             "skipFiles": [
                 "<node_internals>/**"
             ],
-            "program": "${workspaceFolder}\\Day 4\\part1.js"
+            "program": "${workspaceFolder}\\Day 4\\part1_2.js"
         }
     ]
 }

+ 90 - 10
Day 4/part1.js → Day 4/part1_2.js

@@ -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)

+ 18 - 4
package-lock.json

@@ -4,15 +4,29 @@
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
-    "data2grid": {
-      "version": "1.0.2",
-      "resolved": "https://registry.npmjs.org/data2grid/-/data2grid-1.0.2.tgz",
-      "integrity": "sha512-kUlsgpTXx17NWLX+hgvE6RvdKUAuuw/y5Im2cMceclpSmMRWB9tSMJHL6IJFCAmDsIRNAtWtpXJap8Q2jBNDvA=="
+    "deepdash": {
+      "version": "5.3.2",
+      "resolved": "https://registry.npmjs.org/deepdash/-/deepdash-5.3.2.tgz",
+      "integrity": "sha512-Qm2IUr6GsPJQJNATj2g93VvC6n5raH+WtBQw78LNkgxYmlFLV94/l1NcqlsmuPhJV5+GndIsWcPJ6E69kiFIAg==",
+      "requires": {
+        "lodash": "^4.17.20",
+        "lodash-es": "^4.17.15"
+      }
     },
     "fs": {
       "version": "0.0.1-security",
       "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
       "integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ="
+    },
+    "lodash": {
+      "version": "4.17.20",
+      "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz",
+      "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA=="
+    },
+    "lodash-es": {
+      "version": "4.17.15",
+      "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.15.tgz",
+      "integrity": "sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ=="
     }
   }
 }

+ 2 - 1
package.json

@@ -9,6 +9,7 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
-    "fs": "0.0.1-security"
+    "fs": "0.0.1-security",
+    "lodash": "^4.17.20"
   }
 }