Day 17
authorGreg Burri <greg.burri@gmail.com>
Sun, 17 Dec 2017 15:42:00 +0000 (16:42 +0100)
committerGreg Burri <greg.burri@gmail.com>
Sun, 17 Dec 2017 15:42:00 +0000 (16:42 +0100)
AdventOfCode2017/AdventOfCode2017.fsproj
AdventOfCode2017/Day17.fs [new file with mode: 0644]
AdventOfCode2017/Program.fs
Tests/Day17 tests.fs [new file with mode: 0644]
Tests/Tests.fsproj

index 3dac584..57bd951 100644 (file)
@@ -25,7 +25,7 @@
     <PlatformTarget>AnyCPU</PlatformTarget>
     <DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
     <Prefer32Bit>true</Prefer32Bit>
-    <StartArguments>16</StartArguments>
+    <StartArguments>17</StartArguments>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
@@ -74,6 +74,7 @@
     <Compile Include="Day14Compact.fs" />
     <Compile Include="Day15.fs" />
     <Compile Include="Day16.fs" />
+    <Compile Include="Day17.fs" />
     <Compile Include="Program.fs" />
     <None Include="App.config" />
     <Content Include="Data\day01.input">
     <Content Include="Data\day16.input">
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Content>
+    <Content Include="Data\day17.input">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
     <Content Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
diff --git a/AdventOfCode2017/Day17.fs b/AdventOfCode2017/Day17.fs
new file mode 100644 (file)
index 0000000..ead02fd
--- /dev/null
@@ -0,0 +1,19 @@
+module AdventOfCode2017.Day17
+
+open System.Collections.Generic
+
+let spinLock1 (moves : int) =
+    let buffer = List [ 0 ]
+    let mutable pos = 0
+    for i = 1 to 2017 do
+        pos <- (pos + moves) % buffer.Count + 1
+        buffer.Insert (pos, i)
+    buffer.[(pos + 1) % buffer.Count]
+
+let spinLock2 (moves : int) =
+    let mutable valueAt1 = 0
+    let mutable pos = 0
+    for i = 1 to 50_000_000 do
+        pos <- (pos + moves) % i + 1
+        if pos = 1 then valueAt1 <- i
+    valueAt1
\ No newline at end of file
index 1f9f1a7..299f276 100644 (file)
@@ -75,6 +75,10 @@ let day16 () =
     let input = File.ReadAllText "Data/day16.input" |> Day16.parseInput
     sprintf "part1 = %A, part2 = %A" (Day16.dance 16 1 input) (Day16.dance 16 1_000_000_000 input)
 
+let day17 () =
+    let input = File.ReadAllText "Data/day17.input" |> int
+    sprintf "part1 = %A, part2 = %A" (Day17.spinLock1 input) (Day17.spinLock2 input)
+
 let doDay (n : int) =
     let sw = Diagnostics.Stopwatch ()
     sw.Start ()
@@ -84,7 +88,7 @@ let doDay (n : int) =
         |  2 -> day02 ()
         |  3 -> day03 ()
         |  4 -> day04 ()
-        |  5  -> day05 ()
+        |  5 -> day05 ()
         |  6 -> day06 ()
         |  7 -> day07 ()
         |  8 -> day08 ()
@@ -96,6 +100,7 @@ let doDay (n : int) =
         | 14 -> day14 ()
         | 15 -> day15 ()
         | 16 -> day16 ()
+        | 17 -> day17 ()
         | _ -> raise <| NotImplementedException ()
     printfn "Result of day %i: %s (time : %i ms)" n result sw.ElapsedMilliseconds
 
diff --git a/Tests/Day17 tests.fs b/Tests/Day17 tests.fs
new file mode 100644 (file)
index 0000000..8b5b7b5
--- /dev/null
@@ -0,0 +1,18 @@
+namespace AdventOfCode2017.Tests
+
+open System
+open Xunit
+open Xunit.Abstractions
+open Swensen.Unquote
+
+open AdventOfCode2017
+
+type ``Day17 tests`` (output : ITestOutputHelper) =
+
+    [<Fact>]
+    let ``(Part1) From web page`` () =
+        Day17.spinLock1 3 =! 638
+
+    [<Fact>]
+    let ``(Part2) From web page`` () =
+        Day17.spinLock2 3 =! 1222153
\ No newline at end of file
index 99d77ca..c9a9bd6 100644 (file)
@@ -71,6 +71,7 @@
     <Compile Include="Day14 tests.fs" />
     <Compile Include="Day15 tests.fs" />
     <Compile Include="Day16 tests.fs" />
+    <Compile Include="Day17 tests.fs" />
     <Content Include="packages.config" />
     <Content Include="App.config" />
   </ItemGroup>