-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
66 lines (51 loc) · 1.69 KB
/
__init__.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from typing import List
from aocpy import BaseChallenge
from dataclasses import dataclass
FORWARD = "forward"
UP = "up"
DOWN = "down"
@dataclass
class Instruction:
direction: str
magnitude: int
def parse(instr: str) -> List[Instruction]:
o = []
for line in instr.strip().splitlines():
direction, magnitude = line.split(" ")
o.append(
Instruction(direction, int(magnitude)),
)
return o
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
depth = 0
horizontal = 0
instructions = parse(instr)
for instruction in instructions:
if instruction.direction == FORWARD:
horizontal += instruction.magnitude
elif instruction.direction == UP:
depth -= instruction.magnitude
elif instruction.direction == DOWN:
depth += instruction.magnitude
else:
raise ValueError(f"unknown direction {instruction.direction}")
return depth * horizontal
@staticmethod
def two(instr: str) -> int:
depth = 0
horizontal = 0
aim = 0
instructions = parse(instr)
for instruction in instructions:
if instruction.direction == FORWARD:
horizontal += instruction.magnitude
depth += instruction.magnitude * aim
elif instruction.direction == UP:
aim -= instruction.magnitude
elif instruction.direction == DOWN:
aim += instruction.magnitude
else:
raise ValueError(f"unknown direction {instruction.direction}")
return depth * horizontal