day04.ts 986 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import * as O from "fp-ts/Option";
  2. import { findFirst, map, reduce } from "fp-ts/lib/Array";
  3. import { pipe } from "fp-ts/lib/function";
  4. import { draws as testDraws, RawTable, tables as testTables } from "./input.test";
  5. type Value = {
  6. value: number,
  7. marked: boolean
  8. };
  9. type Table = {
  10. lastMarked?: number,
  11. table: Value[][]
  12. }
  13. const calc = (draws: number[]) => (table: Table) => pipe(
  14. draws,
  15. reduce(table, (t, d) => pipe(
  16. [...t.table],
  17. map(findFirst((v: Value) => v.value === d)),
  18. reduce(O.none as O.Option<Value>, (v, o) => pipe(o, O.alt(() => v))),
  19. O.fold<Value, Table>(
  20. () => t,
  21. v => {
  22. v.marked = true;
  23. return {
  24. lastMarked: d,
  25. table: t.table
  26. };
  27. }
  28. )))
  29. );
  30. const results = pipe(
  31. testTables,
  32. map<RawTable, Table>(t => ({
  33. table: pipe(t, map(r => pipe(r, map<number, Value>(v => ({ value: v, marked: false })))))
  34. })),
  35. map(calc(testDraws))
  36. );
  37. console.log("results: ", results[0]);