aoc-2024/day3.ts

41 lines
1,023 B
TypeScript
Raw Normal View History

2024-12-03 22:16:37 -05:00
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());