Advent of Code: Day 2 Solution

This one was actually easier

Today the problem was about traversing a matrix (both square and diamond shaped) and seeing where you end up. This was actually quite similar to day one.

Just like yesterday, the code is available on Github, but I will also be posting my solution here as well.

Part One:

# Read in the input file.
with open ("input.txt", "r") as myfile:  
    instructions=myfile.readlines()

keypad = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]  
directionLookup = {  
    'U': (-1, 0),
    'D': (1, 0),
    'L': (0, -1),
    'R': (0, 1)
}

for instruction in instructions:  
    currentKey = [1, 1]
    instruction = instruction.replace('\n', '')
    for letter in instruction:
        # change the key in the proper direction
        currentKey[0] += directionLookup[letter][0]
        currentKey[1] += directionLookup[letter][1]

        # Error checking for the key being out of bounds
        if currentKey[0] < 0:
            currentKey[0] = 0
        if currentKey[0] > 2:
            currentKey[0] = 2

        if currentKey[1] < 0:
            currentKey[1] = 0
        if currentKey[1] > 2:
            currentKey[1] = 2

    # Print the final key after the instruction
    print keypad[currentKey[0]][currentKey[1]]

Part Two:

# Read in the input file.
with open ("input.txt", "r") as myfile:  
    instructions=myfile.readlines()

keypad = [[0, 0, 1, 0, 0], [0, 2, 3, 4, 0], [5, 6, 7, 8, 9], [0, 'A', 'B', 'C', 0], [0, 0, 'D', 0, 0]]  
directionLookup = {  
    'U': (-1, 0),
    'D': (1, 0),
    'L': (0, -1),
    'R': (0, 1)
}
currentKey = [2, 0]  
for instruction in instructions:  
    instruction = instruction.replace('\n', '')
    for letter in instruction:
        # change the key in the proper direction
        currentKey[0] += directionLookup[letter][0]
        currentKey[1] += directionLookup[letter][1]

        # Error checking for the key being out of bounds
        if currentKey[0] < 0:
            currentKey[0] = 0
        if currentKey[0] > 4:
            currentKey[0] = 4

        if currentKey[1] < 0:
            currentKey[1] = 0
        if currentKey[1] > 4:
            currentKey[1] = 4

        # Error checking for the key not being a valid one
        if keypad[currentKey[0]][currentKey[1]] == 0:
            currentKey[0] -= directionLookup[letter][0]
            currentKey[1] -= directionLookup[letter][1]

    # Print the final key after the instruction
    print keypad[currentKey[0]][currentKey[1]]

For Reference: My input file is available on Github here, the solutions I came up with were:

  • Part One: 18843
  • Part Two: 67BB9