Day 19
authorUmmon <greg.burri@gmail.com>
Tue, 19 Dec 2017 08:35:06 +0000 (09:35 +0100)
committerUmmon <greg.burri@gmail.com>
Tue, 19 Dec 2017 08:35:06 +0000 (09:35 +0100)
AdventOfCode2017/AdventOfCode2017.fsproj
AdventOfCode2017/Day19.fs [new file with mode: 0644]
AdventOfCode2017/Program.fs
Tests/Day19 tests.fs [new file with mode: 0644]
Tests/Tests.fsproj

index 2d2f009..81703e0 100644 (file)
@@ -25,7 +25,7 @@
     <PlatformTarget>AnyCPU</PlatformTarget>
     <DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
     <Prefer32Bit>true</Prefer32Bit>
-    <StartArguments>18</StartArguments>
+    <StartArguments>19</StartArguments>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
@@ -37,7 +37,7 @@
     <PlatformTarget>AnyCPU</PlatformTarget>
     <DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
     <Prefer32Bit>true</Prefer32Bit>
-    <StartArguments>16</StartArguments>
+    <StartArguments>19</StartArguments>
   </PropertyGroup>
   <PropertyGroup>
     <MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
@@ -77,6 +77,7 @@
     <Compile Include="Day17.fs" />
     <Compile Include="Day18Part1.fs" />
     <Compile Include="Day18Part2.fs" />
+    <Compile Include="Day19.fs" />
     <Compile Include="Program.fs" />
     <None Include="App.config" />
     <Content Include="Data\day01.input">
     <Content Include="Data\day18.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
+    <Content Include="Data\day19.input">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
     <Content Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
diff --git a/AdventOfCode2017/Day19.fs b/AdventOfCode2017/Day19.fs
new file mode 100644 (file)
index 0000000..bb10ce5
--- /dev/null
@@ -0,0 +1,22 @@
+module AdventOfCode2017.Day19
+
+open System
+
+let followThePath (lines : string[]) : string * int =
+    let rec next (i, j) (di, dj) (str : string) (n : int) =
+        let i', j' = i + di, j + dj
+        let c = lines.[i'].[j']
+        if c = '+' then
+            let pos =
+                [ '-', 0, 1; '|', -1, 0; '-', 0, -1; '|', 1, 0 ]
+                |> List.pick (
+                    fun (c', ndi, ndj) ->
+                        let ni, nj = i' + ndi, j' + ndj
+                        if (ni, nj) <> (i, j) && (Char.IsLetter lines.[ni].[nj] || lines.[ni].[nj] = c') then Some (ndi, ndj) else None
+                )
+            next (i', j') pos str (n + 1)
+        elif Char.IsWhiteSpace c |> not then
+            next (i', j') (di, dj) (if Char.IsLetter c then str + string c else str) (n + 1)
+        else
+            str, n
+    next (0, lines.[0].IndexOf '|') (1, 0) "" 1
index f845472..8123d7b 100644 (file)
@@ -83,6 +83,11 @@ let day18 () =
     let input = File.ReadAllLines "Data/day18.input"
     sprintf "part1 = %A, part2 = %A" (Day18Part1.run (Day18Part1.parseInput input)) (Day18Part2.run (Day18Part2.parseInput input))
 
+let day19 () =
+    let input = File.ReadAllLines "Data/day19.input"
+    let word, length = Day19.followThePath input
+    sprintf "part1 = %A, part2 = %A" word length
+
 let doDay (n : int) =
     let sw = Diagnostics.Stopwatch ()
     sw.Start ()
@@ -106,6 +111,7 @@ let doDay (n : int) =
         | 16 -> day16 ()
         | 17 -> day17 ()
         | 18 -> day18 ()
+        | 19 -> day19 ()
         | _ -> raise <| NotImplementedException ()
     printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds
 
diff --git a/Tests/Day19 tests.fs b/Tests/Day19 tests.fs
new file mode 100644 (file)
index 0000000..9b59a38
--- /dev/null
@@ -0,0 +1,40 @@
+namespace AdventOfCode2017.Tests
+
+open System
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day19 tests`` (output : ITestOutputHelper) =
+
+    [<Fact>]
+    let ``(Part1) From web page`` () =
+        let input =
+            [|
+                "     |           "
+                "     |  +--+     "
+                "     A  |  C     "
+                " F---|----E|--+  "
+                "     |  |  |  D  "
+                "     +B-+  +--+  "
+                "                 "
+            |]
+
+        Day19.followThePath input |> fst =! "ABCDEF"
+
+    [<Fact>]
+    let ``(Part2) From web page`` () =
+        let input =
+            [|
+                "     |           "
+                "     |  +--+     "
+                "     A  |  C     "
+                " F---|----E|--+  "
+                "     |  |  |  D  "
+                "     +B-+  +--+  "
+                "                 "
+            |]
+
+        Day19.followThePath input |> snd =! 38
\ No newline at end of file
index 32cfbf5..73549e4 100644 (file)
@@ -73,8 +73,9 @@
     <Compile Include="Day16 tests.fs" />
     <Compile Include="Day17 tests.fs" />
     <Compile Include="Day18 tests.fs" />
-    <Content Include="packages.config" />
+    <Compile Include="Day19 tests.fs" />
     <Content Include="App.config" />
+    <Content Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="FSharp.Core">