-
Notifications
You must be signed in to change notification settings - Fork 0
Solve 2025 day 4 #55
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 4 #55
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
20a054f
Create solution file for part 1
katzuv 8280b55
Add example input
katzuv e0e3acf
Add function that parses grid from input
katzuv b3f6b85
Add max adjacent paper rolls constant
katzuv 337d27b
Count accessible paper rolls
katzuv d75d979
Make counting function to work around empty spots as well
katzuv 3484a51
Remove redundant casting to list
katzuv 3e6f963
Use `sum()` to count accessible cells as well
katzuv d2e8bbd
Create solution file for part 2
katzuv b2c3ca3
Extract accessible paper rolls counting to a function
katzuv 5032507
Get accessible rolls instead of just counting them
katzuv 19f23d4
Revert "Remove redundant casting to list"
katzuv 296515e
Fix type hints
katzuv d3fce39
Add functions that clears already accessed rolls
katzuv c10f4cd
Use example input
katzuv ad89e93
Count total accessible rolls
katzuv 535adab
Remove redundant copying
katzuv beea472
Use empty cell constant
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,63 @@ | ||
| import itertools | ||
| import sys | ||
|
|
||
| example_input = """ | ||
| ..@@.@@@@. | ||
| @@@.@.@.@@ | ||
| @@@@@.@.@@ | ||
| @.@@@@..@. | ||
| @@.@@@@.@@ | ||
| .@@@@@@@.@ | ||
| .@.@.@.@@@ | ||
| @.@@@.@@@@ | ||
| .@@@@@@@@. | ||
| @.@.@@@.@. | ||
| """.strip() | ||
|
|
||
| PAPER_ROLL = "@" | ||
| MAX_ADJACENT_PAPER_ROLLS = 3 | ||
|
|
||
| Grid = list[list[str]] | ||
| Position = tuple[int, int] | ||
|
|
||
|
|
||
| def get_grid(input_text: str) -> Grid: | ||
| return [list(line) for line in input_text.splitlines()] | ||
|
|
||
|
|
||
| def get_adjacent_rolls_number(grid: Grid, row: int, column: int): | ||
| min_row = max(row - 1, 0) | ||
| max_row = min(row + 1, len(grid) - 1) | ||
| min_column = max(column - 1, 0) | ||
| max_column = min(column + 1, len(grid[0]) - 1) | ||
| return ( | ||
| sum( | ||
| grid[current_row][current_column] == PAPER_ROLL | ||
| for current_row, current_column in itertools.product( | ||
| range(min_row, max_row + 1), range(min_column, max_column + 1) | ||
| ) | ||
| ) | ||
| # Exclude the cell we're counting adjacent around. | ||
| - (grid[row][column] == PAPER_ROLL) | ||
| ) | ||
|
|
||
|
|
||
| def get_accessible_rolls(grid: Grid) -> list[Position]: | ||
| return [ | ||
| (row, column) | ||
| for row, column in itertools.product(range(len(grid)), range(len(grid[0]))) | ||
| if ( | ||
| grid[row][column] == PAPER_ROLL | ||
| and get_adjacent_rolls_number(grid, row, column) <= MAX_ADJACENT_PAPER_ROLLS | ||
| ) | ||
| ] | ||
|
|
||
|
|
||
| def get_answer(input_text: str): | ||
| grid = get_grid(input_text) | ||
| return len(get_accessible_rolls(grid)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| puzzle_input = sys.argv[1] if len(sys.argv) > 1 else example_input | ||
| 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,29 @@ | ||
| import sys | ||
|
|
||
| import p1 | ||
|
|
||
| EMPTY_CELL = "." | ||
|
|
||
|
|
||
| def clean_accessed_rolls( | ||
| grid: p1.Grid, accessible_rolls_positions: list[p1.Position] | ||
| ) -> None: | ||
| for row, column in accessible_rolls_positions: | ||
| grid[row][column] = EMPTY_CELL | ||
|
|
||
|
|
||
| def get_answer(input_text: str): | ||
| grid = p1.get_grid(input_text) | ||
| total_accessible_rolls = 0 | ||
| while True: | ||
| accessible_rolls = p1.get_accessible_rolls(grid) | ||
| if not accessible_rolls: | ||
| break | ||
| total_accessible_rolls += len(accessible_rolls) | ||
|
katzuv marked this conversation as resolved.
|
||
| clean_accessed_rolls(grid, accessible_rolls) | ||
| return total_accessible_rolls | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| puzzle_input = sys.argv[1] if len(sys.argv) > 1 else p1.example_input | ||
| 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.