Day 16
authorGreg Burri <greg.burri@gmail.com>
Sun, 17 Dec 2017 15:14:07 +0000 (16:14 +0100)
committerGreg Burri <greg.burri@gmail.com>
Sun, 17 Dec 2017 15:14:07 +0000 (16:14 +0100)
AdventOfCode2017/AdventOfCode2017.fsproj
AdventOfCode2017/Day16.fs
AdventOfCode2017/Program.fs
Tests/Day16 tests.fs

index 6f5af7e..3dac584 100644 (file)
@@ -37,8 +37,7 @@
     <PlatformTarget>AnyCPU</PlatformTarget>
     <DocumentationFile>bin\$(Configuration)\$(AssemblyName).XML</DocumentationFile>
     <Prefer32Bit>true</Prefer32Bit>
-    <StartArguments>
-    </StartArguments>
+    <StartArguments>16</StartArguments>
   </PropertyGroup>
   <PropertyGroup>
     <MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
index 25e51dd..8a6d7a6 100644 (file)
@@ -1 +1,57 @@
-module AdventOfCode2017.Day16
\ No newline at end of file
+module AdventOfCode2017.Day16
+
+open System
+open Day06
+
+type DanceMove =
+    | Spin of int
+    | Exchange of int * int
+    | Partner of char * char
+
+let parseInput (input : string) : DanceMove list =
+    input.Split ','
+    |> List.ofArray
+    |> List.map (
+        fun move ->
+            if move.[0] = 's' then
+                Spin (int move.[1..])
+            elif move.[0] = 'x' then
+                let pos = move.[1..].Split '/'
+                Exchange (int pos.[0], int pos.[1])
+            else
+                Partner (move.[1], move.[3])
+    )
+
+let dance (size : int) (nb : int) (moves : DanceMove list) : string =
+    let initialState = Array.init size (fun i -> char i + 'a')
+    let danceFloor = Array.copy initialState
+
+    let find c = danceFloor |> Array.findIndex ((=) c)
+    let swap p1 p2 =
+        let tmp = danceFloor.[p1]
+        danceFloor.[p1] <- danceFloor.[p2]
+        danceFloor.[p2] <- tmp
+
+    let mutable n = nb
+    while n <> 0 do
+        for move in moves do
+            match move with
+            | Spin s ->
+                for i = 1 to s do
+                    let last = danceFloor.[size - 1]
+                    for j in size - 1 .. -1 .. 1 do
+                        danceFloor.[j] <- danceFloor.[j - 1]
+                    danceFloor.[0] <- last
+            | Exchange (p1, p2) ->
+                swap p1 p2
+            | Partner (a, b) ->
+                swap (find a) (find b)
+
+        n <- n - 1
+        if danceFloor |=| initialState then
+            n <- nb % (nb - n)
+
+    String danceFloor
+
+
+
index d026faf..1f9f1a7 100644 (file)
@@ -72,8 +72,8 @@ let day15 () =
     sprintf "part1 = %A, part2 = %A" (Day15.nbSimilarities1 genA genB) (Day15.nbSimilarities2 genA genB)
 
 let day16 () =
-    let input = File.ReadAllLines "Data/day16.input"
-    sprintf "part1 = %A, part2 = %A" () ()
+    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 doDay (n : int) =
     let sw = Diagnostics.Stopwatch ()
index 054c63f..09404cc 100644 (file)
@@ -1,5 +1,6 @@
 namespace AdventOfCode2017.Tests
 
+open System
 open Xunit
 open Xunit.Abstractions
 open Swensen.Unquote
@@ -10,8 +11,10 @@ type ``Day16 tests`` (output : ITestOutputHelper) =
 
     [<Fact>]
     let ``(Part1) From web page`` () =
-        ()
+        let input = "s1,x3/4,pe/b"
+        Day16.dance 5 1 (Day16.parseInput input) =! "baedc"
 
     [<Fact>]
     let ``(Part2) From web page`` () =
-        ()
\ No newline at end of file
+        let input = "s1,x3/4,pe/b"
+        Day16.dance 5 2 (Day16.parseInput input) =! "ceadb"
\ No newline at end of file