From: Greg Burri Date: Sat, 9 Dec 2017 10:55:10 +0000 (+0100) Subject: Day 9 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=a77036afcf8fd96d4567fc656f910a13ce52d814;p=advent_of_code_2017.git Day 9 --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index b0c1b37..d8c9d47 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 8 + 9 pdbonly @@ -64,6 +64,7 @@ + @@ -87,6 +88,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day9.fs b/AdventOfCode2017/Day9.fs new file mode 100644 index 0000000..eba06af --- /dev/null +++ b/AdventOfCode2017/Day9.fs @@ -0,0 +1,13 @@ +module AdventOfCode2017.Day9 + +let score (input : string) = + let rec next (l : int) (lSum : int) (gb : bool) (gbSize : int) = + function + | '{' :: tail when not gb -> next (l + 1) (lSum + l) gb gbSize tail + | '}' :: tail when not gb -> next (l - 1) lSum gb gbSize tail + | '<' :: tail when not gb -> next l lSum true gbSize tail + | '!' :: _ :: tail when gb -> next l lSum gb gbSize tail + | '>' :: tail when gb -> next l lSum false gbSize tail + | a :: tail -> next l lSum gb (gbSize + if gb then 1 else 0) tail + | [] -> lSum, gbSize + List.ofSeq input |> next 1 0 false 0 \ No newline at end of file diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index a53a948..a3d56fb 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -38,6 +38,11 @@ let day8 () = let part1, part2 = Day8.execute input sprintf "part1 = %A, part2 = %A" part1 part2 +let day9 () = + let input = File.ReadAllText "Data/day9.input" + let part1, part2 = Day9.score input + sprintf "part1 = %A, part2 = %A" part1 part2 + let doDay (n : int) = let sw = Diagnostics.Stopwatch () sw.Start () @@ -51,6 +56,7 @@ let doDay (n : int) = | 6 -> day6 () | 7 -> day7 () | 8 -> day8 () + | 9 -> day9 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds diff --git a/Tests/Day9 tests.fs b/Tests/Day9 tests.fs new file mode 100644 index 0000000..8f60226 --- /dev/null +++ b/Tests/Day9 tests.fs @@ -0,0 +1,30 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day9 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Day9.score "{}" |> fst =! 1 + Day9.score "{{{}}}" |> fst =! 6 + Day9.score "{{},{}}" |> fst =! 5 + Day9.score "{{{},{},{{}}}}" |> fst =! 16 + Day9.score "{,,,}" |> fst =! 1 + Day9.score "{{},{},{},{}}" |> fst =! 9 + Day9.score "{{},{},{},{}}" |> fst =! 9 + Day9.score "{{},{},{},{}}" |> fst =! 3 + + [] + let ``(Part2) From web page`` () = + Day9.score "<>" |> snd =! 0 + Day9.score "" |> snd =! 17 + Day9.score "<<<<>" |> snd =! 3 + Day9.score "<{!>}>" |> snd =! 2 + Day9.score "" |> snd =! 0 + Day9.score ">" |> snd =! 0 + Day9.score """<{o"i!a,<{i""" |> snd =! 10 \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index fe9fc9c..0bbea9f 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -63,6 +63,7 @@ +