This commit is contained in:
2023-12-07 15:16:58 +01:00
parent 05de161249
commit 1326fe7e33
5 changed files with 122 additions and 0 deletions

34
day07/Part2.hs Normal file
View File

@@ -0,0 +1,34 @@
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