1 | 'use strict';
|
2 |
|
3 | const arrayEqual = require('../../../utils/arrayEqual');
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function 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 |
|
42 |
|
43 |
|
44 | function namedAreas(areas) {
|
45 | const names = new Set(areas.flat());
|
46 |
|
47 | names.delete('.');
|
48 |
|
49 | return [...names];
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | function findNotContiguousOrRectangular(areas) {
|
58 | return namedAreas(areas).filter((name) => !isContiguousAndRectangular(areas, name));
|
59 | }
|
60 |
|
61 | module.exports = findNotContiguousOrRectangular;
|