🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ndani14

ndani14's solution

to Rectangles in the Java Track

Published at Apr 13 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));
    }

}
public class RectangleCounter {

    public int countRectangles(String[] inputGrid) {
        return new Solver(inputGrid).count();
    }

    private class Solver {
        private final int height;
        private final int width;
        private final String[] inputGrid;

        public Solver(String[] inputGrid) {
            this.inputGrid = inputGrid;
            this.height = inputGrid.length;
            this.width = height != 0 ? inputGrid[0].length() : 0;
        }

        public int count() {
            var sum = 0;

            for (var y = 0; y < height; y++) {
                for (var x = 0; x < width; x++) {
                    if (inputGrid[y].charAt(x) == '+')
                        sum += walkDown(x, y, x, y);
                }
            }

            return sum;
        }

        private int walkDown(int x, int y, int startX, int startY) {
            if (x >= width) return 0;
            if (y >= height) return 0;

            if (inputGrid[y].charAt(x) == '|')
                return walkDown(x, y + 1, startX, startY);
            else if (inputGrid[y].charAt(x) == '+')
                return walkDown(x, y + 1, startX, startY) + walkRight(x + 1, y, startX, startY);

            return 0;
        }

        private int walkRight(int x, int y, int startX, int startY) {
            if (x >= width) return 0;
            if (y >= height) return 0;

            if (inputGrid[y].charAt(x) == '-')
                return walkRight(x + 1, y, startX, startY);
            else if (inputGrid[y].charAt(x) == '+')
                return walkRight(x + 1, y, startX, startY) + walkUp(x, y - 1, startX, startY);

            return 0;
        }

        private int walkUp(int x, int y, int startX, int startY) {
            if (y < startY) return 0;
            if (y == startY && inputGrid[y].charAt(x) == '+')
                return walkLeft(x, y, startX, startY);

            if (inputGrid[y].charAt(x) == '|')
                return walkUp(x, y - 1, startX, startY);
            else if (inputGrid[y].charAt(x) == '+')
                return walkUp(x, y - 1, startX, startY);

            return 0;
        }

        private int walkLeft(int x, int y, int startX, int startY) {
            if (x == startX && y == startY) return 1;
            if (x < startX) return 0;

            if (inputGrid[y].charAt(x) == '-')
                return walkLeft(x - 1, y, startX, startY);
            else if (inputGrid[y].charAt(x) == '+')
                return walkLeft(x - 1, y, startX, startY);

            return 0;
        }
    }
}

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?