Day 6
authorUmmon <greg.burri@gmail.com>
Wed, 6 Dec 2017 07:53:26 +0000 (08:53 +0100)
committerUmmon <greg.burri@gmail.com>
Wed, 6 Dec 2017 07:53:26 +0000 (08:53 +0100)
AdventOfCode2017/AdventOfCode2017.fsproj
AdventOfCode2017/Day6.fs [new file with mode: 0644]
AdventOfCode2017/Program.fs
Tests/Day6 tests.fs [new file with mode: 0644]
Tests/Tests.fsproj

index 5a9b3ef..0af4157 100644 (file)
@@ -25,7 +25,7 @@
     <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>
@@ -61,6 +61,7 @@
     <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">
@@ -75,6 +76,9 @@
     <None Include="Data\day5.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </None>
+    <None Include="Data\day6.input">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </None>
     <Content Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
diff --git a/AdventOfCode2017/Day6.fs b/AdventOfCode2017/Day6.fs
new file mode 100644 (file)
index 0000000..85fac4c
--- /dev/null
@@ -0,0 +1,25 @@
+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
index 574d6d9..b5116bb 100644 (file)
@@ -23,6 +23,11 @@ let day5 () =
     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
@@ -31,6 +36,7 @@ let doDay (n : int) =
         | 3 -> day3 ()
         | 4 -> day4 ()
         | 5 -> day5 ()
+        | 6 -> day6 ()
         | _ -> raise <| NotImplementedException ()
     printfn "Result of day %i: %s" n result
 
diff --git a/Tests/Day6 tests.fs b/Tests/Day6 tests.fs
new file mode 100644 (file)
index 0000000..9d2d0ad
--- /dev/null
@@ -0,0 +1,19 @@
+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
index 1b36559..fe51460 100644 (file)
@@ -60,6 +60,7 @@
     <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>