findNotContiguousOrRectangular.js 1.1 KB

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