29 lines
No EOL
724 B
JavaScript
29 lines
No EOL
724 B
JavaScript
const fs = require("fs")
|
|
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)=>{
|
|
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) |