day 3
This commit is contained in:
parent
7df462c08f
commit
7acfe9c402
1 changed files with 40 additions and 0 deletions
40
day3.ts
Normal file
40
day3.ts
Normal file
|
@ -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());
|
Loading…
Reference in a new issue