A defense satellite patrols an 8x8 grid of orbital sectors. Exactly one sector holds the interceptor. Some other sectors hold drifting debris fragments, and some hold shield satellites that block sensor beams; the remaining sectors are empty space.
From its own sector, the interceptor fires a straight tracking beam in each of the four orthogonal directions -- up, down, left, and right. Each beam travels sector by sector, passing freely through empty sectors, until it reaches the first sector along that direction that is not empty. If that first non-empty sector contains a debris fragment, the interceptor captures it. If that first non-empty sector instead contains a shield satellite, the beam is blocked and nothing is captured in that direction, regardless of what lies further beyond the shield satellite. A beam that reaches the edge of the grid without hitting anything captures nothing.
Given the grid, determine how many debris fragments the interceptor captures in total (at most one per direction, so the answer is between 0 and 4).
The input consists of exactly 8 lines, each containing exactly 8 characters with no spaces, describing the grid rows from top to bottom. Each character is one of:
Print a single integer: the total number of debris fragments captured.
The grid is exactly 8 rows by 8 columns. Exactly one cell contains 'I'; every other cell contains 'd', 's', or '.'.
Example 1
Input
........ ........ ...d.... ...s.... ..dI.d.. ........ ...s.... ...d....
Expected
2
Explanation
The interceptor sits at row 4, column 3 (0-indexed). Left: the very next cell (row4,col2) is 'd', so that beam captures. Right: (row4,col4) is empty, then (row4,col5) is 'd', so that beam also captures. Up: the very next cell (row3,col3) is 's', blocking the beam immediately, so the 'd' further up at row2 is never reached. Down: (row5,col3) is empty, but (row6,col3) is 's', blocking before the 'd' at row7 is reached. Total captures: 2.
Example 2
Input
I..d.... d....... ........ ........ ........ ........ ........ ........
Expected
2
Explanation
The interceptor sits at the top-left corner (row0, col0), so the up and left beams have no cells to travel through and capture nothing. Right: cells (0,1) and (0,2) are empty, then (0,3) is 'd' -- captured. Down: the very next cell (1,0) is 'd' -- captured. Total captures: 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →