In [1]:
#!/usr/bin/env
import numpy, re
class BrokenScreen(object):
def __init__(self, x, y):
self.screen = numpy.zeros((x,y))
def rect(self, x, y):
self.screen[0:y,0:x] = 1
def rotate_column(self, x, by):
self.screen[0:,x] = numpy.roll(self.screen[0:,x], by)
def rotate_row(self, y, by):
self.screen[y, 0:] = numpy.roll(self.screen[y, 0:], by)
def cmd(self, input_str):
m = re.match('rect (\d+)x(\d+)', input_str)
if m:
self.rect(int(m.group(1)), int(m.group(2)))
return
m = re.match('rotate (?:row|column) y=(\d+) by (\d+)', input_str)
if m:
self.rotate_row(int(m.group(1)), int(m.group(2)))
return
m = re.match('rotate (?:row|column) x=(\d+) by (\d+)', input_str)
if m:
self.rotate_column(int(m.group(1)), int(m.group(2)))
return
print "UNKNOWN COMMAND", input_str
def __str__(self):
return '\n'.join([''.join([('#' if e else '.') for e in row]) for row in self.screen])
In [2]:
scr = BrokenScreen(3,7)
print str(scr)
print "rect 3x2"
scr.cmd("rect 3x2")
print str(scr)
print "rotate column x=1 by 1"
scr.cmd("rotate column x=1 by 1")
print str(scr)
print "rotate row y=0 by 4"
scr.cmd("rotate row y=0 by 4")
print str(scr)
print "rotate row x=1 by 1"
scr.cmd("rotate row x=1 by 1")
print str(scr)
In [4]:
scr = BrokenScreen(6,50)
with open('Dec8.input', 'r') as f:
for line in f:
scr.cmd(line)
print scr.screen.sum()
print scr