1234567891011121314151617181920212223242526272829303132333435 |
- #!/usr/bin/python3
- import sys
- class Submarine:
- def __init__(self):
- self.position = 0
- self.depth = 0
- self.direction_lookup = {
- 'forward': (0, 1, 1),
- 'up': (1, 0, -1),
- 'down': (1, 0, 1)
- }
- print('position', self.position, self.depth)
- def encode_position(self):
- return self.position * self.depth
- def move(self, direction, units):
- try:
- vertical, horizontal, multiplier = self.direction_lookup[direction]
- self.position += horizontal * multiplier * units
- self.depth += vertical * multiplier * units
- print('new position', self.position, self.depth)
- except KeyError:
- print('invalid command')
- sub = Submarine()
- for line in sys.stdin:
- assert ' ' in line
- direction, units = line.rstrip('\n').split()
- print(direction, units)
- sub.move(direction, int(units))
- print('distance {}'.format(sub.encode_position()))
|