module Part2 where import Commons import Data.List (sort, sortOn) getHandValueTypeFromSorted :: [Int] -> Int getHandValueTypeFromSorted [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 getHandValueTypeFromSorted [a, b, c, d, e] | a == e = 6 | a == d || b == e = 5 | a == c && d == e || a == b && c == e = 4 | a == c || b == d || c == e = 3 | a == b && (c == d || d == e) || b == c && d == e = 2 | a == b || b == c || c == d || d == e = 1 | otherwise = 0 getHandValue :: Hand -> HandValue getHandValue hand = HandValue {hand = hand, value = 100 ^ 5 * (getHandValueTypeFromSorted . 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