parent
a02391702d
commit
82cce7c6a4
|
@ -0,0 +1,55 @@
|
|||
import Data.Function (on)
|
||||
import Data.List
|
||||
import Data.Char
|
||||
|
||||
data Hand =
|
||||
C Int Int Int Int Int
|
||||
deriving (Eq, Show)
|
||||
|
||||
instance Ord Hand where
|
||||
compare x y =
|
||||
let r = (compare `on` handType) x y
|
||||
f (C x1 x2 x3 x4 x5) = (x1, x2, x3, x4, x5)
|
||||
in case r of
|
||||
EQ -> (compare `on` f) x y
|
||||
_ -> r
|
||||
|
||||
handType :: Hand -> Int
|
||||
handType (C x1 x2 x3 x4 x5) =
|
||||
case cList of
|
||||
[5] -> 6
|
||||
[1, 4] -> 5
|
||||
[2, 3] -> 4
|
||||
[1, 1, 3] -> 3
|
||||
[1, 2, 2] -> 2
|
||||
[1, 1, 1, 2] -> 1
|
||||
_ -> 0
|
||||
where
|
||||
cList = sort $ map length $ group $ sort [x1, x2, x3, x4, x5]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
input <- parse <$> readFile "input"
|
||||
print $ day07a input
|
||||
|
||||
|
||||
day07a :: [(Hand, Int)] -> Int
|
||||
day07a = sum . zipWith (*) [1..] . map snd . sort
|
||||
|
||||
parse :: String -> [(Hand, Int)]
|
||||
parse = map (f . words) . lines
|
||||
where f [x,y] = (parseHand x, read y)
|
||||
f x = error ("parse: Malformed input: " ++ show x)
|
||||
parseHand = g . map digitToCard
|
||||
g [x1, x2, x3, x4, x5] = C x1 x2 x3 x4 x5
|
||||
g x = error ("parseHand: Malformed input: " ++ show x)
|
||||
|
||||
digitToCard :: Char -> Int
|
||||
digitToCard x
|
||||
| isDigit x = digitToInt x
|
||||
| x == 'T' = 10
|
||||
| x == 'J' = 11
|
||||
| x == 'Q' = 12
|
||||
| x == 'K' = 13
|
||||
| x == 'A' = 14
|
||||
| otherwise = error ("digitToCard: Malformed input: " ++ show x)
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,5 @@
|
|||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
Loading…
Reference in New Issue
Block a user