Day 2 - Advent of Code 2016

Working solutions for the day 2 puzzles.

Part One

""" day_02_01.py """

# usage: python3 day_02_01.py < input

keypad = {1: [1, 4, 1, 2], 2: [2, 5, 1, 3], 3: [3, 6, 2, 3],
          4: [1, 7, 4, 5], 5: [2, 8, 4, 6], 6: [3, 9, 5, 6],
          7: [4, 7, 7, 8], 8: [5, 8, 7, 9], 9: [6, 9, 8, 9]}

compass = {'U': 0, 'D': 1, 'L': 2, 'R': 3}

key = 5
code = ''
while True:
    try:
        line = input()
    except EOFError:
        break
    for i in line:
        key = keypad[key][compass[i]]
    code = code + str(key)

print(code)

Part Two

""" day_02_02.py """

# usage: python3 day_02_02.py < input

keypad = {'1': ['1', '3', '1', '1'], '2': ['2', '6', '2', '3'],
          '3': ['1', '7', '2', '4'], '4': ['4', '8', '3', '4'],
          '5': ['5', '5', '5', '6'], '6': ['2', 'A', '5', '7'],
          '7': ['3', 'B', '6', '8'], '8': ['4', 'C', '7', '9'],
          '9': ['9', '9', '8', '9'], 'A': ['6', 'A', 'A', 'B'],
          'B': ['7', 'D', 'A', 'C'], 'C': ['8', 'C', 'B', 'C'],
          'D': ['B', 'D', 'D', 'D']}

compass = {'U': 0, 'D': 1, 'L': 2, 'R': 3}

key = '5'
code = ''
while True:
    try:
        line = input()
    except EOFError:
        break
    for i in line:
        key = keypad[key][compass[i]]
    code = code + key

print(code)