This commit is contained in:
2023-12-04 10:31:36 +01:00
parent fd46f6dfa2
commit e746e22b0b
5 changed files with 90 additions and 0 deletions

24
day04/Part2.hs Normal file
View File

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