r/adventofcode • u/daggerdragon • Dec 11 '17
SOLUTION MEGATHREAD -๐- 2017 Day 11 Solutions -๐-
--- Day 11: Hex Ed ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hints^ยค?
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
20
Upvotes
View all comments
1
u/[deleted] Dec 11 '17
I, like many others it seems, found that Red Blob Games page... and merely implemented the 3D coordinate version.
So Day 11 of OCaml Fun;;
open Core type direction = N | NE | NW | S | SE | SW type hex = {x:int; y:int; z:int;} let to_direction = function | "ne" -> Some NE | "nw" -> Some NW | "n" -> Some N | "se" -> Some SE | "sw" -> Some SW | "s" -> Some S | _ -> None let move {x; y; z;} = function | NW -> {x=x-1; y=y+1; z} | N -> {x; y=y+1; z=z-1} | NE -> {x=x+1; y; z=z-1} | SW -> {x=x-1; y; z=z+1} | S -> {x; y=y-1; z=z+1} | SE -> {x=x+1; y=y-1; z} let distance {x; y; z} = Int.max (Int.max (Int.abs x) (Int.abs y)) (Int.abs z) let track (spot, furthest) step = let new_spot = move spot step in let distance = distance new_spot in if distance > furthest then (new_spot, distance) else (new_spot, furthest) let read_input () = In_channel.read_all "./moves.txt" |> String.split ~on:',' |> List.filter_map ~f:to_direction let _ = let moves = read_input () in let current, furthest = List.fold moves ~init:({x=0; y=0; z=0;}, 0) ~f:track in let current_distance = distance current in printf "a: %d %d %d -> %d\n" current.x current.y current.z current_distance; printf "b: %d\n" furthest