From: Greg Burri Date: Sun, 17 Dec 2017 16:28:55 +0000 (+0100) Subject: Add a functional version of day 17 part 2. X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=2df8368cb53397c0d12a1c2368da600a47127674;p=advent_of_code_2017.git Add a functional version of day 17 part 2. --- diff --git a/AdventOfCode2017/AdventOfCode2017.fsproj b/AdventOfCode2017/AdventOfCode2017.fsproj index 57bd951..2fbe84d 100644 --- a/AdventOfCode2017/AdventOfCode2017.fsproj +++ b/AdventOfCode2017/AdventOfCode2017.fsproj @@ -37,7 +37,7 @@ AnyCPU bin\$(Configuration)\$(AssemblyName).XML true - 16 + 17 11 diff --git a/AdventOfCode2017/Day17.fs b/AdventOfCode2017/Day17.fs index ead02fd..5aba6d3 100644 --- a/AdventOfCode2017/Day17.fs +++ b/AdventOfCode2017/Day17.fs @@ -16,4 +16,13 @@ let spinLock2 (moves : int) = 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 + valueAt1 + +// Four times slower than 'spinLock2'. +let spinLock2' (moves : int) = + seq { 1 .. 50_000_000 } + |> Seq.fold ( + fun (pos, valueAt1) i -> + let pos' = (pos + moves) % i + 1 + pos', if pos' = 1 then i else valueAt1 + ) (0, 0) |> snd \ No newline at end of file