advent/2016/02-1.py

46 lines
707 B
Python
Raw Normal View History

2017-01-14 00:34:25 +00:00
# Python!
import collections
class grid:
keys = [
[ 7, 8, 9 ],
[ 4, 5, 6 ],
[ 1, 2, 3 ],
]
current_pos = [1, 1]
def move(self, direction):
if direction in ['U', 'D']:
pindex = 1
elif direction in ['L', 'R']:
pindex = 0
if direction in ['U', 'R']:
step = 1
elif direction in ['D', 'L']:
step = -1
if 0 <= self.current_pos[pindex] + step <= 2:
self.current_pos[pindex] += step
def get_key(self):
x = self.current_pos[0]
y = self.current_pos[1]
key = self.keys[y][x]
return key
g = grid()
with open('02.txt') as file:
lines = file.read().splitlines()
code = ''
for line in lines:
for char in line:
g.move(char)
code += str(g.get_key())
print(code)