summary refs log blame commit diff stats
path: root/day11.ml
blob: 285f7553d41894656cdf8a2dd2829b5c63685b0c (plain) (tree)






































                                                                                    
#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"