Skip to content

Instantly share code, notes, and snippets.

@randrews
Last active December 28, 2017 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randrews/e3ab033c5fd0286d858d851d321423f0 to your computer and use it in GitHub Desktop.
Save randrews/e3ab033c5fd0286d858d851d321423f0 to your computer and use it in GitHub Desktop.
import io, re
def read_instructions(filename):
file = io.open(filename)
rect_pat = re.compile('rect (\d+)x(\d+)')
row_pat = re.compile('rotate row y=(\d+) by (\d+)')
col_pat = re.compile('rotate column x=(\d+) by (\d+)')
instructions = []
for line in file:
rect_m = rect_pat.match(line)
row_m = row_pat.match(line)
col_m = col_pat.match(line)
if rect_m:
instructions.append(('rect', int(rect_m[1]), int(rect_m[2])))
elif row_m:
instructions.append(('row', int(row_m[1]), int(row_m[2])))
elif col_m:
instructions.append(('col', int(col_m[1]), int(col_m[2])))
return instructions
def new_lcd():
lcd = []
for y in range(0, 6):
lcd.append([])
for x in range(0, 50):
lcd[y].append(False)
return lcd
def run_rect(lcd, w, h):
for y in range(0, h):
for x in range(0, w):
lcd[y][x] = True
def run_row(lcd, row_num, shift):
row = lcd[row_num]
lcd[row_num] = row[len(row)-shift:] + row[0:len(row)-shift]
def run_col(lcd, col_num, shift):
col = []
for y in range(0, len(lcd)): col.append(lcd[y][col_num])
col = col[len(col)-shift:] + col[0:len(col)-shift]
for y in range(0, len(lcd)): lcd[y][col_num] = col[y]
def run_instructions(instructions):
lcd = new_lcd()
for inst, arg1, arg2 in instructions:
if inst == 'rect': run_rect(lcd, arg1, arg2)
elif inst == 'row': run_row(lcd, arg1, arg2)
elif inst == 'col': run_col(lcd, arg1, arg2)
return lcd
def count_pixels(lcd):
count = 0
for row in lcd:
for pixel in row:
if pixel: count+= 1
return count
def print_lcd(lcd):
for row in lcd:
str = ''
for pixel in row:
if pixel: str += '#'
else: str += '.'
print(str)
finished_lcd = run_instructions(read_instructions('day8.txt'))
print('Part 1: %d' % count_pixels(finished_lcd))
print('Part 2:')
print_lcd(finished_lcd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment