Navigation Menu

Skip to content

Commit

Permalink
one more solution plus a better version of 42 that uses the io monad …
Browse files Browse the repository at this point in the history
…correctly
  • Loading branch information
Evan Klitzke committed Apr 26, 2008
1 parent d1d9bff commit 05989d8
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 6 deletions.
18 changes: 13 additions & 5 deletions 42/EulerProblem.hs
Expand Up @@ -3,6 +3,7 @@ module Main where
import Data.Char import Data.Char
import Data.Set hiding (map) import Data.Set hiding (map)
import Foreign import Foreign
import Control.Monad


triangles = fromList [(n * (n + 1)) `div` 2 | n <- [1..500]] triangles = fromList [(n * (n + 1)) `div` 2 | n <- [1..500]]


Expand All @@ -11,11 +12,18 @@ ordOffset = (ord 'A') - 1
score :: String -> Int score :: String -> Int
score s = sum [(ord c) - ordOffset | c <- s] score s = sum [(ord c) - ordOffset | c <- s]


wordsTxt :: [String] wordsTxt :: IO [String]
wordsTxt = read ("[" ++ (unsafePerformIO (readFile "words.txt")) ++ "]") wordsTxt = do words <- readFile "words.txt"
let words' = "[" ++ words ++ "]"
return $ read words'


wordScores = map score wordsTxt wordScores :: IO [Int]
answer = length [w | w <- wordScores, w `member` triangles] wordScores = do words <- wordsTxt
return $ map score words


answer :: IO Int
answer = do scores <- wordScores
return $ length [w | w <- scores, w `member` triangles]


main = print answer main = do a <- answer
print a
43 changes: 42 additions & 1 deletion 54/EulerProblem.hs
@@ -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
25 changes: 25 additions & 0 deletions 55/EulerProblem.hs
@@ -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]
11 changes: 11 additions & 0 deletions 56/EulerProblem.hs
@@ -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
39 changes: 39 additions & 0 deletions 57/EulerProblem.hs
@@ -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 ::

0 comments on commit 05989d8

Please sign in to comment.