37 lines
1.1 KiB
Haskell
37 lines
1.1 KiB
Haskell
module Main where
|
|
|
|
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
|
|
|
|
main :: IO ()
|
|
main = do
|
|
content <- readFile "sample.txt"
|
|
(print . partOne) content
|
|
let tmp = (zip [1 ..] . map partTwoPerLine . lines) content
|
|
mapM_ print tmp
|