Day 13
authorUmmon <greg.burri@gmail.com>
Wed, 13 Dec 2017 07:54:33 +0000 (08:54 +0100)
committerUmmon <greg.burri@gmail.com>
Wed, 13 Dec 2017 07:54:33 +0000 (08:54 +0100)
AdventOfCode2017/AdventOfCode2017.fsproj
AdventOfCode2017/Day13.fs [new file with mode: 0644]
AdventOfCode2017/Program.fs
Tests/Day13 tests.fs [new file with mode: 0644]
Tests/Tests.fsproj

index da66644..074c067 100644 (file)
@@ -25,7 +25,7 @@
     <PlatformTarget>AnyCPU</PlatformTarget>
     <DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
     <Prefer32Bit>true</Prefer32Bit>
-    <StartArguments>12</StartArguments>
+    <StartArguments>13</StartArguments>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
@@ -68,6 +68,7 @@
     <Compile Include="Day10.fs" />
     <Compile Include="Day11.fs" />
     <Compile Include="Day12.fs" />
+    <Compile Include="Day13.fs" />
     <Compile Include="Program.fs" />
     <None Include="App.config" />
     <Content Include="Data\day1.input">
     <Content Include="Data\day12.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
+    <Content Include="Data\day13.input">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
     <Content Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
diff --git a/AdventOfCode2017/Day13.fs b/AdventOfCode2017/Day13.fs
new file mode 100644 (file)
index 0000000..127d18f
--- /dev/null
@@ -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
index 933a60a..0d32674 100644 (file)
@@ -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 (file)
index 0000000..5d3437e
--- /dev/null
@@ -0,0 +1,32 @@
+namespace AdventOfCode2017.Tests
+
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day13 tests`` (output : ITestOutputHelper) =
+
+    [<Fact>]
+    let ``(Part1) From web page`` () =
+        let input =
+            [|
+                "0: 3"
+                "1: 2"
+                "4: 4"
+                "6: 4"
+            |]
+        Day13.severity (Day13.parseInput input) |> fst =! 24
+
+
+    [<Fact>]
+    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
index a6097ba..22aeb3a 100644 (file)
@@ -67,6 +67,7 @@
     <Compile Include="Day10 tests.fs" />
     <Compile Include="Day11 tests.fs" />
     <Compile Include="Day12 tests.fs" />
+    <Compile Include="Day13 tests.fs" />
     <Content Include="packages.config" />
     <Content Include="App.config" />
   </ItemGroup>