Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions 2021/d02/directions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FORWARD = 'forward'
DOWN = 'down'
UP = 'up'
57 changes: 57 additions & 0 deletions 2021/d02/p1.py
Original file line number Diff line number Diff line change
@@ -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()
40 changes: 40 additions & 0 deletions 2021/d02/p2.py
Original file line number Diff line number Diff line change
@@ -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()