Post Reply 
HHC 2015 RPN programming Contest is now open
10-05-2015, 06:33 PM (This post was last modified: 10-07-2015 04:48 PM by Jeff O..)
Post: #94
RE: HHC 2015 RPN programming Contest is now open
As is my usual practice, I worked on the challenge for a while before checking the discussion here.

I initially resisted methods that required the sort routine. I worked through a few methods, best I could do was 32 steps/56 bytes using flags. I kept having nagging misgivings about using flags, when to clear them, etc. So I worked on a scheme where I stored ones in a sequence of registers, and “optimized” that down to 42 steps/66 bytes. With this concept, I kept having nagging misgivings about using registers and when to clear them. So I finally PMed Gene and asked about the propriety of using flags and/or registers. He said the rules did not prevent it, but that “in reality the routine is in the middle of a program that uses the first 15 user flags and has most registers spoken for.”

So I decided to abandon my reticence to using the sort routine and see if that would help me develop shorter/better programs. After a few iterations, I arrived at 25 step/45 byte version that re-uses register 5, but no other registers or flags, and a 26 step/46 byte version that uses no flags or registers and preserves all of the die values, in case they are needed later in the Yahtzee program. These two programs are presented below for those who might be interested.

I thought about the problem for a couple of days, trying to come up with alternate approaches that might lead to shorter programs, but had no flashes of inspiration, so I finally read this thread. As I suspected, there were some approaches that made sense and I might have arrived at independently had I kept devoting time to the problem, and others I would never have thought of in million years (e.g., using prime numbers.) Thanks Gene for another fun project.

Code:

Version 1, 25 steps/45 bytes, walks on the sorted die value stored in Register 5.
001    LBL RR        
002    XEQ SORT      execute external sort routine
003    4             enter 4 for loop index 
004    x<>05         exchange with value in R5, R5 will be loop index 
005    3             enter 3 for initial "sum of differences"
006    x<>y          exchange for correct order
007    LBL 01        
008    RCL IND 05    recall value in register pointed to by index
009    -             find difference between adjacent regiser values
010    1             enter 1
011    x<y?          difference 2 or more?
012    GTO 02        if so, these two values cannot be part of a straight, go to 
                     routine to reset sum of differences
013    RDN           if less than 2 (1 or 0) roll down to get difference and 
                     sum to x and y
014    -             subtract difference from previous "sum of differences"
015    x=0?          does running sum of differences equal 0
016    GTO 05        if so, there is a small straight, branch to end routine
017    GTO 03        if not, branch to loop check
018    LBL 02        branch point for difference greater than 1
019    3             enter 3 to reset sum of differences to three
020    LBL 03        loop check label
021    RCL IND 05    recall value in register pointed to by index for next difference
022    DSE 05        decrement index
023    GTO 01        loop back to compare two more values
024    LBL 05        end routine, will be zero for straight, value in R1 for no straight
025    END           end

Version 2, 26 steps/46 bytes, preserves all die values.
001    LBL RR        
002    XEQ SORT      execute external sort routine
003    4             enter 4 for loop index
004    ENTER         separate 4 from the following 3
005    3             enter 3 for initial "sum of differences"
006    RCL 05        recall value in register 5 for first difference comparison
007    LBL 01        
008    RCL IND Z     recall value in register pointed to by index
009    -             subtract Rn from Rn+1
010    1             enter 1
011    x<y?          difference 2 or more?
012    GTO 02        if so, these two values cannot be part of a straight, go to 
                     routine to reset sum of differences
013    RDN           if less than 2 (1 or 0) roll down to get difference and 
                     sum to x and y
014    -             subtract difference from previous "sum of differences"
015    x=0?          does running sum of differences equal 0
016    GTO 05        if so, there is a small straight, branch to end routine
017    GTO 03        if not branch to loop check
018    LBL 02        branch point for difference greater than 1
019    RCL ST T      recall index
020    3             enter 3 to reset sum of differences to three
021    LBL 03        loop check label
022    RCL IND Y     recall value in register pointed to by index for next difference
023    DSE Z         decrement index
024    GTO 01        loop back to compare two more values
025    LBL 05        end routine, will be zero for straight, value in R1 for no straight
026    END           end

- edited for the sake of brevity, improve program comments, and to correct typos

Dave - My mind is going - I can feel it.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: HHC 2015 RPN programming Contest is now open - Jeff O. - 10-05-2015 06:33 PM



User(s) browsing this thread: 7 Guest(s)