Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. def matrix_to_dict(seq, empty_sentinel="0"):
  2. return {(x+1j*y):item for y,row in enumerate(seq) for x,item in enumerate(row) if item != empty_sentinel}
  3.  
  4. def get_digit_sequence(pad, instructions):
  5. deltas = dict((c,1j**i) for i,c in enumerate("DLUR", 1))
  6. pad = matrix_to_dict(pad)
  7. pos = next(k for k,v in pad.iteritems() if v=="5")
  8. digits = []
  9. for row in instructions:
  10. for c in row:
  11. if pos + deltas[c] in pad:
  12. pos += deltas[c]
  13. digits.append(pad[pos])
  14. return digits
  15.  
  16.  
  17. with open("input") as file:
  18. instructions = file.read().strip().split("\n")
  19.  
  20. pads = ["""\
  21. 123
  22. 456
  23. 789""",
  24.  
  25. """\
  26. 001
  27. 0234
  28. 56789
  29. 0ABC
  30. 00D"""]
  31.  
  32. for pad in pads:
  33. print(get_digit_sequence(pad.split(), instructions))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement