summaryrefslogtreecommitdiff
path: root/day-8/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'day-8/index.ts')
-rw-r--r--day-8/index.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/day-8/index.ts b/day-8/index.ts
new file mode 100644
index 0000000..258b27b
--- /dev/null
+++ b/day-8/index.ts
@@ -0,0 +1,58 @@
+import fs from "fs";
+
+function range(first: number, second?: number) {
+ const start = second ? first : 0;
+ const end = second ? second : first;
+
+ return new Array(end - start)
+ .fill("fuck you js")
+ .map((_wtf, index) => index + start);
+}
+
+const input = fs.readFileSync("input.txt").toString();
+
+const array = input
+ .split("\n")
+ .flatMap((line) => line.split(""))
+ .map(Number);
+
+console.log(array);
+
+const N = Math.sqrt(array.length);
+console.assert(Math.round(N) === N, "Matrix should be square");
+
+const visibilitySet = new Set<number>();
+
+function lookup(startIndex: number, step: number) {
+ range(N).reduce((acc, stepIndex) => {
+ const currentIndex = startIndex + stepIndex * step;
+ const item = array[currentIndex];
+
+ if (item > acc) {
+ visibilitySet.add(currentIndex);
+ return item;
+ }
+ return acc;
+ }, -1);
+}
+
+const edgeSize = N - 2;
+
+// Down
+range(1, 1 + edgeSize).map((index) => lookup(index, N));
+// Up
+range(1 + (N - 1) * N, 1 + edgeSize + (N - 1) * N).map((index) =>
+ lookup(index, -N),
+);
+// Right
+range(edgeSize)
+ .map((v) => N * (v + 1))
+ .map((index) => lookup(index, 1));
+
+// Left
+range(edgeSize)
+ .map((v) => N * (v + 1) + N - 1)
+ .map((index) => lookup(index, -1));
+
+console.log(visibilitySet);
+console.log(Array.from(visibilitySet.values()).length + 4);