Skip to content

Instantly share code, notes, and snippets.

@samsartor
Created January 31, 2018 16:29
Show Gist options
  • Save samsartor/25780c662081c80600790b55aa4cf3a7 to your computer and use it in GitHub Desktop.
Save samsartor/25780c662081c80600790b55aa4cf3a7 to your computer and use it in GitHub Desktop.
Plays the "linear" version of Solitaire
type Card = (Int, Int)
type Deck = [Card]
type Hand = [Card]
collase_one :: Hand -> Hand
collase_one ((_, ra) : _ : _ : (_, rb) : rest) | ra == rb = rest
collase_one (a@(sa, _) : _ : _ : b@(sb, _) : rest) | sa == sb = [a, b] ++ rest
collase_one t = t
collape_all :: Hand -> Hand
collape_all = until (\d -> (collase_one d) == d) collase_one
play :: (Deck, Hand) -> [(Deck, Hand)]
play s@([], _) = [s]
play s@(deck, hand) = s : play (tail deck, collape_all $ head deck : hand)
score :: Deck -> Int
score deck = length . snd . last $ play (deck, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment