Spencer Racca-Gwozdzik and Gabriel Guinn
Rohan Crossland and Ethan Spence
Jacob Endrina and Lucas Calaff
Mackenzie Leibee and Noah Sprenger
Noah McCullough and Stephen Rice
Ella Slattery and Jinwoo Choi
Bonacic Bonacic and Alex Lopez
Grey Roppolo and Devan Meyer
Brendan Bell and Cian Monaghan
Brett Garon and Noah Zimmer
Asa Bonner and Liam Bradley and Lucas Takiff
The task is to "simulate" the behavior of a group of unusual asteroids that all travel along the same line, and at exactly the same speed. Some of them are moving to the left though, while others are moving to the right. You're given a queue of integers representing a row of asteroids where the sign specifies which way they're moving (negative values are moving to the left, and positive values are moving to the right), and the magnitude (the absolute value of the integer) tells you how big the asteroid is. If asteroids collide, the result is a new asteroid that's the sum of the two input values. If a 5
collides with a -3
for example, the result would be 2
— a smaller asteroid that's still travelling to the right. (If the sum is zero, the two asteroids wipe each other out and there's nothing left.)
You need to write a program that takes a queue of integers as input and returns a queue of values describing the state of the system after all collisions have occurred. Below are some sample inputs and outputs to give you a better sense of how the process should work. (The values are shown with [ ]
's but they really are queues.)
Input Output Explanation [5, -3] [2] The two collide, leaving one asteroid [4, 5, -3] [4, 2] The 5 hits -3, but result doesn't collide with the 4 [5, -5] [ ] The two wipe each other out [-2, 5, -3, -1] [-2, 1] The -2 goes left, the 5 hits both -3 and -1 [2, 8, -1, 10, -2, 6, -4, -7] [2, 7, 3] It's complicated...
When simulating asteroids, we only get collisions if an asteroid is moving to the right (positive) and encounters a later left-moving (negative) asteroid. There's a similar logic here to the problems in the previous paragraph: When we find a right-moving asteroid, we don't yet know who it's going to hit. (It might not even hit another asteroid!) Right-moving asteroids can therefore be pushed onto a stack, and when we encounter a left-moving asteroid we can "collide it" with the asteroid on the top of the stack. It's a little more complicated than matching parenthesis though, since after the collision we could end up with a left-moving asteroid that might cause additional collisions (with asteroids on the stack), or a right-moving asteroid that needs to be pushed back on the stack or, if the the two have the same magnitude, they might obliterate one another completely.
Also, note that we determine "survivors" from left to right, as we process the input queue. (Those are the ones we're sure aren't going to run into anything — either because they're moving to the left and there are no right-moving asteroids to their left, or there are right-moving asteroids present at the end of the collision process that won't ever hit anything.) That means we don't need the full generality of a list to store the results. A queue will work just fine, since we only need to store new survivors at the end of the queue.
Asteroids
and that it contains a static method called collide
that takes an input queue of Integer
s and returns a queue of Integer
s. You'll lose points if those names and types aren't exactly right.
collide
works correctly, and only uses stacks and queues.
@param
and @return
directives) above each method. When you're convinced it's ready to go, zip up your project folder and submit it via the Canvas submission tool for Assignment #4.