From 6c600fb97df06ea34afdd117847d1439e1f8a0cc Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 3 Dec 2023 20:09:22 +0300 Subject: feat(day-3): add first part solution --- day-3/script.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 day-3/script.ts (limited to 'day-3/script.ts') diff --git a/day-3/script.ts b/day-3/script.ts new file mode 100644 index 0000000..1793e48 --- /dev/null +++ b/day-3/script.ts @@ -0,0 +1,36 @@ +import fs from "fs"; + +const input = fs.readFileSync("./input.txt").toString(); + +const lineLength = input.split("\n")[0].length; + +const chars = input.split("\n").slice(0, -1).join(""); + +const numbers = Array.from(chars.matchAll(/[\d]+/g)).map((match) => ({ + number: Number(match[0]), + position: { + start: match.index || 0, + end: (match.index || 0) + match[0].length, + }, +})); + +const symbols = Array.from(chars.matchAll(/[^\.\d]/g)).map((match) => ({ + symbol: match[0], + position: match.index || 0, +})); + +const result = numbers + .filter((number) => { + return !!symbols.find( + (symbol) => + symbol.position === number.position.start - 1 || + symbol.position === number.position.end || + (symbol.position >= number.position.start - 1 - lineLength && + symbol.position <= number.position.end - lineLength) || + (symbol.position >= number.position.start - 1 + lineLength && + symbol.position <= number.position.end + lineLength), + ); + }) + .reduce((acc, number) => acc + number.number, 0); + +console.log({ result }); -- cgit v1.2.3