Part 1 done, 2 in process
This commit is contained in:
parent
bfcbd531be
commit
5cf0989ff0
4 changed files with 243 additions and 2 deletions
|
@ -1,5 +1,36 @@
|
|||
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
|
||||
putStrLn "Hello, Haskell!"
|
||||
content <- readFile "sample.txt"
|
||||
(print . partOne) content
|
||||
let tmp = (zip [1 ..] . map partTwoPerLine . lines) content
|
||||
mapM_ print tmp
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue