summary refs log tree commit diff stats
path: root/day11.ml
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-02-18 15:11:04 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-02-18 15:11:04 -0800
commit74ae7d8e8b05babdc8d3972827036fa6f0b2cade (patch)
tree0e875ee58d6fdf5a568e3f82bf58f590871680ab /day11.ml
parentf3d54ee9e83324da6c2c8fc90ea2b5d5d749b0a5 (diff)
downloadAdventOfCode2017-74ae7d8e8b05babdc8d3972827036fa6f0b2cade.tar.gz
solutions up to day 14
Diffstat (limited to 'day11.ml')
-rw-r--r--day11.ml39
1 files changed, 39 insertions, 0 deletions
diff --git a/day11.ml b/day11.ml
new file mode 100644
index 0000000..285f755
--- /dev/null
+++ b/day11.ml
@@ -0,0 +1,39 @@
+#use "topfind";;
+#thread;;
+#require "core";;
+#require "stdio";;
+
+open Core
+open Stdio
+
+type step = {x: int; y: int; z: int}
+
+let manhattan coords =
+    (abs coords.x + abs coords.y + abs coords.z) / 2
+
+let incrementStep curr = function
+    | "n"  -> {x = curr.x;      y = curr.y + 1; z = curr.z - 1}
+    | "nw" -> {x = curr.x - 1;  y = curr.y + 1; z = curr.z}
+    | "ne" -> {x = curr.x + 1;  y = curr.y;     z = curr.z - 1}
+    | "sw" -> {x = curr.x - 1;  y = curr.y;     z = curr.z + 1}
+    | "se" -> {x = curr.x + 1;  y = curr.y - 1; z = curr.z}
+    | "s"  -> {x = curr.x;      y = curr.y - 1; z = curr.z + 1}
+    | _ -> {x=0; y=0; z=0}
+
+let part1 steps =
+    List.fold ~init:{x = 0; y = 0; z = 0} ~f:incrementStep steps
+    |> manhattan
+
+let part2 steps =
+    List.fold steps 
+              ~init:({x = 0; y = 0; z = 0}, 0) 
+              ~f:(fun (position, maxSteps) step ->
+                  let newStep = incrementStep position step in
+                  (newStep, max (manhattan newStep) maxSteps))
+    |> (fun (_, x) -> x)
+
+let input = In_channel.read_all "day11.txt" |> String.strip |> String.split ~on:',' 
+
+let () = 
+    part1 input |> Printf.printf "%d\n";
+    part2 input |> Printf.printf "%d\n"