-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathonestar_day12.js
More file actions
86 lines (69 loc) · 2.5 KB
/
onestar_day12.js
File metadata and controls
86 lines (69 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const fs = require('fs');
let coordenadas = [[0, 1], [1, 0], [-1, 0], [0, -1]];
const validOrillas = (i, j, n) => {
return (0 <= i && i < n) && (0 <= j && j < n)
}
// DFS
let visitadosAllRegiones = new Set();
const regions = (i, j, matrix, n) => {
let area = 0;
let perimetro = 0;
let arrTempx = [];
arrTempx.push(i);
let arrTempy = [];
arrTempy.push(j)
let visitRegionSame = new Set();
while (arrTempx?.length > 0) {
let currentx = arrTempx.pop();
let currenty = arrTempy.pop();
// visitado para contemplar en cualquier region
visitadosAllRegiones.add(JSON.stringify([currentx,currenty]));
// visitado para su region y su respectivo conteo
if(visitRegionSame.has(JSON.stringify([currentx,currenty]))){
continue;
}
visitRegionSame.add(JSON.stringify([currentx,currenty]));
area ++;
console.log("letra:", matrix[currentx][currenty], JSON.stringify([currentx,currenty]));
// mira todas las coordenadas y mete en la pila todas las que se puedan recorrer
for (let i = 0; i < coordenadas?.length; i++) {
let k = currentx + coordenadas[i][0];
let g = currenty + coordenadas[i][1];
// si se pasó
if (!validOrillas(k, g, n)) {
perimetro ++;
continue;
}
//si es diferente
if (matrix[currentx][currenty] != matrix[k][g]) {
perimetro ++;
continue;
}
// si no visitado
// console.log(JSON.stringify([k,g]))
arrTempx.push(k)
arrTempy.push(g)
// console.log("después pila", arrTempx, arrTempy, k,g)
}
}
// console.log("area:", area, ", ", "perimetro:",perimetro);
return area * perimetro;
}
fs.readFile('file.txt', (err, data) => {
if (err) throw err;
// contar perímetro por cada nodo (sin contar otro nodo igual)
// contar área por varios nodos juntos
// marcar visitados los que ya fueron contados en un área
// definir matriz de coordenadas sin diagonal
let mapRegions = data.toString().split("\n");
let mapSize = mapRegions.length;
let res = 0;
for (let i = 0; i < mapRegions.length; i++) {
for (let j = 0; j < mapRegions[0].length; j++) {
if(!visitadosAllRegiones.has(JSON.stringify([i,j]))){
res += regions(i,j,mapRegions,mapSize);
}
}
}
console.log(res)
});