From e9d6f8cc5d7e6959dfe60cab2ca859b3e8fc7788 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 3 Dec 2023 20:17:42 +0300 Subject: feat(day-3): add part 2 solution --- day-3/script.ts | 16 ++++++++++------ 1 file 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 }); -- cgit v1.2.3