Skip to content

Instantly share code, notes, and snippets.

@craftybones
Created August 8, 2019 07:52
Show Gist options
  • Save craftybones/807079365c8837945a22de8f63b4020c to your computer and use it in GitHub Desktop.
Save craftybones/807079365c8837945a22de8f63b4020c to your computer and use it in GitHub Desktop.
import Data.List
groupN :: Int -> [a] -> [[a]]
groupN _ [] = []
groupN 0 _ = undefined
groupN n x = take n x : groupN n (drop n x)
transpose :: [[a]] -> [[a]]
transpose ([]:_) = []
transpose x = map head x : (transpose (map tail x))
removeZeroes = filter (/= 0)
pairPartitions = concatMap (groupN 2)
sumOf = foldl (+) 0
sumPairs = map sumOf
concatZeroes = (++ repeat 0)
compressLeft = concatZeroes . sumPairs . pairPartitions . group . removeZeroes
moveRowLeft = (take 4) . compressLeft
moveRowRight = reverse . moveRowLeft . reverse
moveGridLeft = map moveRowLeft
moveGridRight = map moveRowRight
moveGridUp = transpose . moveGridLeft . transpose
moveGridDown = transpose . moveGridRight . transpose
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment