diff --git a/2021/d02/directions.py b/2021/d02/directions.py new file mode 100644 index 00000000..67ef9208 --- /dev/null +++ b/2021/d02/directions.py @@ -0,0 +1,3 @@ +FORWARD = 'forward' +DOWN = 'down' +UP = 'up' diff --git a/2021/d02/p1.py b/2021/d02/p1.py new file mode 100644 index 00000000..9051211b --- /dev/null +++ b/2021/d02/p1.py @@ -0,0 +1,57 @@ +import collections +from pathlib import Path +from typing import Iterator + +import directions + + +INPUT_FILE_PATH = Path('..', 'inputs', '2.txt') + + +Command = collections.namedtuple('Command', ('direction', 'step')) + + +def get_commands_from_input(input_text: str) -> list[Command]: + """ + :param input_text: input test to process + :return: commands from the input + """ + commands = [] + for command in input_text.splitlines(): + direction, step = command.split() + commands.append(Command(direction, int(step))) + return commands + + +def get_position(commands: Iterator[Command], horizontal: int = 0, depth: int = 0) -> tuple[int, int]: + """ + Get position the submarine would be at after completing the given commands. + :param commands: list of command to follow + :param horizontal: initial horizontal position, defaults to 0 + :param depth: initial depth, defaults to 0 + :return: horizontal and depth of the submarine after completing the given commands + """ + for command in commands: + step = command.step + match command.direction: + case directions.FORWARD: + horizontal += step + case directions.DOWN: + depth += step + case directions.UP: + depth -= step + + return horizontal, depth + + +def main(): + input_text = INPUT_FILE_PATH.read_text() + measurements = get_commands_from_input(input_text) + + horizontal_position, depth = get_position(measurements) + product = horizontal_position * depth + print(f"Product of the submarine's positional position with its depth: {product}") + + +if __name__ == '__main__': + main() diff --git a/2021/d02/p2.py b/2021/d02/p2.py new file mode 100644 index 00000000..54c7bcfc --- /dev/null +++ b/2021/d02/p2.py @@ -0,0 +1,40 @@ +from typing import Iterator + +import directions +from p1 import INPUT_FILE_PATH, get_commands_from_input, Command + + +def get_position(commands: Iterator[Command], horizontal: int = 0, depth: int = 0, aim: int = 0) -> tuple[int, int]: + """ + Get position the submarine would be at after completing the given commands. + :param commands: list of command to follow + :param horizontal: initial horizontal position, defaults to 0 + :param depth: initial depth, defaults to 0 + :param aim: initial aim, defaults to 0 + :return: horizontal and depth of the submarine after completing the given commands + """ + for command in commands: + step = command.step + match command.direction: + case directions.FORWARD: + horizontal += step + depth += aim * step + case directions.DOWN: + aim += step + case directions.UP: + aim -= step + + return horizontal, depth + + +def main(): + input_text = INPUT_FILE_PATH.read_text() + measurements = get_commands_from_input(input_text) + + horizontal_position, depth = get_position(measurements) + product = horizontal_position * depth + print(f"Product of the submarine's positional position with its depth: {product}") + + +if __name__ == '__main__': + main()