Skip to content

Populate grid lazily#40

Merged
maneatingape merged 1 commit intomaneatingape:mainfrom
ebblake:2016-13
Feb 12, 2026
Merged

Populate grid lazily#40
maneatingape merged 1 commit intomaneatingape:mainfrom
ebblake:2016-13

Conversation

@ebblake
Copy link
Contributor

@ebblake ebblake commented Feb 12, 2026

Description

Rather than populating 2704 cells up front, then running the maze until all (reachable) cells have been visited, it is more efficient to populate the grid as we explore, and to quit exploring as soon as we have our answer. Cuts runtime from 6.4us to 3.1us on my machine.

It is not worth the overhead of trying to steer the part 1 solution with an A* search.

Type of change

  • Performance improvement
  • Bug fix
  • Other

Checklist

  • Pull request title and commit messages are clear and informative.
  • Documentation has been updated if necessary.
  • Code style matches the existing code. This one is somewhat subjective, but try to "fit in" by
    using the same naming conventions. Code should be portable, avoiding any architecture
    specific intrinsics.
  • Tests pass cargo test
  • Code is formatted cargo fmt -- `find . -name "*.rs"`
  • Code is linted cargo clippy --all-targets --all-features

Formatting and linting also can be executed by running just
(if installed) on the command line at the project root.

Rather than populating 2704 cells up front, then running the maze
until all (reachable) cells have been visited, it is more efficient to
populate the grid as we explore, and to quit exploring as soon as we
have our answer.  Cuts runtime from 6.4us to 3.1us on my machine.

It is not worth the overhead of trying to steer the part 1 solution
with an A* search.
@ebblake
Copy link
Contributor Author

ebblake commented Feb 12, 2026

For curiosity's sake, I coded up an A* solution (the heuristic being: if steps <= 50 {0} else {abs(x-31)+abs(y-39)} which forces BFS behavior on every point less than 50, then steers the remaining search by Manhattan distance to the goal); on my input, that reduced the number of insertions into todo from 326 down to 207 with the answer found in 197 pops, but the overhead of tracking a priority queue (where the interesting priority buckets all increment by 1) cost more than the savings. Instrumenting the code shows that for my input, lazy grid population only makes 1266 calls to at, compared to the previous 2704 grid points calculated; with A*, that drops further to 782 calls to at.

@ebblake
Copy link
Contributor Author

ebblake commented Feb 12, 2026

I noticed there were no unit tests, even though the puzzle description DOES give a part 1 example of distance to 7,4. Another possible refactor would be splitting out a testable search, but lazy computation then runs into recomputing maze points per part rather than the current setup of solving both parts at once.

Copy link
Owner

@maneatingape maneatingape left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

@maneatingape maneatingape merged commit 09ee9c8 into maneatingape:main Feb 12, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants