AdventOfCodeSolutions/2024/day3/part2.js

25 lines
659 B
JavaScript
Raw Permalink Normal View History

2024-12-03 16:53:09 -05:00
#!/usr/bin/env node
var content = require('fs').readFileSync(process.argv[2],'utf8');
//content = `do()${content}don't()`;
//var doregex = /do\(\).+don't\(\)/g
var mulregex = /mul\((\d{1,3}),(\d{1,3})\)/g
var numregex = /\d+/g
//var blocks = content.match(doregex);
function content2sum(content) {
var muls = content.match(mulregex);
var sum=0;
for(var mul of muls) {
var digits = mul.match(numregex);
var values = digits.map(i=>parseInt(i));
//console.log({digits,values});
sum+=values[0]*values[1];
}
return sum;
}
var sum=content.split('do()').map(i=>i.split("don't()")[0]).map(content2sum).reduce((a,b)=>a+b);
console.log('Result:',sum);