<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
- <StartArguments>13</StartArguments>
+ <StartArguments>14</StartArguments>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Compile Include="Day11.fs" />
<Compile Include="Day12.fs" />
<Compile Include="Day13.fs" />
+ <Compile Include="Day14.fs" />
<Compile Include="Program.fs" />
<None Include="App.config" />
<Content Include="Data\day1.input">
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
--- /dev/null
+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
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 ()
| 11 -> day11 ()
| 12 -> day12 ()
| 13 -> day13 ()
+ | 14 -> day14 ()
| _ -> raise <| NotImplementedException ()
printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds
--- /dev/null
+namespace AdventOfCode2017.Tests
+
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day14 tests`` (output : ITestOutputHelper) =
+
+ [<Fact>]
+ let ``(Part1) From web page`` () =
+ Day14.nbOfUsedSquares "flqrgnkx" =! 8108
+
+ [<Fact>]
+ let ``(Part2) From web page`` () =
+ Day14.nbOfConnectedRegions "flqrgnkx" =! 1242
\ No newline at end of file
<Compile Include="Day11 tests.fs" />
<Compile Include="Day12 tests.fs" />
<Compile Include="Day13 tests.fs" />
+ <Compile Include="Day14 tests.fs" />
<Content Include="packages.config" />
<Content Include="App.config" />
</ItemGroup>