-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
21e527f
448c956
dd051e0
75389b6
78736fc
239d468
589c929
64d1606
1fd7355
5ffbd15
5018d7f
f98fc85
5675ce9
eabf084
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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') | ||
|
||
|
||
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(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() |
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() |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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?