| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import { init, reduce, tail, zip, zipWith, 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(
- pipe(as, init, getOrElse<number[]>(() => [])),
- pipe(as, tail, getOrElse<number[]>(() => []))
- ),
- map(([a, b]) => lt(Ord)(a, b)),
- reduce(0, (acc, n) => n ? acc + 1 : acc)
- );
- const increases = pipe(
- depthMeasurements,
- increaseCount
- );
- console.log("the number of times a depth measurement increases", increases);
- const slidingWindows = (as: number[]): [number, number, number][] => pipe(
- zip(
- zip(
- pipe(as, init, getOrElse<number[]>(() => [])),
- pipe(as, tail, getOrElse<number[]>(() => []))
- ),
- pipe(as, tail, chain(tail), getOrElse<number[]>(() => [])),
- ),
- map(([[a, b], c]) => [a, b, c])
- );
- const slidingWindowSumsIncreases = pipe(
- depthMeasurements,
- slidingWindows,
- map(concatAll(MonoidSum)),
- increaseCount
- );
- console.log("the number of times a 3 length sliding window measurement sum increases", slidingWindowSumsIncreases);
|