-
Notifications
You must be signed in to change notification settings - Fork 0
Solve 2025 day 6 #58
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
Merged
Merged
Solve 2025 day 6 #58
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0bdecc7
Create solution file for part 1
katzuv 4e4148d
Add input example
katzuv 8da4de7
Remove stripping from input example to keep spaces after the last `+`
katzuv 90bca9b
Add function that parses list of problems from input
katzuv 79bc841
Sum problems results
katzuv 61079fe
Combine two comprehensions into one and use a map for mapping symbols…
katzuv fc07824
part Create solution file for part 2
katzuv d89618f
Add function that parses numbers from the input text
katzuv d533619
Revert "Add function that parses numbers from the input text"
katzuv 593cd2a
Add function that "transposes" the number columns
katzuv c107d74
Sum results according to Cephalopod math
katzuv 4905cca
Replace "column" with "row" in comment
katzuv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import functools | ||
| import operator | ||
| import re | ||
| import sys | ||
| from collections.abc import Callable | ||
|
|
||
| input_example = """123 328 51 64 | ||
| 45 64 387 23 | ||
| 6 98 215 314 | ||
| * + * + """ | ||
|
|
||
| Problem = tuple[list[int], Callable[[int, int], int]] | ||
|
|
||
| OPERATIONS_MAP = {"+": operator.add, "*": operator.mul} | ||
|
|
||
|
|
||
| def get_problems(input_text: str) -> list[Problem]: | ||
| lines = input_text.splitlines() | ||
| number_lines = lines[:-1] | ||
| return [ | ||
| ( | ||
| [int(line[m.start() : m.end()]) for line in number_lines], | ||
| OPERATIONS_MAP[m.group().strip()], | ||
| ) | ||
| for m in re.finditer(r"[+*]\s*", lines[-1]) | ||
| ] | ||
|
|
||
|
|
||
| def get_answer(input_text: str): | ||
| problems = get_problems(input_text) | ||
| return sum( | ||
| functools.reduce(operation, numbers) for (numbers, operation) in problems | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| puzzle_input = sys.argv[1] if len(sys.argv) > 1 else input_example | ||
| print(get_answer(puzzle_input)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import functools | ||
| import sys | ||
|
|
||
| import p1 | ||
|
|
||
|
|
||
| def transpose_numbers(number_lines: list[str]) -> list[list[int]]: | ||
| columns_count = len(number_lines[0]) | ||
| number_lines = [ | ||
| [line[column] for line in number_lines] for column in range(columns_count) | ||
| ] | ||
| transposed_number_lines = [[]] | ||
| for line in number_lines: | ||
|
katzuv marked this conversation as resolved.
Comment on lines
+9
to
+13
|
||
| try: | ||
| transposed_number_lines[-1].append(int("".join(line))) | ||
| except ValueError: | ||
| # Whitespace column, create a new problem. | ||
| transposed_number_lines.append([]) | ||
|
|
||
|
katzuv marked this conversation as resolved.
Comment on lines
+12
to
+19
|
||
| return transposed_number_lines | ||
|
|
||
|
|
||
| def get_answer(input_text: str): | ||
| lines = input_text.splitlines() | ||
| number_lines, operators_line = lines[:-1], lines[-1] | ||
| number_lines = transpose_numbers(number_lines) | ||
|
|
||
| return sum( | ||
| functools.reduce(p1.OPERATIONS_MAP[sign], numbers) | ||
| for (numbers, sign) in zip(number_lines, operators_line.split(), strict=True) | ||
| ) | ||
|
katzuv marked this conversation as resolved.
|
||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| puzzle_input = sys.argv[1] if len(sys.argv) > 1 else p1.input_example | ||
|
|
||
| print(get_answer(puzzle_input)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.