You get a call to come up to the bridge to assist with a surveying task. It seems your ship is passing through an asteroid belt of some form. The onboard space geologist is quite excited by the find and wants to catalogue as much as possible while they are in the area.
The ship has sensors that have taken a detailed reading of the nearby environment, measuring the size and density of the various asteroids and minor planets in the vicinity. You have been tasked with calculating the mean mass of the asteroids and other bodies in the system.
The following represents readings from the ship sensor system. A 0
denotes empty space, where as a number 1
through 9
indicates the density level of an object occupying a portion of space.
0 0 2 3 0 1 1 0
1 0 3 1 0 0 0 0
4 0 1 0 0 0 8 6
0 0 0 0 0 7 0 0
2 0 0 0 0 0 0 0
6 4 4 0 0 1 0 0
0 0 0 0 0 0 0 0
5 5 3 0 0 0 0 0
Fortunately for you these are very square-shaped asteroids, so any individual asteroid will be represented through vertically or horizontally adjacent grid squares, never diagonally. This means that on the right edge of the 3rd and 4th rows of the illustration above, the 086
and 700
indicate two separate asteroids.
The nature of the data collected is especially convenient as it will allow you to use a technique known as depth first search if you wish.
Once you have isolated individual asteroids, the mass of each asteroid is calculated by summing together it's density values.
Based on the above grid, the various asteroids, and their masses, are:
2+3+3+1+1 = 10
1+1 = 2
1+4 = 5
8+6 = 14
7 = 7
2+6+4+4 = 16
1 = 1
5+5+3 = 13
In this example there are 8 astroids with a total of 68 units of mass. This indicates an average mass of 8.5
units, which can be truncated to 8
units.
The input data represents a 100 x 100 grid of space. What is the truncated average mass of the asteroids present?
Created by Edward Zhang and Paul Baumgarten.
A video walkthrough for learning how to solve this problem is available here.