From 6b5b836f3981cc88b4511eac7bac96969dc9ad76 Mon Sep 17 00:00:00 2001 From: Ummon Date: Mon, 18 Dec 2017 11:22:23 +0100 Subject: [PATCH] More concise day 16 --- AdventOfCode2017/Day16.fs | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/AdventOfCode2017/Day16.fs b/AdventOfCode2017/Day16.fs index d032c45..d6a69aa 100644 --- a/AdventOfCode2017/Day16.fs +++ b/AdventOfCode2017/Day16.fs @@ -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 -- 2.45.2