63 lines
1.4 KiB
Python
63 lines
1.4 KiB
Python
# Python!
|
|
|
|
with open('08.txt') as file:
|
|
lines = file.read().splitlines()
|
|
|
|
class display:
|
|
def __init__(self):
|
|
self.pixels = [list([' '] * 50) for x in range(6)]
|
|
|
|
def rect(self, A, B):
|
|
for i in range(0, B):
|
|
self.pixels[i][:A] = ['#'] * A
|
|
|
|
def wrap(self, it, n):
|
|
steps = (-1 * n) % len(it)
|
|
return it[steps:] + it[:steps]
|
|
|
|
def rotate_row(self, index, steps):
|
|
row = self.pixels[index]
|
|
self.pixels[index] = self.wrap(row, steps)
|
|
|
|
def rotate_column(self, index, steps):
|
|
cols = [x for x in zip(*self.pixels)]
|
|
rotated_col = self.wrap(cols[index], steps)
|
|
for i in range(6):
|
|
self.pixels[i][index] = rotated_col[i]
|
|
|
|
def rotate(self, t, index, steps):
|
|
if t == 'row': self.rotate_row(index, steps)
|
|
if t == 'column': self.rotate_column(index, steps)
|
|
|
|
def show(self):
|
|
print('- ' * 53)
|
|
for row in self.pixels:
|
|
print('| ', ' '.join(row), ' |')
|
|
print('- ' * 53)
|
|
|
|
def count_lit(self):
|
|
count = 0
|
|
for row in self.pixels: count += row.count('#')
|
|
return count
|
|
|
|
def translate(self, line):
|
|
words = line.split()
|
|
if words[0] == 'rect':
|
|
dims = words[1].split('x')
|
|
x = int(dims[0])
|
|
y = int(dims[1])
|
|
self.rect(x, y)
|
|
elif words[0] == 'rotate':
|
|
t = words[1]
|
|
index = int(words[2][2:])
|
|
steps = int(words[4])
|
|
self.rotate(t, index, steps)
|
|
else:
|
|
print('ERROR: INSTRUCTION NOT UNDERSTOOD. PLEASE PRESS RED BUTTON AND SCREAM LOUDLY.')
|
|
|
|
|
|
screen = display()
|
|
for line in lines:
|
|
screen.translate(line)
|
|
|
|
screen.show() |