69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
![]() |
#!/usr/bin/env python
|
||
|
|
||
|
import re
|
||
|
import argparse
|
||
|
|
||
|
SIZE_X = 50
|
||
|
SIZE_Y = 6
|
||
|
|
||
|
RE_RECT = re.compile(r'rect (\d+)x(\d+)$')
|
||
|
RE_ROTATE_C = re.compile(r'rotate column x=(\d+) by (\d+)$')
|
||
|
RE_ROTATE_R = re.compile(r'rotate row y=(\d+) by (\d+)$')
|
||
|
|
||
|
def load_file(filename):
|
||
|
with open(filename) as f:
|
||
|
for line in f:
|
||
|
yield line.strip()
|
||
|
|
||
|
def do_rect(display, size_x, size_y):
|
||
|
for x in range(size_x):
|
||
|
for y in range(size_y):
|
||
|
display[y][x] = 1
|
||
|
|
||
|
def do_rotate_row(display, y, p):
|
||
|
p = SIZE_X - p
|
||
|
display[y] = display[y][p:] + display[y][:p]
|
||
|
|
||
|
def do_rotate_column(display, x, p):
|
||
|
data = [display[i][x] for i in range(SIZE_Y)]
|
||
|
p = SIZE_Y - p
|
||
|
data = data[p:] + data[:p]
|
||
|
for i in range(SIZE_Y):
|
||
|
display[i][x] = data[i]
|
||
|
|
||
|
def main(args):
|
||
|
DISPLAY = []
|
||
|
for y in range(SIZE_Y):
|
||
|
line = []
|
||
|
for x in range(SIZE_X):
|
||
|
line.append(0)
|
||
|
DISPLAY.append(line)
|
||
|
|
||
|
for LINE in load_file(args.input):
|
||
|
match = RE_RECT.search(LINE)
|
||
|
if match:
|
||
|
do_rect(DISPLAY, int(match.group(1)), int(match.group(2)))
|
||
|
continue
|
||
|
match = RE_ROTATE_C.search(LINE)
|
||
|
if match:
|
||
|
do_rotate_column(DISPLAY, int(match.group(1)), int(match.group(2)))
|
||
|
continue
|
||
|
match = RE_ROTATE_R.search(LINE)
|
||
|
if match:
|
||
|
do_rotate_row(DISPLAY, int(match.group(1)), int(match.group(2)))
|
||
|
continue
|
||
|
raise Exception("unkown instruction: {}".format(LINE))
|
||
|
|
||
|
count = 0
|
||
|
for y in range(SIZE_Y):
|
||
|
for x in range(SIZE_X):
|
||
|
count += DISPLAY[y][x]
|
||
|
print(count)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
parser = argparse.ArgumentParser(description='AdventOfCode 2016 Day 01')
|
||
|
parser.add_argument('--input', '-i', action='store', required=True, help='input file')
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
main(args)
|