From 2431b0fd8dfa33b3a12a44393f1d2a822c1549a5 Mon Sep 17 00:00:00 2001 From: Stefan Harmuth Date: Thu, 2 Dec 2021 06:29:31 +0100 Subject: [PATCH] day02: code beautification --- day02.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/day02.py b/day02.py index e3b83e6..98bf72e 100644 --- a/day02.py +++ b/day02.py @@ -1,5 +1,21 @@ from aoc import AOCDay -from typing import Any +from typing import Any, List + + +def follow_directions(path: List) -> (int, int, int): + pos = 0 + depth = 0 + aim = 0 + for direction in path: + if direction[0] == 'forward': + pos += direction[1] + depth += aim * direction[1] + elif direction[0] == 'down': + aim += direction[1] + elif direction[0] == 'up': + aim -= direction[1] + + return pos, depth, aim class Day(AOCDay): @@ -8,30 +24,12 @@ class Day(AOCDay): def part1(self) -> Any: path = self.getInputAsArraySplit(" ", [str, int]) - pos = 0 - depth = 0 - for direction in path: - if direction[0] == 'forward': - pos += direction[1] - elif direction[0] == 'down': - depth += direction[1] - elif direction[0] == 'up': - depth -= direction[1] + pos, _, depth = follow_directions(path) return pos * depth def part2(self) -> Any: path = self.getInputAsArraySplit(" ", [str, int]) - pos = 0 - depth = 0 - aim = 0 - for direction in path: - if direction[0] == 'forward': - pos += direction[1] - depth += aim * direction[1] - elif direction[0] == 'down': - aim += direction[1] - elif direction[0] == 'up': - aim -= direction[1] + pos, depth, _ = follow_directions(path) return pos * depth