import { reduce, tail, zip, map } from "fp-ts/lib/Array"; import { pipe } from "fp-ts/lib/function"; import { MonoidSum, Ord } from "fp-ts/lib/number"; import { getOrElse, chain } from "fp-ts/lib/Option"; import { lt } from "fp-ts/lib/Ord"; import { concatAll } from "fp-ts/lib/Monoid"; import { depthMeasurements } from "./input1"; const increaseCount = (as: number[]): number => pipe( zip( as, pipe(as, tail, getOrElse(() => [])) ), map(([a, b]) => lt(Ord)(a, b)), reduce(0, (acc, n) => n ? acc + 1 : acc) ); const increases = pipe( depthMeasurements, increaseCount ); console.log("number of times a depth measurement increases", increases); const slidingWindows = (as: number[]): [number, number, number][] => pipe( zip( zip( as, pipe(as, tail, getOrElse(() => [])) ), pipe(as, tail, chain(tail), getOrElse(() => [])), ), map(([[a, b], c]) => [a, b, c]) ); const slidingWindowSumsIncreases = pipe( depthMeasurements, slidingWindows, map(concatAll(MonoidSum)), increaseCount ); console.log("number of times a 3 length sliding window measurement sum increases", slidingWindowSumsIncreases);