June 5, 2023

Astronomy Board Game: A HackerRank Solution in Python

In an astronomy board game, N planets in an imaginary universe do not follow the normal law of gravitation. All the planets are positioned in a row. The planetary system can be in a stable state only if the sum of the mass of all planets at even positions is equal to the sum of the mass of planets at the odd positions. Initially, the system is not stable, but a player can destroy one planet to make it stable. Find the planet that should be destroyed to make the system stable. If no such planet exists, then return -1. If there are multiple such planets, then destroy the planet with the smallest index and return the index of the destroyed planet.

Example

Let N=5 and planets = (2,4,6,3,4]. 
Destroying the fourth planet of mass 3 will result in planets = [2,4,6,4], and here, the sum of odd positioned planets is (2+6)=8, and the sum of even positioned planets is (4+4)=8, and both are equal now. Hence, we destroy the fourth planet.

Astronomy board game hackerrank solution 

Hi, everyone! I'm Akshay, and today I'm going to share with you my solution to an interesting problem that involves the intersection of computer science and astronomy.

Imagine we're playing an innovative board game where we're entrusted with the stability of a planetary system. This isn't just any planetary system, though. The stability condition here is unique: the sum of the mass of all planets at even positions has to be equal to the sum of the mass of planets at the odd positions. The tricky part is that, initially, the system is not stable. As a player, we can only destroy one planet to make it stable.

We need to write a program to determine which planet to destroy to make the system stable. If there's no such planet, the program should return -1. If there are multiple options, we have to destroy the planet with the smallest index.

Let's walk through the Python solution.

def stabilize_planets(N, planets):
    even_sum = sum(planets[i] for i in range(0, N, 2))
    odd_sum = sum(planets[i] for i in range(1, N, 2))
    
    if even_sum == odd_sum: 
        return -1

    for i in range(N):
        if i % 2 == 0:
            even_sum -= planets[i]
        else:
            odd_sum -= planets[i]
        
        if even_sum == odd_sum: 
            return i
            
        if i % 2 == 0:
            odd_sum += planets[i]
        else:
            even_sum += planets[i]
            
    return -1

This stabilize_planets function works in the following way:

First, we calculate the initial sum of the masses of the planets at even and odd positions. If they're already equal, we return -1 because there's no need to destroy any planet.

We then loop over each planet, first subtracting its mass from the sum of the masses at its position (even or odd). We check if the sums are now equal. If they are, we return the index of the current planet, which should be destroyed to achieve stability.

If the sums are not equal, we add the mass of the planet back to the sum of the opposite position (odd if the planet is at an even position and vice versa).

If we loop through all the planets without finding a match, we return -1.

This function should be able to handle the planetary stability problem efficiently!

Remember, programming is not just about getting the correct solution; it's also about coming up with an efficient strategy and implementing it in a clean, understandable way. Hope you enjoyed this tutorial and found it helpful. Happy coding!