Last active
June 26, 2017 14:13
-
-
Save AvocadosConstant/509d2a973d23e10090921aee66be8f2f to your computer and use it in GitHub Desktop.
Solution to http://100.70.19.162:8000/contest/32/13
This file contains 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
import sys | |
""" | |
Solution to http://100.70.19.162:8000/contest/32/13 | |
Note that the instructions say that (x, y) means column x, row y; | |
however, the sample cases show the opposite. | |
This solution is based on the sample cases therefore (x, y) | |
means column y, row x. Which is dumb. | |
""" | |
# sanitize instructions of undo/redo calls | |
def clean_undo_redo(instrs): | |
instrs.reverse() | |
call_stack = [] | |
undo_stack = [] | |
while instrs: | |
cur = instrs.pop() | |
if cur == 'undo': | |
undo_stack.append(call_stack.pop()) | |
elif cur == 'redo': | |
if undo_stack: | |
call_stack.append(undo_stack.pop()) | |
else: | |
undo_stack = [] | |
call_stack.append(cur) | |
return call_stack | |
# creates a 0-initialized 2d list of size dims[0] X dims[1] | |
def create_canvas(dims): | |
w, h = int(dims[1]), int(dims[0]); | |
return [[0 for x in range(w)] for y in range(h)] | |
def print_canvas(c): | |
print('\n'.join([' '.join([str(item) for item in row]) for row in c])) | |
# checks a point (x, y) is within bounds of a 2d list grid | |
def point_in_grid(point, grid): | |
x, y = point[0], point[1] | |
return x >= 0 and x < len(grid) and y >= 0 and y < len(grid[0]) | |
def dot(x, y, color, c): | |
c[x][y] = color | |
return c | |
def square(x1, y1, x2, y2, color, c): | |
for dx in range(min(x1, x2), max(x1, x2) + 1): | |
c[dx][y1] = color | |
c[dx][y2] = color | |
for dy in range(min(y1, y2), max(y1, y2) + 1): | |
c[x1][dy] = color | |
c[x2][dy] = color | |
return c | |
def fill(x, y, color, c): | |
region = c[x][y] | |
queue = [(x, y)] | |
# floodfill | |
while queue: | |
cur = queue.pop(0) | |
cx, cy = cur[0], cur[1] | |
if (point_in_grid(cur, c) | |
and c[cx][cy] != color | |
and c[cx][cy] == region): | |
c[cx][cy] = color | |
queue.append((cx + 1, cy)) | |
queue.append((cx - 1, cy)) | |
queue.append((cx, cy + 1)) | |
queue.append((cx, cy - 1)) | |
return c | |
# handles instructions and delegates commands to respective draw calls | |
def delegate(params, c): | |
params = params.split(' ') | |
cmd = params.pop(0) | |
params = list(map(int, params)) | |
#print('\n', params) | |
if cmd == 'dot': | |
return dot(params[0], params[1], params[2], c) | |
elif cmd == 'square': | |
return square(params[0], params[1], params[2], params[3], params[4], c) | |
elif cmd == 'fill': | |
return fill(params[0],params[1], params[2], c) | |
return c | |
# input parsing | |
data = sys.stdin.read().splitlines() | |
canvas = create_canvas(data.pop(0).split(',')) | |
#print_canvas(canvas) | |
# remove semicolons | |
data = list(map(lambda s: s[:-1], data)) | |
data = clean_undo_redo(data) | |
for instr in data: | |
canvas = delegate(instr, canvas) | |
print_canvas(canvas) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment