tangled
alpha
login
or
join now
tom.sherman.is
/
aoc-2025
0
fork
atom
this repo has no description
0
fork
atom
overview
issues
pulls
pipelines
Day 6
tom.sherman.is
3 months ago
08387b97
c922b017
verified
This commit was signed with the committer's
known signature
.
tom.sherman.is
SSH Key Fingerprint:
SHA256:s683B2DU0yXjYHmKfzMF5t1GryE92wn75ZgRoLJ76HY=
+83
1 changed file
expand all
collapse all
unified
split
src
day06.ts
+83
src/day06.ts
···
1
1
+
import { sumArray } from "../lib/util.ts";
2
2
+
3
3
+
export function solvePart1(input: string) {
4
4
+
const operations = parseInput(input);
5
5
+
console.log(operations);
6
6
+
7
7
+
const solutions = operations.map(({ op, values }) => {
8
8
+
const transformedValues = values.map((val) => Number(val.trim()));
9
9
+
if (op === "+") {
10
10
+
return transformedValues.reduce((sum, val) => sum + val, 0);
11
11
+
} else {
12
12
+
return transformedValues.reduce((prod, val) => prod * val, 1);
13
13
+
}
14
14
+
});
15
15
+
16
16
+
return String(sumArray(solutions));
17
17
+
}
18
18
+
19
19
+
export function solvePart2(input: string) {
20
20
+
const operations = parseInput(input);
21
21
+
22
22
+
const solutions = operations.map(({ op, values }) => {
23
23
+
const valueDigits = values.map((val) =>
24
24
+
String(val).split("").map((char) => char === " " ? null : Number(char))
25
25
+
);
26
26
+
27
27
+
const transformedValues = transposeMatrix(valueDigits).map((col) =>
28
28
+
digitsToNumber(col.filter((digit) => digit !== null))
29
29
+
);
30
30
+
31
31
+
if (op === "+") {
32
32
+
return transformedValues.reduce((sum, val) => sum + val, 0);
33
33
+
} else {
34
34
+
return transformedValues.reduce((prod, val) => prod * val, 1);
35
35
+
}
36
36
+
});
37
37
+
38
38
+
return String(sumArray(solutions));
39
39
+
}
40
40
+
41
41
+
type Operation = {
42
42
+
op: "+" | "*";
43
43
+
values: string[];
44
44
+
};
45
45
+
46
46
+
function parseInput(input: string): Operation[] {
47
47
+
const rows = input.split("\n");
48
48
+
// Remove the last empty row if present
49
49
+
if (rows[rows.length - 1] === "") {
50
50
+
rows.pop();
51
51
+
}
52
52
+
53
53
+
const valuesRows = rows.slice(0, -1);
54
54
+
const operationRow = rows.at(-1)!;
55
55
+
56
56
+
const operations: Operation[] = [];
57
57
+
58
58
+
const opColRegex = /(?:(?<op>\*|\+)\s*\s)/g;
59
59
+
60
60
+
const matches = operationRow.matchAll(opColRegex);
61
61
+
62
62
+
const matchesArray = Array.from(matches);
63
63
+
for (const [index, match] of matchesArray.entries()) {
64
64
+
const isLast = matchesArray.length - 1 === index;
65
65
+
const colWidth = match[0]!.length - Number(!isLast);
66
66
+
const op = match.groups!.op as "+" | "*";
67
67
+
68
68
+
const values = valuesRows.map((row) =>
69
69
+
row.slice(match.index!, match.index! + colWidth)
70
70
+
);
71
71
+
operations.push({ op, values });
72
72
+
}
73
73
+
74
74
+
return operations;
75
75
+
}
76
76
+
77
77
+
function transposeMatrix<T>(matrix: T[][]): T[][] {
78
78
+
return matrix[0]!.map((_, colIndex) => matrix.map((row) => row[colIndex]!));
79
79
+
}
80
80
+
81
81
+
function digitsToNumber(digits: number[]): number {
82
82
+
return digits.reduce((num, digit) => num * 10 + digit, 0);
83
83
+
}