From 09931c7c4df0ca7cf634342807311270d78b9bbc Mon Sep 17 00:00:00 2001 From: Ummon Date: Mon, 11 Dec 2017 09:12:39 +0100 Subject: [PATCH] Compacting to beat faty's code in term of lines... --- AdventOfCode2017/Day11.fs | 16 +++------------- AdventOfCode2017/Program.fs | 2 +- Tests/Day11 tests.fs | 16 ++++++++-------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/AdventOfCode2017/Day11.fs b/AdventOfCode2017/Day11.fs index 71e9934..e2ccf32 100644 --- a/AdventOfCode2017/Day11.fs +++ b/AdventOfCode2017/Day11.fs @@ -1,24 +1,14 @@ 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 diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 0c95606..fe5954f 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -48,7 +48,7 @@ let day10 () = sprintf "part1 = %A, part2 = %A" (Day10.knotHash1 input 256) (Day10.knotHash2 input) let day11 () = - let input = File.ReadAllText "Data/day11.input" |> Day11.parseInput + let input = File.ReadAllText "Data/day11.input" let part1, part2 = Day11.distanceInHex input sprintf "part1 = %A, part2 = %A" part1 part2 diff --git a/Tests/Day11 tests.fs b/Tests/Day11 tests.fs index 4dc12bf..bb94e16 100644 --- a/Tests/Day11 tests.fs +++ b/Tests/Day11 tests.fs @@ -10,14 +10,14 @@ type ``Day11 tests`` (output : ITestOutputHelper) = [] 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 [] 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 -- 2.45.2