solution.py 988 B

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/python3
  2. import sys
  3. class Submarine:
  4. def __init__(self):
  5. self.position = 0
  6. self.depth = 0
  7. self.direction_lookup = {
  8. 'forward': (0, 1, 1),
  9. 'up': (1, 0, -1),
  10. 'down': (1, 0, 1)
  11. }
  12. print('position', self.position, self.depth)
  13. def encode_position(self):
  14. return self.position * self.depth
  15. def move(self, direction, units):
  16. try:
  17. vertical, horizontal, multiplier = self.direction_lookup[direction]
  18. self.position += horizontal * multiplier * units
  19. self.depth += vertical * multiplier * units
  20. print('new position', self.position, self.depth)
  21. except KeyError:
  22. print('invalid command')
  23. sub = Submarine()
  24. for line in sys.stdin:
  25. assert ' ' in line
  26. direction, units = line.rstrip('\n').split()
  27. print(direction, units)
  28. sub.move(direction, int(units))
  29. print('distance {}'.format(sub.encode_position()))