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 qualified Data.Text.Read as T
|
||||||
import GHC.Num (integerFromInt)
|
import GHC.Num (integerFromInt)
|
||||||
import Data.Either.Combinators (fromRight')
|
import Data.Either.Combinators (fromRight')
|
||||||
import Debug.Trace (traceShowId)
|
|
||||||
|
|
||||||
parseNums :: [T.Text] -> S.Set Integer
|
parseNums :: [T.Text] -> S.Set Integer
|
||||||
parseNums nums = S.fromList $ fst . fromRight' . T.decimal @Integer <$> nums
|
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
|
<$> T.splitOn (T.pack "|") numbers
|
||||||
parseCard _ = error "parseCard error"
|
parseCard _ = error "parseCard error"
|
||||||
|
|
||||||
getWinnings :: Maybe Integer -> [Integer] -> Integer
|
getWinnings :: Integer -> [Integer] -> Integer
|
||||||
getWinnings Nothing arr = integerFromInt (length arr) + getWinnings (Just $ integerFromInt (length arr)) arr
|
getWinnings 0 _ = 0
|
||||||
getWinnings (Just 0) _ = 0
|
getWinnings n (h : t) = (h + getWinnings h t) + getWinnings (n - 1) t
|
||||||
getWinnings (Just n) (h : t) = (h + getWinnings (Just h) t) + getWinnings (Just $ n - 1) t
|
getWinnings _ [] = error "Out of bounds"
|
||||||
|
|
||||||
|
getWinningsTotal :: [Integer] -> Integer
|
||||||
|
getWinningsTotal arr = (\len -> len + getWinnings len arr) $ integerFromInt (length arr)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
cards <- (parseCard . T.splitOn (T.pack ": ") . T.pack <$>) . lines <$> getContents
|
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