My Weblog

Blog about programming and math

SPOJ 8612. Penney Game

SPOJ 8612. Penney Game is an easy simulation problem . Accepted Haskell code , not very Haskellish but accepted .

import Data.List
import qualified Data.ByteString.Char8 as BS



countfN :: String -> ( Int , Int , Int , Int , Int , Int , Int , Int ) -> String
countfN [ x , y ]( a , b , c , d , e , f , g , h )  = foldl ( \ x y ->  x ++ " " ++ show y ) ( show a ) [ b , c , d , e , f , g , h ]   
countfN ( x : y : z : xs ) ( a , b , c , d , e , f , g , h ) 
	| x : y : z :[] == "TTT" = countfN ( y : z : xs ) ( a + 1 , b , c , d , e , f , g , h )
	| x : y : z :[] == "TTH" = countfN ( y : z : xs ) ( a  , b + 1 , c , d , e , f , g , h )
	| x : y : z :[] == "THT" = countfN ( y : z : xs ) ( a  , b , c + 1 , d , e , f , g , h )
	| x : y : z :[] == "THH" = countfN ( y : z : xs ) ( a  , b , c , d + 1 , e , f , g , h )
	| x : y : z :[] == "HTT" = countfN ( y : z : xs ) ( a  , b , c , d , e + 1 , f , g , h )
	| x : y : z :[] == "HTH" = countfN ( y : z : xs ) ( a  , b , c , d , e , f + 1 , g , h )
	| x : y : z :[] == "HHT" = countfN ( y : z : xs ) ( a  , b , c , d , e , f , g + 1 , h )
	| otherwise =  countfN ( y : z : xs ) ( a  , b , c , d , e , f , g , h + 1 )


solve :: String -> String 
solve xs = countfN xs ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ) 
 
format :: [ String ] -> [ String ] 
format [] = []
format ( x : y : xs ) = y : format xs 

main = interact $ unlines . zipWith (\x y -> show x ++ " " ++  y ) [1..] . map solve . format . tail . lines 
Advertisements

August 5, 2011 - Posted by | Programming | , , ,

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: