<PlatformTarget>AnyCPU</PlatformTarget>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
<Prefer32Bit>true</Prefer32Bit>
- <StartArguments>5</StartArguments>
+ <StartArguments>6</StartArguments>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Compile Include="Day3.fs" />
<Compile Include="Day4.fs" />
<Compile Include="Day5.fs" />
+ <Compile Include="Day6.fs" />
<Compile Include="Program.fs" />
<None Include="App.config" />
<None Include="Data\day1.input">
<None Include="Data\day5.input">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
+ <None Include="Data\day6.input">
+ <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+ </None>
<Content Include="packages.config" />
</ItemGroup>
<ItemGroup>
--- /dev/null
+module AdventOfCode2017.Day6
+
+open System
+
+type Blocks = int[]
+
+let parseInput (str : string) : int[] =
+ str.Split ([| '\r'; '\t'; ' ' |], StringSplitOptions.RemoveEmptyEntries) |> Array.map int
+
+let nbRedistribution (blocks : Blocks) =
+ let rec next (previous : Blocks list) =
+ let blocks = List.head previous |> Array.copy
+ let i, max = blocks |> Array.indexed |> Array.maxBy snd
+ blocks.[i] <- 0
+ for offset = i + 1 to i + max do
+ let i' = offset % blocks.Length
+ blocks.[i'] <- blocks.[i'] + 1
+
+ match previous |> List.tryFindIndex ((=) blocks) with
+ | Some i -> previous, i + 1
+ | None -> next (blocks :: previous)
+
+ let blockList, cycleLength = next [ blocks ]
+
+ List.length blockList, cycleLength
let input = File.ReadAllText "Data/day5.input" |> Day5.parseInput
sprintf "part1 = %A, part2 = %A" (Day5.nbSteps1 input) (Day5.nbSteps2 input)
+let day6 () =
+ let input = File.ReadAllText "Data/day6.input" |> Day6.parseInput
+ let part1, part2 = Day6.nbRedistribution input
+ sprintf "part1 = %A, part2 = %A" part1 part2
+
let doDay (n : int) =
let result =
match n with
| 3 -> day3 ()
| 4 -> day4 ()
| 5 -> day5 ()
+ | 6 -> day6 ()
| _ -> raise <| NotImplementedException ()
printfn "Result of day %i: %s" n result
--- /dev/null
+namespace AdventOfCode2017.Tests
+
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day6 tests`` (output : ITestOutputHelper) =
+
+ [<Fact>]
+ let ``(Part1) From web page`` () =
+ let l, _ = Day6.nbRedistribution [| 0; 2; 7; 0 |]
+ l =! 5
+
+ [<Fact>]
+ let ``(Part2) From web page`` () =
+ let _, cycleLength = Day6.nbRedistribution [| 0; 2; 7; 0 |]
+ cycleLength =! 4
\ No newline at end of file
<Compile Include="Day3 tests.fs" />
<Compile Include="Day4 tests.fs" />
<Compile Include="Day5 tests.fs" />
+ <Compile Include="Day6 tests.fs" />
<Content Include="packages.config" />
<Content Include="App.config" />
</ItemGroup>