From d59517f12bb3a4b60d0d97d6842e90274bfba3dd Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Sun, 17 Dec 2017 16:14:07 +0100 Subject: [PATCH] Day 16 --- AdventOfCode2017/AdventOfCode2017.fsproj | 3 +- AdventOfCode2017/Day16.fs | 58 +++++++++++++++++++++++- AdventOfCode2017/Program.fs | 4 +- Tests/Day16 tests.fs | 7 ++- 4 files changed, 65 insertions(+), 7 deletions(-) diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 6f5af7e..3dac584 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -37,8 +37,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - - + 16 11 diff --git a/AdventOfCode2017/Day16.fs b/AdventOfCode2017/Day16.fs index 25e51dd..8a6d7a6 100644 --- a/AdventOfCode2017/Day16.fs +++ b/AdventOfCode2017/Day16.fs @@ -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 + + + diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index d026faf..1f9f1a7 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -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 () diff --git a/Tests/Day16 tests.fs b/Tests/Day16 tests.fs index 054c63f..09404cc 100644 --- a/Tests/Day16 tests.fs +++ b/Tests/Day16 tests.fs @@ -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) = [] let ``(Part1) From web page`` () = - () + let input = "s1,x3/4,pe/b" + Day16.dance 5 1 (Day16.parseInput input) =! "baedc" [] 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 -- 2.45.2