This commit is contained in:
Gattix 2023-12-07 14:04:36 +01:00
parent 4bbed10f38
commit 1bb30457aa

View File

@ -1,6 +1,6 @@
import Data.Char
import Data.Function (on) import Data.Function (on)
import Data.List import Data.List
import Data.Char
data Hand = data Hand =
C Int Int Int Int Int C Int Int Int Int Int
@ -27,7 +27,7 @@ handType (C x1 x2 x3 x4 x5) =
where where
cList = sort $ map length $ group sList cList = sort $ map length $ group sList
sList = sort [x1, x2, x3, x4, x5] sList = sort [x1, x2, x3, x4, x5]
j = length (filter (==1) sList) j = length (filter (== 1) sList)
main :: IO () main :: IO ()
main = do main = do
@ -35,17 +35,17 @@ main = do
print $ day07 (parse False input) print $ day07 (parse False input)
print $ day07 (parse True input) print $ day07 (parse True input)
day07 :: [(Hand, Int)] -> Int day07 :: [(Hand, Int)] -> Int
day07 = sum . zipWith (*) [1..] . map snd . sort day07 = sum . zipWith (*) [1 ..] . map snd . sort
parse :: Bool -> String -> [(Hand, Int)] parse :: Bool -> String -> [(Hand, Int)]
parse p2 = map (f . words) . lines parse p2 = map (f . words) . lines
where f [x,y] = (parseHand x, read y) where
f x = error ("parse: Malformed input: " ++ show x) f [x, y] = (parseHand x, read y)
parseHand = g . map (digitToCard p2) f x = error ("parse: Malformed input: " ++ show x)
g [x1, x2, x3, x4, x5] = C x1 x2 x3 x4 x5 parseHand = g . map (digitToCard p2)
g x = error ("parseHand: Malformed input: " ++ show x) g [x1, x2, x3, x4, x5] = C x1 x2 x3 x4 x5
g x = error ("parseHand: Malformed input: " ++ show x)
digitToCard :: Bool -> Char -> Int digitToCard :: Bool -> Char -> Int
digitToCard p2 x digitToCard p2 x