Advent of Code 2016

--- Day 8: Two-Factor Authentication --- You come across a door implementing what you can only assume is an implementation of two-factor authentication after a long game of requirements telephone. To get past the door, you first swipe a keycard (no problem; there was one on a nearby desk). Then, it displays a code on a little screen, and you type that code on a keypad. Then, presumably, the door unlocks. Unfortunately, the screen has been smashed. After a few minutes, you've taken everything apart and figured out how it works. Now you just have to work out what the screen would have displayed. The magnetic strip on the card you swiped encodes a series of instructions for the screen; these instructions are your puzzle input. The screen is 50 pixels wide and 6 pixels tall, all of which start off, and is capable of three somewhat peculiar operations: - rect AxB turns on all of the pixels in a rectangle at the top-left of the screen which is A wide and B tall. - rotate row y=A by B shifts all of the pixels in row A (0 is the top row) right by B pixels. Pixels that would fall off the right end appear at the left end of the row. - rotate column x=A by B shifts all of the pixels in column A (0 is the left column) down by B pixels. Pixels that would fall off the bottom appear at the top of the column. For example, here is a simple sequence on a smaller screen: rect 3x2 creates a small rectangle in the top-left corner: ###.... ###.... ....... rotate column x=1 by 1 rotates the second column down by one pixel: #.#.... ###.... .#..... rotate row y=0 by 4 rotates the top row right by four pixels: ....#.# ###.... .#..... rotate column x=1 by 1 again rotates the second column down by one pixel, causing the bottom pixel to wrap back to the top: .#..#.# #.#.... .#..... As you can see, this display technology is extremely powerful, and will soon dominate the tiny-code-displaying-screen market. That's what the advertisement on the back of the display tries to convince you, anyway. There seems to be an intermediate check of the voltage used by the display: after you swipe your card, if the screen did work, how many pixels should be lit?

In [1]:
from collections import namedtuple

In [2]:
class Screen:
    
    def __init__(self, rows=6, cols=50):
        self.rows = rows
        self.cols = cols
        self.screen = [[0 for _ in range(cols)] for _ in range(rows)]
        
    def load(self, instruction):
        if instruction.startswith('rect'):
            cols, rows = [range(int(i)) for i in instruction.split()[1].split('x')]
            for row in rows:
                for col in cols:
                    self._turn_on(row, col)
        else:
            _, axis, position, _, movement = instruction.split()
            position = int(position.split('=')[1])
            movement = int(movement)
            if axis == 'row':
                self._rotate_row(position, movement)
            else:
                self._rotate_column(position, movement)
                #for row in range(self.rows):
                #    for _ in range(int(movement)):
                #        print(movement)
                #        self._switch(row % self.rows, position)
                
    def _set_value(self, row, column, value):
        self.screen[row][column] = value
        
    def _turn_on(self, row, column):
        self._set_value(row, column, 1)
        
    def _turn_off(self, row, column):
        self._set_value(row, column, 0)
        
    def _switch(self, row, column):
        self.screen[row][column] = 0 if self.screen[row][column] else 1
        
    def _rotate_column(self, column, movement):
        values = [self.screen[r][column] for r in range(self.rows)]
        for index, row in enumerate(range(self.rows)):
            self._set_value(row, column, values[(index - movement) % self.rows]) 
            
    def _rotate_row(self, row, movement):
        values = [self.screen[row][c] for c in range(self.cols)]
        for index, col in enumerate(range(self.cols)):
            self._set_value(row, col, values[(index - movement) % self.cols]) 
        
    def print_screen(self):
        for r in self.screen:
            print(''.join(['#' if c else '.' for c in r]))
        
    def get_screen(self):
        return self.screen
    
    def get_lit_pixels(self):
        return sum([sum(i) for i in self.screen])

In [3]:
# Test
screen = Screen(rows=3, cols=7)
screen.load('rect 3x2')
screen.load('rotate column x=1 by 1')
screen.load('rotate row y=0 by 4')
screen.load('rotate column x=1 by 1')
assert screen.get_screen() == [[0, 1, 0, 0, 1, 0, 1], [1, 0, 1, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0]]
assert screen.get_lit_pixels() == 6

In [4]:
data = [line.strip() for line in open('data/day_8.txt', 'r')]

screen = Screen()
for instruction in data:
    screen.load(instruction)
screen.get_lit_pixels()


Out[4]:
110
--- Part Two --- You notice that the screen is only capable of displaying capital letters; in the font it uses, each letter is 5 pixels wide and 6 tall. After you swipe your card, what code is the screen trying to display?

In [5]:
screen.print_screen()


####...##.#..#.###..#..#..##..###..#....#...#..##.
...#....#.#..#.#..#.#.#..#..#.#..#.#....#...#...#.
..#.....#.####.#..#.##...#....#..#.#.....#.#....#.
.#......#.#..#.###..#.#..#....###..#......#.....#.
#....#..#.#..#.#.#..#.#..#..#.#....#......#..#..#.
####..##..#..#.#..#.#..#..##..#....####...#...##..