From 5a2f2af56147b1245f72d973c1935c46e451fe33 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Thu, 14 Dec 2017 20:50:42 +0100 Subject: [PATCH] Day 15 template --- AdventOfCode2017/AdventOfCode2017.fsproj | 9 +++++++- AdventOfCode2017/Day15.fs | 26 ++++++++++++++++++++++++ AdventOfCode2017/Program.fs | 10 ++++++--- Tests/Day15 tests.fs | 17 ++++++++++++++++ Tests/Tests.fsproj | 1 + 5 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 AdventOfCode2017/Day15.fs create mode 100644 Tests/Day15 tests.fs diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 436c8cf..f2d6bcc 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 14 + 15 pdbonly @@ -71,6 +71,7 @@ + @@ -106,6 +107,12 @@ PreserveNewest + + PreserveNewest + + + PreserveNewest + diff --git a/AdventOfCode2017/Day15.fs b/AdventOfCode2017/Day15.fs new file mode 100644 index 0000000..96bc829 --- /dev/null +++ b/AdventOfCode2017/Day15.fs @@ -0,0 +1,26 @@ +module AdventOfCode2017.Day15 + +let hash = Day10.knotHash2Encoding (fun i -> System.Convert.ToString(i, 2).PadLeft(8, '0')) + +let buildMatrix (input : string) = + let mat = Array2D.zeroCreate 128 128 + for i = 0 to 127 do + input + "-" + (string i) |> hash |> Seq.iteri (fun j c -> mat.[i, j] <- int c - int '0') + mat + +let nbOfUsedSquares (input : string) = + let mutable i = 0 + buildMatrix input |> Array2D.iter (fun b -> i <- i + b) + i + +let nbOfConnectedRegions (input : string) = + let m = buildMatrix input + + let rec remove i j = + if i >= 0 && i < 128 && j >= 0 && j < 128 && m.[i, j] = 1 then + m.[i, j] <- 0 + 1 + remove (i + 1) j * remove (i - 1) j * remove i (j + 1) * remove i (j - 1) + else + 0 + + [ for i in 0 .. 127 do for j in 0 .. 127 -> remove i j ] |> List.sum \ No newline at end of file diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 0c14451..0c6b4de 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -63,9 +63,12 @@ let day13 () = sprintf "part1 = %A, part2 = %A" part1 part2 let day14 () = - let part1 = Day14.nbOfUsedSquares "hwlqcszp" - let part2 = Day14.nbOfConnectedRegions "hwlqcszp" - sprintf "part1 = %A, part2 = %A" part1 part2 + let input = File.ReadAllText "Data/day14.input" + sprintf "part1 = %A, part2 = %A" (Day14.nbOfUsedSquares input) (Day14.nbOfConnectedRegions input) + +let day15 () = + let input = File.ReadAllText "Data/day15.input" + sprintf "part1 = %A, part2 = %A" () () let doDay (n : int) = let sw = Diagnostics.Stopwatch () @@ -86,6 +89,7 @@ let doDay (n : int) = | 12 -> day12 () | 13 -> day13 () | 14 -> day14 () + | 15 -> day15 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds diff --git a/Tests/Day15 tests.fs b/Tests/Day15 tests.fs new file mode 100644 index 0000000..cad5296 --- /dev/null +++ b/Tests/Day15 tests.fs @@ -0,0 +1,17 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day15 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + () + + [] + let ``(Part2) From web page`` () = + () \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 3b76237..96bd24e 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -69,6 +69,7 @@ + -- 2.45.2