Post Reply 
HHC 2015 RPN programming Contest is now open
10-04-2015, 08:50 PM
Post: #93
RE: HHC 2015 RPN programming Contest is now open
Here's another solution. It's 25 steps, 40 bytes and uses 2 registers.

The idea is to sort the numbers and then count the number of times that the difference between adjacent numbers is 1. If the difference is ever greater than 1, then reset the counter.

In pseudo-code:
Code:
SORT
last_die = 9  // pretent last die was a large number
For i = 5 to 1
    current_die = RCL IND i
    diff := last_die - current_die
    if diff > 1 then
        sum := 0;
    diff := 0;
    else
    sum := sum + diff;
    if sum = 3 then
        CLX
    RTN
RTN some non-zero value

Notice that because last_die is initialized to zero, the first time through teh loop diff is always greater than 1 so sum gets reset to 0.

Here is the code with byte counts and comments.

R06 is the index register
R07 is the value of the last die

Code:
01 LBL 'RR'   6 bytes
02 XEQ 'SORT' 6 bytes
03 5          1 byte        // Store index
04 STO 06     1 byte
05 9          1 byte        // and fake last die value
06 STO 07     1 byte

07 LBL 01     1 byte        // Running sum of diffs is in X
08 X<>Y       1 byte        // put sum back in X
09 RCL 07     1 byte        // last die
10 RCL IND 06 2 bytes       // current die
11 STO 07     1 byte        // update last die
12 -          1 byte        // get difference between last and current die
13 1          1 byte
14 X<Y?       1 byte        // difference > 1?
15 CLST       1 byte        // sum := 0, diff := 0
16 ROLL DOWN  1 byte
17 +          1 byte        // Update sum of differences
18 3          1 byte
19 X=Y?       1 byte        // If you found a run then
20 CLX        1 byte        // clear it
21 X=0?       1 byte        // and
22 RTN        1 byte        // return

23 DSE 06     2 bytes       // Loop
24 GTO 01     2 bytes
25 END        3 bytes
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 - David Hayden - 10-04-2015 08:50 PM



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