Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
33 changes: 33 additions & 0 deletions 2021/d01/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from pathlib import Path
from typing import Iterator

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) -> list[int]:
Copy link
Owner Author

Choose a reason for hiding this comment

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

Suggested change
def get_measurements_from_input(input_text: str) -> list[int]:
def get_measurements_from_input(input_text: str) -> list[int]:t

"""
:param input_text: input test to process
:return: split measurements from the input
"""
return list(map(int, input_text.splitlines()))


def get_depth_measurements_increases(measurements: Iterator[int]) -> int:
"""
:param measurements: measurements to parse
:return: count of the number of times a depth measurement increases
"""
couples = zip(measurements[:-1], measurements[1:])
Copy link
Collaborator

Choose a reason for hiding this comment

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

pairs is a better term.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thanks, after I finished my solution I found out there's a new pairwise function in itertools. Figured out if I was going to change something, better to use the new function already.

return sum(couple[1] > couple[0] for couple in couples)


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 Iterator

from p1 import INPUT_FILE_PATH, get_measurements_from_input, get_depth_measurements_increases

_DEFAULT_WINDOW_SIZE = 3


def get_measurements_windows(measurements: Iterator[int], window_size: int = _DEFAULT_WINDOW_SIZE) -> list[tuple[int]]:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Think how you can use zip for the general window size. Hint: argument list unpacking.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Was 5675ce9 what you expected?

"""
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
"""
windows = []
stop_index = (len(measurements) // window_size) * window_size
for i in range(stop_index):
windows.append(measurements[i:i + window_size])
return windows


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()