In [1]:
myinput = 'input2.txt'
In [2]:
def pad_move(move, digit):
moves = {'R': 1, 'L': -1, 'U': -3, 'D': 3}
border = {'R': {3,6,9}, 'L': {1,4,7}, 'U': {1,2,3}, 'D': {7,8,9}}
new_digit = digit
if not digit in border[move]:
new_digit += moves[move]
return new_digit
def digit_sequence(myinput):
with open(myinput, 'rt') as f:
bathroom = []
digit = 5
for line in f:
for move in list(line.rstrip()):
digit = pad_move(move, digit)
bathroom.append(digit)
return int(''.join([str(i) for i in bathroom]))
In [3]:
digit_sequence(myinput)
Out[3]:
In [4]:
digit_letters = {10:'A',11:'B',12:'C',13:'D'}
def fancy_pad_move(move, digit):
f_u = lambda x: x-2 if x in {3, 13} else x-4
f_d = lambda x: x+2 if x in {1, 11} else x+4
moves = {'R': lambda x: x+1, 'L': lambda x: x-1, 'U': f_u, 'D': f_d}
border = {'R': {1,4,9,12,13}, 'L': {1,2,5,10,13}, 'U': {5,2,1,4,9}, 'D': {5,10,13,12,9}}
new_digit = digit
if not digit in border[move]:
new_digit = moves[move](digit)
return new_digit
def fancy_digit_sequence(myinput):
with open(myinput, 'rt') as f:
bathroom = []
digit = 5
for line in f:
for move in list(line.rstrip()):
digit = fancy_pad_move(move, digit)
if digit > 9:
letter = digit_letters[digit]
bathroom.append(letter)
else:
bathroom.append(digit)
return ''.join([str(i) for i in bathroom])
In [5]:
fancy_digit_sequence(myinput)
Out[5]: