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

e>

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):

```python
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.