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.180 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.179 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.178 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.177 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.176 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.175 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.174 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.173 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.172 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.171 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.170 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.169 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.167 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.166 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.165 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.164 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.163 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.162 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.161 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.160 tracks/ecmascript/exercises/rectangles/example.js