Sha256: f4cf332eccab7d24eec383fd761b59bc6bcb75802b066f70abf8e36a391d6a02

Contents?: true

Size: 1.31 KB

Versions: 117

Compression:

Stored size: 1.31 KB

Contents

class Rectangles {

  static count(diagram) {
    const rows = diagram.length;
    const cols = rows ? diagram[0].length : 0;

    let rectangles = 0;

    // All possible topleft corners
    for (let y = 0; y < rows - 1; y++) {
      for (let x = 0; x < cols - 1; x++) {
        if (diagram[y].charAt(x) === '+') {
          // All possible bottomright corners
          for (let j = y + 1; j < rows; j++) {
            for (let i = x + 1; i < cols; i++) {
              // Check if all corners are valid
              if (diagram[j].charAt(i) === '+' && diagram[y].charAt(i) === '+' && diagram[j].charAt(x) === '+') {
                let validRectangle = true;

                // Check if all sides are valid
                for (let s = x + 1; s < i; s++) if (!'+-'.includes(diagram[y].charAt(s))) validRectangle = false;
                for (let s = x + 1; s < i; s++) if (!'+-'.includes(diagram[j].charAt(s))) validRectangle = false;
                for (let t = y + 1; t < j; t++) if (!'+|'.includes(diagram[t].charAt(x))) validRectangle = false;
                for (let t = y + 1; t < j; t++) if (!'+|'.includes(diagram[t].charAt(i))) validRectangle = false;

                if (validRectangle) rectangles++;
              }
            }
          }
        }
      }
    }

    return rectangles;
  }
}

export default Rectangles;

Version data entries

117 entries across 117 versions & 1 rubygems

Version Path
trackler-2.2.1.139 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.138 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.137 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.136 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.135 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.134 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.133 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.132 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.131 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.130 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.129 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.128 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.127 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.126 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.125 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.124 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.123 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.122 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.121 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.120 tracks/ecmascript/exercises/rectangles/example.js