Created
December 18, 2025 19:21
-
-
Save lineargraph/56948ae9ccfd136a3ca5dfefd68b967e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const grid = [ | |
| ['P', 'X', 'P', 'X', 'X', 'M', 'X', 'X', 'M', 'X'], | |
| ['M', 'X', 'M', 'X', 'X', 'P', 'X', 'X', 'P', 'X'], | |
| ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'], | |
| ['X', 'P', 'X', 'P', 'M', 'X', 'P', 'M', 'X', 'M'], | |
| ['X', 'M', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'P'], | |
| ['X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'], | |
| ['X', 'P', 'X', 'M', 'P', 'X', 'M', 'P', 'X', 'M'], | |
| ['X', 'M', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'], | |
| ['X', 'X', 'X', 'P', 'X', 'X', 'P', 'X', 'P', 'X'], | |
| ['P', 'X', 'M', 'X', 'X', 'M', 'X', 'X', 'M', 'X'], | |
| ]; | |
| function getAdjacentCells(row, col, grid) { | |
| const adjacent = []; | |
| const directions = [ | |
| [-1, -1], [-1, 0], [-1, 1], | |
| [0, -1], [0, 1], | |
| [1, -1], [1, 0], [1, 1] | |
| ]; | |
| for (const [dr, dc] of directions) { | |
| const newRow = row + dr; | |
| const newCol = col + dc; | |
| if (newRow >= 0 && newRow < grid.length && newCol >= 0 && newCol < grid[0].length) { | |
| adjacent.push(grid[newRow][newCol]); | |
| } | |
| } | |
| return adjacent; | |
| } | |
| function countXCells(grid) { | |
| let count = 0; | |
| for (let row = 0; row < grid.length; row++) { | |
| for (let col = 0; col < grid[row].length; col++) { | |
| if (grid[row][col] === 'X') { | |
| count++; | |
| } | |
| } | |
| } | |
| return count; | |
| } | |
| function verifyGrid(grid) { | |
| const violations = []; | |
| for (let row = 0; row < grid.length; row++) { | |
| for (let col = 0; col < grid[row].length; col++) { | |
| if (grid[row][col] === 'X') { | |
| const adjacent = getAdjacentCells(row, col, grid); | |
| const hasP = adjacent.includes('P'); | |
| const hasM = adjacent.includes('M'); | |
| if (!hasP || !hasM) { | |
| violations.push({ | |
| position: [row, col], | |
| adjacent: adjacent, | |
| hasP: hasP, | |
| hasM: hasM | |
| }); | |
| } | |
| } | |
| } | |
| } | |
| return violations; | |
| } | |
| const violations = verifyGrid(grid); | |
| const xCount = countXCells(grid); | |
| console.log(`Total X cells: ${xCount}\n`); | |
| if (violations.length === 0) { | |
| console.log('All X cells have at least one P and one M adjacent!'); | |
| } else { | |
| console.log(`Found ${violations.length} violation(s):\n`); | |
| violations.forEach(v => { | |
| console.log(` Position [${v.position[0]}, ${v.position[1]}]:`); | |
| console.log(` Adjacent cells: [${v.adjacent.join(', ')}]`); | |
| console.log(` Has P: ${v.hasP}, Has M: ${v.hasM}`); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment