Skip to content

Instantly share code, notes, and snippets.

@CoGrammarCodeReview
Created August 6, 2021 12:52
Show Gist options
  • Save CoGrammarCodeReview/ad478fd583270fd162d3e7125d69f3c0 to your computer and use it in GitHub Desktop.
Save CoGrammarCodeReview/ad478fd583270fd162d3e7125d69f3c0 to your computer and use it in GitHub Desktop.
Minesweeper Challenge for Mock Interviews
This challenge is based on the game Minesweeper.
Create a function that takes a grid of # and -,
where each hash (#) represents a mine and each dash
(-) represents a mine-free spot. Return an array where
each dash is replaced by a digit indicating the number
of mines immediately adjacent to the spot
(horizontally, vertically, and diagonally).
Examples
numGrid([
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "-", "-", "-", "-"],
["-", "-", "-", "-", "-"]
]) ➞ [
["0", "0", "0", "0", "0"],
["0", "1", "1", "1", "0"],
["0", "1", "#", "1", "0"],
["0", "1", "1", "1", "0"],
["0", "0", "0", "0", "0"],
]
numGrid([
["-", "-", "-", "-", "#"],
["-", "-", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "-", "-", "-", "-"],
["#", "-", "-", "-", "-"]
]) ➞ [
["0", "0", "0", "1", "#"],
["0", "1", "1", "2", "1"],
["0", "1", "#", "1", "0"],
["1", "2", "1", "1", "0"],
["#", "1", "0", "0", "0"]
]
numGrid([
["-", "-", "-", "#", "#"],
["-", "#", "-", "-", "-"],
["-", "-", "#", "-", "-"],
["-", "#", "#", "-", "-"],
["-", "-", "-", "-", "-"]
]) ➞ [
["1", "1", "2", "#", "#"],
["1", "#", "3", "3", "2"],
["2", "4", "#", "2", "0"],
["1", "#", "#", "2", "0"],
["1", "2", "2", "1", "0"],
]
Source: https://edabit.com/challenge/voZCqTGMSNjCrRhf9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment