For a description of this problem, please check the Advent of Code website.
Example Input:
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
Part 1 Solution:
const fs = require('fs');const data = fs.readFileSync('./input.txt', 'utf8');const lines = data.trim().split('\n');let result = 0;lines.forEach(line => {const matches = line.match(/mul\(\d+,\d+\)/g); // I hate regexmatches.forEach(match => {let numbers = match.replace("mul(", "").replace(")", "").split(",") ;result += parseInt(numbers[0]) * parseInt(numbers[1]);})})console.log(result)
Part 2 Solution:
const fs = require('fs');const data = fs.readFileSync('./input.txt', 'utf8');const lines = data.trim().split('\n');let result = 0;let disabled = false;lines.forEach(line => {const matches = line.match(/mul\(\d+,\d+\)|do\(\)|don't\(\)/g); // I really hate regex// note that this is relying on the matches being returned in ordermatches.forEach(match => {if(match == "do()") {disabled = false;} else if(match == "don't()") {disabled = true;} else if (!disabled) {let numbers = match.replace("mul(", "").replace(")", "").split(",") ;result += parseInt(numbers[0]) * parseInt(numbers[1]);}})})console.log(result)