From 7d2815856c521638553fb7cb1826dbd073757222 Mon Sep 17 00:00:00 2001 From: Ummon Date: Tue, 19 Dec 2017 09:35:06 +0100 Subject: [PATCH] Day 19 --- AdventOfCode2017/AdventOfCode2017.fsproj | 8 +++-- AdventOfCode2017/Day19.fs | 22 +++++++++++++ AdventOfCode2017/Program.fs | 6 ++++ Tests/Day19 tests.fs | 40 ++++++++++++++++++++++++ Tests/Tests.fsproj | 3 +- 5 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 AdventOfCode2017/Day19.fs create mode 100644 Tests/Day19 tests.fs diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 2d2f009..81703e0 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 18 + 19 pdbonly @@ -37,7 +37,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 16 + 19 11 @@ -77,6 +77,7 @@ + @@ -133,6 +134,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day19.fs b/AdventOfCode2017/Day19.fs new file mode 100644 index 0000000..bb10ce5 --- /dev/null +++ b/AdventOfCode2017/Day19.fs @@ -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 diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index f845472..8123d7b 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -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 index 0000000..9b59a38 --- /dev/null +++ b/Tests/Day19 tests.fs @@ -0,0 +1,40 @@ +namespace AdventOfCode2017.Tests + +open System +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day19 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + let input = + [| + " | " + " | +--+ " + " A | C " + " F---|----E|--+ " + " | | | D " + " +B-+ +--+ " + " " + |] + + Day19.followThePath input |> fst =! "ABCDEF" + + [] + let ``(Part2) From web page`` () = + let input = + [| + " | " + " | +--+ " + " A | C " + " F---|----E|--+ " + " | | | D " + " +B-+ +--+ " + " " + |] + + Day19.followThePath input |> snd =! 38 \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 32cfbf5..73549e4 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -73,8 +73,9 @@ - + + -- 2.45.2