Part 1
In this puzzle we are given instructions for operations on a display panel. The panel size is given as 50 * 6 pixels and there are 3 operations: rect turns on pixels on the top left corner, rotate row shifts pixels in a certain row around and rotate column does the same for a column. We are first asked to get the number of pixels turned on after all instructions.
We start by initiliazing a 2D array of booleans indicating if the pixel is turned on or off. Each line of the instructions is then parsed via regex for the matching instruction. The rect operation can be done in numpy by just setting a whole region to True. For the rotations there is also a useful numpy function called roll, which we call with the parameters parsed before.
def turn_on(w, l):
field[0:l, 0:w] = True
def rotate_row(index, amount):
field[index, :] = numpy.roll(field[index, :], amount)
def rotate_column(index, amount):
field[:, index] = numpy.roll(field[:, index], amount)
After all instructions, we count the number of pixels turned on for the result.
Part 2
In part 2, we are asked to look at the resulting pixel configuration. The panel will show the answer als as 6*5 pixel letters.
Since we already have the resulting pixels, we just have to find a better display method than printing out the array. We take the array line by line and convert the True values into 'X' characters and the False into spaces. To better seperate the characters, we insert addtional spaces between the letters. The answer can then be read by a human. Implementing a optical character recognition or a lookup table for the characters could be done to turn the answer into a machine readable string.
No comments:
Post a Comment