Skip to content

Instantly share code, notes, and snippets.

@kevinchappell
Created October 13, 2020 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinchappell/f25e5d4d5d5687df8ec13144c96c1f82 to your computer and use it in GitHub Desktop.
Save kevinchappell/f25e5d4d5d5687df8ec13144c96c1f82 to your computer and use it in GitHub Desktop.
EMP Grid Challenge
# safe? should return true or false if the absolute coordinate digit sum is a multiple of 3
def safe?(x,y)
# [true, false].sample
# ([x, y].map(&:abs).to_s.chars.map(&:to_i).sum) % 3 == 0
(x.abs + y.abs) % 3 == 0
end
def fill_cell(x, y)
safe?(x,y) ? '■ ' : '□ '
end
def generate_grid(grid_base_size)
grid_size = *(-(grid_base_size / 2)..(grid_base_size / 2))
grid = "\n"
grid_size.each do |x|
row = ''
grid_size.each do |y|
col = fill_cell(x, y)
row += col
end
grid += "#{row}\n"
end
print grid
end
generate_grid(30)
# There is a robot which can move around on a grid. The robot is placed at point (0,0) on a grid with positive and negative coordinate values.
# Some points are dangerous and contain EMP Mines. Create a map for the robot to avoid the EMP Mines.
# Mines are located the where the sum digits of abs(x) plus the sum of the digits of abs(y) are a multiple of 3. For example:
# The point (3, 2) is not safe because 3 + 2 = 5 and not a multiple of 3.
# The point (-1, -2) is safe because 1 + 2 = 3, which is a multiple of 3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment