Files
advent-of-code-2023/day07/Part2.hs
2023-12-07 15:28:32 +01:00

35 lines
1.6 KiB
Haskell

module Part2 where
import Commons
import Data.List (sort, sortOn)
getHandValueTypeFromSortedJokers :: [Int] -> Int
getHandValueTypeFromSortedJokers [0, b, c, d, e] | e == 0 = 6
| d == 0 || b == e = 6
| c == 0 && d == e || b == 0 && c == e = 6
| c == 0 || b == d || c == e = 5
| b == 0 && (c == d || d == e) = 5
| b == c && d == e = 4
| b == 0 || b == c || c == d || d == e = 3
| otherwise = 1
getHandValueTypeFromSortedJokers values = getHandValueTypeFromSorted values
getHandValue :: Hand -> HandValue
getHandValue hand = HandValue {hand = hand, value = 100 ^ 5 * (getHandValueTypeFromSortedJokers . sort) (values hand)
+ getHandSecondValue (values hand)}
sortHands :: [Hand] -> [Hand]
sortHands = map hand . sortHandValues . map getHandValue
applyJokers :: [Int] -> [Int]
applyJokers [] = []
applyJokers (v: t) | v == 11 = 0: applyJokers t
| otherwise = v: applyJokers t
applyJokersToHands :: [Hand] -> [Hand]
applyJokersToHands = map (\ hand -> Hand {values = applyJokers (values hand), bid = bid hand})
getTotalWinnings :: [Hand] -> Int
getTotalWinnings = getWinningsFromSorted 1 . sortHands . applyJokersToHands