2.py 773 B

1234567891011121314151617181920212223242526272829303132
  1. import util
  2. from collections import namedtuple
  3. input = util.get_input("2.input")
  4. State = namedtuple('State', ['x', 'y', 'aim'])
  5. def run(ops):
  6. state = State(0, 0, 0)
  7. for line in input:
  8. [op, arg] = line.split(' ')
  9. op = ops[op]
  10. state = op(state, int(arg))
  11. return state
  12. ops = {
  13. 'up': lambda s, a: State(s.x, s.y - a, 0),
  14. 'down': lambda s, a: State(s.x, s.y + a, 0),
  15. 'forward': lambda s, a: State(s.x + a, s.y, 0),
  16. }
  17. state = run(ops)
  18. print(state.x * state.y)
  19. ops = {
  20. 'up': lambda s, a: State(s.x, s.y, s.aim - a),
  21. 'down': lambda s, a: State(s.x, s.y, s.aim + a),
  22. 'forward': lambda s, a: State(s.x + a, s.y + a * s.aim, s.aim),
  23. }
  24. state = run(ops)
  25. print(state.x * state.y)