Day 4
This commit is contained in:
28
day04/Commons.hs
Normal file
28
day04/Commons.hs
Normal file
@@ -0,0 +1,28 @@
|
||||
module Commons where
|
||||
|
||||
import Data.List.Utils (split)
|
||||
import GHC.IO.Handle (isEOF)
|
||||
|
||||
|
||||
data Card = Card { cid :: Int, nCopies :: Int, winningNumbers :: [Int], numbers :: [Int] } deriving (Show)
|
||||
|
||||
|
||||
parseNumbers :: String -> [Int]
|
||||
parseNumbers [] = []
|
||||
parseNumbers (' ': n1: n2: t) = read [n1, n2]: parseNumbers t
|
||||
|
||||
parseCard :: String -> Card
|
||||
parseCard ('C': 'a': 'r': 'd': ' ': i1: i2: i3: ':': t) = let (rawWN: rawN: _) = split " |" t
|
||||
in Card {cid = read [i1, i2, i3],
|
||||
nCopies = 1,
|
||||
winningNumbers = parseNumbers rawWN,
|
||||
numbers = parseNumbers rawN}
|
||||
|
||||
parse :: IO [Card]
|
||||
parse = do done <- isEOF
|
||||
if done
|
||||
then return []
|
||||
else do line <- getLine
|
||||
let card = parseCard line
|
||||
otherCards <- parse
|
||||
return (card: otherCards)
|
||||
Reference in New Issue
Block a user