Skip to content

Conversation

@ianbanks
Copy link
Contributor

This is a general implementation of 2025 day 9, part 2, that works on arbitrary valid inputs (including the example input) and does not exploit the giant "notch" that many other faster implementation depend on.

It is an 18-times speed up; the existing implementation on my machine takes:

test year2025::day09::part2_bench ... bench:     841,808.75 ns/iter (+/- 62,161.00)

This new sweep line implementation takes:

test year2025::day09::part2_bench ... bench:      46,770.71 ns/iter (+/- 5,586.57)

It works by a sweep line algorithm, scanning an imaginary horizontal line from top to bottom, processing red tiles that fall on this line, in left to right order.

As it scans, it calculates which intervals on the imaginary line are within the tiled region (that is, it determines the intersection of the sweep line and the tiled region, which is a set of intervals of x-coordinates).

After determining these intervals, the red tiles on the sweep line within these intervals are added to a list of candidate tiles (the tiles that could be the top left or right tile of a rectangle). The maximum left and right extent of any rectangle that could be made with this tile are also tracked, and as the sweep line moves down and edges and corners cut into this extent, it is reduced. Candidates whose extents become empty are removed from the candidate list.

In this way, only about 200 red tile "Candidate"s at most are being tracked during the sweep, and the list of descending edges never exceeds 8 edges on my input.

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. The code was formatted with rustfmt and clippy has no warnings.

@ianbanks ianbanks force-pushed the year2025-day9-y-scanning-part-two branch from 415b5b5 to 37476b9 Compare December 15, 2025 08:54
@ianbanks
Copy link
Contributor Author

Sorry, the rustfmt changes were subtle enough that I didn't notice I hadn't applied them to the committed file. The amended commit should pass rustfmt --check.

@maneatingape maneatingape merged commit 1e884c5 into maneatingape:main Dec 15, 2025
2 of 3 checks passed
@maneatingape
Copy link
Owner

maneatingape commented Dec 15, 2025

Excellent solution, much faster and more elegant!

Massive drop in benchmark time from 668µs to 128µs.

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