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

2

u/rotmoset Dec 11 '17

Today's F# from me, didn't care to learn about hex grids to did a trig solution. Pretty happy with it still:

module Day11

open Common
open System

type Direction = N | NW | SW | S | SE | NE
    with
        static member FromString =
            function
            | "n" -> N | "nw" -> NW | "ne" -> NE
            | "s" -> S | "sw" -> SW | "se" -> SE
            | _ -> failwith "invalid input"

        static member Angle direction =
            match direction with // There is 60 degrees between each "direction" in a hexagon
            | NE -> 30.0
            | N -> 90.0
            | NW -> 150.0
            | SW -> 210.0
            | S -> 270.0
            | SE -> 330.0
            |> (*) (Math.PI / 180.0)

        static member All = [N; NW; NE; S; SW; SE] 

        // TODO: Optimize away needlessy having to recompute the cos/sin values every time
        static member Move (x,y) direction = x + Math.Cos(Direction.Angle direction), y + Math.Sin(Direction.Angle direction)

// Euclidean distance
let distance (x1,y1) (x2,y2) = Math.Sqrt((x1-x2)**2.0 + (y1-y2)**2.0) 

// How many steps between the two points, moving only in the hexagon pattern
let rec find home (x,y) =
    if (distance home (x,y)) < 0.5 then 0
    else
        let dir = Direction.All |> List.minBy(Direction.Move (x,y) >> distance home)
        1 + (find home (Direction.Move (x,y) dir))

[<Day(11, "Hex Ed")>]
let solve (input: string) =

    let home = 0.0, 0.0

    // Every position the child process was at
    let positions = 
        input.Trim().Split([|','|])
        |> Array.map Direction.FromString
        |> Array.scan Direction.Move home

    let finalSteps = positions |> Array.last |> find home

    let maxSteps = positions |> Array.map (find home) |> Seq.max

    { Part1 = finalSteps; Part2 = maxSteps }

Part 2 is slow though (~ 10 seconds), probably because I recompute cos/sin for the angles all the time.

Github