module Aoc where
import Text.Parsec
import Text.Parsec.Char
import Text.Parsec.Combinator
import Text.Parsec.String (Parser)
readAndParseStdin :: Parser a -> IO a
readAndParseStdin parser = do
content <- getContents
case parse parser "" content of
Left parseError -> error $ show parseError
Right doc -> return doc
parseMultipleGrids :: Parser [[[Bool]]]
parseMultipleGrids = parseGrid `sepBy` string "\n"
parseGrid :: Parser [[Bool]]
parseGrid = parseGridRow `endBy` char '\n'
parseGridRow :: Parser [Bool]
parseGridRow = many1 parseGridTile
parseGridTile :: Parser Bool
parseGridTile =
choice
[ True <$ char '#',
False <$ char '.'
]
parseMultiChoiceGrid :: Parser [[Maybe Bool]]
parseMultiChoiceGrid = parseMultiChoiceGridRow `endBy` char '\n'
parseMultiChoiceGridRow :: Parser [Maybe Bool]
parseMultiChoiceGridRow = many1 parseMultiChoiceGridTile
parseMultiChoiceGridTile :: Parser (Maybe Bool)
parseMultiChoiceGridTile =
choice
[ Just True <$ char 'O',
Just False <$ char '#',
Nothing <$ char '.'
]
printMultiChoiceGrid :: [[Maybe Bool]] -> IO ()
printMultiChoiceGrid xs = putStrLn $ unlines $ (map . map) multiChoiceGridTileChar xs
where
multiChoiceGridTileChar (Just True) = 'O'
multiChoiceGridTileChar (Just False) = '#'
multiChoiceGridTileChar Nothing = '.'
shoelace :: [(Int, Int)] -> Double
shoelace vertices =
let pairs = zip vertices $ tail vertices
sumProd (x1, y1) (x2, y2) = x1 * y2 - x2 * y1
in fromIntegral (abs . sum $ zipWith sumProd vertices (tail vertices)) / 2