-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-2.ts
48 lines (36 loc) · 1.21 KB
/
3-2.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
export default function main(input: string): string {
let sum = 0;
const lines = input.split('\n');
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
for (let j = 0; j < line.length; j++) {
const char = line[j];
if (char === '*') {
const partNums: number[] = [];
for (let i_ = -1; i_ <= 1; i_++) {
for (let j_ = -1; j_ <= 1; j_++) {
if (i_ === 0 && j_ === 0) continue;
const char = lines[i_ + i]?.[j_ + j];
if (char !== undefined && /\d/.test(char)) {
const partNumI = i_ + i;
let partNumJ = j_ + j;
// find the left-most digit of the part number,
// so we can slice from there and match the whole number
while (/\d/.test(lines[partNumI][partNumJ - 1])) {
partNumJ--;
}
const partNum = +lines[partNumI].slice(partNumJ).match(/\d+/)![0];
if (!partNums.includes(partNum)) {
partNums.push(partNum);
}
}
}
}
if (partNums.length === 2) {
sum += partNums[0] * partNums[1];
}
}
}
}
return sum.toString();
}