Skip to content

Commit

Permalink
Change solution challenge 17 v2
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsi15 committed Dec 23, 2024
1 parent 08779b9 commit f3b96c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 88 deletions.
104 changes: 17 additions & 87 deletions challenges-2024/challenge-17/detectBombs.js
Original file line number Diff line number Diff line change
@@ -1,98 +1,28 @@
export function detectBombs(grid) {
const result = []
// console.log(grid.length)
const positions = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
]

for (let i = 0; i < grid.length; i++) {
const counts = []
const row = grid[i]

for (let j = 0; j < row.length; j++) {
const isBorderTop = i === 0
const isBorderBottom = i === grid.length - 1
const isBorderLeft = j === 0
const isBorderRight = j === grid[i].length - 1

if (isBorderTop && isBorderLeft) {
const next = grid[i][j + 1]
const bottom = grid[i + 1][j]
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) {
const prev = grid[i][j - 1]
const bottom = grid[i + 1][j]
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) {
const next = grid[i][j + 1]
const top = grid[i - 1][j]
const diagonal = grid[i - 1][j + 1]

let count = [next, top, diagonal].filter(Boolean).length
console.log({ count })
counts.push(count)
} else if (isBorderTop) {
const prev = grid[i][j - 1]
const next = grid[i][j + 1]
const bottom = grid[i + 1][j]
const diagonalLeft = grid[i + 1][j - 1]
const diagonalRight = grid[i + 1][j + 1]

let count = [prev, next, bottom, diagonalLeft, diagonalRight].filter(Boolean).length
counts.push(count)
} else if (isBorderBottom) {
const top = grid[i - 1][j]
const prev = grid[i][j - 1]
const next = grid[i][j + 1]
const diagonal = grid[i - 1][j + 1]
const diagonalBottom = grid[i - 1][j - 1]

let count = [top, prev, next, diagonal, diagonalBottom].filter(Boolean).length
counts.push(count)
} else if (isBorderLeft) {
const next = grid[i][j + 1]
const top = grid[i - 1][j]
const bottom = grid[i + 1][j]
const diagonal = grid[i + 1][j + 1]
const diagonalTop = grid[i - 1][j + 1]

let count = [next, top, bottom, diagonal, diagonalTop].filter(Boolean).length
counts.push(count)
} else if (isBorderRight) {
const prev = grid[i][j - 1]
const top = grid[i - 1][j]
const bottom = grid[i + 1][j]
const diagonal = grid[i + 1][j - 1]
const diagonalTop = grid[i - 1][j - 1]

let count = [prev, top, bottom, diagonal, diagonalTop].filter(Boolean).length
counts.push(count)
} else {
const next = grid[i][j + 1]
const prev = grid[i][j - 1]
const bottom = grid[i + 1][j]
const top = grid[i - 1][j]
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]
const validPositions = positions.filter(([rowOffset, colOffset]) => {
const newRow = i + rowOffset
const newCol = j + colOffset
return newRow >= 0 && newRow < grid.length && newCol >= 0 && newCol < grid[i].length && grid[newRow][newCol]
})

let count = [
next,
prev,
bottom,
top,
diagonalBottomRight,
diagonalTopRight,
diagonalBottomLeft,
diagonalTopLeft,
].filter(Boolean).length
counts.push(count)
}
counts.push(validPositions.length)
}

result.push(counts)
Expand Down
2 changes: 1 addition & 1 deletion challenges-2024/challenge-17/test/detectBombs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('Files names', () => {
])

expect(result).toBeDefined()
expect(result).toBe([
expect(result).toEqual([
[1, 2, 1],
[2, 1, 1],
[1, 1, 1],
Expand Down

0 comments on commit f3b96c0

Please sign in to comment.