Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
one more solution plus a better version of 42 that uses the io monad …
…correctly
- Loading branch information
Evan Klitzke
committed
Apr 26, 2008
1 parent
d1d9bff
commit 05989d8
Showing
5 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,18 +1,59 @@ | |||
module Main where | module Main where | ||
|
|
||
import Foreign | |||
import Data.Char | |||
|
|||
-- Define some card types so that we can compare cards naturally | -- Define some card types so that we can compare cards naturally | ||
|
|
||
data Suit = Hearts | Clubs | Diamonds | Spades deriving (Eq, Ord) | data Suit = Hearts | Clubs | Diamonds | Spades deriving (Eq, Ord) | ||
data Card = Card Int Suit deriving (Eq) | data Card = Card Int Suit deriving (Eq) | ||
type Hand = [Card] | type Hand = [Card] | ||
|
|
||
type HandRank = HighCard | OnePair | TwoPair | TheeKind | Straight | Flush | FullHouse | FourKind | StraightFlush | RoyalFlush deriving (Eq, Ord) | |||
|
|||
instance Ord Card where | instance Ord Card where | ||
compare (Card a b) (Card c d) | compare (Card a b) (Card c d) | ||
| a > c = GT | | a > c = GT | ||
| a < c = LT | | a < c = LT | ||
| a == c = compare b d | | a == c = compare b d | ||
|
|
||
-- kind of a cop out since i couldn't figure out how to make instances of read work | |||
cardRead :: String -> Card | |||
cardRead (x:xs) = Card (num x) (suitRead xs) | |||
where | |||
num n | |||
| n == 'A' = 1 | |||
| n `elem` "23456789" = digitToInt n | |||
| n == 'T' = 10 | |||
| n == 'J' = 11 | |||
| n == 'Q' = 12 | |||
| n == 'K' = 13 | |||
suitRead "H" = Hearts | |||
suitRead "C" = Clubs | |||
suitRead "D" = Diamonds | |||
suitRead "S" = Spades | |||
|
|||
-- End card definitions | -- End card definitions | ||
|
|
||
lineToHands :: String -> (Hand, Hand) | |||
lineToHands l = (mcr h1, mcr h2) | |||
where | |||
lineWords = words l | |||
h1 = take 5 lineWords | |||
h2 = drop 5 lineWords | |||
mcr = map cardRead | |||
|
|||
pokerHands :: [(Hand, Hand)] | |||
pokerHands = map lineToHands (lines pokerTxt) | |||
where | |||
pokerTxt = unsafePerformIO (readFile "poker.txt") | |||
|
|||
tieBreaker :: Hand -> Hand -> Bool | |||
tieBreaker | |||
|
|||
a :: Card | |||
a = cardRead "4H" | |||
|
|||
b = cardRead "TD" | |||
|
|
||
main = print (compare a b) | main = print pokerHands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,25 @@ | |||
module Main where | |||
|
|||
palindrome :: Eq a => [a] -> Bool | |||
palindrome [] = True | |||
palindrome [_] = True | |||
palindrome (x:xs) = (x == (last xs)) && (palindrome (init xs)) | |||
|
|||
isNumPalindrome :: (Show a, Integral a) => a -> Bool | |||
isNumPalindrome = palindrome . show | |||
|
|||
reverseNum :: (Show a, Integral a, Read a) => a -> a | |||
reverseNum = read . reverse . show | |||
|
|||
--reverseAndAdd :: Integral a => a -> a | |||
reverseAndAdd n = n + (reverseNum n) | |||
|
|||
--lyrchel :: Integral a => a -> Bool | |||
lyrchel n = lyrchel_ 50 n | |||
where | |||
lyrchel_ 0 _ = True | |||
lyrchel_ c n = if isNumPalindrome raa then False else lyrchel_ (c-1) raa | |||
where | |||
raa = reverseAndAdd n | |||
|
|||
main = print $ length [x | x <- [1..9999], lyrchel x] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,11 @@ | |||
module Main where | |||
|
|||
import Data.Char | |||
|
|||
digitalSum :: Integral a => a -> Int | |||
digitalSum = sum . map digitToInt . show | |||
|
|||
powers :: Integral a => [a] | |||
powers = [a^b | a <- [1..99], b <- [1..99]] | |||
|
|||
main = print $ maximum $ map digitalSum powers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
data Fraction = Fract Int Int | |||
|
|||
num :: Fraction -> Int | |||
num Frac n d = n | |||
|
|||
denom :: Fraction -> Int | |||
denom Frac n d = d | |||
|
|||
-- Remove the first element in a list matching a pattern | |||
removeFirst :: Eq a => a -> [a] -> [a] | |||
removeFirst _ [] = [] | |||
removeFirst t (x:xs) = if x == t then xs else x : (removeFirst t xs) | |||
|
|||
reduce :: Fraction -> Fraction | |||
reduce Frac n d | |||
|
|||
--isReduced :: Fraction -> Bool | |||
|
|||
fracDiv :: Int -> Fraction -> Fraction | |||
fracDiv x (Frac n d) = Frac ( | |||
|
|||
|
|||
multisetDifference :: Eq a => [a] -> [a] -> [a] | |||
multisetDifference as [] = as | |||
multisetDifference [] _ = [] | |||
multisetDifference (a:as) bs | |||
| a `elem` bs = multisetDifference as (removeFirst a bs) | |||
| otherwise = a : (multisetDifference as bs) | |||
|
|||
-- reduces a fraction. for example, reduceFrac 2 4 = (1, 2) | |||
--reduceFrac :: Integer -> Integer -> (Integer, Integer) | |||
reduceFrac a b = (product as', product bs') | |||
where | |||
as = factor a | |||
bs = factor b | |||
as' = multisetDifference as bs | |||
bs' = multisetDifference bs as | |||
|
|||
fracDiv :: |