More concise day 16
authorUmmon <greg.burri@gmail.com>
Mon, 18 Dec 2017 10:22:23 +0000 (11:22 +0100)
committerUmmon <greg.burri@gmail.com>
Mon, 18 Dec 2017 10:22:23 +0000 (11:22 +0100)
AdventOfCode2017/Day16.fs

index d032c45..d6a69aa 100644 (file)
@@ -43,16 +43,15 @@ let dance (size : int) (nb : int) (moves : DanceMove list) : string =
                 swap (find a) (find b)
 
     let cycle =
-        (initialState, Seq.initInfinite id)
-        ||> Seq.scan (
-            fun previous _ ->
-                let current = previous.[*]
-                applyMoves current
-                current
+        (0, initialState)
+        |> Array.unfold (
+            fun (i, current) ->
+                if i <> 0 && (i > nb || current |=| initialState) then
+                    None
+                else
+                    let next = current.[*]
+                    applyMoves next
+                    Some (current, (i + 1, next))
         )
-        |> Seq.indexed
-        |> Seq.takeWhile (fun (i, state) -> i = 0 || i <= nb && not (state |=| initialState))
-        |> Seq.map snd
-        |> Array.ofSeq
 
     cycle.[nb % cycle.Length] |> String
\ No newline at end of file