From: Greg Burri Date: Fri, 22 Dec 2017 19:59:53 +0000 (+0100) Subject: Day 21 (WIP) X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=446af4ea857eebe0599fcb136bec6f7550350e99;p=advent_of_code_2017.git Day 21 (WIP) --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 4cc42dc..6ff95f3 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 20 + 21 pdbonly @@ -37,7 +37,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 20 + 21 11 @@ -79,6 +79,7 @@ + @@ -141,15 +142,14 @@ PreserveNewest + - - FSharp.Core - FSharp.Core.dll - $(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\$(TargetFSharpCoreVersion)\FSharp.Core.dll + ..\packages\FSharp.Core.4.2.3\lib\net45\FSharp.Core.dll + diff --git a/AdventOfCode2017/Day21.fs b/AdventOfCode2017/Day21.fs new file mode 100644 index 0000000..ff0da05 --- /dev/null +++ b/AdventOfCode2017/Day21.fs @@ -0,0 +1,79 @@ +module AdventOfCode2017.Day21 + +open System + +type M = bool[,] + +let parseInput (lines : string[]) : (M * M) list = + let readMat (str : string) = + str.Split '/' |> Array.map (Array.ofSeq >> Array.map ((=) '#')) |> array2D + + (* + + let l = str.Length + let m = Array2D.zeroCreate l l : M + let rows = str.Split '/' + for i = 0 to l - 1 do + for j = 0 to l - 1 do + m.[i, j] <- rows.[i].[j] = '#' + m*) + + + lines + |> List.ofArray + |> List.map ( + fun line -> + let matPair = line.Split ([| "=>" |], StringSplitOptions.RemoveEmptyEntries) + readMat matPair.[0], readMat matPair.[1] + ) + +let fractalArt (patterns : (M * M) list) : int = + let turn (m : M) = + let l = Array2D.length1 m + let m' = Array2D.zeroCreate l l + if Array2D.length1 m = 2 then + m'.[0,0] <- m.[1,0] + m'.[0,1] <- m.[0,0] + m'.[1,1] <- m.[0,1] + m'.[1,0] <- m.[1,1] + else + m'.[0,0] <- m.[2,0] + m'.[0,1] <- m.[1,0] + m'.[0,2] <- m.[0,0] + m'.[1,2] <- m.[0,1] + m'.[2,2] <- m.[0,2] + m'.[2,1] <- m.[1,2] + m'.[2,0] <- m.[2,2] + m'.[1,0] <- m.[2,1] + m' + + let flip (m : M) = + let l = Array2D.length1 m + let m' = Array2D.zeroCreate l l + if Array2D.length1 m = 2 then + m'.[0,0] <- m.[0,1] + m'.[0,1] <- m.[0,0] + m'.[1,1] <- m.[1,0] + m'.[1,0] <- m.[1,1] + else + m'.[0,0] <- m.[0,2] + m'.[1,0] <- m.[1,2] + m'.[2,0] <- m.[2,2] + m'.[0,2] <- m.[0,0] + m'.[1,2] <- m.[1,0] + m'.[2,2] <- m.[2,0] + m' + + let variants (m : M) : M list = + let l = List.unfold (fun (i, m) -> if i < 4 then Some (m, (i + 1, turn m)) else None) (0, m) + if Array2D.length1 m > 2 then l @ (l |> List.map flip) else l + + let next (m : M) : M = + let l = Array2D.length1 m + if l % 2 = 0 then + let l' = l + l / 2 + else + + let mutable n = 0 + next (array2 [ [ false; true; false ]; [ false; false; true ]; [ true; true; true] ]) |> Array2D.iter (fun e -> if e then n <- n + 1) + n \ No newline at end of file diff --git a/AdventOfCode2017/packages.config b/AdventOfCode2017/packages.config index a90f359..42fe79c 100644 --- a/AdventOfCode2017/packages.config +++ b/AdventOfCode2017/packages.config @@ -1,4 +1,5 @@  + \ No newline at end of file diff --git a/Tests/Day21 tests.fs b/Tests/Day21 tests.fs new file mode 100644 index 0000000..c5677c5 --- /dev/null +++ b/Tests/Day21 tests.fs @@ -0,0 +1,18 @@ +namespace AdventOfCode2017.Tests + +open System +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day21 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + () + + [] + let ``(Part2) From web page`` () = + () \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 1987918..4576901 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -75,6 +75,7 @@ +