UNPKG

1.14 kBJavaScriptView Raw
1'use strict';
2
3const arrayEqual = require('../../../utils/arrayEqual');
4
5/**
6 *
7 * @param {string[][]} areas
8 * @param {string} name
9 * @returns {boolean}
10 */
11function isContiguousAndRectangular(areas, name) {
12 const indicesByRow = areas.map((row) => {
13 const indices = [];
14 let idx = row.indexOf(name);
15
16 while (idx !== -1) {
17 indices.push(idx);
18 idx = row.indexOf(name, idx + 1);
19 }
20
21 return indices;
22 });
23
24 for (let i = 0; i < indicesByRow.length; i++) {
25 for (let j = i + 1; j < indicesByRow.length; j++) {
26 if (indicesByRow[i].length === 0 || indicesByRow[j].length === 0) {
27 continue;
28 }
29
30 if (!arrayEqual(indicesByRow[i], indicesByRow[j])) {
31 return false;
32 }
33 }
34 }
35
36 return true;
37}
38
39/**
40 *
41 * @param {string[][]} areas
42 * @returns {string[]}
43 */
44function namedAreas(areas) {
45 const names = new Set(areas.flat());
46
47 names.delete('.');
48
49 return [...names];
50}
51
52/**
53 *
54 * @param {string[][]} areas
55 * @returns {string[]}
56 */
57function findNotContiguousOrRectangular(areas) {
58 return namedAreas(areas).filter((name) => !isContiguousAndRectangular(areas, name));
59}
60
61module.exports = findNotContiguousOrRectangular;