Initial commit with days 1, 2, and 3
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
47
day02/day02.py
Executable file
47
day02/day02.py
Executable file
@@ -0,0 +1,47 @@
|
||||
#!/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)
|
||||
Reference in New Issue
Block a user