karl.hudgell hace 4 años
padre
commit
5932965dcd
Se han modificado 2 ficheros con 27 adiciones y 36 borrados
  1. 13 17
      Day 2/part1.js
  2. 14 19
      Day 2/part2.js

+ 13 - 17
Day 2/part1.js

@@ -7,26 +7,22 @@ let invalid = []
 function find_occurences(str, char_to_count) {
     return str.split(char_to_count).length - 1;
 }
-async function main() {
 
-    for (let index = 0; index < data.length; index++) {
-        const element = data[index];
-        let info = element[0].split(' ')
-        let info2 = info[0].split('-')
-        let count = await find_occurences(element[1], info[1])
-        if (count >= info2[0]) {
-            if (count <= info2[1]) {
-                // console.log('valid')
-            } else {
-                invalid.push(element)
-            }
+data.forEach(element => {
+    let info = element[0].split(' ')
+    let info2 = info[0].split('-')
+    let count = find_occurences(element[1], info[1])
+    if (count >= info2[0]) {
+        if (count <= info2[1]) {
+            console.log(element)
         } else {
             invalid.push(element)
         }
-        // console.log('valid')
+    } else {
+        invalid.push(element)
     }
-    let answer = data.length - invalid.length
-    console.log(answer)
-}
+    console.log(element)
+});
 
-main()
+let answer = data.length - invalid.length
+console.log('Valid ' + answer)

+ 14 - 19
Day 2/part2.js

@@ -4,24 +4,19 @@ let data = JSON.parse(fs.readFileSync('./Day 2/data.json'))
 
 let invalid = []
 
-async function main() {
+data.forEach(element => {
+    let info = element[0].split(' ')
+    let info2 = info[0].split('-')
+    let stringSplit = element[1].split('')
 
-    for (let index = 0; index < data.length; index++) {
-        const element = data[index];
-        let info = element[0].split(' ')
-        let info2 = info[0].split('-')
-        let stringSplit = element[1].split('')
-
-        if (stringSplit[info2[0]] === info[1] && stringSplit[info2[1]] === info[1]) {
+    if (stringSplit[info2[0]] === info[1] && stringSplit[info2[1]] === info[1]) {
+        invalid.push(element)
+    } else
+        if (stringSplit[info2[0]] !== info[1] && stringSplit[info2[1]] !== info[1]) {
             invalid.push(element)
-        } else
-            if (stringSplit[info2[0]] !== info[1] && stringSplit[info2[1]] !== info[1]) {
-                invalid.push(element)
-            } else {
-                console.log(element)
-            }
-    }
-    let answer = data.length - invalid.length
-    console.log('Valid ' + answer)
-}
-main()
+        } else {
+            console.log(element)
+        }
+});
+let answer = data.length - invalid.length
+console.log('Valid ' + answer)