From 1848efb6b999cafd2a960d09aa091a2bef9a27b1 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 24 Nov 2023 23:56:21 +0300 Subject: feat(day-8): first problem --- day-8/index.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 day-8/index.ts (limited to 'day-8/index.ts') 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(); + +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); -- cgit v1.2.3