r/adventofcode • • 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^ยค?

Spoiler


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

253 comments sorted by

View all comments

Show parent comment

1

u/ludic Dec 12 '17
type coord = {x: int; y: int; z: int}
type state = {coord: coord; maxDistance: int}

let solveDay11 (input: string) =
    let distance coord =
        List.max [abs coord.x; abs coord.y; abs coord.z]

    let step (c: coord) = function
        | "ne" -> {c with x=c.x + 1; z=c.z - 1}
        | "n"  -> {c with y=c.y + 1; z=c.z - 1}
        | "nw" -> {c with y=c.y + 1; x=c.x - 1}
        | "sw" -> {c with z=c.z + 1; x=c.x - 1}
        | "s"  -> {c with z=c.z + 1; y=c.y - 1}
        | "se" -> {c with x=c.x + 1; y=c.y - 1}
        | _ -> failwith "Wrong direction"

    let folder state dir =
        let nextCoord = step state.coord dir
        {coord = nextCoord; maxDistance = max (distance nextCoord) state.maxDistance}

    let endState = 
        input.Split(',')
        |> Array.fold folder {coord={x=0; y=0; z=0}; maxDistance=0} 

    (distance endState.coord, endState.maxDistance)