From: Greg Burri <greg.burri@gmail.com> 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 @@ <PlatformTarget>AnyCPU</PlatformTarget> <DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile> <Prefer32Bit>true</Prefer32Bit> - <StartArguments>20</StartArguments> + <StartArguments>21</StartArguments> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <DebugType>pdbonly</DebugType> @@ -37,7 +37,7 @@ <PlatformTarget>AnyCPU</PlatformTarget> <DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile> <Prefer32Bit>true</Prefer32Bit> - <StartArguments>20</StartArguments> + <StartArguments>21</StartArguments> </PropertyGroup> <PropertyGroup> <MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion> @@ -79,6 +79,7 @@ <Compile Include="Day18Part2.fs" /> <Compile Include="Day19.fs" /> <Compile Include="Day20.fs" /> + <Compile Include="Day21.fs" /> <Compile Include="Program.fs" /> <None Include="App.config" /> <Content Include="Data\day01.input"> @@ -141,15 +142,14 @@ <Content Include="Data\day20.input"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> + <None Include="Data\day21.input" /> <Content Include="packages.config" /> </ItemGroup> <ItemGroup> - <Reference Include="mscorlib" /> <Reference Include="FSharp.Core"> - <Name>FSharp.Core</Name> - <AssemblyName>FSharp.Core.dll</AssemblyName> - <HintPath>$(MSBuildProgramFiles32)\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\$(TargetFSharpCoreVersion)\FSharp.Core.dll</HintPath> + <HintPath>..\packages\FSharp.Core.4.2.3\lib\net45\FSharp.Core.dll</HintPath> </Reference> + <Reference Include="mscorlib" /> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Numerics" /> 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 @@ <?xml version="1.0" encoding="utf-8"?> <packages> + <package id="FSharp.Core" version="4.2.3" targetFramework="net462" /> <package id="System.ValueTuple" version="4.4.0" targetFramework="net462" /> </packages> \ 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) = + + [<Fact>] + let ``(Part1) From web page`` () = + () + + [<Fact>] + 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 @@ <Compile Include="Day18 tests.fs" /> <Compile Include="Day19 tests.fs" /> <Compile Include="Day20 tests.fs" /> + <Compile Include="Day21 tests.fs" /> <Content Include="App.config" /> <Content Include="packages.config" /> </ItemGroup>