| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import { reduce } from "fp-ts/lib/Array";
- import { pipe } from "fp-ts/lib/function";
- import { moves } from "./input";
- import { moves as testMoves } from "./input.test";
- const position = pipe(
- moves,
- reduce([0, 0], ([horizontal, depth], [dir, len]) => {
- switch (dir) {
- case "forward":
- return [horizontal + len, depth];
- case "down":
- return [horizontal, depth + len];
- case "up":
- return [horizontal, depth - len];
- default:
- return [horizontal, depth];
- }
- })
- );
- console.log("the final position is", position);
- console.log("the product of position coordinates is", position[0] * position[1]);
- const positionWithAim = pipe(
- moves,
- reduce([0, 0, 0], ([aim, horizontal, depth], [dir, len]) => {
- switch (dir) {
- case "down":
- return [aim + len, horizontal, depth];
- case "up":
- return [aim - len, horizontal, depth];
- case "forward":
- return [aim, horizontal + len, depth + (aim * len)];
- default:
- return [aim, horizontal, depth];
- }
- })
- );
- console.log("the final position with aim is", positionWithAim);
- console.log("the product of position coordinates is", positionWithAim[1] * positionWithAim[2]);
|