*Find my full solution repo here.*

Day 13’s puzzle wasn’t so bad, although I spent a long time debugging something
**really** silly. I also made things much more complicated than they needed to
be when determining how to move the joystick.

## Breakout!

The data structure for this puzzle is similar to the one I used on the Space Police puzzle - a 2d grid with inverted y-axis.

The main loop is simple. Draw the screen and then count the blocks. Bam.

```
while output[1] != "halt":
x = computer.compute([])[0]
y = computer.compute([])[0]
check_bounds(screen, x, y)
output = computer.compute([])
place_tile(screen, x, y, output[0])
print("part 1: " + str(count_tiles(screen, 2)))
```

`check_bounds(screen, x, y)`

will grow the boundaries of the screen if
necessary. This allows for arbitrary screen sizes.

## I Overcomplicated AI (Like Everyone Else)

For part 2, you really just have to keep the score output and move the paddle. I made “move the paddle” way more complicated than it needed to be for 45 minutes or so. I was tracking the vector that the ball was travelling in and trying to move the paddle to the point where the ball-path would intersect with the paddle’s horizontal plane.

Of course, you can just move the paddle to always be under the ball… I eventually realized this and had a solid facepalm moment.

Lastly, I mentioned that I had a **really silly bug**.

I first wrote a function to find the ball:

```
def find_ball(screen):
x = -1
y = -1
for index_y, row in enumerate(screen):
for index_x, col in enumerate(row):
if col == 4:
x = index_x
y = index_y
return (x, y)
```

Then I thought, “This would also be useful for finding the paddle”, so I refactored:

```
def find_tile(screen, tile):
x = -1
y = -1
for index_y, row in enumerate(screen):
for index_x, col in enumerate(row):
if col == 4:
x = index_x
y = index_y
return (x, y)
```

But the paddle was always showing up as the same position as the ball, even when that was obviously not the case. Do you see the bug?

I forgot to change the constant ‘4’ to use the ’tile’ parameter. Whoops!

All in all, I thought this was a great puzzle and next time I have to pay closer attention to my refactors.

*Find my full solution repo here.*