Last active
December 8, 2018 14:39
-
-
Save gilligan/d0899065363b7e5644aa7e76d606e429 to your computer and use it in GitHub Desktop.
Advent of Code - Day 3 Part 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List (group, sort) | |
import Text.Trifecta | |
data FabricRect = FabricRect { fId :: Integer | |
, fWidth :: Integer | |
, fHeight :: Integer | |
, fX :: Integer | |
, fY :: Integer | |
} deriving (Show) | |
parseFabric :: Parser FabricRect | |
parseFabric = do | |
_ <- char '#' | |
fId <- integer | |
_ <- string "@ " | |
fX <- integer | |
_ <- char ',' | |
fY <- integer | |
_ <- string ": " | |
fWidth <- integer | |
_ <- char 'x' | |
fHeight <- integer | |
return $ FabricRect fId fWidth fHeight fX fY | |
getFabrics :: FilePath -> IO (Result [FabricRect]) | |
getFabrics path = do | |
inputData <- lines <$> readFile path | |
return $ sequence | |
$ parseString parseFabric mempty <$> inputData | |
getPoints :: FabricRect -> [(Integer, Integer)] | |
getPoints (FabricRect _ w h xPos yPos) = [(x,y) | x <- [xPos..xPos+w-1], y <- [yPos..yPos+h-1]] | |
solve :: FilePath -> IO () | |
solve p = do | |
fs <- getFabrics p | |
case fs of | |
Success fs -> print $ length $ filter (\x -> length x > 1) . group . sort $ foldMap getPoints fs | |
_ -> print "failed to parse" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment