Skip to content

Instantly share code, notes, and snippets.

@hassanshamim
Last active October 11, 2016 00:29
Show Gist options
  • Save hassanshamim/274b406e6da97393150192000268e0cd to your computer and use it in GitHub Desktop.
Save hassanshamim/274b406e6da97393150192000268e0cd to your computer and use it in GitHub Desktop.
zoombit or w/e it's called
# -*- coding: utf-8 -*-
"""
>>> process([[1, 2, 4], [5, 1, 4], [1, 2, 3]], (0,2), 4)
[[-1, -1, -1], [5, -1, -1], [-1, -1, -1]]
>>> process([[1, 3, 2], [1, 1, 4], [0, 4, 3]],(0,2), 3)
[[-1, -1, -1], [-1, -1, 4], [-1, 4, 3]]
>>> process([[1, 3, 2], [1, 1, 4], [0, 4, 3]],(0,0), 2)
[[-1, 3, 2], [-1, -1, 4], [-1, 4, 3]]
>>> process([[1, 3, 2], [1, 1, 4], [0, 4, 3]],(2,2), 8)
[[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
>>> process([[1, 3, 2, 3], [1, 1, 4, 6], [0, 69, 3, 1], [0, 4, 3, 1]],(3,3), 3)
[[1, 3, 2, 3], [1, 1, 4, 6], [0, 69, -1, -1], [0, 4, -1, -1]]
"""
def process(board, start, strength):
"""
takes board, a multidimensional NxN array, start, a tuple of y, x coordinates
and strength as arguments. "Infects" starting at start coordinates by switching
elements to -1 if their value is less than or equal to the infection strength.
Infects adjacent cells at right angles if present cell was infected.
returns list containing finished game board
"""
# Move inside function as they vary based on board input
y_max = len(board)
x_max = len(board[0])
# start p_queue
p_queue = [start]
# Process infection branching with a for pipeline
for y, x in p_queue: # Just a heads up this works because we're only appending.
# Cell isn't valid.
# could define as its own function if we wanted to be more descriptive
# Also moved from the look_* functions to the process function
# You could modify the post-infection code to only append valid coords
# to the p_queue.
#Also, I think by moving the logic into this function a bit
# it would make it a bit easier to turn into a recursive one.
if not ((0 <= x < x_max) and (0 <= y < y_max)):
continue
# Cell is already infected, move on
if board[y][x] < 0:
continue
if board[y][x] <= strength:
# infection triggered, update board
board[y][x] = -1
# since we infected, look around and add result to queue for next infect call
adjacent_cells = [(y+1, x), (y-1, x), (y, x+1), (y, x-1)]
p_queue.extend(adjacent_cells)
return board
def process_recursive(board, position, strength):
y, x = position
try: # skip invalid and inficted cells
val = board[y][x]
if val < 0 or val > strength:
return board #maybe it infects nothing
except IndexError:
return
board[y][x] = -1
adjacent_cells = [(y+1, x), (y-1, x), (y, x+1), (y, x-1)]
for cell in adjacent_cells:
process_recursive(board, cell, strength)
return board
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment