1
0
Fork 0
adventofcode/src/Day03.hs

27 lines
849 B
Haskell
Raw Normal View History

2018-11-26 02:32:15 +00:00
module Day03 (main) where
2018-12-03 18:08:06 +00:00
import Data.Text (empty, split, pack, unpack)
import Data.IntMap (IntMap, fromListWith, size, notMember)
import qualified Data.IntMap as M (filter)
2018-12-03 18:08:06 +00:00
type Claim = (Int, [Int])
parse :: String -> Claim
parse str =
2018-12-03 18:08:06 +00:00
let i:l:t:w:h:[] = map (read . unpack) . filter (/= empty) . split (flip elem $ " #@,:x") . pack $ str
in (i, [r * 1000 + c | r <- [t .. t + h - 1], c <- [l .. l + w - 1]])
2018-12-03 18:08:06 +00:00
overlapMap :: [Claim] -> IntMap Int
overlapMap = M.filter (> 1) . fromListWith (+) . (flip zip) (repeat 1) . concat . map snd
part1 :: [Claim] -> Int
2018-12-03 18:08:06 +00:00
part1 = size . overlapMap
part2 :: [Claim] -> Int
part2 claims = fst . head . filter (all (flip notMember $ overlapMap claims) . snd) $ claims
2018-11-26 02:32:15 +00:00
main :: IO ()
main = do
input <- map parse . lines <$> readFile "input/03.txt"
print $ part1 input
print $ part2 input