Minesweeper is a popular computer game that you have probably wasted time playing where you try to reveal the cells that are mines in a rectangular grid based on hints of how many neighboring mines each non-mine cell has. And in case you haven't played it, do so here.
A nifty mathematical fact about a Minesweeper grid (a.k.a. board) is that:
A board and its complement have the same mine total number. (Proof)
That is to say that if you have a completely revealed Minesweeper grid, the sum all the numbers on that grid, i.e. the mine total, will equal the mine total of the complement of the grid, which is the grid where every mine has been replaced with a non-mine and every non-mine replaced with a mine.
For example, for the Minesweeper grid
**1..
34321
*2**1
the mine total is 1 + 3 + 4 + 3 + 2 + 1 + 2 + 1 = 17.
The grid's complement is
24***
*****
3*44*
which has mine total 2 + 4 + 3 + 4 + 4 = 17 again.
Write a program that takes in an arbitrary Minesweeper grid in text form where *
represents a mine and 1
through 8
represent the number of mines adjacent to a non-mine cell. You can use .
or 0
or
(space) to represent cells with no mine neighbors, your choice. You can assume that the input grid will be correctly marked, i.e. each non-mine cell will accurately denote the total number of mines immediately adjacent to it orthogonally or diagonally.
Your program needs to print the complement of the grid in the same format (using the same .
, 0
, or
as you expected in the input).
The shortest code in bytes wins.
- Instead of a program you may write a function that takes the input grid as a string and prints or returns the complement grid.
- A trailing newline in the input or output is fine, but there should be no other characters besides those that form the grid.
- You can assume a 1×1 grid will be the smallest input.
Test Cases
All inputs and outputs could be swapped as the complement of the complement is the original grid. The grids can be rotated as well for further test cases.
Input:
111
1*1
111
Output:
***
*8*
***
Input:
.
Output:
*
Input:
*11*1.1**1...1***1.....1*****1..........
Output:
1**2***11*****1.1*******1...1***********
Input: (Cut The Knot example)
**212*32
333*33**
1*22*333
222222*1
*33*2232
2**22*2*
Output:
24***4**
***7**64
*8**7***
******8*
4**7****
*33**5*3
?
) on the line after the final line of the board acceptable, or could I take the number of input lines through the command line? \$\endgroup\$