Initial commit with days 1, 2, and 3

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2021-12-03 12:36:35 -08:00
commit adb46f38a8
8 changed files with 4326 additions and 0 deletions

47
day02/day02.py Executable file
View 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)