Files
adventofcode-2021/day02/day02.py
2021-12-03 12:36:35 -08:00

48 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
from typing import Sequence, Tuple
def part1(lines: Sequence[Tuple[str, int]]):
horz = 0
depth = 0
for direction, amount in lines:
if direction == "forward":
horz += amount
elif direction == "down":
depth += amount
elif direction == "up":
depth -= amount
else:
assert False, f"not sure how to handle {direction}"
print(f"horizontal position: {horz}")
print(f"depth: {depth}")
print(f"product: {depth * horz}")
def part2(lines: Sequence[Tuple[str, int]]):
horz = 0
depth = 0
aim = 0
for direction, amount in lines:
if direction == "forward":
horz += amount
depth += aim * amount
elif direction == "down":
aim += amount
elif direction == "up":
aim -= amount
else:
assert False, f"not sure how to handle {direction}"
print(f"horizontal position: {horz}")
print(f"depth: {depth}")
print(f"product: {depth * horz}")
lines = [(direction, int(amount)) for direction, amount in map(str.split, sys.stdin)]
print("Part 1")
part1(lines)
print("Part 2")
part2(lines)