From: Greg Burri Date: Sun, 17 Dec 2017 15:42:00 +0000 (+0100) Subject: Day 17 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=6c4b02880a4598f1e8b7230891c1ef115c56334c;p=advent_of_code_2017.git Day 17 --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 3dac584..57bd951 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -25,7 +25,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 16 + 17 pdbonly @@ -74,6 +74,7 @@ + @@ -124,6 +125,9 @@ PreserveNewest + + PreserveNewest + diff --git a/AdventOfCode2017/Day17.fs b/AdventOfCode2017/Day17.fs new file mode 100644 index 0000000..ead02fd --- /dev/null +++ b/AdventOfCode2017/Day17.fs @@ -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 diff --git a/AdventOfCode2017/Program.fs b/AdventOfCode2017/Program.fs index 1f9f1a7..299f276 100644 --- a/AdventOfCode2017/Program.fs +++ b/AdventOfCode2017/Program.fs @@ -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 index 0000000..8b5b7b5 --- /dev/null +++ b/Tests/Day17 tests.fs @@ -0,0 +1,18 @@ +namespace AdventOfCode2017.Tests + +open System +open Xunit +open Xunit.Abstractions +open Swensen.Unquote + +open AdventOfCode2017 + +type ``Day17 tests`` (output : ITestOutputHelper) = + + [] + let ``(Part1) From web page`` () = + Day17.spinLock1 3 =! 638 + + [] + let ``(Part2) From web page`` () = + Day17.spinLock2 3 =! 1222153 \ No newline at end of file diff --git a/Tests/Tests.fsproj b/Tests/Tests.fsproj index 99d77ca..c9a9bd6 100644 --- a/Tests/Tests.fsproj +++ b/Tests/Tests.fsproj @@ -71,6 +71,7 @@ +