+39
day06/part1.ts
+39
day06/part1.ts
···
1
+
import { readFileSync } from "fs";
2
+
3
+
const input = readFileSync("./input.txt", "utf8")
4
+
.split(/\n/)
5
+
.map((line) => line.trim().split(/\s+/));
6
+
7
+
const mathProblems = new Array(input[0].length)
8
+
.fill(null)
9
+
.map((n) => [] as (number | string)[]);
10
+
11
+
for (let i = 0; i < input.length; i++) {
12
+
for (let j = 0; j < input[i].length; j++) {
13
+
mathProblems[j].push(input[i][j]);
14
+
}
15
+
}
16
+
17
+
for (let i = 0; i < mathProblems.length; i++) {
18
+
mathProblems[i] = [
19
+
...mathProblems[i].slice(0, mathProblems[i].length - 1).map(Number),
20
+
mathProblems[i][mathProblems[i].length - 1]
21
+
];
22
+
}
23
+
24
+
let answersSum = 0;
25
+
26
+
for (const mathProblem of mathProblems) {
27
+
answersSum +=
28
+
mathProblem[mathProblem.length - 1] === "+"
29
+
? (mathProblem.slice(0, mathProblem.length - 1) as number[]).reduce(
30
+
(a, c) => a + c,
31
+
0
32
+
)
33
+
: (mathProblem.slice(0, mathProblem.length - 1) as number[]).reduce(
34
+
(a, c) => a * c,
35
+
1
36
+
);
37
+
}
38
+
39
+
console.log(answersSum);