diff --git a/2024/03/p1/index.js b/2024/03/p1/index.js index 66513f6..2ed21f5 100644 --- a/2024/03/p1/index.js +++ b/2024/03/p1/index.js @@ -1,19 +1,19 @@ -const fs = require('fs'); +const fs = require('node:fs'); const readline = require('readline'); async function processLineByLine() { - const fileStream = fs.createReadStream('input.txt'); + const fileStream = fs.createReadStream('../input.txt'); const rl = readline.createInterface({ input: fileStream, crlfDelay: Infinity }); - let t = 0 + let t = 0 for await (const line of rl) { - let m = line.match(/mul\([0-9]{1,3}, ?[0-9]{1,3}\)/g); - m = m.map(e => e.split('(')[1].split(')')[0].split(',')) - .forEach(([a,b]) => t+=-a*-b) + let m = line.match(/mul\([0-9]{1,3}, ?[0-9]{1,3}\)/g); + m = m.map(e => e.split('(')[1].split(')')[0].split(',')) + .forEach(([a, b]) => t += -a * -b) } console.log(t); } diff --git a/2024/03/p2/index.js b/2024/03/p2/index.js new file mode 100644 index 0000000..06de9f4 --- /dev/null +++ b/2024/03/p2/index.js @@ -0,0 +1,32 @@ +const fs = require('node:fs'); +const readline = require('readline'); + +async function processLineByLine() { + const fileStream = fs.createReadStream('../input.txt'); + + const rl = readline.createInterface({ + input: fileStream, + crlfDelay: Infinity + }); + + let t = 0 + let foo_do = true; + for await (const line of rl) { + let m = line.match(/(?:mul\([0-9]{1,3}, ?[0-9]{1,3}\)|do(?:n't)?\(\))/g); + m = m.forEach(e => { + if(e.startsWith('mul') && foo_do) { + let [a,b] = e + .split('(')[1] + .split(')')[0] + .split(',') + + t += -a * -b; + } + if(e.startsWith('do')) foo_do = true; + if(e.startsWith('don')) foo_do = false; + }) + } + console.log(t); +} + +processLineByLine();