Files
advent-of-code-2023/day04/Part2.hs
2023-12-04 10:32:27 +01:00

25 lines
944 B
Haskell

module Part2 where
import Commons
getNWinningNumbers :: [Int] -> [Int] -> Int
getNWinningNumbers _ [] = 0
getNWinningNumbers winningNumbers (n: t) | n `elem` winningNumbers = 1 + getNWinningNumbers winningNumbers t
| otherwise = getNWinningNumbers winningNumbers t
updateNCopies :: Int -> Card -> [Card] -> [Card]
updateNCopies 0 card otherCards = otherCards
updateNCopies n card (c: t) = c {nCopies = nCopies card + nCopies c}: updateNCopies (n - 1) card t
updateCopies :: Card -> [Card] -> [Card]
updateCopies card otherCards = let nWinningNumbers = getNWinningNumbers (winningNumbers card) (numbers card)
in updateNCopies nWinningNumbers card otherCards
updateAllCopies :: [Card] -> [Card]
updateAllCopies [] = []
updateAllCopies (c: t) = c: updateAllCopies (updateCopies c t)
getNCopies :: [Card] -> [Int]
getNCopies cards = map nCopies (updateAllCopies cards)