From: Ummon Date: Fri, 15 Dec 2017 08:19:40 +0000 (+0100) Subject: Day 15 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=84e08a8a33fd6b676e1a1dc42f2a5617f9c2ff5d;p=advent_of_code_2017.git Day 15 --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index f2d6bcc..6571eca 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -37,6 +37,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true + 15 11 diff --git a/AdventOfCode2017/Day15.fs b/AdventOfCode2017/Day15.fs index 96bc829..880189d 100644 --- a/AdventOfCode2017/Day15.fs +++ b/AdventOfCode2017/Day15.fs @@ -1,26 +1,23 @@ module AdventOfCode2017.Day15 -let hash = Day10.knotHash2Encoding (fun i -> System.Convert.ToString(i, 2).PadLeft(8, '0')) +let modulo = 2147483647L -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 generator (factor : int64) (init : int64) : int64 seq = + Seq.unfold ( + fun n -> + let n' = n * factor % modulo + Some (n', n') + ) init -let nbOfUsedSquares (input : string) = - let mutable i = 0 - buildMatrix input |> Array2D.iter (fun b -> i <- i + b) - i +let nbSimilarities genA genB n = + Seq.zip genA genB + |> Seq.take n + |> Seq.fold (fun s (a, b) -> s + if int16 a = int16 b then 1 else 0) 0 -let nbOfConnectedRegions (input : string) = - let m = buildMatrix input +let nbSimilarities1 (a : int64) (b : int64) = + nbSimilarities (generator 16807L a) (generator 48271L b) 40_000_000 - 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 +let nbSimilarities2 (a : int64) (b : int64) = + let genA = generator 16807L a |> Seq.filter (fun v -> v % 4L = 0L) + let genB = generator 48271L b |> Seq.filter (fun v -> v % 8L = 0L) + nbSimilarities genA genB 5_000_000 \ No newline at end of file diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 0c6b4de..d69d9bb 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -67,8 +67,9 @@ let day14 () = 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 input = File.ReadAllLines "Data/day15.input" + let genA, genB = int64 input.[0], int64 input.[1] + sprintf "part1 = %A, part2 = %A" (Day15.nbSimilarities1 genA genB) (Day15.nbSimilarities2 genA genB) let doDay (n : int) = let sw = Diagnostics.Stopwatch () diff --git a/Tests/Day15 tests.fs b/Tests/Day15 tests.fs index cad5296..ffa414f 100644 --- a/Tests/Day15 tests.fs +++ b/Tests/Day15 tests.fs @@ -10,8 +10,10 @@ type ``Day15 tests`` (output : ITestOutputHelper) = [] let ``(Part1) From web page`` () = - () + let a, b = 65L, 8921L + Day15.nbSimilarities1 a b =! 588 [] let ``(Part2) From web page`` () = - () \ No newline at end of file + let a, b = 65L, 8921L + Day15.nbSimilarities2 a b =! 309 \ No newline at end of file