From: Ummon Date: Thu, 14 Dec 2017 12:53:04 +0000 (+0100) Subject: A compact version of day 14 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=5d0a992dcb587a5ffcddd8bf269d631a227bd43a;p=advent_of_code_2017.git A compact version of day 14 --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 34965a5..436c8cf 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -70,6 +70,7 @@ + diff --git a/AdventOfCode2017/Day14Compact.fs b/AdventOfCode2017/Day14Compact.fs new file mode 100644 index 0000000..3a3b13f --- /dev/null +++ b/AdventOfCode2017/Day14Compact.fs @@ -0,0 +1,17 @@ +module AdventOfCode2017.Day14Compact + +let regions (input : string) = + let m = Array2D.zeroCreate 128 128 + for i = 0 to 127 do + input + "-" + (string i) + |> Day10.knotHash2Encoding (fun i -> System.Convert.ToString(i, 2).PadLeft(8, '0')) + |> Seq.iteri (fun j c -> m.[i, j] <- int c - int '0') + let mutable n = 0 + let rec remove i j = + if i >= 0 && i < 128 && j >= 0 && j < 128 && m.[i, j] = 1 then + n <- n + 1 + 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, n \ No newline at end of file diff --git a/Tests/Day14 tests.fs b/Tests/Day14 tests.fs index d8af382..655d1b2 100644 --- a/Tests/Day14 tests.fs +++ b/Tests/Day14 tests.fs @@ -14,4 +14,12 @@ type ``Day14 tests`` (output : ITestOutputHelper) = [] let ``(Part2) From web page`` () = - Day14.nbOfConnectedRegions "flqrgnkx" =! 1242 \ No newline at end of file + Day14.nbOfConnectedRegions "flqrgnkx" =! 1242 + + [] + let ``(Part1, compact) From web page`` () = + Day14Compact.regions "flqrgnkx" |> snd =! 8108 + + [] + let ``(Part2, compact) From web page`` () = + Day14Compact.regions "flqrgnkx" |> fst =! 1242 \ No newline at end of file