summaryrefslogtreecommitdiff
path: root/day-3/script.ts
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-12-03 20:09:22 +0300
committereug-vs <eugene@eug-vs.xyz>2023-12-03 20:09:22 +0300
commit6c600fb97df06ea34afdd117847d1439e1f8a0cc (patch)
tree142d2d5c7e3521d8a132604d8516c64cc8f10bf7 /day-3/script.ts
parent60740f6e984066f47130b5fdd849eedfeb26bf94 (diff)
downloadaoc-2023-6c600fb97df06ea34afdd117847d1439e1f8a0cc.tar.gz
feat(day-3): add first part solution
Diffstat (limited to 'day-3/script.ts')
-rw-r--r--day-3/script.ts36
1 files changed, 36 insertions, 0 deletions
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 });