From 2f87eb624b229c40f0d6ec5c0aeb7ab4ec2061eb Mon Sep 17 00:00:00 2001 From: Ummon Date: Wed, 13 Dec 2017 08:54:33 +0100 Subject: [PATCH] Day 13 --- AdventOfCode2017/AdventOfCode2017.fsproj | 6 ++++- AdventOfCode2017/Day13.fs | 17 +++++++++++++ AdventOfCode2017/Program.fs | 6 +++++ Tests/Day13 tests.fs | 32 ++++++++++++++++++++++++ Tests/Tests.fsproj | 1 + 5 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 AdventOfCode2017/Day13.fs create mode 100644 Tests/Day13 tests.fs diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index da66644..074c067 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 12 + 13 pdbonly @@ -68,6 +68,7 @@ + @@ -100,6 +101,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day13.fs b/AdventOfCode2017/Day13.fs new file mode 100644 index 0000000..127d18f --- /dev/null +++ b/AdventOfCode2017/Day13.fs @@ -0,0 +1,17 @@ +module AdventOfCode2017.Day13 + +open System + +let parseInput (lines : string[]) = + lines + |> Array.map ( + fun line -> + let values = line.Split ([| ':'; ' ' |], StringSplitOptions.RemoveEmptyEntries) + int values.[0], int values.[1] + ) + +let severity (input : (int * int)[]) : int * int = + let severity (f : int -> int -> int) delay = + input |> Array.sumBy (fun (depth, range) -> if (depth + delay) % (2 * range - 2) = 0 then f depth range else 0) + + severity (*) 0, Seq.initInfinite (fun i -> i, severity (+) i) |> Seq.pick (fun (i, s) -> if s = 0 then Some i else None) \ No newline at end of file diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 933a60a..0d32674 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -57,6 +57,11 @@ let day12 () = let part1, part2 = Day12.graphCount input sprintf "part1 = %A, part2 = %A" part1 part2 +let day13 () = + let input = File.ReadAllLines "Data/day13.input" |> Day13.parseInput + let part1, part2 = Day13.severity input + sprintf "part1 = %A, part2 = %A" part1 part2 + let doDay (n : int) = let sw = Diagnostics.Stopwatch () sw.Start () @@ -74,6 +79,7 @@ let doDay (n : int) = | 10 -> day10 () | 11 -> day11 () | 12 -> day12 () + | 13 -> day13 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds diff --git a/Tests/Day13 tests.fs b/Tests/Day13 tests.fs new file mode 100644 index 0000000..5d3437e --- /dev/null +++ b/Tests/Day13 tests.fs @@ -0,0 +1,32 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day13 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + let input = + [| + "0: 3" + "1: 2" + "4: 4" + "6: 4" + |] + Day13.severity (Day13.parseInput input) |> fst =! 24 + + + [] + let ``(Part2) From web page`` () = + let input = + [| + "0: 3" + "1: 2" + "4: 4" + "6: 4" + |] + Day13.severity (Day13.parseInput input) |> snd =! 10 \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index a6097ba..22aeb3a 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -67,6 +67,7 @@ + -- 2.45.2