summaryrefslogtreecommitdiff
path: root/day-3
diff options
context:
space:
mode:
Diffstat (limited to 'day-3')
-rw-r--r--day-3/script.ts16
1 files changed, 10 insertions, 6 deletions
diff --git a/day-3/script.ts b/day-3/script.ts
index 1793e48..f693928 100644
--- a/day-3/script.ts
+++ b/day-3/script.ts
@@ -19,10 +19,11 @@ const symbols = Array.from(chars.matchAll(/[^\.\d]/g)).map((match) => ({
position: match.index || 0,
}));
-const result = numbers
- .filter((number) => {
- return !!symbols.find(
- (symbol) =>
+const result = symbols
+ .filter((symbol) => symbol.symbol === "*")
+ .reduce((acc, symbol) => {
+ const adjacentNumbers = numbers.filter(
+ (number) =>
symbol.position === number.position.start - 1 ||
symbol.position === number.position.end ||
(symbol.position >= number.position.start - 1 - lineLength &&
@@ -30,7 +31,10 @@ const result = numbers
(symbol.position >= number.position.start - 1 + lineLength &&
symbol.position <= number.position.end + lineLength),
);
- })
- .reduce((acc, number) => acc + number.number, 0);
+
+ return adjacentNumbers.length === 2
+ ? acc + adjacentNumbers[0].number * adjacentNumbers[1].number
+ : acc;
+ }, 0);
console.log({ result });