Day 4
This commit is contained in:
24
day04/Part2.hs
Normal file
24
day04/Part2.hs
Normal 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)
|
||||
Reference in New Issue
Block a user