import Text.Megaparsec.Text (Parser)
import qualified Control.Applicative as CA
--- import Data.Vector ((!), (//))
import qualified Data.Vector as V
import Data.List
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'
+ (p', v') = V.unzip pv'
updatePV p v a = (p + v + a, v + a)
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
+ 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
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
},
{
"cell_type": "code",
- "execution_count": 123,
+ "execution_count": 135,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "448"
+ "570"
]
},
"metadata": {},
}
],
"source": [
- "part2 500 particles"
+ "part2 35 particles"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 137,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[(10,1000),(11,979),(12,979),(13,973),(14,955),(15,932),(16,921),(17,906),(18,874),(19,858),(20,831),(21,821),(22,809),(23,795),(24,791),(25,771),(26,752),(27,723),(28,703),(29,669),(30,648),(31,634),(32,622),(33,617),(34,589),(35,570),(36,542),(37,522),(38,494),(39,481),(40,448),(41,448),(42,448),(43,448),(44,448),(45,448)]"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "[(n, part2 n particles) | n <- [10..45]]"
]
},
{