Skip to content

Commit

Permalink
wibble
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 4, 2023
1 parent 25e6afd commit 636e610
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions solutions/src/2023/04.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,18 @@ module Main where
import Advent (format)
import Data.List (intersect)

-- |
-- | Parse the input and print out the answers to both parts.
--
-- >>> :main
-- 21485
-- 11024379
main :: IO ()
main =
do input <- [format|2023 4 (Card +%d:( +%d)* %|( +%d)*%n)*|]
let wins = toWins input
let wins = [length (a `intersect` b) | (_, a, b) <- input]
print (sum (map points wins))
print (sum (asPart2 wins))

toWins :: [(Int, [Int], [Int])] -> [Int]
toWins input = [length (a `intersect` b) | (_, a, b) <- input]

-- | Convert wins to points for part 1
points :: Int -> Int
points 0 = 0
Expand All @@ -55,6 +52,4 @@ points n = 2 ^ (n - 1)
-- | Convert a list of wins for each card into the number of cards
-- each card turns into.
asPart2 :: [Int] -> [Int]
asPart2 = foldr f []
where
f wins rest = (1 + sum (take wins rest)) : rest
asPart2 = foldr (\wins xs -> (1 + sum (take wins xs)) : xs) []

0 comments on commit 636e610

Please sign in to comment.