|
|
@@ -0,0 +1,42 @@
|
|
|
+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]);
|