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.159 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.158 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.157 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.156 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.155 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.154 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.153 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.152 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.151 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.150 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.149 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.148 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.147 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.146 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.145 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.144 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.143 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.142 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.141 tracks/ecmascript/exercises/rectangles/example.js
trackler-2.2.1.140 tracks/ecmascript/exercises/rectangles/example.js