Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import Generator, Literal
- Direction = Literal['forward', 'up', 'down']
- Distance = int
- Instruction = tuple[Direction, Distance]
- def parse_directions(filepath) -> Generator[Instruction, None, None]:
- with open(filepath, 'r') as f:
- for line in f:
- direction, distance = line.split()
- yield direction, int(distance)
- class Submarine_v1:
- def __init__(self) -> None:
- self.depth = 0
- self.horizontal_position = 0
- def navigate(self, instruction: Instruction) -> None:
- match instruction:
- case ("forward", forward_distance):
- self.horizontal_position += forward_distance
- case ("down", depth_down):
- self.depth += depth_down
- case ("up", depth_up):
- self.depth -= depth_up
- class Submarine_v2:
- def __init__(self) -> None:
- self.depth = 0
- self.horizontal_position = 0
- self.aim = 0
- def navigate(self, instruction: Instruction) -> None:
- match instruction:
- case ("forward", forward_distance):
- self.horizontal_position += forward_distance
- self.depth += self.aim * forward_distance
- case ("down", aim_down):
- self.aim += aim_down
- case ("up", aim_up):
- self.aim -= aim_up
- def part_a():
- fp = r"data/day02.txt"
- submarine = Submarine_v1()
- for instruction in parse_directions(fp):
- submarine.navigate(instruction)
- return submarine.depth * submarine.horizontal_position
- def part_b():
- fp = r"data/day02.txt"
- submarine = Submarine_v2()
- for instruction in parse_directions(fp):
- submarine.navigate(instruction)
- return submarine.depth * submarine.horizontal_position
- if __name__ == '__main__':
- print(part_a())
- print(part_b())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement