parent
82cce7c6a4
commit
4bbed10f38
|
@ -17,36 +17,40 @@ instance Ord Hand where
|
||||||
handType :: Hand -> Int
|
handType :: Hand -> Int
|
||||||
handType (C x1 x2 x3 x4 x5) =
|
handType (C x1 x2 x3 x4 x5) =
|
||||||
case cList of
|
case cList of
|
||||||
[5] -> 6
|
[5] -> 7
|
||||||
[1, 4] -> 5
|
[1, 4] -> 6 + signum j
|
||||||
[2, 3] -> 4
|
[2, 3] -> 4 + 3 * signum j
|
||||||
[1, 1, 3] -> 3
|
[1, 1, 3] -> 3 + 3 * signum j
|
||||||
[1, 2, 2] -> 2
|
[1, 2, 2] -> 2 + 2 * j
|
||||||
[1, 1, 1, 2] -> 1
|
[1, 1, 1, 2] -> 1 + 2 * signum j
|
||||||
_ -> 0
|
_ -> 0 + signum j
|
||||||
where
|
where
|
||||||
cList = sort $ map length $ group $ sort [x1, x2, x3, x4, x5]
|
cList = sort $ map length $ group sList
|
||||||
|
sList = sort [x1, x2, x3, x4, x5]
|
||||||
|
j = length (filter (==1) sList)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- parse <$> readFile "input"
|
input <- readFile "input"
|
||||||
print $ day07a input
|
print $ day07 (parse False input)
|
||||||
|
print $ day07 (parse True input)
|
||||||
|
|
||||||
|
|
||||||
day07a :: [(Hand, Int)] -> Int
|
day07 :: [(Hand, Int)] -> Int
|
||||||
day07a = sum . zipWith (*) [1..] . map snd . sort
|
day07 = sum . zipWith (*) [1..] . map snd . sort
|
||||||
|
|
||||||
parse :: String -> [(Hand, Int)]
|
parse :: Bool -> String -> [(Hand, Int)]
|
||||||
parse = map (f . words) . lines
|
parse p2 = map (f . words) . lines
|
||||||
where f [x,y] = (parseHand x, read y)
|
where f [x,y] = (parseHand x, read y)
|
||||||
f x = error ("parse: Malformed input: " ++ show x)
|
f x = error ("parse: Malformed input: " ++ show x)
|
||||||
parseHand = g . map digitToCard
|
parseHand = g . map (digitToCard p2)
|
||||||
g [x1, x2, x3, x4, x5] = C x1 x2 x3 x4 x5
|
g [x1, x2, x3, x4, x5] = C x1 x2 x3 x4 x5
|
||||||
g x = error ("parseHand: Malformed input: " ++ show x)
|
g x = error ("parseHand: Malformed input: " ++ show x)
|
||||||
|
|
||||||
digitToCard :: Char -> Int
|
digitToCard :: Bool -> Char -> Int
|
||||||
digitToCard x
|
digitToCard p2 x
|
||||||
| isDigit x = digitToInt x
|
| isDigit x = digitToInt x
|
||||||
|
| x == 'J' && p2 = 1
|
||||||
| x == 'T' = 10
|
| x == 'T' = 10
|
||||||
| x == 'J' = 11
|
| x == 'J' = 11
|
||||||
| x == 'Q' = 12
|
| x == 'Q' = 12
|
||||||
|
|
Loading…
Reference in New Issue
Block a user