Add 2024/day3/part1.js

This commit is contained in:
x0x7 2024-12-03 10:03:03 -05:00
parent 601c8e0af7
commit e44ff953cb
1 changed files with 18 additions and 0 deletions

18
2024/day3/part1.js Normal file
View File

@ -0,0 +1,18 @@
#!/usr/bin/env node
var content = require('fs').readFileSync(process.argv[2],'utf8');
var mulregex = /mul\((\d{1,3}),(\d{1,3})\)/g
var numregex = /\d+/g
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];
}
console.log('Result:',sum);