Set puzzle: Difference between revisions

→‎{{header|Haskell}}: Specified imports, tidied isSet and getHand, applied hlint hindent
(→‎{{header|Haskell}}: Specified imports, tidied isSet and getHand, applied hlint hindent)
Line 1,750:
 
=={{header|Haskell}}==
<lang haskell>import DataControl.ListMonad.State
(State, evalState, replicateM, runState, state)
import System.Random (StdGen, newStdGen, randomR)
import Control.Monad.State
import Data.List (find, nub, sort)
 
combinations :: Int -> [a] -> [[a]]
combinations 0 _ = [[]]
combinations _ [] = []
combinations k (y:ys) = map (y :) (combinations (k - 1) ys) ++ combinations k ys
 
data Color
data Color = Red | Green | Purple deriving (Show, Enum, Bounded, Ord, Eq)
= Red
data Symbol = Oval | Squiggle | Diamond deriving (Show, Enum, Bounded, Ord, Eq)
| Green
data Count = One | Two | Three deriving (Show, Enum, Bounded, Ord, Eq)
| Purple
data Shading = Solid | Open | Striped deriving (Show, Enum, Bounded, Ord, Eq)
data Color = Red | Green | Purple deriving (Show, Enum, Bounded, Ord, Eq)
 
data Card = Card {Symbol
= Oval
color :: Color,
| Squiggle
symbol :: Symbol,
| Diamond
count :: Count,
data Symbol = Oval | Squiggle | Diamond deriving (Show, Enum, Bounded, Ord, Eq)
shading :: Shading
} deriving (Show)
 
data Count
-- Identify a set of three cards by counting all attribute types.
= One
| Two
| Three
data Count = One | Two | Three deriving (Show, Enum, Bounded, Ord, Eq)
 
data Shading
= Solid
| Open
| Striped
data Shading = Solid | Open | Striped deriving (Show, Enum, Bounded, Ord, Eq)
 
data Card = Card
{ color :: Color,
, symbol :: Symbol,
, count :: Count,
, shading :: Shading
} deriving (Show)
 
-- Identify a set of three cards by counting all attribute types.
-- if each count is 3 or 1 ( not 2 ) the the cards compose a set.
isSet :: [Card] -> Bool
isSet cs =
let colorCounttotal = length $. nub $. sort $. mapflip colormap cs
in notElem 2 [total color, total symbol, total count, total shading]
symbolCount = length $ nub $ sort $ map symbol cs
countCount = length $ nub $ sort $ map count cs
shadingCount = length $ nub $ sort $ map shading cs
in colorCount /= 2 && symbolCount /= 2 && countCount /= 2 && shadingCount /= 2
 
-- Get a random card from a deck. Returns the card and removes it from the deck.
getCard :: State (StdGen, [Card]) Card
getCard =
getCard = state $ \(gen, cs) -> let (i, newGen) = randomR (0, length cs - 1) gen
state $
(a,b) = splitAt i cs
\(gen, cs) ->
in (head b, (newGen, a ++ tail b))
getCard = state $ \(gen, cs) -> let (i, newGen) = randomR (0, length cs - 1) gen
(a, b) = splitAt i cs
in (head b, (newGen, a ++ tail b))
 
-- Get a hand of cards. Starts with new deck and then removes the
-- appropriate number of cards from that deck.
getHand :: Int -> State StdGen [Card]
getHand n = state $ \gen ->
state $
let deck = [Card co sy ct sh |
\gen ->
co <- [minBound..maxBound],
let az sy <-= [minBound .. maxBound],
deck ct <- [minBound..maxBound],=
[ Card co sy ct sh <- [minBound..maxBound]]
(a,(newGen,| _))co =<- runStateaz (replicateM n getCard) (gen,deck)
in (a , newGen)sy <- az
let deck = [Card co sy, ct sh<- |az
putSet, stsh =<- doaz ]
(a, (newGen, _)) = runState (replicateM n getCard) (gen, deck)
in (a, newGen)
 
-- Get an unbounded number of hands of the appropriate number of cards.
getManyHands :: Int -> State StdGen [[Card]]
getManyHands n = (sequence . repeat) (getHand n)
 
-- Deal out hands of the appropriate size until one with the desired number
Line 1,807 ⟶ 1,831:
showSolutions :: Int -> Int -> IO ()
showSolutions cardCount solutionCount = do
putStrLn ""$
putStrLn $ "Showing hand of " ++ show cardCount ++ " cards with " ++ show solutionCount ++ " solutions."
"Showing hand of gen" <- newStdGen++
putStrLn $ "Showing hand of " ++ show cardCount ++ " cards with " ++ show solutionCount ++ " solutions."
let Just z = find (\ls -> length (filter isSet $ combinations 3 ls) == solutionCount) $
gen <- newStdGen
evalState (getManyHands cardCount) gen
let Just z =
mapM_ print z
let Justfind z((solutionCount ==) find. (\lslength ->. length (filter isSet $. combinations 3 ls) == solutionCount) $
putStrLn ""
evalState (getManyHands cardCount) gen
putStrLn "Solutions:"
mapM_ print z
mapM_ putSet $ filter isSet $ combinations 3 z where
putStrLn ""
putSet st = do
putStrLn "Solutions:"
mapM_ print st
mapM_ putSet $ filter isSet $ combinations 3 z where
putStrLn ""
where
putSet st = do
mapM_ print zst
putStrLn ""
 
-- Show a hand of 9 cards with 4 solutions
-- and a hand of 12 cards with 6 solutions.
main :: IO ()
main = do
showSolutions 9 4
showSolutions 12 6</lang>
{{out}}
<pre style="font-size:80%">Showing hand of 9 cards with 4 solutions.
9,659

edits