Welcome! Are you ready to dive into the world of array manipulations? Today, we’ll explore an engaging problem that combines logic, simulation, and programming skills. Imagine a small town with houses, balloons, and a unique sharing game.
Let’s uncover the solution step by step!
In a small town, houses are numbered sequentially from 1 to n
, each initially holding a specific number of balloons. During a town festival, the houses participate in a game where they share balloons according to these rules:
- At each step, each house sends half of its balloons to the next house. For the last house, the next house is the first one, creating a circular pattern.
- The number of balloons shared is always rounded down (e.g., if a house has 5 balloons, it sends 2).
- The game ends when the number of balloons at each house stops changing between steps.
Your task is to implement a Ruby function simulate_balloon_game(balloons)
that simulates this game and returns the number of steps it takes for the balloon distribution to stabilize.
Example:
If balloons = [4, 1, 2]
, the output should be 3
. Here’s why:
- After step 1:
[3, 3, 1]
- After step 2:
[2, 3, 2]
- After step 3:
[2, 3, 2]
(no change from the previous step)
Thus, it takes 3 steps for the game to stabilize.
This task requires understanding cyclic arrays, where the last index wraps around to the first. For example, house n
sends balloons to house 1
. This cyclical behavior makes it essential to handle array indices carefully. Using modular arithmetic (%
) helps manage this wrapping behavior efficiently.
We’ll start by setting up a loop to simulate each step of the game. The loop will terminate when the balloon distribution stops changing.
Here's the foundation of our solution:
Ruby1def simulate_balloon_game(balloons) 2 steps = 0 3 loop do 4 steps += 1 5 new_balloons = balloons.dup # Create a copy to hold updated values 6 # Balloon sharing logic will be implemented here 7 break if new_balloons == balloons # Stop when no change occurs 8 balloons = new_balloons # Update for the next step 9 end 10 steps 11end
This snippet tracks the number of steps and ensures that we terminate the loop once the balloon distribution stabilizes.
Now, we’ll implement the core logic: sharing balloons among houses. Each house sends half of its balloons to the next house, and the last house wraps around to the first. The updated balloon counts are stored in a new array to avoid modifying the original array mid-loop.
Here’s how we’ll add the sharing logic:
Ruby1 n = balloons.length 2 (0...n).each do |i| 3 share = balloons[i] / 2 # Calculate balloons to share 4 new_balloons[i] -= share # Deduct shared balloons 5 new_balloons[(i + 1) % n] += share # Add to the next house 6 end
Using modular arithmetic, (i + 1) % n
, ensures the last house shares balloons with the first, maintaining the circular nature of the game.
Here’s the complete implementation of the simulate_balloon_game
function, incorporating all the steps:
Ruby1def simulate_balloon_game(balloons) 2 n = balloons.length 3 steps = 0 4 5 loop do 6 steps += 1 7 new_balloons = balloons.dup # Duplicate to avoid modifying the array mid-update 8 9 (0...n).each do |i| 10 share = balloons[i] / 2 # Balloons to share 11 new_balloons[i] -= share # Reduce balloons at current house 12 new_balloons[(i + 1) % n] += share # Add balloons to the next house 13 end 14 15 break if new_balloons == balloons # Stop if the array doesn't change 16 balloons = new_balloons # Update the original array for the next iteration 17 end 18 19 steps 20end
This implementation efficiently simulates the game and calculates the number of steps required for the balloon distribution to stabilize.
Congratulations! You’ve successfully tackled a real-world-inspired simulation using array manipulation and cyclic logic. This task demonstrated how to manage dynamic processes and efficiently terminate based on specific conditions.
Keep practicing similar challenges to deepen your understanding and build your problem-solving skills. Happy coding!