ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# rasmusrim's solution

## to Rectangles in the Java Track

Published at Apr 24 2020 · 0 comments
Instructions
Test suite
Solution

Count the rectangles in an ASCII diagram like the one below.

``````   +--+
++  |
+-++--+
|  |  |
+--+--+
``````

The above diagram contains 6 rectangles:

``````

+-----+
|     |
+-----+
``````
``````   +--+
|  |
|  |
|  |
+--+
``````
``````   +--+
|  |
+--+

``````
``````

+--+
|  |
+--+
``````
``````

+--+
|  |
+--+
``````
``````
++
++

``````

You may assume that the input is always a proper rectangle (i.e. the length of every line equals the length of the first line).

## Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

# Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

``````\$ gradle test
``````

Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore("Remove to run test")` annotation.

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### RectangleCounterTest.java

``````import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class RectangleCounterTest {

private RectangleCounter rectangleCounter;

@Before
public void setUp() {
rectangleCounter = new RectangleCounter();
}

@Test
public void testInputWithNoRowsContainsNoRectangles() {
String[] inputGrid = new String[]{};

assertEquals(0, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testInputWithNoColumnsContainsNoRectangles() {
String[] inputGrid = new String[]{""};

assertEquals(0, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testNonTrivialInputWithNoRectangles() {
String[] inputGrid = new String[]{" "};

assertEquals(0, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testInputWithOneRectangle() {
String[] inputGrid = new String[]{
"+-+",
"| |",
"+-+"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testInputWithTwoRectanglesWithoutSharedEdges() {
String[] inputGrid = new String[]{
"  +-+",
"  | |",
"+-+-+",
"| |  ",
"+-+  "
};

assertEquals(2, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testInputWithFiveRectanglesWithSharedEdges() {
String[] inputGrid = new String[]{
"  +-+",
"  | |",
"+-+-+",
"| | |",
"+-+-+"
};

assertEquals(5, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatRectangleOfHeightOneIsCounted() {
String[] inputGrid = new String[]{
"+--+",
"+--+"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatRectangleOfWidthOneIsCounted() {
String[] inputGrid = new String[]{
"++",
"||",
"++"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatOneByOneSquareIsCounted() {
String[] inputGrid = new String[]{
"++",
"++"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatIncompleteRectanglesAreNotCounted() {
String[] inputGrid = new String[]{
"  +-+",
"    |",
"+-+-+",
"| | -",
"+-+-+"
};

assertEquals(1, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatRectanglesOfDifferentSizesAreAllCounted() {
String[] inputGrid = new String[]{
"+------+----+",
"|      |    |",
"+---+--+    |",
"|   |       |",
"+---+-------+"
};

assertEquals(3, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testThatIntersectionsWithoutCornerCharacterDoNotCountAsRectangleCorners() {
String[] inputGrid = new String[]{
"+------+----+",
"|      |    |",
"+------+    |",
"|   |       |",
"+---+-------+"
};

assertEquals(2, rectangleCounter.countRectangles(inputGrid));
}

@Ignore("Remove to run test")
@Test
public void testLargeInputWithManyRectangles() {
String[] inputGrid = new String[]{
"+---+--+----+",
"|   +--+----+",
"+---+--+    |",
"|   +--+----+",
"+---+--+--+-+",
"+---+--+--+-+",
"+------+  | |",
"          +-+"
};

assertEquals(60, rectangleCounter.countRectangles(inputGrid));
}

}``````
``````import java.awt.*;

public class RectangleCounter {
private String[][] grid;

public int countRectangles(String[] inputGrid) {

if (inputGrid.length == 0) {
return 0;
}

if (inputGrid[0].length() == 0) {
return 0;
}

grid = new String[inputGrid.length][inputGrid[0].length()];

int index = 0;
for (var line : inputGrid) {
String[] characters = line.split("");
grid[index] = characters;
index++;
}

int rectangles = 0;

for (int row = 0; row < grid.length; row++) {
for (int column = 0; column < grid[0].length; column++) {
if (grid[row][column].equals("+")) {
rectangles += findRectanglesStartingAt(new Point(column, row));
}
}
}

return rectangles;
}

private int findRectanglesStartingAt(Point point) {
int startingColumn = (int) point.getX() + 1;
int row = (int) point.getY();
int rectangles = 0;

for (var column = startingColumn; column < grid[0].length; column++) {
String columnContent = grid[row][column];
if (columnContent.equals("+")) {
rectangles += findRectanglesDownward(point, new Point(column, row));
}
}

return rectangles;
}

private int findRectanglesDownward(Point firstUpperCorner, Point secondUpperCorner) {
int startRow = (int) firstUpperCorner.getY() + 1;
int rectanglesFound = 0;

for (int row = startRow; row < grid.length; row++) {
String contentOfFirstLowerCorner = grid[row][(int) firstUpperCorner.getX()];
String contentOfSecondLowerCorner = grid[row][(int) secondUpperCorner.getX()];

if (contentOfFirstLowerCorner.equals("+") && contentOfSecondLowerCorner.equals("+")) {
rectanglesFound++;

System.out.println("Found rectangle " + firstUpperCorner.toString() + "," + secondUpperCorner.toString() + ", x=" + (int) firstUpperCorner.getX() + ", y=" + row + ", x=" + (int) secondUpperCorner.getX() + ", y=" + row);
} else {

if (!isValidCharacter(contentOfFirstLowerCorner) || !isValidCharacter(contentOfSecondLowerCorner)) {
break;
}
}

}

return rectanglesFound;

}

private boolean isValidCharacter(String character) {
return character.equals("|") || character.equals("+");

}

}``````