generated from mariotacke/template-advent-of-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.js
27 lines (20 loc) · 915 Bytes
/
part1.js
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
module.exports = (input) => {
const schematic = input
.split('\n')
.map((line) => line.trim());
const empty = '.'.repeat(schematic[0].length);
let sumOfPartNumbers = 0;
for (let i = 0; i < schematic.length; i++) {
const matches = schematic[i].matchAll(/(\d+)/g);
for (const match of matches) {
const j = match.index;
const above = (schematic[i - 1] || empty).slice(Math.max(j - 1, 0), j + match[0].length + 1);
const below = (schematic[i + 1] || empty).slice(Math.max(j - 1, 0), j + match[0].length + 1);
const before = schematic[i].slice(j - 1, j) || '.';
const after = schematic[i].slice(j + match[0].length, j + match[0].length + 1) || '.';
const adjacentSymbols = (above + below + before + after).replace(/\d+/g, '.').replace(/\./g, '');
sumOfPartNumbers += adjacentSymbols.length ? +match[0] : 0;
}
}
return sumOfPartNumbers;
};