Skip to the content

The puddles in the yard

Problem

Doña Refugio, the caretaker, goes out to check the yard as soon as the rain stops. She does not care about the wet tiles one by one, but about the puddles: two wet tiles that touch side by side are the same puddle, and one that only touches another at a corner is a puddle of its own. Write a function that takes yard, a list of strings of the same length, where # is a wet tile and . a dry one. The yard is at most 12 rows by 12 tiles, and it may come in empty or with empty rows.

Return a whole number: how many puddles there are in the yard.

With ["##.", ".#.", "..#"] there are 2. The first three wet tiles touch side by side and make a single puddle; the last one only touches the middle one at a corner, so it is a puddle of its own. If no tile is wet, you return 0.

Examples

  • The example

    ["##.", ".#.", "..#"] → 2

  • The yard stayed dry

    ["...", "...", "..."] → 0

  • A puddle shaped like an L

    [".#..", ".#..", ".##.", "...."] → 1

  • A wet tile in each corner

    ["#.#", "...", "#.#"] → 4

  • Wet tiles along a diagonal

    ["#..", ".#.", "..#"] → 3

  • A yard of a single row

    ["#.##.#"] → 3

  • A yard of a single column

    ["#", ".", "#", "#"] → 2

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def puddles(yard):
    pass

JavaScript

function puddles(yard) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →