From b50b34035b885157b3268816eaad0eaf95411c78 Mon Sep 17 00:00:00 2001 From: Ummon Date: Mon, 11 Dec 2017 09:35:31 +0100 Subject: [PATCH] Use coordinates system from https://www.redblobgames.com/grids/hexagons/ --- AdventOfCode2017/Day11.fs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/AdventOfCode2017/Day11.fs b/AdventOfCode2017/Day11.fs index 400e0eb..df2a86e 100644 --- a/AdventOfCode2017/Day11.fs +++ b/AdventOfCode2017/Day11.fs @@ -1,21 +1,19 @@ module AdventOfCode2017.Day11 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 distance (x, y, z) = (abs x + abs y + abs z) / 2 let destination, furthest = moves.Split ',' |> Seq.fold ( - fun ((x, y), furthest) m -> + fun ((x, y, z), furthest) m -> let next = 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) + | "n" -> (x , y + 1, z - 1) + | "ne" -> (x + 1, y , z - 1) + | "se" -> (x + 1, y - 1, z ) + | "s" -> (x , y - 1, z + 1) + | "sw" -> (x - 1, y , z + 1) + | _ -> (x - 1, y + 1, z ) next, distance next |> max furthest - ) ((0, 0), 0) + ) ((0, 0, 0), 0) distance destination, furthest \ No newline at end of file -- 2.45.2