Use better slope estimate and geometry for less intcode#73
Conversation
|
Eric's official sample shows that the black box is just computing a linear inequality: Starting scale at 50 requires about 100 test() to find the first estimate. Each later scale by 2 requires between 2 and 6 further probes (possibly between 0 and 4 if the loop logic is improved) to cut the error between the approximate and actual slope in half. With no pre-filtering by approximate slope, crawling the diagonals on part 1 needs around 200 test() (better than brute force 2500 probes) and part 2 walking along the diagonal until reaching the winning spot needs around 700-1000 probes. But with pre-filtering and intelligent starting points, part 1 can probe fewer than 100 times, and part 2 around 10 times. You could also break the black box and extract the 3 constants to solve this exactly with zero lines of intcode run. |
|
The following hack (post-patch) makes it easier to see the effects of different scale and part 2 starting values: when run with With scale = 50 and x,y of 0,0 (the pre-patch condition), on my input, it produces: With scale starting at 10 and grown past 1024, and x,y determined by geometry, it produces: Additionally, you can inspect how often a given point is being probed more than once, to decide if memoization is worthwhile. Pre-patch: Post-patch: So no point is being probed 3 times (good), and the number of points probed twice is fairly small (unchanged by my patch), and memoization is going to cost more memory than time saved. Further improvements could be made by enhancing part 1 to more closely scan the two lines (advancing left and right independently by 0, 1, or 2) rather than doing a binary search on EACH line and therefore having a several wasted calls to test() when precheck() returns true for a point definitively in the middle of the line, rather than on the 2-cell border that needs actual probing. |
c46d358 to
fbc6cb7
Compare
|
I also tried scale>2048 - it cost 4 more test in parse but saved 7 in part2. That is starting to reach the point of diminishing returns |
Cut the number of test() runs of intcode (on my input) from 453 to just 145, by improving parse to come up with a more accurate slope estimate in fewer probes, for fewer false positives in part 1, and then by using that more accurate slope to start part 2 much closer to the actual answer. On my laptop, this cuts parse/part1/part2 runtime from 200/215/540us to 97/199/42us.
Description
Cut the number of test() runs of intcode (on my input) from 453 to
just 150, by spending a little bit of time during parse to refine the
slope estimate for fewer false positives in part 1, and then by using
that more accurate slope to start part 2 much closer to the actual
answer.
On my laptop, this cuts parse/part1/part2 runtime from 200/215/540us
to 97/199/42us.
Type of change
Checklist
using the same naming conventions. Code should be portable, avoiding any
architecture-specific intrinsics.
cargo testcargo fmt -- `find . -name "*.rs"`cargo clippy --all-targets --all-featuresFormatting and linting also can be executed by running
just(if installed) on the command line at the project root.