6 kyu

Counting ASCII Art Squares

63 of 99mnemonix666

Description:

Your task is to write a function which counts the number of squares contained in an ASCII art picture.

The input pictures contain rectangles---some of them squares---drawn with the characters -, |, and +, where - and | are used to represent horizontal and vertical sides, and + is used to represent corners and intersections. Each picture may contain multiple, possibly overlapping, rectangles.

A simple example input looks like this:

+--+  +----+
|  |  |    |    +-+
|  |  +----+    | |
+--+            +-+

There are two squares and one rectangle in this picture, so your function should return 2 for this input.

The following picture does not contain any squares, so the answer for this one is 0:

+------+
|      |
+------+

Here is another, more complex input:

+---+
|   |
| +-+-+
| | | |
+-+-+ |
  |   |
  +---+

The answer for this one is 3: two big squares, and a smaller square formed by the intersection of the two bigger ones. Similarly, the answer for the following picture is 5:

+-+-+
| | |
+-+-+
| | |
+-+-+

A more complex one below, with a lot of entangledrectangles, but there is one single square in there

+---+        *    +---+   Those 3 are
|   |        *    |   |   superposed:
|   |        *    |   |         +--+
+--+|+--+    *    |   |    +--+ +--+          +--+
+--++|  |    *    +---+    |  |               |  |
+--+-+--+    *             +--+ +--+    +-+   +--+
   | |       *                  +--+    | |
   | |       *                          | |
   +-+       *                          +-+

Note that there is no 3x3 square right in the middle of the shape, because there is no intersection/+ on the top border of that "square to be".

You are going to implement a function count_squares() which takes an ASCII art picture as input and returns the number of squares the picture shows. The input to that function is an array of strings, where each string corresponds to a line of the ASCII art picture. Each string is guaranteed to contain only the characters -, |, +, and (space).

The smallest valid square has a side length of 2 and is represented by four + characters arranged in a square; a single + character is not considered a square.

Have fun!

Geometry
Algorithms
Puzzles

Stats:

CreatedNov 22, 2018
PublishedNov 25, 2018
Warriors Trained483
Total Skips28
Total Code Submissions1787
Total Times Completed99
Python Completions63
C++ Completions23
Rust Completions24
Total Stars21
% of votes with a positive feedback rating91% of 46
Total "Very Satisfied" Votes40
Total "Somewhat Satisfied" Votes4
Total "Not Satisfied" Votes2
Total Rank Assessments4
Average Assessed Rank
6 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • mnemonix666 Avatar
  • Blind4Basics Avatar
  • Voile Avatar
Ad