Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

one more solution plus a better version of 42 that uses the io monad …

…correctly
  • Loading branch information...
commit 05989d8e8bb37e55a188153d9697fb9eb33e965f 1 parent d1d9bff
Evan Klitzke authored
View
18 42/EulerProblem.hs
@@ -3,6 +3,7 @@ module Main where
import Data.Char
import Data.Set hiding (map)
import Foreign
+import Control.Monad
triangles = fromList [(n * (n + 1)) `div` 2 | n <- [1..500]]
@@ -11,11 +12,18 @@ ordOffset = (ord 'A') - 1
score :: String -> Int
score s = sum [(ord c) - ordOffset | c <- s]
-wordsTxt :: [String]
-wordsTxt = read ("[" ++ (unsafePerformIO (readFile "words.txt")) ++ "]")
+wordsTxt :: IO [String]
+wordsTxt = do words <- readFile "words.txt"
+ let words' = "[" ++ words ++ "]"
+ return $ read words'
-wordScores = map score wordsTxt
-answer = length [w | w <- wordScores, w `member` triangles]
+wordScores :: IO [Int]
+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
View
43 54/EulerProblem.hs
@@ -1,18 +1,59 @@
module Main where
+import Foreign
+import Data.Char
+
-- Define some card types so that we can compare cards naturally
data Suit = Hearts | Clubs | Diamonds | Spades deriving (Eq, Ord)
data Card = Card Int Suit deriving (Eq)
type Hand = [Card]
+type HandRank = HighCard | OnePair | TwoPair | TheeKind | Straight | Flush | FullHouse | FourKind | StraightFlush | RoyalFlush deriving (Eq, Ord)
+
instance Ord Card where
compare (Card a b) (Card c d)
| a > c = GT
| a < c = LT
| 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
+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
View
25 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]
View
11 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
View
39 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 ::
Please sign in to comment.
Something went wrong with that request. Please try again.