From c68d7fd09bb33aa370dc3bd6f969c3118419d843 Mon Sep 17 00:00:00 2001 From: x0x7 Date: Tue, 3 Dec 2024 16:53:09 -0500 Subject: [PATCH] Add 2024/day3/part2.js --- 2024/day3/part2.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2024/day3/part2.js diff --git a/2024/day3/part2.js b/2024/day3/part2.js new file mode 100644 index 0000000..347e6af --- /dev/null +++ b/2024/day3/part2.js @@ -0,0 +1,25 @@ +#!/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); \ No newline at end of file