{-# Language QuasiQuotes #-}
module Main (main) where
import Advent (format)
import Data.List (intersect)
main :: IO ()
IO ()
main =
do [(Int, [Int], [Int])]
input <- [format|2023 4 (Card +%d:( +%d)* %|( +%d)*%n)*|]
let wins :: [Int]
wins = [[Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length ([Int]
a [Int] -> [Int] -> [Int]
forall a. Eq a => [a] -> [a] -> [a]
`intersect` [Int]
b) | (Int
_, [Int]
a, [Int]
b) <- [(Int, [Int], [Int])]
input]
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ((Int -> Int) -> [Int] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map Int -> Int
points [Int]
wins))
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum ([Int] -> [Int]
asPart2 [Int]
wins))
points :: Int -> Int
points :: Int -> Int
points Int
0 = Int
0
points Int
n = Int
2 Int -> Int -> Int
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
asPart2 :: [Int] -> [Int]
asPart2 :: [Int] -> [Int]
asPart2 = (Int -> [Int] -> [Int]) -> [Int] -> [Int] -> [Int]
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Int
wins [Int]
xs -> Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [Int] -> Int
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (Int -> [Int] -> [Int]
forall a. Int -> [a] -> [a]
take Int
wins [Int]
xs) Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: [Int]
xs) []