Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 4, 2023
1 parent fb0482a commit 25e6afd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
1 change: 0 additions & 1 deletion solutions/solutions.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -1006,4 +1006,3 @@ executable sln_2023_03
executable sln_2023_04
import: day
main-is: 2023/04.hs
build-depends: containers
30 changes: 14 additions & 16 deletions solutions/src/2023/04.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@ Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
-}
module Main where

import Data.Map (Map)
import Data.Map qualified as Map
import Data.Set qualified as Set

import Advent (format)
import Data.List (intersect)

-- |
--
Expand All @@ -43,20 +40,21 @@ import Advent (format)
main :: IO ()
main =
do input <- [format|2023 4 (Card +%d:( +%d)* %|( +%d)*%n)*|]
let wins = countWins input
let becomes =
Map.mapWithKey
(\i w -> 1 + sum [becomes Map.! j | j <- [i+1 .. i+w]] :: Int)
wins
print (sum (fmap points wins))
print (sum becomes)

countWins :: [(Int, [Int], [Int])] -> Map Int Int
countWins input = Map.fromList
[ (i, length (Set.fromList a `Set.intersection` Set.fromList b))
| (i, a, b) <- input]
let wins = toWins 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
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

0 comments on commit 25e6afd

Please sign in to comment.