HHC 2012 RPN Programming Contest - 10-Step Way-After-Nashville Solution Message #1 Posted by Jeff O. on 11 Oct 2012, 8:49 a.m.
The HHC 2012 RPN Programming Contest seems to be the gift that keeps on giving, for me at least. Despite doubts expressed in my "...Conundrum" thread regarding improving on my previous 12-step best, let alone equaling or bettering Paul's 11-step program, I was able to do both. I obviously failed miserably regarding my intention to stop trying.
My 12-step previous best was based on the following equation:
steps between two points = (D2+D1) * (D2-D1+1)/2 - (y1 - y2) - D1 where D2 = x2+y2 and D1= x1 + y1
This can be transformed into the following equation:
steps between two points = (D2^2 - D1^2 + D2 - D1)/2 + y2 - y1
I managed to write a 10 step program implementing the above as follows:
X Y Z T L I
beginning values in stack -> y2 x2 y1 x1 - -
001 <>YTXZ re-order stack for next step, enables x2 x1 y2 y1 - -
complex add to create D1 and D2
simultaneously
002 CPX+ Complex add to create D2 in X, D2 D1 y2 y1 x2 x1
D1 in Y, y2 and y1 still in Z and T
003 CPX x^2 create D2^2-D1^2 in X (also 2xD1xD2 in D2^2-D1^2 2D1*D2 y2 y1 D2 D1
Y, not needed)
004 RCL+L Add D2 from register L (Last X) D2^2-D1^2+D2 2D1*D2 y2 y1 D2^2-D1^2 D1
005 RCL-I subtract D1 which is still in register I D2^2-D1^2+D2-D1 2D1*D2 y2 y1 D2^2-D1^2+D2 D1
(Last Y from CPX x^2 operation)
006 <>XZTY re-order stack D2^2-D1^2+D2-D1 y2 y1 2D1*D2 D2^2-D1^2+D2 D1
007 2 enter 2 2 D2^2-D1^2+D2-D1 y2 y1 D2^2-D1^2+D2 D1
008 / divide by 2 (D2^2-D1^2+D2-D1)/2 y2 y1 y1 2 D1
009 + add y2 D2^2-D1^2+D2-D1)/2+y2 y1 y1 y1 (D2^2-D1^2+D2-D1)/2 D1
010 RCL-Y subtract y1 (D2^2-D1^2+D2-D1)/2+y2-y1
I guess the only new technique used above, or at least a technique which I do not believe was used in any other solutions, is taking advantage of the fact that complex operations copy the Y value into I but subsequent real operations do not alter the I register, so it can be used later. Also, was there a consensus reached that L (and hopefully by extension I) count as stack registers and so the above would be considered all-stack??
Of course my original equation can also be transformed into the above form, so I might have gotten there eventually had I not read and gained insights from the Forum posts. But I doubt it. I also fully acknowledge that Paul or Dave or many others could have gotten to 10 steps had they kept at it as long as I did. Hopefully, this will be my last word.
Thanks for the challenge Gene.
..
|