day06.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import * as A from "fp-ts/Array";
  2. import * as N from "fp-ts/number";
  3. import { pipe } from "fp-ts/lib/function";
  4. import { concatAll } from "fp-ts/lib/Monoid";
  5. import { ages as testAges } from "./input.test";
  6. import { ages as inputAges } from "./input";
  7. type Generations = number[];
  8. const GEN_CYCLES = 9;
  9. const GEN_RESTART_CYCLE = 6;
  10. const DAYS = 256;
  11. const parseAges = (input: string): Generations => pipe(
  12. input.split(","),
  13. A.map(parseInt),
  14. A.reduce(A.replicate(GEN_CYCLES, 0) as Generations, (gens, age) => A.unsafeUpdateAt(age, gens[age] + 1, gens))
  15. );
  16. const generations = parseAges(
  17. // testAges
  18. inputAges
  19. );
  20. const simulateDay = (gens: Generations): Generations => pipe(
  21. gens,
  22. A.rotate(-1),
  23. A.splitAt(GEN_RESTART_CYCLE),
  24. A.reduceWithIndex([] as Generations, (i, gens, part) => i == 0 ? part : A.concat(A.unsafeUpdateAt(0, part[0] + part[2], part))(gens))
  25. );
  26. const simulate = (generations: Generations, days: number): Generations => {
  27. if (days === 0) {
  28. return generations;
  29. }
  30. return simulate(simulateDay(generations), days - 1);
  31. }
  32. const entities = concatAll(N.MonoidSum)(simulate(generations, DAYS));
  33. console.log(entities);