Skip to content

Instantly share code, notes, and snippets.

@gilligan
Last active December 8, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilligan/d0899065363b7e5644aa7e76d606e429 to your computer and use it in GitHub Desktop.
Save gilligan/d0899065363b7e5644aa7e76d606e429 to your computer and use it in GitHub Desktop.
Advent of Code - Day 3 Part 1
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