SpinalHDL
1 / 1
Description
Bunker Network
You've located an enemy bunker network arranged on a grid (up to 128x128). Each bunker provides mutual defensive support to its neighbors - a bunker with 4 or more adjacent bunkers (in the 8 surrounding positions) is too well-protected to assault directly.
However, bunkers with fewer than 4 neighbors are vulnerable and can be destroyed. Your reconnaissance team needs to identify how many bunkers are currently vulnerable to attack.
Given a 2D grid indicating bunker positions, output the count of bunkers that have fewer than 4 adjacent bunkers.
Example grid (X = bunker, . = empty):
X X X
X X .
X . .
- Position (0,0): 3 neighbors → vulnerable
- Position (0,1): 4 neighbors → protected
- Position (0,2): 2 neighbors → vulnerable
- Position (1,0): 4 neighbors → protected
- Position (1,1): 5 neighbors → protected
- Position (2,0): 2 neighbors → vulnerable
Answer: 3 vulnerable bunkers
Input
grid
2D Array of Boolean (1-bit)2D grid of bunker positions.
Output
16-bit Unsigned Integer
Count of vulnerable bunkers.