Editor

Minesweeper Numbers

#273 · Grids and matrices · Intermediate · 60 XP · about 12 min

A Minesweeper board is a list of strings of equal length, one per row: '*' is a mine and '.' is a safe square. Before play starts, the game writes on every safe square how many of its neighbours hold a mine. A square has up to eight neighbours: the squares touching it above, below, to the left, to the right and diagonally.

Write mine_counts(board) that returns the board as a new list of strings, with every '.' replaced by its count as a single digit ('0' to '8') and every mine kept as '*'.

Examples

mine_counts(['*...',
             '..*.',
             '....'])      # returns ['*211', '12*1', '0111']
mine_counts(['.'])         # returns ['0']
mine_counts(['***',
             '*.*',
             '***'])       # returns ['***', '*8*', '***']

In the first board, the square to the right of the top-left mine touches two mines: that one, and the mine diagonally below to its right.

Constraints

  • 0 to 100 rows, all of the same length (1 to 100); an empty board [] gives [].

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.