-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReto #17.js
More file actions
132 lines (107 loc) · 3.12 KB
/
Reto #17.js
File metadata and controls
132 lines (107 loc) · 3.12 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/** Reto #17: 🎄 El panel de luces navideñas
*
* En el Polo Norte han montado un panel de luces navideñas 🎄✨ para decorar el taller. Cada luz puede estar encendida con un color o apagada.
El panel se representa como una matriz donde cada celda puede ser:
'.' → luz apagada
'R' → luz roja
'G' → luz verde
Los elfos quieren saber si en el panel existe una línea de 4 luces del mismo color encendidas y alineadas (solo horizontal ↔ o vertical ↕). Las luces apagadas ('.') no cuentan.
*/
console.log(hasFourLights([
['.', '.', '.', '.', '.'],
['R', 'R', 'R', 'R', '.'],
['G', 'G', '.', '.', '.']
]));
// true → hay 4 luces rojas en horizontal
console.log(hasFourLights([
['G', '.', '.'],
['G', '.', '.'],
['G', '.', '.'],
['G', '.', '.']
]));
console.log(hasFourLights([
['.', '.','R', 'R'],
['R', 'R', '.','.'],
['.', '.', '.','.'],
['.', '.', '.','.']
]));
console.log(hasFourLights([
['.', 'G', '.', '.'],
['.', 'G', '.', '.'],
['.', 'G', '.', '.'],
['.', 'G', '.', '.']
]));
// true → hay 4 luces verdes en vertical
console.log(hasFourLights([
['R', 'G', 'R'],
['G', 'R', 'G'],
['G', 'R', 'G']
]));
// false → no hay 4 luces del mismo color seguidas
/**
* @param {string[][]} board
* @returns {boolean}
*/
function hasFourLights(board) {
let redRow = 0;
let greenRow = 0 ;
const rowLength = board[0].length;
const cheinrowLight = board.flat().join('');
//4 luces en Fila
for(let i = 0; i < cheinrowLight.length; i++){
if(i % rowLength === 0)
{
redRow=0;
greenRow=0;
}
redRow = cheinrowLight[i] === 'R' ? redRow + 1 : 0;
greenRow = cheinrowLight[i] === 'G' ? greenRow + 1 : 0;
if(redRow === 4 || greenRow === 4)
return true;
}
//4 luces en columna
for(let i = 0; i < board[0].length;i++){
let redcol = 0;
let greencol = 0;
for(let j =0 ; j < board.length; j++){
redcol = board[j][i] === 'R' ? redcol + 1 : 0;
greencol = board[j][i] === 'G' ? greencol + 1 : 0;
if(redcol === 4 || greencol === 4)
return true;
}
}
/** OPTIMIZACION
*
* const rows = board.length;
if (rows === 0) return false;
const cols = board[0].length;
// Recorremos cada celda una sola vez
for (let y = 0; y < rows; y++) {
for (let x = 0; x < cols; x++) {
const color = board[y][x];
// Optimización micro: Si la luz está apagada, pasamos a la siguiente
if (color === '.') continue;
// 1. COMPROBACIÓN HORIZONTAL (Lookahead)
// Solo verificamos si quedan al menos 3 espacios a la derecha (x + 3 < cols)
if (x + 3 < cols) {
if (color === board[y][x + 1] &&
color === board[y][x + 2] &&
color === board[y][x + 3]) {
return true;
}
}
// 2. COMPROBACIÓN VERTICAL (Lookahead)
// Solo verificamos si quedan al menos 3 espacios abajo (y + 3 < rows)
if (y + 3 < rows) {
if (color === board[y + 1][x] &&
color === board[y + 2][x] &&
color === board[y + 3][x]) {
return true;
}
}
}
}
*
*/
return false
}