Skip to content

Commit 43427cc

Browse files
committed
Solve 2023 day 4 part 1
1 parent 99c93b2 commit 43427cc

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

2023/Day4/javascript/solve.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const fs = require("node:fs");
2+
const path = require("node:path");
3+
4+
const input = fs
5+
.readFileSync(path.join(__dirname, "../input.txt"), "utf8")
6+
.split("\n")
7+
.filter((line) => line !== "");
8+
9+
const part1 = (cards) => {
10+
const results = [];
11+
for (const card of cards) {
12+
const [_, winnindNumbersString, drawnNumbersString] =
13+
/Card +\d+: ([ \d]+) +\| +([ \d]+)/.exec(card);
14+
const winnindNumbers = winnindNumbersString
15+
.trim()
16+
.split(" ")
17+
.map((num) => Number.parseInt(num, 10))
18+
.filter((num) => !isNaN(num));
19+
20+
const drawnNumbers = drawnNumbersString
21+
.trim()
22+
.split(" ")
23+
.map((num) => Number.parseInt(num, 10))
24+
.filter((num) => !isNaN(num));
25+
26+
let matches = [];
27+
28+
for (const num of drawnNumbers) {
29+
if (winnindNumbers.includes(num)) {
30+
matches.push(num);
31+
}
32+
}
33+
34+
matches.length > 0 && results.push(2 ** (matches.length - 1));
35+
}
36+
37+
return results.reduce((sum, curr) => sum + curr, 0);
38+
};
39+
40+
console.log(part1(input));

0 commit comments

Comments
 (0)