Skip to content

Commit 8e93bb0

Browse files
committed
03/2024
1 parent 6722c06 commit 8e93bb0

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

2024/03/index.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { getPuzzle } from "../../utils";
2+
3+
const puzzleInput = getPuzzle(__dirname).trim();
4+
5+
const sumMuls = (input: string) => {
6+
return input.match(/mul\((\d+,\d+)\)/gm).reduce((acc, mul) => {
7+
const [num1, num2] = mul.replace("mul(", "").replace(")", "").split(",");
8+
9+
return acc + parseInt(num1) * parseInt(num2);
10+
}, 0);
11+
};
12+
13+
// Part 1
14+
(() => {
15+
console.time("part 1");
16+
const total = sumMuls(puzzleInput);
17+
console.log("part1 total ::", total);
18+
console.timeEnd("part 1");
19+
})();
20+
21+
// Part 2
22+
(() => {
23+
console.time("part 2");
24+
const puzzle = puzzleInput.replaceAll("\n", "");
25+
26+
const DO_OPERATOR = "do()";
27+
const DONT_OPERATOR = "don't()";
28+
29+
const DO_KEYWORD = "%%DO%%";
30+
const DONT_KEYWORD = "%%DON'T%%";
31+
32+
const withDoesAndDonts = puzzle
33+
.split(DONT_OPERATOR)
34+
.flatMap((entry) => [entry, DONT_KEYWORD])
35+
.slice(0, -1)
36+
.flatMap((entry) => {
37+
if (entry === DONT_KEYWORD) {
38+
return entry;
39+
}
40+
41+
if (entry.includes(DO_OPERATOR)) {
42+
return entry
43+
.split(DO_OPERATOR)
44+
.flatMap((subEntry) => [subEntry, DO_KEYWORD])
45+
.slice(0, -1);
46+
}
47+
48+
return entry;
49+
});
50+
51+
let enabled = true;
52+
let newPuzzle = "";
53+
54+
for (let i = 0; i < withDoesAndDonts.length; i++) {
55+
const entry = withDoesAndDonts[i];
56+
57+
if (entry === DO_KEYWORD) {
58+
enabled = true;
59+
} else if (entry === DONT_KEYWORD) {
60+
enabled = false;
61+
} else if (enabled) {
62+
newPuzzle += entry;
63+
}
64+
}
65+
66+
const total = sumMuls(newPuzzle);
67+
68+
console.log("part2 total ::", total);
69+
console.timeEnd("part 2");
70+
})();

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
| Day | Part 1 | Part 2 |
88
| :---------------------------------------: | :----: | :----: |
9+
| [03](https://adventofcode.com/2024/day/3) ||  ✅ |
910
| [02](https://adventofcode.com/2024/day/2) ||  ✅ |
1011
| [01](https://adventofcode.com/2024/day/1) ||  ✅ |
1112

0 commit comments

Comments
 (0)