Files
advent-of-code-2021/day-02/day-02.py
Pascal Lais a23013420d
All checks were successful
continuous-integration/drone/push Build is passing
Update 'day-02/day-02.py'
2021-12-06 07:43:00 +01:00

43 lines
912 B
Python

#!/usr/bin/env 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)