Created
December 3, 2016 18:26
-
-
Save Ape/887787348a86736e5404381e35ef4bcf to your computer and use it in GitHub Desktop.
Aoc 2016 Day 2 with Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import numpy as np | |
KEYPADS = [ | |
np.array([ | |
["1", "2", "3"], | |
["4", "5", "6"], | |
["7", "8", "9"], | |
]), | |
np.array([ | |
[" ", " ", "1", " ", " "], | |
[" ", "2", "3", "4", " "], | |
["5", "6", "7", "8", "9"], | |
[" ", "A", "B", "C", " "], | |
[" ", " ", "D", " ", " "], | |
]) | |
] | |
DIRECTIONS = { | |
"L": ( 0, -1), | |
"R": ( 0, 1), | |
"U": (-1, 0), | |
"D": ( 1, 0), | |
} | |
lines = sys.stdin.readlines() | |
for keypad in KEYPADS: | |
result = "" | |
position = next(zip(*np.where(keypad == "5"))) | |
for line in lines: | |
for char in line.strip(): | |
newPosition = tuple(np.add(position, DIRECTIONS[char])) | |
if newPosition in zip(*np.where(keypad)): | |
position = newPosition | |
result += keypad[position] | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment