aoc2021/day02.py

47 lines
1.0 KiB
Python

from tools.aoc import AOCDay
from typing import Any, List
def follow_directions(path: List) -> (int, int, int):
pos = depth = aim = 0
for direction, value in path:
if direction == 'forward':
pos += value
depth += aim * value
elif direction == 'down':
aim += value
elif direction == 'up':
aim -= value
return pos, depth, aim
class Day(AOCDay):
inputs = [
[
(150, "test_input02"),
(2120749, "input02")
],
[
(900, "test_input02"),
(2138382217, "input02")
]
]
def part1(self) -> Any:
path = self.getInputAsArraySplit(" ", [str, int])
pos, _, depth = follow_directions(path)
return pos * depth
def part2(self) -> Any:
path = self.getInputAsArraySplit(" ", [str, int])
pos, depth, _ = follow_directions(path)
return pos * depth
if __name__ == '__main__':
day = Day(2)
day.run(verbose=True)