Skip to content

Commit

Permalink
solution challenge 17 v2
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsi15 committed Dec 21, 2024
1 parent a04c07e commit 5f2f828
Showing 1 changed file with 36 additions and 25 deletions.
61 changes: 36 additions & 25 deletions challenges-2024/challenge-17/detectBombs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 5f2f828

Please sign in to comment.