<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
+ <StartArguments>15</StartArguments>
</PropertyGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
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
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 ()
[<Fact>]
let ``(Part1) From web page`` () =
- ()
+ let a, b = 65L, 8921L
+ Day15.nbSimilarities1 a b =! 588
[<Fact>]
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