Skip to content

Commit 0905878

Browse files
committed
[ts] 2024:03: Solve day03
1 parent 7525942 commit 0905878

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

2024/day03/day03.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// @ts-ignore - tl_ls is missing @types/node
2+
import { EOL } from "node:os";
3+
// @ts-ignore - tl_ls doesn't know about Bun
4+
const input = (await Bun.file("input").text()).trim().replaceAll(EOL, " ");
5+
6+
let allMulInstructions = 0;
7+
let activeMulInstructions = 0;
8+
9+
let ctx: string = "";
10+
let conditionalsEnabled = true;
11+
for (let char of input) {
12+
ctx += char;
13+
if (ctx.endsWith("do()")) {
14+
conditionalsEnabled = true;
15+
ctx = "";
16+
} else if (ctx.endsWith("don't()")) {
17+
conditionalsEnabled = false;
18+
ctx = "";
19+
} else {
20+
const mulInstruction = ctx.match(/mul\((\d{1,3}),(\d{1,3})\)$/);
21+
if (!mulInstruction) continue;
22+
23+
const result = parseInt(mulInstruction[1]) * parseInt(mulInstruction[2]);
24+
allMulInstructions += result;
25+
if (conditionalsEnabled) {
26+
activeMulInstructions += result;
27+
}
28+
}
29+
}
30+
31+
console.log("Part 1:", allMulInstructions);
32+
console.log("Part 2:", activeMulInstructions);

0 commit comments

Comments
 (0)