diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-12-03 20:17:42 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-12-03 20:17:42 +0300 |
commit | e9d6f8cc5d7e6959dfe60cab2ca859b3e8fc7788 (patch) | |
tree | 1e5b340b0ee02a01a1f42b47fe64c24cc9169813 | |
parent | 6c600fb97df06ea34afdd117847d1439e1f8a0cc (diff) | |
download | aoc-2023-e9d6f8cc5d7e6959dfe60cab2ca859b3e8fc7788.tar.gz |
feat(day-3): add part 2 solution
-rw-r--r-- | day-3/script.ts | 16 |
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 }); |