diff --git a/3/index.js b/3/index.js index 89ac79b..ca613e5 100644 --- a/3/index.js +++ b/3/index.js @@ -1,17 +1,29 @@ const fs = require("fs") -const regEx = /mul\(\d+,\d+\)/g +const regEx = /mul\(\d+,\d+\)|do\(\)|don\'t\(\)/g let total = 0 +let count = true const input = fs.readFileSync("input.txt","utf-8") const result = input.match(regEx) // console.log("hello world") console.log(result) result.forEach((mulString)=>{ - const numRegex= /\d+/g - const numbers = mulString.match(numRegex) - console.log(...numbers) - const aVal =parseInt(numbers[0]) - const bVal=parseInt(numbers[1]) - total += aVal*bVal -}) + if (mulString=="do()"){ + count=true + return + } + if(mulString=="don't()"){ + count=false + return + } + if(count){ + const numRegex= /\d+/g + const numbers = mulString.match(numRegex) + // console.log(...numbers) + const aVal =parseInt(numbers[0]) + const bVal=parseInt(numbers[1]) + total += aVal*bVal + } +}) +// console.log(dontLocations.length) console.log("total: ",total) \ No newline at end of file diff --git a/3/index3a.js b/3/index3a.js new file mode 100644 index 0000000..89ac79b --- /dev/null +++ b/3/index3a.js @@ -0,0 +1,17 @@ +const fs = require("fs") +const regEx = /mul\(\d+,\d+\)/g +let total = 0 +const input = fs.readFileSync("input.txt","utf-8") +const result = input.match(regEx) +// console.log("hello world") +console.log(result) +result.forEach((mulString)=>{ + const numRegex= /\d+/g + const numbers = mulString.match(numRegex) + console.log(...numbers) + const aVal =parseInt(numbers[0]) + const bVal=parseInt(numbers[1]) + total += aVal*bVal +}) + +console.log("total: ",total) \ No newline at end of file