-
Notifications
You must be signed in to change notification settings - Fork 0
Solve 2021 day 01 #1
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
base: main
Are you sure you want to change the base?
Changes from 8 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,33 @@ | ||||||
| from pathlib import Path | ||||||
| from typing import Iterator | ||||||
|
|
||||||
| INPUT_FILE_PATH = Path('..', 'inputs', '1.txt') | ||||||
|
|
||||||
|
|
||||||
| def get_measurements_from_input(input_text: str) -> list[int]: | ||||||
|
||||||
| def get_measurements_from_input(input_text: str) -> list[int]: | |
| def get_measurements_from_input(input_text: str) -> list[int]:t |
katzuv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
katzuv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Outdated
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.
pairs is a better term.
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.
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.
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.
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 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?
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.
And the main function and defining the file's locations and printing the result...
| 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]]: | ||
|
||
| """ | ||
| 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() | ||
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?