From e664f2f9c3b2ef0034d94baa32e04e1e99665353 Mon Sep 17 00:00:00 2001 From: Ummon Date: Thu, 14 Dec 2017 09:41:42 +0100 Subject: [PATCH] Day 14 --- AdventOfCode2017/AdventOfCode2017.fsproj | 3 ++- AdventOfCode2017/Day10.fs | 6 +++-- AdventOfCode2017/Day14.fs | 32 ++++++++++++++++++++++++ AdventOfCode2017/Program.fs | 6 +++++ Tests/Day14 tests.fs | 17 +++++++++++++ Tests/Tests.fsproj | 1 + 6 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 AdventOfCode2017/Day14.fs create mode 100644 Tests/Day14 tests.fs diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 074c067..34965a5 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 13 + 14 pdbonly @@ -69,6 +69,7 @@ + diff --git a/AdventOfCode2017/Day10.fs b/AdventOfCode2017/Day10.fs index a5cdb5d..ec3a4f1 100644 --- a/AdventOfCode2017/Day10.fs +++ b/AdventOfCode2017/Day10.fs @@ -21,5 +21,7 @@ let knotHash (nbRounds : int) (reduce : int[] -> string) (lengths : int list) (s let knotHash1 (str : string) = knotHash 1 (fun s -> s.[0] * s.[1] |> string) (str.Split ',' |> List.ofArray |> List.map int) -let knotHash2 (str : string) = - knotHash 64 (Array.chunkBySize 16 >> Array.map (Array.reduce (^^^) >> sprintf "%02x") >> Array.reduce (+)) (List.append (str |> List.ofSeq |> List.map int) [ 17; 31; 73; 47; 23 ]) 256 \ No newline at end of file +let knotHash2Encoding (encoding : int -> string) (str : string) = + knotHash 64 (Array.chunkBySize 16 >> Array.map (Array.reduce (^^^) >> encoding) >> Array.reduce (+)) (List.append (str |> List.ofSeq |> List.map int) [ 17; 31; 73; 47; 23 ]) 256 + +let knotHash2 = knotHash2Encoding (sprintf "%02x") \ No newline at end of file diff --git a/AdventOfCode2017/Day14.fs b/AdventOfCode2017/Day14.fs new file mode 100644 index 0000000..76bf281 --- /dev/null +++ b/AdventOfCode2017/Day14.fs @@ -0,0 +1,32 @@ +module AdventOfCode2017.Day14 + +open System + +let hash = Day10.knotHash2Encoding (fun i -> Convert.ToString(i, 2).PadLeft(8, '0')) + +let buildMatrix (input : string) : bool[,] = + let mat = Array2D.zeroCreate 128 128 + for i = 0 to 127 do + input + "-" + (string i) |> hash |> Seq.iteri (fun j c -> if c = '1' then mat.[i, j] <- true) + mat + +let nbOfUsedSquares (input : string) = + let mutable i = 0 + buildMatrix input |> Array2D.iter (fun b -> if b then i <- i + 1) + 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] then + m.[i, j] <- false + remove (i + 1) j |> ignore + remove (i - 1) j |> ignore + remove i (j + 1) |> ignore + remove i (j - 1) |> ignore + 1 + else + 0 + + seq { for i in 0 .. 127 do for j in 0 .. 127 -> remove i j } |> Seq.sum \ No newline at end of file diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 0d32674..0c14451 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -62,6 +62,11 @@ let day13 () = let part1, part2 = Day13.severity input 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 doDay (n : int) = let sw = Diagnostics.Stopwatch () sw.Start () @@ -80,6 +85,7 @@ let doDay (n : int) = | 11 -> day11 () | 12 -> day12 () | 13 -> day13 () + | 14 -> day14 () | _ -> raise <| NotImplementedException () printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds diff --git a/Tests/Day14 tests.fs b/Tests/Day14 tests.fs new file mode 100644 index 0000000..d8af382 --- /dev/null +++ b/Tests/Day14 tests.fs @@ -0,0 +1,17 @@ +namespace AdventOfCode2017.Tests + +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day14 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Day14.nbOfUsedSquares "flqrgnkx" =! 8108 + + [] + let ``(Part2) From web page`` () = + Day14.nbOfConnectedRegions "flqrgnkx" =! 1242 \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 22aeb3a..3b76237 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -68,6 +68,7 @@ + -- 2.45.2