Files
advent-of-code-2021/day-02/day-02.py

43 lines
908 B
Python

#!/usr/bin/python3
from pathlib import Path
def part_1(input):
h_pos = 0
depth = 0
for line in input:
cmd, rng = line.split()
if 'forward' == cmd:
h_pos += int(rng)
elif 'up' == cmd:
depth -= int(rng)
elif 'down' == cmd:
depth += int(rng)
result = h_pos * depth
print("Part 1 result:", result)
def part_2(input):
h_pos = 0
depth = 0
aim = 0
for line in input:
cmd, rng = line.split()
if 'forward' == cmd:
h_pos += int(rng)
depth += aim * int(rng)
elif 'up' == cmd:
aim -= int(rng)
elif 'down' == cmd:
aim += int(rng)
result = h_pos * depth
print("Part 2 result:", result)
input = list()
p = Path(__file__).with_name('input.txt')
with open(p) as f:
input = f.readlines()
part_1(input)
part_2(input)