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);