-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2-1.py
41 lines (27 loc) · 839 Bytes
/
day2-1.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
from dataclasses import dataclass
@dataclass
class Command:
direction: str
value: int
class Submarine:
def __init__(self):
self.depth = 0
self.position = 0
self.ops = {"forward": self.forward, "up": self.up, "down": self.down}
def do(self, command: Command):
self.ops[command.direction](command.value)
def forward(self, value):
self.position += value
def up(self, value):
self.depth -= value
def down(self, value):
self.depth += value
commands = []
with open("day2-1-input.txt", "r") as file:
for line in file:
direction, value = line.strip().split(" ")
commands.append(Command(direction, int(value)))
submarine = Submarine()
for command in commands:
submarine.do(command)
print(submarine.depth * submarine.position)