Skip to content

Instantly share code, notes, and snippets.

@dbalan
Created June 3, 2016 09:19
Show Gist options
  • Save dbalan/177863198ea6593cee14024d70d3f965 to your computer and use it in GitHub Desktop.
Save dbalan/177863198ea6593cee14024d70d3f965 to your computer and use it in GitHub Desktop.
import Data.List
longest :: [Int] -> Int
longest ns = maybe 0 id $ elemIndex m zeroCount
where
zeroCount = tail $ scanl (\x y -> if (y == 0) then x+1 else 0) 1 ns
m = maximum zeroCount
cycleAtPoint :: [Int] -> [Int]
cycleAtPoint ns = take (length ns) $ drop (1 + longest ns) $ cycle ns
accum :: [Int] -> [Int]
accum ns = tail $ scanl (+) 0 ns
{-
segments :: [Int] -> [[Int]]
segments ns = undefined
where
accumulated = accum $ cycleAtPoint ns
matched = groupBy (\x y -> (x == y && x == 0) || (x /= 0 && y /= 0)) ns
operations :: [Int] -> Int
operations ns = (sum $ map length $ segments ns) - 1
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment