module AdventOfCode2017.Day11
-let parseInput (input : string) : string list = input.Split ',' |> List.ofArray
-
-let distanceInHex (moves : string list) =
+let distanceInHex (moves : string) =
let distance (x, y) =
let x, y = abs x, abs y
if y >= x then y + (x - y) / 2 else x
let destination, furthest =
- moves
- |> List.fold (
+ moves.Split ',' |> Seq.fold (
fun ((x, y), furthest) m ->
- let pos =
- match m with
- | "n" -> (x , y + 2)
- | "ne" -> (x + 1, y + 1)
- | "se" -> (x + 1, y - 1)
- | "s" -> (x , y - 2)
- | "sw" -> (x - 1, y - 1)
- | _ -> (x - 1, y + 1)
+ let pos = match m with "n" -> (x, y + 2) | "ne" -> (x + 1, y + 1) | "se" -> (x + 1, y - 1) | "s" -> (x, y - 2) | "sw" -> (x - 1, y - 1) | _ -> (x - 1, y + 1)
pos, distance pos |> max furthest
) ((0, 0), 0)
distance destination, furthest
\ No newline at end of file
[<Fact>]
let ``(Part1) From web page`` () =
- Day11.distanceInHex (Day11.parseInput "ne,ne,ne") |> fst =! 3
- Day11.distanceInHex (Day11.parseInput "ne,ne,sw,sw") |> fst =! 0
- Day11.distanceInHex (Day11.parseInput "ne,ne,s,s") |> fst =! 2
- Day11.distanceInHex (Day11.parseInput "se,sw,se,sw,sw") |> fst =! 3
+ Day11.distanceInHex "ne,ne,ne" |> fst =! 3
+ Day11.distanceInHex "ne,ne,sw,sw" |> fst =! 0
+ Day11.distanceInHex "ne,ne,s,s" |> fst =! 2
+ Day11.distanceInHex "se,sw,se,sw,sw" |> fst =! 3
[<Fact>]
let ``(Part2) From web page`` () =
- Day11.distanceInHex (Day11.parseInput "ne,ne,ne") |> snd =! 3
- Day11.distanceInHex (Day11.parseInput "ne,ne,sw,sw") |> snd =! 2
- Day11.distanceInHex (Day11.parseInput "ne,ne,s,s") |> snd =! 2
- Day11.distanceInHex (Day11.parseInput "se,sw,se,sw,sw") |> snd =! 3
\ No newline at end of file
+ Day11.distanceInHex "ne,ne,ne" |> snd =! 3
+ Day11.distanceInHex "ne,ne,sw,sw" |> snd =! 2
+ Day11.distanceInHex "ne,ne,s,s" |> snd =! 2
+ Day11.distanceInHex "se,sw,se,sw,sw" |> snd =! 3
\ No newline at end of file