From 5f2f828a8325fb22255022bdf27f52eb7163298c Mon Sep 17 00:00:00 2001 From: John Serrano Date: Sat, 21 Dec 2024 10:54:53 -0500 Subject: [PATCH] solution challenge 17 v2 --- challenges-2024/challenge-17/detectBombs.js | 61 ++++++++++++--------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/challenges-2024/challenge-17/detectBombs.js b/challenges-2024/challenge-17/detectBombs.js index 747cd82..c2e0ad6 100644 --- a/challenges-2024/challenge-17/detectBombs.js +++ b/challenges-2024/challenge-17/detectBombs.js @@ -3,8 +3,9 @@ export function detectBombs(grid) { // console.log(grid.length) for (let i = 0; i < grid.length; i++) { const counts = [] + const row = grid[i] - for (let j = 0; j < grid.length; j++) { + for (let j = 0; j < row.length; j++) { // const isBorder = (i === 0 || i === grid.length - 1 || j === 0 || j === grid[i].length - 1); const isBorderTop = i === 0 const isBorderBottom = i === grid.length - 1 @@ -19,6 +20,7 @@ export function detectBombs(grid) { const diagonal = grid[i + 1][j + 1] let count = [next, bottom, diagonal].filter(Boolean).length + console.log({ count }) counts.push(count) } else if (isBorderTop && isBorderRight) { // prev, bottom, diagonal @@ -28,6 +30,7 @@ export function detectBombs(grid) { const diagonal = grid[i + 1][j - 1] let count = [prev, bottom, diagonal].filter(Boolean).length + console.log({ count }) counts.push(count) } else if (isBorderBottom && isBorderLeft) { // next, top, diagonal @@ -37,18 +40,18 @@ export function detectBombs(grid) { const diagonal = grid[i - 1][j + 1] let count = [next, top, diagonal].filter(Boolean).length + console.log({ count }) counts.push(count) } else if (isBorderTop) { // next, prev, bottom, diagonal, diagonalTop // const crr = grid[i][j] const prev = grid[i][j - 1] const next = grid[i][j + 1] - console.log(grid[i + 1][j - 1]) // diagonal left - console.log(grid[i + 1][j + 1]) // diagonal right + const bottom = grid[i + 1][j] const diagonalLeft = grid[i + 1][j - 1] const diagonalRight = grid[i + 1][j + 1] - let count = [prev, next, diagonalLeft, diagonalRight].filter(Boolean).length + let count = [prev, next, bottom, diagonalLeft, diagonalRight].filter(Boolean).length counts.push(count) } else if (isBorderBottom) { // next, prev, top, diagonal, diagonalBottom @@ -87,43 +90,51 @@ export function detectBombs(grid) { const prev = grid[i][j - 1] const bottom = grid[i + 1][j] const top = grid[i - 1][j] - const diagonal = grid[i + 1][j + 1] - const diagonalTop = grid[i - 1][j + 1] - const diagonalBottom = grid[i + 1][j - 1] + const diagonalTopRight = grid[i - 1][j + 1] + const diagonalBottomRight = grid[i + 1][j + 1] const diagonalTopLeft = grid[i - 1][j - 1] + const diagonalBottomLeft = grid[i + 1][j - 1] - let count = [next, prev, bottom, top, diagonal, diagonalTop, diagonalBottom, diagonalTopLeft].filter( - Boolean - ).length + let count = [ + next, + prev, + bottom, + top, + diagonalBottomRight, + diagonalTopRight, + diagonalBottomLeft, + diagonalTopLeft, + ].filter(Boolean).length counts.push(count) } - // if (j == 1) { - // break - // } } - console.log(counts) + // console.log(counts) - // result.push(counts) + result.push(counts) } + + return result } -const result = detectBombs([ - [true, false, false], - [false, true, false], // diagonal tiene una bomba - [false, false, false], -]) +// const result = detectBombs([ +// [true, false, false], +// [false, true, false], // diagonal tiene una bomba +// [false, false, false], +// ]) -console.log(result) +// console.log(result) // [ // [1, 2, 1], // [2, 1, 1], // [1, 1, 1] // ] -// detectBombs([ -// [true, false], -// [false, false], -// ]) +const result = detectBombs([ + [true, true], + [true, true], + [false, false], +]) +console.log(result) // [ // [0, 1], // [1, 1]