Board Score

Asked at Airbnb

Reported by the problem's creator, not verified

A board game is scored from a grid of tiles. Each tile is a two-character string: a capital letter for its area type, followed by a digit for the number of crowns on it, for example "G2" is a type G tile with 2 crowns.

A region is a maximal group of tiles of the same area type that are connected through shared sides (up, down, left, right; touching only at a corner does not connect). The crown digits do not matter for connecting, only the letter. The score of a region is the number of tiles in it multiplied by the total crowns on those tiles. A region with no crowns scores 0. The score of the board is the sum of the scores of all its regions.

Write board_score(board), where board is a rectangular list of rows of tile strings, and return the score of the board.

Examples

  1. Example 1

    Input: board = [["G1","G2","W0","W1","S1"],["G2","G3","W0","W1","S1"],["S2","S3","S1","G1","S1"],["G1","G2","W0","W1","S1"],["G1","G2","W0","W1","S1"]]

    Output: 116

    Explanation: The regions are: top-left G (4 cells, 8 crowns, 32), top W (4 cells, 2 crowns, 8), the S column on the right edge (5 cells, 5 crowns, 25), the row S2 S3 S1 (3 cells, 6 crowns, 18), the lone G1 (1 cell, 1 crown, 1), bottom-left G (4 cells, 6 crowns, 24) and bottom W (4 cells, 2 crowns, 8). The total is 32 + 8 + 25 + 18 + 1 + 24 + 8 = 116.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.