Solve day 9 part 1 by excluding "interior" tiles #18
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.
This reduces the part 1 time to 9 times lower; previously it took:
And now part1 and part2 are down to double digit microseconds:
It works by excluding "interior" tiles that could not possibly be part of the largest rectangle:
The comment of the filtering function
get_potential_left_corner_tiles(...)explains it a bit more.After filtering there are only around 50 tiles per corner.
It is possible to optimize
part1(...)further so that it does fewer than the approximately 50x50 comparisons, but the performance gain is only about 15% (mainly due to the input being a circle or diamond: almost all of the top left corner points are to the left of the bottom right corner points, for example) and the resulting code is much more difficult to prove or reason about correctness.As per CONTRIBUTING.md, I agree that I have authored 100% of the content, that I have the necessary rights to the content and that the content I am contributing is provided under the project license.