| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- import * as O from "fp-ts/Option";
- import { findFirst, map, reduce } from "fp-ts/lib/Array";
- import { pipe } from "fp-ts/lib/function";
- import { draws as testDraws, RawTable, tables as testTables } from "./input.test";
- type Value = {
- value: number,
- marked: boolean
- };
- type Table = {
- lastMarked?: number,
- table: Value[][]
- }
- const calc = (draws: number[]) => (table: Table) => pipe(
- draws,
- reduce(table, (t, d) => pipe(
- [...t.table],
- map(findFirst((v: Value) => v.value === d)),
- reduce(O.none as O.Option<Value>, (v, o) => pipe(o, O.alt(() => v))),
- O.fold<Value, Table>(
- () => t,
- v => {
- v.marked = true;
- return {
- lastMarked: d,
- table: t.table
- };
- }
- )))
- );
- const results = pipe(
- testTables,
- map<RawTable, Table>(t => ({
- table: pipe(t, map(r => pipe(r, map<number, Value>(v => ({ value: v, marked: false })))))
- })),
- map(calc(testDraws))
- );
- console.log("results: ", results[0]);
|