# AoC 2016 Day 2: Pin Typer

### Source: Bathroom Security

Part 1: Take a keypad with the following layout:

1 2 3
4 5 6
7 8 9


Using the instructions UDLR for Up, Down, Left, and Right respectively, move a ‘finger’ around over the keypad. At the end of each line of instructions, output the current digit.

First, let’s define the grid. We’ll use the same trick with complex numbers as [last time]https://blog.jverkamp.com/2016/12/01/aoc-2016-day-1-taxicab-simulator/.

grid = {
'-1-1j': 1, '+0-1j': 2, '+1-1j': 3,
'-1+0j': 4, '+0+0j': 5, '+1+0j': 6,
'-1+1j': 7, '+0+1j': 8, '+1+1j': 9,
}

From there, we can move around the grid, make sure we don’t move off the end, and output a character at the end of each line:

location = 0+0j
deltas = {'U':  0-1j, 'L': -1+0j, 'R':  1+0j, 'D':  0+1j}
code = ''

with open(args.input_file, 'r') as fin:
for line in fin:
for command in line.strip():
new_location = location + deltas[command]
if new_location in grid:
location = new_location

code += str(grid[location])

Part 2: Use the following keypad instead:

- - 1 - -
- 2 3 4 -
5 6 7 8 9
- A B C -
- - D - -


(The - have been added to avoid issues with whitespace stripping.)

This is actually a kind of neat extension to the problem. The solution is the same, but the input has to change. What I did for this was to load the grid from a file (so any arbitrary grid shape can be used):

grid = {}

with open(args.grid_file, 'r') as fin:
for imag, line in enumerate(fin):
if not line.strip(): continue

for real, char in enumerate(line.strip()):
if char != '-':
grid[complex(real, imag)] = char

if char == '5':
location = complex(real, imag)

It would be interesting to use a grid like this:

1 2 - 3 4
5 - - - 6
7 8 9 0 A


Or even one with discontinuities.

A neat problem.