Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve 2021 day 01 #1

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
34 changes: 34 additions & 0 deletions 2021/d01/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import itertools
from pathlib import Path
from typing import Sequence, Iterable


INPUT_FILE_PATH = Path('..', 'inputs', '1.txt')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use __file__ instead of a relative path.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how __file__ fits in here, could you explain?



def get_measurements_from_input(input_text: str) -> Iterable[int]:
"""
:param input_text: input test to process
:return: split measurements from the input
"""
return map(int, input_text.splitlines())


def get_depth_measurements_increases(measurements: Sequence[int]) -> int:
"""
:param measurements: measurements to parse
:return: count of the number of times a depth measurement increases
"""
return sum(couple[1] > couple[0] for couple in itertools.pairwise(measurements))


def main():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write the I/O logic once, and for each problem implement only the pure logic: A function receiving the input as a string and returning the output as a string.

Then you can maybe think about an automation for running all the problems and uploading the results.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it will be very helpful, getting the input from the file is one line. Or did you mean something else?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the main function and defining the file's locations and printing the result...

input_text = INPUT_FILE_PATH.read_text()
measurements = get_measurements_from_input(input_text)

depth_measurements_increases = get_depth_measurements_increases(measurements)
print(f'Amount of measurements which are larger than the previous measurement: {depth_measurements_increases}')


if __name__ == '__main__':
main()
34 changes: 34 additions & 0 deletions 2021/d01/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from typing import Iterable

from p1 import INPUT_FILE_PATH, get_measurements_from_input, get_depth_measurements_increases


_DEFAULT_WINDOW_SIZE = 3


def get_measurements_windows(measurements: Iterable[int], window_size: int = _DEFAULT_WINDOW_SIZE) -> Iterable[
tuple[int]]:
"""
Create sliding windows of measurements and return them.
Stop when there are not enough measurements to produce a window with the given size.
:param measurements: measurements to create windows from
:param window_size: size of every window
:return: measurements windows
"""
measurements = list(measurements)
indented_measurements = (measurements[i:] for i in range(window_size))
return zip(*indented_measurements)


def main():
input_text = INPUT_FILE_PATH.read_text()
measurements = get_measurements_from_input(input_text)
windows = get_measurements_windows(measurements)
windows_sums = [sum(window) for window in windows]

sums_increases = get_depth_measurements_increases(windows_sums)
print(f'Amount of measurements windows sums which are larger than the previous sum: {sums_increases}')


if __name__ == '__main__':
main()