82cce7c6a4
part 2 might mess things up a little
56 lines
1.3 KiB
Haskell
56 lines
1.3 KiB
Haskell
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)
|