Prettier day 04
This commit is contained in:
parent
9ff5861b5d
commit
c79673ec86
|
@ -7,7 +7,6 @@ import qualified Data.Text as T
|
|||
import qualified Data.Text.Read as T
|
||||
import GHC.Num (integerFromInt)
|
||||
import Data.Either.Combinators (fromRight')
|
||||
import Debug.Trace (traceShowId)
|
||||
|
||||
parseNums :: [T.Text] -> S.Set Integer
|
||||
parseNums nums = S.fromList $ fst . fromRight' . T.decimal @Integer <$> nums
|
||||
|
@ -19,12 +18,15 @@ parseCard [T.words -> [T.unpack -> "Card", T.decimal @Integer -> Right _], numbe
|
|||
<$> T.splitOn (T.pack "|") numbers
|
||||
parseCard _ = error "parseCard error"
|
||||
|
||||
getWinnings :: Maybe Integer -> [Integer] -> Integer
|
||||
getWinnings Nothing arr = integerFromInt (length arr) + getWinnings (Just $ integerFromInt (length arr)) arr
|
||||
getWinnings (Just 0) _ = 0
|
||||
getWinnings (Just n) (h : t) = (h + getWinnings (Just h) t) + getWinnings (Just $ n - 1) t
|
||||
getWinnings :: Integer -> [Integer] -> Integer
|
||||
getWinnings 0 _ = 0
|
||||
getWinnings n (h : t) = (h + getWinnings h t) + getWinnings (n - 1) t
|
||||
getWinnings _ [] = error "Out of bounds"
|
||||
|
||||
getWinningsTotal :: [Integer] -> Integer
|
||||
getWinningsTotal arr = (\len -> len + getWinnings len arr) $ integerFromInt (length arr)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
cards <- (parseCard . T.splitOn (T.pack ": ") . T.pack <$>) . lines <$> getContents
|
||||
print $ getWinnings Nothing (integerFromInt . length . S.toList . foldl1 S.intersection <$> cards)
|
||||
print $ getWinningsTotal (integerFromInt . length . S.toList . foldl1 S.intersection <$> cards)
|
Loading…
Reference in New Issue