2024-01-26 13:17:52 +01:00
|
|
|
module Main where
|
|
|
|
|
2024-01-26 22:02:25 +01:00
|
|
|
import Data.HashSet (HashSet)
|
|
|
|
import qualified Data.HashSet as HS
|
|
|
|
import Data.List.Split (splitOn)
|
|
|
|
|
|
|
|
partOne :: String -> Int
|
|
|
|
partOne = sum . map computePoints . filter (not . null) . map partOnePerLine . lines
|
|
|
|
|
|
|
|
partOnePerLine :: String -> [Int]
|
|
|
|
partOnePerLine = hashIntersect . strToInt . beforeHashSet
|
|
|
|
|
|
|
|
computePoints :: [Int] -> Int
|
|
|
|
computePoints n = 2 ^ (length n - 1)
|
|
|
|
|
|
|
|
hashIntersect :: (HashSet Int, HashSet Int) -> [Int]
|
|
|
|
hashIntersect (x, y) = HS.toList (HS.intersection x y)
|
|
|
|
|
|
|
|
strToInt :: (String, String) -> (HashSet Int, HashSet Int)
|
|
|
|
strToInt (win, cards) = ((HS.fromList . map getInt . words) win, (HS.fromList . map getInt . words) cards)
|
|
|
|
|
|
|
|
getInt :: String -> Int
|
|
|
|
getInt n = read n :: Int
|
|
|
|
|
|
|
|
beforeHashSet :: String -> (String, String)
|
|
|
|
beforeHashSet = (\v -> (head v, last v)) . splitOn "|" . last . splitOn ":"
|
|
|
|
|
|
|
|
partTwoPerLine :: String -> Int
|
|
|
|
partTwoPerLine = length . partOnePerLine
|
|
|
|
|
2024-01-26 13:17:52 +01:00
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2024-01-26 22:02:25 +01:00
|
|
|
content <- readFile "sample.txt"
|
|
|
|
(print . partOne) content
|
|
|
|
let tmp = (zip [1 ..] . map partTwoPerLine . lines) content
|
|
|
|
mapM_ print tmp
|