41 lines
864 B
Python
41 lines
864 B
Python
import sys
|
|
from math import prod
|
|
from typing import Literal, cast
|
|
|
|
lines = sys.stdin.read().splitlines()
|
|
|
|
commands = [
|
|
(cast(Literal["forward", "up", "down"], (p := line.split())[0]), int(p[1]))
|
|
for line in lines
|
|
]
|
|
|
|
|
|
def depth_and_position(use_aim: bool):
|
|
aim, pos, depth = 0, 0, 0
|
|
for command, value in commands:
|
|
d_depth = 0
|
|
match command:
|
|
case "forward":
|
|
pos += value
|
|
depth += value * aim
|
|
case "up":
|
|
d_depth = -value
|
|
case "down":
|
|
d_depth = value
|
|
|
|
if use_aim:
|
|
aim += d_depth
|
|
else:
|
|
depth += value
|
|
|
|
return depth, pos
|
|
|
|
|
|
# part 1
|
|
answer_1 = prod(depth_and_position(False))
|
|
print(f"answer 1 is {answer_1}")
|
|
|
|
# part 2
|
|
answer_2 = prod(depth_and_position(True))
|
|
print(f"answer 2 is {answer_2}")
|