From: Ummon Date: Mon, 11 Dec 2017 07:44:54 +0000 (+0100) Subject: Day 11 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=772ccf597a6763b0de388b2208e43ef85a7631c8;p=advent_of_code_2017.git Day 11 --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 6aa72c3..3596929 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 10 + 11 pdbonly @@ -66,6 +66,7 @@ + @@ -92,7 +93,7 @@ PreserveNewest - + PreserveNewest diff --git a/AdventOfCode2017/Day11.fs b/AdventOfCode2017/Day11.fs new file mode 100644 index 0000000..6adba52 --- /dev/null +++ b/AdventOfCode2017/Day11.fs @@ -0,0 +1,24 @@ +module AdventOfCode2017.Day11 + +let parseInput (input : string) : string list = input.Split ',' |> List.ofArray + +let distanceInHex (moves : string list) = + let distance (x, y) = + let x, y = abs x, abs y + if y >= x then y + (x - y) / 2 else x + + let rec next (x, y) further (moves : string list) = + let further' = distance (x, y) |> max further + match moves with + | "n" :: xs -> next (x, y + 2) further' xs + | "ne" :: xs -> next (x + 1, y + 1) further' xs + | "se" :: xs -> next (x + 1, y - 1) further' xs + | "s" :: xs -> next (x, y - 2) further' xs + | "sw" :: xs -> next (x - 1, y - 1) further' xs + | "nw" :: xs -> next (x - 1, y + 1) further' xs + | _ -> (x, y), further' + + let destination, further = next (0, 0) 0 moves + distance destination, further + + diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index f26861b..0c95606 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -47,6 +47,11 @@ let day10 () = let input = "83,0,193,1,254,237,187,40,88,27,2,255,149,29,42,100" sprintf "part1 = %A, part2 = %A" (Day10.knotHash1 input 256) (Day10.knotHash2 input) +let day11 () = + let input = File.ReadAllText "Data/day11.input" |> Day11.parseInput + let part1, part2 = Day11.distanceInHex input + sprintf "part1 = %A, part2 = %A" part1 part2 + let doDay (n : int) = let sw = Diagnostics.Stopwatch () sw.Start () @@ -62,6 +67,7 @@ let doDay (n : int) = | 8 -> day8 () | 9 -> day9 () | 10 -> day10 () + | 11 -> day11 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds diff --git a/Tests/Day11 tests.fs b/Tests/Day11 tests.fs new file mode 100644 index 0000000..4dc12bf --- /dev/null +++ b/Tests/Day11 tests.fs @@ -0,0 +1,23 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +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 + + [] + 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 diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index aec44f8..21e504c 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -65,6 +65,7 @@ +