-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2-2.py
executable file
·48 lines (35 loc) · 1.1 KB
/
2-2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
import re
import sys
instructions = [insn.strip() for insn in open('2.input').readlines()]
instruction_format = re.compile("(?P<inst>forward|down|up) (?P<oper>[0-9]+)")
class Submarine:
def __init__(self):
self.horiz_pos = 0
self.depth = 0
self.aim = 0
def forward(self, steps):
self.horiz_pos += steps
self.depth += self.aim * steps
if self.depth < 0:
print(f"Illegal direction: forward {steps}")
sys.exit()
def down(self, steps):
self.aim += steps
def up(self, steps):
self.aim -= steps
def __str__(self):
return f"<{self.__class__.__name__}: ({self.horiz_pos}, {self.depth}, {self.aim})>"
sub = Submarine()
for insn in instructions:
m = instruction_format.match(insn)
if m['inst'] == 'forward':
sub.forward(int(m['oper']))
elif m['inst'] == 'down':
sub.down(int(m['oper']))
elif m['inst'] == 'up':
sub.up(int(m['oper']))
else:
print(f"Illegal instruction: {insn}")
sys.exit()
print(f"{sub.horiz_pos * sub.depth}")