X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=src%2Fadvent20%2Fadvent20.hs;h=4822db9ec324c983cee9017925a74533840d30cf;hb=HEAD;hp=5aac088be3b995678a2f72e9bf43536be7ad1869;hpb=3a37ea61c6b83fc84fcf3369c77c399a91fd9759;p=advent-of-code-17.git diff --git a/src/advent20/advent20.hs b/src/advent20/advent20.hs index 5aac088..4822db9 100644 --- a/src/advent20/advent20.hs +++ b/src/advent20/advent20.hs @@ -13,7 +13,6 @@ import qualified Text.Megaparsec.Lexer as L import Text.Megaparsec.Text (Parser) import qualified Control.Applicative as CA --- import Data.Vector ((!), (//)) import qualified Data.Vector as V import Data.List @@ -35,7 +34,7 @@ main = do text <- TIO.readFile "data/advent20.txt" let particles = successfulParse text print $ part1 particles - print $ part2 500 particles + print $ part2 50 particles part1 :: [Particle] -> Int @@ -60,20 +59,25 @@ simulateC t particles = simulateC (t - 1) (map step particles') step :: Particle -> Particle step particle = particle {position = p', velocity = v'} - where pv' = V.zipWith3 updatePV (position particle) (velocity particle) (acceleration particle) - !(p', v') = V.unzip pv' + where pv' = V.zipWith3 updatePV (position particle) + (velocity particle) + (acceleration particle) + (p', v') = V.unzip pv' updatePV p v a = (p + v + a, v + a) -- Checks whether a particle could ever get closer to the origin than it is now. quiescent :: Particle -> Bool quiescent particle = and qDimensions - where qDimensions = V.zipWith3 sameSigns (position particle) (velocity particle) (acceleration particle) - sameSigns !p !v !a = if a == 0 && v == 0 - then True - else if a == 0 - then signum p == signum v - else signum p == signum v && signum v == signum a + where qDimensions = V.zipWith3 sameSigns (position particle) + (velocity particle) + (acceleration particle) + sameSigns p v a = if a == 0 && v == 0 + then True + else if a == 0 + then signum p == signum v + else signum p == signum v + && signum v == signum a withMinX particles = minX `elemIndices` absXs @@ -84,11 +88,15 @@ pAbsX :: Particle -> Integer pAbsX particle = V.foldl1' (+) $ V.map abs (position particle) - removeColliders particles = particles' where positions = map position particles - duplicatePositions = S.fromList $ concat $ filter (\g -> length g > 1) $ group $ sort positions - particles' = filter (\p -> not (S.member (position p) duplicatePositions)) particles + duplicatePositions = S.fromList $ concat + $ filter (\g -> length g > 1) + $ group + $ sort positions + particles' = filter (\p -> not (S.member (position p) + duplicatePositions)) + particles @@ -101,17 +109,20 @@ integer = lexeme L.integer signedInteger = L.signed sc integer symbol = L.symbol sc -separator = symbol ", " comma = symbol "," particlesP = particleP `sepBy` space -particleP = particlify <$> (symbol "p=" *> vecP <* separator) - <*> (symbol "v=" *> vecP <* separator) +particleP = particlify <$> (symbol "p=" *> vecP <* comma) + <*> (symbol "v=" *> vecP <* comma) <*> (symbol "a=" *> vecP) - where particlify p v a = Particle {position = p, velocity = v, acceleration = a} + where particlify p v a = Particle { position = p + , velocity = v + , acceleration = a + } -vecP = V.fromList <$> between (symbol "<") (symbol ">") (signedInteger `sepBy` comma) +vecP = V.fromList <$> between (symbol "<") (symbol ">") + (signedInteger `sepBy` comma) successfulParse :: Text -> [Particle]