diff --git a/day3.ts b/day3.ts new file mode 100644 index 0000000..c81dbcb --- /dev/null +++ b/day3.ts @@ -0,0 +1,40 @@ +import fs from "node:fs"; + +const input = fs.readFileSync("inputs/day3").toString(); + + +function puzzle1() { + const mul_regex = /mul\(([0-9]{1,3}),([0-9]{1,3})\)/g; + let sum = 0; + let match: RegExpExecArray | null = null; + while ((match = mul_regex.exec(input)) !== null) { + sum += +match[1] * + +match[2]; + } + return sum; +} + +console.log("Puzzle 1", puzzle1()); + +function puzzle2() { + const toggle_regex = /(?:do|don't)\(\)|mul\(([0-9]{1,3}),([0-9]{1,3})\)/g; + let enabled = true; + let sum = 0; + let match: RegExpExecArray | null = null; + while ((match = toggle_regex.exec(input)) !== null) { + switch (match[0]) { + case "do()": + enabled = true; + break; + case "don't()": + enabled = false; + break; + default: + if (!enabled) break; + sum += +match[1] * + +match[2]; + break; + } + } + return sum; +} + +console.log("Puzzle 2", puzzle2());