From aef09db12a6b5e0260eda49c1bed3067c8f7c686 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 20 Dec 2017 21:50:43 +0000 Subject: [PATCH] Tidied some long lines --- src/advent20/advent20.hs | 30 ++++++++++++++++++++++-------- src/advent20/advent20.ipynb | 6 +++--- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/advent20/advent20.hs b/src/advent20/advent20.hs index a7656a4..4d852a6 100644 --- a/src/advent20/advent20.hs +++ b/src/advent20/advent20.hs @@ -34,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 @@ -59,7 +59,9 @@ 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) + 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) @@ -67,12 +69,15 @@ step particle = particle {position = p', velocity = v'} -- 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) + 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 + else signum p == signum v + && signum v == signum a withMinX particles = minX `elemIndices` absXs @@ -85,8 +90,13 @@ 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 @@ -106,10 +116,14 @@ particlesP = particleP `sepBy` space particleP = particlify <$> (symbol "p=" *> vecP <* separator) <*> (symbol "v=" *> vecP <* separator) <*> (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] diff --git a/src/advent20/advent20.ipynb b/src/advent20/advent20.ipynb index 20f0074..c4512d1 100644 --- a/src/advent20/advent20.ipynb +++ b/src/advent20/advent20.ipynb @@ -948,13 +948,13 @@ }, { "cell_type": "code", - "execution_count": 135, + "execution_count": 138, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "570" + "448" ] }, "metadata": {}, @@ -962,7 +962,7 @@ } ], "source": [ - "part2 35 particles" + "part2 40 particles" ] }, { -- 2.34.1