HHC 2012 Programming Contests: annotations, comments ...?



#19

As a relative newbie to RPL -- and to anything more than simple keystroke RPN -- I would find it very useful to have annotated solutions to the HHC 2012 Programming Contest problems. Equally useful would be discussions of nice insights into the problems and of nice bits of code.

All that's easy for me to ask, of course; I'd be most appreciative of any efforts in those directions. And thanks in advance for such.

Peter


#20

My solution is commented in this post.

Note that Bill Butler has written analyses of several past programming problems in Datafile. I wrote an analysis in Datafile one year too.

Dave


#21

David, I just read your message as I was headed for bed. I'll follow those links in the morning.

Thanks a lot for your reply. The generosity on view in this Forum is wonderful.

#22

Using the definitions below I ended up with the same formula as David Hayden: n = (s+1)*ds/2+dy

  • si = xi + yi, where i = 1, 2
  • s = s1 + s2
  • ds = s2 - s1
  • dy = y2 - y1

Instead of listing my own solution I'm commenting Werner's program as it is an improvement:

                        X                   Y                   Z                   T
00 { 19-Byte Prgm } y2 x2 y1 x1
01 STO+ ST Y y2 s2 y1 x1
02 RDN s2 y1 x1 y2
03 RDN y1 x1 y2 s2
04 STO- ST Z y1 x1 dy s2
05 + s1 dy s2 s2
06 X<>Y dy s1 s2 s2
07 RDN s1 s2 s2 dy
08 STO+ ST Z s1 s2 s dy
09 - ds s dy dy
10 STO* ST Y ds s*ds dy dy
11 + (s+1)*ds dy dy dy
12 2 2 (s+1)*ds dy dy
13 / (s+1)*ds/2 dy dy dy
14 + (s+1)*ds/2+dy dy dy dy
15 .END.
Kind regards

Thomas

Edited: 25 Sept 2012, 7:17 a.m.

#23

@==============================================================
@ HHC 2012 RPL challenge.
@ Size : 140 Byte
@ Speed : 0.265 sec. for the example in the challenge on real HP50G
@--------------------------------------------------------------

{ 3 6 5 2 3 1 8 2 6 5 4 4 7 4 }

« @ SEE -1- Input list
LIST-> 2. / 1. +  -> s @ SEE -2- 'explode' the list on the stack and size/2+1 in 's'
«
DUP2 @ SEE -3- Trick for one point only input
1. s FOR n R->C s 2. * n - ROLLD NEXT @ SEE -4- Transform in complex number on the stack
@ and put each result in top of the stack
@ SEE -5- : result for the last R->C before the ROLLD wich will put the complex number in level 8 and roll down 2-8 entries to 1-7
0. @ Init the max distance
s 2. + 4. FOR n @ For each number
4. n FOR m @ with each other
OVER m PICK @ SEE -6-
- @ SEE -7-
ABS @ SEE -8-
MAX @ SEE -9-
NEXT
@ The idea is to compare each element to each other
NIP @ NIP is shortcut for SWAP DROP (delete item 2 on the stack)
-1. STEP
NIP
»
» @ SEE -10-


Edited: 25 Sept 2012, 5:34 p.m.

#24

Let's consider the example in the drawing above. The source point is (x1, y1) = (2, 3) and the destination point is (x2, y2) = (3, 5). C1 and C2 in the y-axis are the linear coefficients of the straight lines y = -x + C1 and y = -x + C2, which contain these points. From the drawing,

  C1 = x1 + y1; C1 = 2 + 3; C1 = 5

C2 = x2 + y2; C2 = 4 + 5; C2 = 9
The number of steps marked in orange is
  n1 = C2 - C1; n1 = 9 - 5; n1 = 4
The number of steps marked in blue, from (x1, y1) to C1, is
  n2 = x1; n2 = 2
The number of steps marked in green, from the x-axis to (x2, y2), is
  n3 = y2; n3 = 5
The number of the remaining steps, marked in red, is
        C2-1
----
n4 = \ k ; n4 = (C22 - C2 - C12 - C1)/2; n4 = (92 - 9 - 52 - 5)/2; n4 = 21
/
----
k=C1+1
Finally, the total number of steps is
  n = n1 + n2 + n3 + n4; n = 4 + 2 + 5 + 21; n= 32
or, generically,
  n = C2 - C1 + x1 + y2 + (C22 - C2 - C12 - C1)/2
which can be simplified to
  n = ((x2 + y2)2 + (x2 + y2) - ((x1 + y1)2 + (x1 + y1)))/2 + y2 - y1

The symetry of the latter allows for this HP 50g 55-byte program:

PGM1:

« ROT DUP2 - 5 ROLLD
1 2
START
UNROT + DUP SQ +
NEXT
- 2 / +
»

Example:

2 ENTER 3 ENTER 4 ENTER 5 PGM1   -->   32

This doesn't mean, however, smaller RPL programs are not possible.

Gerson.


Edited: 25 Sept 2012, 9:17 p.m.


#25

« ROT DUP2 - 5 ROLLD
1 2
START
UNROT + DUP SQ +
NEXT
- 2 / +
»

Well done !

But with the Score= Time * Size,perhaps this could have a best score :

« ROT DUP2 - 5 ROLLD
UNROT + DUP SQ +
UNROT + DUP SQ +
- 2 / +
»

#26

Quote:
Well done !

Merci. Venu du maître du RPL, c'est vraiment un compliment ! :-)

Quote:
But with the Score= Time * Size,perhaps this could have a best score

Your're right. I had tried that first, 57.5 bytes I think, running time below 50 milliseconds. START NEXT saved 2.5 bytes, but has introduced a small delay.

That problem was meant for RPN calculators only, hence the scarce number of RPL solutions. A quest for even smaller code using various formulas and tricks might be interesting however.

Cheers,

Gerson.


#27

Probably a silly question but how are you deriving the runtime? Do you encapsulate and call your program with TICKS or something? Possibly something like this:

<< TICKS -> t
<< YOURPRG TICKS t - B->R 8192 / >>
>>

Just curious.

Thanks...


#28

Put the inputs on the stack, followed by the program name in quotes, then TEVAL and press ENTER.


#29

Yes. From the catalog HELP, HP49G,49G+ & 50 g:

Evaluates object and 
returns time for eval.
TEVAL(TCOLLECT(SIN(X)+
COS(X)))
{sqrt2*COS(X-pi/4) s:.052...

It appears TEVAL is available only from the catalog.


#30

When I was judging the contest, I just held down the alpha key and pressed TEVAL then let the alpha key go and pressed eval.

Wasn't too tough. If I were doing it for a lot of situations, I would assign it to a key.

#31

Great! Thanks.

It appears that TEVAL is not in my 48SX I have at work. I will try it on my 48G and 50 at home tonight.


#32

According to the "Command Menu-Path Table" from 50 AUR(?) [Appendix I], TEVAL did not arrive until ROM rev 1.05 -- which is presumably the 49G and onwards. So no luck on my 48G either.

Oh well. I'll try in my 49G+ (2.15).

...time passes...

Tried it on my 49G+ and it seems to return a variable amount: +/- 0.002 s. Not too surprising.

It looks like my little UserRPL knockoff does essentially the same as TEVAL (slightly improved from previous stab -- formatted to look similar to TEVAL) so I should be about to do this on 48g/s as well:

<< TICKS -> t
<< EVAL t - B->R 8192 / "s" ->TAG >>
>>
#33

The expression

  n = ((x2 + y2)2 + (x2 + y2) - ((x1 + y1)2 + (x1 + y1)))/2 + y2 - y1

above can be simplified on the HP-28S (if one is not willing to do it manually) using the EXCO program (page 256 of the Owner's Manual) as follows:

'((x2+y2)^2+(x2+y2)-
((x1+y1)^2+(x1+y1)))
/2+(y2-y1)'

EXCO

'-(x1*y1)-.5*x1^2+x2
*y2+.5*x2^2-.5*y1^2+
.5*y2^2-.5*x1+.5*x2-
1.5*y1+1.5*y2'
which can be further simplified as
'((x2 - x1) + 3*(y2 - y1) + (x2^2 - x1^2) + (y2^2 - y1^2))/2 + (x2*y2 - x1*y1)'
This allows for the following 13-step HP-15C program:

001-    42 34  f CLEAR REG
002- 49 SIGMA+ ; Notice xi and yi are in reversed order on the stack
003- 33 Rv
004- 33 Rv
005- 43 49 g SIGMA-
006- 3 3
007- 45 20 3 RCL* 3 ; R3: SIGMAx
008- 45 40 4 RCL+ 4 ; R4: SIGMAx2
009- 45 40 5 RCL+ 5 ; R5: SIGMAy
010- 45 40 6 RCL+ 6 ; R6: SIGMAy2
011- 2 2
012- 10 /
013- 45 40 7 RCL+ 7 ; R7: SIGMAxy

11111 ENTER
22222 ENTER
33333 ENTER
44444 R/S --> 2 469 130 864 (less than 3 seconds on the HP-15C, about 25 milliseconds on the HP-15C LE)

Gerson.


#34

I love recall register arithmetic and non-standard use of Sigma+, I've done that too in several programs to shorten (and obfuscate) code. But I don't recall ever using Sigma- for that purpose, nice one!


#35

It's only a pity that CLEAR SIGMA clears also the HP-15C stack. On the HP-34C and HP-41 it does not, but then they lack recall arithmetic. More recent RPN calculators, the WP 34s included, have dedicated statistical registers (with neither storage nor recall arithmetic, of course). So it appears this solution will fit the HP-15C and HP-42S only, better on the latter on which CLSIGMA can be used instead of CLEAR REG, thus preserving all registers but the statistical ones.


#36

Version 2 firmware for the 34S doesn't have separate statistical registers. They occupy the high numbered registers instead.

The 34S only got separate (& more accurate) statistical registers once memory partitioning was implemented in version 3.


- Pauli


#37

I think I'll stick to Version 3. That would save only one step anyway.

Gerson.

#38

Gerson,

After pretty much beating wp34s solutions to the RPN contest into the ground, and taking a short break, I of course turned my attention to the 15C. I came up with three 17-step solutions, all of which fail when the sum of either x,y pair exceeds 99,999. This is due me using the same algorithms used in my wp34s solutions, which call for the squaring of this sum, which exceeds the 10 digit capability of the 15C. Rather than spend a lot of time trying to optimize and fix, I just searched through the posts in this thread. I thought I recalled seeing a relatively short solution, and found yours. I want to compliment you on the above 13 step solution that works for the prescribed argument range. Very nice indeed!

Best regards

Jeff


.


#39

Hello Jeff,

Thanks for the compliment! Clearing all registers is a disadvantage, though. It's a pity Clear Stack and Clear Statistics are merged into one single instruction on the Voyagers.

My previous HP-15C attempt was 17-step long too (message #57 in the incomplete original Google-cached thread here).

Best regards,

Gerson.


Possibly Related Threads…
Thread Author Replies Views Last Post
  HHC 2013 RPN Programming Challenge - Final Thought Jeff O. 7 2,483 10-17-2013, 11:02 AM
Last Post: Jeff O.
  HHC 2013 programming contest winners Brian Walsh 52 13,088 09-26-2013, 11:39 PM
Last Post: David Hayden
  Box left at HHC Tim Wessman 1 1,648 09-26-2013, 11:05 PM
Last Post: Wlodek Mier-Jedrzejowicz
  HHC 2013 speakers' files Joe Horn 2 1,673 09-26-2013, 02:51 AM
Last Post: Geoff Quickfall
  HHC 2013 Day 2 Highlights Eddie W. Shore 6 2,663 09-23-2013, 04:03 PM
Last Post: Kimberly Thompson
  HHC 2013: Day 1 Highlights Eddie W. Shore 28 8,160 09-23-2013, 03:22 PM
Last Post: Brad Barton
  HHC / HP Museum Programming Contest for RPN and RPL machines Gene Wright 18 5,798 09-22-2013, 09:39 AM
Last Post: Miguel Toro
  HHC 2013 room numbers David Hayden 2 1,344 09-20-2013, 05:34 PM
Last Post: sjthomas
  FYI: HHC 2013 Programming contests are coming Gene Wright 0 933 09-17-2013, 03:12 PM
Last Post: gene wright
  HHC 2013: One Week To Go Eddie W. Shore 2 1,544 09-13-2013, 05:32 PM
Last Post: Craig Ruff

Forum Jump: