From: Neil Smith Date: Wed, 12 Dec 2018 08:28:12 +0000 (+0000) Subject: Better parser, better display X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=b5225f8527d35a85382e8ab4e9a47646d8b8a2fd;p=advent-of-code-18.git Better parser, better display --- diff --git a/src/advent10/advent10-iterate-zip.hs b/src/advent10/advent10-iterate-zip.hs index ea26113..3d3811b 100644 --- a/src/advent10/advent10-iterate-zip.hs +++ b/src/advent10/advent10-iterate-zip.hs @@ -17,8 +17,8 @@ import qualified Data.Set as S type Coord = (Integer, Integer) -- x, y type Bounds = (Integer, Integer, Integer, Integer) -- minX, maxX, minY, maxY data Particle = Particle {_position :: Coord, _velocity :: Coord} deriving (Eq, Show) -type Swarm = [Particle] -type Grid = S.Set Coord +type Grid = [Particle] +type Matrix = S.Set Coord main :: IO () main = do @@ -28,14 +28,14 @@ main = do putStrLn $ showParticles final print time -part0 :: Swarm -> (Swarm, Int) +part0 :: Grid -> (Grid, Int) part0 particles = (snd $ last $ gridPairs, length gridPairs) where gridPairs = findEnd particles -runParticles :: Swarm -> [Swarm] +runParticles :: Grid -> [Grid] runParticles = iterate updateAll -findEnd :: Swarm -> [(Swarm, Swarm)] +findEnd :: Grid -> [(Grid, Grid)] findEnd particles = takeWhile firstLarger gridPairs where grids = runParticles particles gridPairs = zip grids (drop 1 grids) @@ -43,11 +43,11 @@ findEnd particles = takeWhile firstLarger gridPairs -boundsArea :: Swarm -> Integer +boundsArea :: Grid -> Integer boundsArea particles = (maxX - minX) * (maxY - minY) where (minX, maxX, minY, maxY) = findBounds particles -findBounds :: Swarm -> Bounds +findBounds :: Grid -> Bounds findBounds particles = ( minX -- small x edge , maxX -- large x edge @@ -66,21 +66,21 @@ update particle = particle {_position = (x + vx, y + vy)} (vx, vy) = _velocity particle -updateAll :: Swarm -> Swarm +updateAll :: Grid -> Grid updateAll = map update -showParticles :: Swarm -> String +showParticles :: Grid -> String showParticles particles = intercalate "\n" rows where (minX, maxX, minY, maxY) = findBounds particles - grid = S.fromList $ map _position particles - rows = [showRow y minX maxX grid | y <- [minY..maxY] ] + swarm = S.fromList $ map _position particles + rows = [showRow y minX maxX swarm | y <- [minY..maxY] ] -showCell :: Integer -> Integer -> Grid -> Char +showCell :: Integer -> Integer -> Matrix -> Char showCell x y grid - | (x, y) `S.member` grid = '*' - | otherwise = ' ' + | (x, y) `S.member` grid = '\x2593' + | otherwise = '\x2591' -showRow :: Integer -> Integer -> Integer -> Grid -> String +showRow :: Integer -> Integer -> Integer -> Matrix -> String showRow y minX maxX grid = [showCell x y grid | x <- [minX..maxX] ] -- Parse the input file @@ -105,12 +105,12 @@ particleFileP = many particleP particleP = particlify <$> positionP <*> velocityP where particlify x v = Particle x v -positionP = posPrefix *> pairP <* suffix -velocityP = velPrefix *> pairP <* suffix +positionP = between posPrefix suffix pairP +velocityP = between velPrefix suffix pairP pairP = (,) <$> signedInteger <* commaP <*> signedInteger -successfulParse :: Text -> Swarm +successfulParse :: Text -> Grid successfulParse input = case parse particleFileP "input" input of Left _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err diff --git a/src/advent10/advent10.hs b/src/advent10/advent10.hs index 59d7c47..19b1f70 100644 --- a/src/advent10/advent10.hs +++ b/src/advent10/advent10.hs @@ -12,7 +12,6 @@ import Text.Megaparsec.Char import qualified Text.Megaparsec.Char.Lexer as L import qualified Control.Applicative as CA -import qualified Data.Map.Strict as M import qualified Data.Set as S type Coord = (Integer, Integer) -- x, y @@ -30,13 +29,14 @@ main = do print time -part1 particles - | area' > area = showParticles particles - | otherwise = part1 particles' - where particles' = updateAll particles - area = boundsArea particles - area' = boundsArea particles' +-- part1 particles +-- | area' > area = showParticles particles +-- | otherwise = part1 particles' +-- where particles' = updateAll particles +-- area = boundsArea particles +-- area' = boundsArea particles' +part2 :: Integer -> [Particle] -> (Integer, String) part2 time particles | area' > area = (time, showParticles particles) | otherwise = part2 (time+1) particles' @@ -78,8 +78,8 @@ showParticles particles = intercalate "\n" rows showCell :: Integer -> Integer -> Grid -> Char showCell x y grid - | (x, y) `S.member` grid = '*' - | otherwise = ' ' + | (x, y) `S.member` grid = '\x2593' + | otherwise = '\x2591' showRow :: Integer -> Integer -> Integer -> Grid -> String showRow y minX maxX grid = [showCell x y grid | x <- [minX..maxX] ] @@ -106,8 +106,8 @@ particleFileP = many particleP particleP = particlify <$> positionP <*> velocityP where particlify x v = Particle x v -positionP = posPrefix *> pairP <* suffix -velocityP = velPrefix *> pairP <* suffix +positionP = between posPrefix suffix pairP +velocityP = between velPrefix suffix pairP pairP = (,) <$> signedInteger <* commaP <*> signedInteger