From 1ad14a74a4b397cb0d27398cfc41d16de288417e Mon Sep 17 00:00:00 2001 From: Chris Date: Tue, 3 Dec 2024 20:55:39 +0000 Subject: [PATCH] Day 3 --- 2024/03/index.ts | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 2024/03/index.ts diff --git a/2024/03/index.ts b/2024/03/index.ts new file mode 100644 index 0000000..49193a3 --- /dev/null +++ b/2024/03/index.ts @@ -0,0 +1,29 @@ +const fs = require('fs'); + +let input = fs.readFileSync(__dirname + '/input.txt', 'utf8'); + +let result = 0; + +const expressions: Array = Array.from(input.matchAll(/(do(n't)?)\(\)|mul\((\d{1,3}),(\d{1,3})\)/g)); + +let enabled = true; + +expressions.forEach((match) => { + const expr = match[0]; + + // Part 2 + if (expr.startsWith('do(')) { + enabled = true; + return; + } + if (expr.startsWith('don\'t(')) { + enabled = false; + return + } + // End part 2 + if (enabled) { + result += (parseInt(match[3]) * parseInt(match[4])); + } +}); + +console.log(result);