Skip to content

Commit d385f99

Browse files
committed
finished day 3
1 parent 68b7ab9 commit d385f99

File tree

5 files changed

+51
-2
lines changed

5 files changed

+51
-2
lines changed

.aoc_tiles/tiles/2024/03.png

9.69 KB
Loading

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ node_modules
44
**/*.js
55
**/*.txt
66
**/input.*
7+
**/raw.*
78

89
.aoc_tiles/*
910
!.aoc_tiles/tiles/

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
<!-- AOC TILES BEGIN -->
22
<h1 align="center">
3-
2024 - 4 ⭐ - TypeScript
3+
2024 - 6 ⭐ - TypeScript
44
</h1>
55
<a href="src/2024/day1.ts">
66
<img src=".aoc_tiles/tiles/2024/01.png" width="161px">
77
</a>
88
<a href="src/2024/day2.ts">
99
<img src=".aoc_tiles/tiles/2024/02.png" width="161px">
1010
</a>
11+
<a href="src/2024/day3.ts">
12+
<img src=".aoc_tiles/tiles/2024/03.png" width="161px">
13+
</a>
1114
<h1 align="center">
1215
2023 - 50 ⭐ - TypeScript
1316
</h1>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"lint": "prettier -w src/**/*.ts",
8-
"run": "tsc -p tsconfig.json && node build/day2.js"
8+
"run": "tsc -p tsconfig.json && node build/day3.js"
99
},
1010
"author": "",
1111
"license": "ISC",

src/2024/day3.ts

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { readFileSync } from 'fs';
2+
3+
const input = readFileSync('<path to input data>').toString();
4+
5+
function part1(data: string) {
6+
const regExp = /mul\((?<left>\d{1,3}),(?<right>\d{1,3})\)/gm;
7+
8+
const matches = data.matchAll(regExp);
9+
10+
let result = 0;
11+
12+
for (let m of matches) {
13+
const { left, right } = m.groups!;
14+
15+
result += Number(left) * Number(right);
16+
}
17+
18+
return result;
19+
}
20+
21+
function part2(data: string) {
22+
const regExp =
23+
/(mul\((?<left>\d{1,3}),(?<right>\d{1,3})\))|(do\(\))|(don't\(\))/gm;
24+
25+
const matches = data.matchAll(regExp);
26+
27+
let result = 0;
28+
let mult = true;
29+
for (let m of matches) {
30+
const [match] = m;
31+
if (match === 'do()') {
32+
mult = true;
33+
} else if (match === "don't()") {
34+
mult = false;
35+
} else if (mult) {
36+
const { left, right } = m.groups!;
37+
38+
result += Number(left) * Number(right);
39+
}
40+
}
41+
return result;
42+
}
43+
44+
console.log(part1(input));
45+
console.log(part2(input));

0 commit comments

Comments
 (0)