import * as A from "fp-ts/Array"; import * as RA from "fp-ts/ReadonlyArray"; import * as S from "fp-ts/string"; import * as N from "fp-ts/number"; import { flow, pipe } from "fp-ts/lib/function"; import { heightmap } from "./input"; // import { heightmap } from "./input.test"; import { concatAll } from "fp-ts/lib/Monoid"; type HeightMap = ReadonlyArray>; const parseHeightMap = (input: string): HeightMap => pipe( input, S.split("\n"), RA.map(flow(S.split(""), RA.map(parseInt))) ); const localMinima = (heightMap: HeightMap): number[] => { const minima: number[] = []; for (let i = 0; i < heightMap.length; i++) { for (let j = 0; j < heightMap[i].length; j++) { const up = heightMap[i - 1]?.[j] ?? Number.MAX_VALUE; const down = heightMap[i + 1]?.[j] ?? Number.MAX_VALUE; const left = heightMap[i]?.[j - 1] ?? Number.MAX_VALUE; const right = heightMap[i]?.[j + 1] ?? Number.MAX_VALUE; const value = heightMap[i][j]; if (value < up && value < down && value < left && value < right) { minima.push(value); } } } return minima; } const sumOfRiskLevels = pipe( parseHeightMap(heightmap), localMinima, A.map(n => n + 1), concatAll(N.MonoidSum) ); console.log(sumOfRiskLevels);