Post Reply 
HHC 2015 RPN programming Contest is now open
09-27-2015, 11:54 PM (This post was last modified: 09-28-2015 01:21 PM by Bill (Smithville NJ).)
Post: #21
RE: HHC 2015 RPN programming Contest is now open
I took a pass at it. 40 steps and 75 bytes on an HP-42S using only HP-41C instructions (I hope).

Code:

 01 LBL "RR"
 02 CF 00
 03 CF 01
 04 CF 02
 05 CF 03
 06 CF 04
 07 CF 05
 08 CF 06
 09 0
 10 STO 07
 11 5
 12 STO 10
 13 LBL 01
 14 RCL IND 10
 15 FS? IND X
 16 GTO 02
 17 SF IND X
 18 1
 19 -
 20 10^X
 21 ST+ 07
 22 LBL 02
 23 DSE 10
 24 GTO 01
 25 RCL 07
 26 1111
 27 X=Y?
 28 SF 00
 29 10
 30 *
 31 X=Y?
 32 SF 00
 33 10
 34 *
 35 X=Y?
 36 SF 00
 37 1
 38 FS? 00
 39 0
 40 END

There should be some opportunity to make it smaller, but I'm just happy that I was able to come up with a solution to it. I forgot how rusty I was with programming a calculator (I don't do it much any more). It gave me an opportunity to re-familiarize myself with the HP-41C manual.

Lots of fun.

Bill Smithville

Edit: Minor Change of Program LBL to "RR". Jud now actually read the complete Rules.
Find all posts by this user
Quote this message in a reply
09-28-2015, 12:21 AM
Post: #22
RE: HHC 2015 RPN programming Contest is now open
(09-27-2015 09:02 PM)Egan Ford Wrote:  I think my solution is the same as yours, however a bit longer and one less register.

Gerson, Egan,
Very nice solutions! Here is what I finished last night. I have not edited it for optimization but nothing is obvious to me without rethinking how it's done: http://www.enterhp.com/images/HHC2015_AllenT.zip
(Zip file includes python test code, and 2 routines to check every possible YATZEE solution, along with a RAW file for Free42.)

I chose base 2 so I can do the bitmasks (16) and (15) in 3 bytes.

Code:

00 { 66-Byte Prgm }
01>LBL "RR"
02 CLX
03 STO 08        #08 is the accumulator for 2^x
04 6            #First value to check for
05 STO 06        # value register
06>LBL 00        #inner loop for checking 01-05
07 5            # indirect register pointer
08 STO 00        
09 CF 01        # Flag 01 is used (idempotent) to 
10>LBL 01        #<-   calculate 2^x later
11 RCL IND 00    #  |
12 RCL 06        #  |  if register matches current value
13 X=Y?        #  |
14 SF 01        #  |  set the flag
15 DSE 00        #--
16 GTO 01        # loop through all registers
17 FC? 01        # If found none of this value go to next lower dice
18 GTO 02        
19 2            # otherwise 
20 RCL 06
21 Y^X        # add 2^x 
22 STO+ 08        # to (08)  (max once per dice value!) 
23>LBL 02        
24 DSE 06        # get the next lowest dice value
25 GTO 00           # check all registers again
26>LBL 03        # (08) could hold a value from 1 to 127
27 2            # the first pass shifts (08) back to range(0,63) 0x111111
28 STO÷ 08             
29 RCL 08        # don't need to worry about LSB fractional part of (08)
30 IP            # NOTE: this could be done: if(x AND (x>>1) AND x(>>2))>0 then match!
31 X<Y?        # if the result is less than 2, quit
32 GTO 04
33 16            # mod 2^n zeros out all bits higher than n (Same as x OR (2^n)-1)
34 MOD        # enforces 00xxxx
35 15            # look for 0x1111 (4 in a row)
36 X!=Y?        # only has to find it once 
37 GTO 03        # if not found, repeat division (shift right)
38 CLST        # Clear stack to return 0 if found
39>LBL 04
40 X<>Y        # The value from line 27 (2) will be on Stack Y if not a Small straight
41 .END.

Here is my 41-byte 42s solution:

Code:

00 { 41-Byte Prgm }
01>LBL "QQ"
02 5
03 STO 00
04 CLX
05>LBL 01
06 2
07 RCL IND 00
08 Y^X
09 OR
10 DSE 00
11 GTO 01
12 3
13 STO 00
14 X<>Y
15>LBL 02
16 STO ST Y
17 2
18 ÷
19 AND
20 DSE 00
21 GTO 02
22 X>0?
23 CLST
24 X<>Y
25 END

17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b

Find all posts by this user
Quote this message in a reply
09-28-2015, 12:53 AM (This post was last modified: 09-28-2015 01:33 AM by Egan Ford.)
Post: #23
RE: HHC 2015 RPN programming Contest is now open
HP 41C
40 Bytes
26 Lines

Code:

  1 LBL RR                  ; 11 SigmaREG required?
  2 CL Sigma                ; count numbers in sequence
  3 6                       ; search for 6, 5, 4, 3, 2, 1 loop
  4 STO 07
  5 LBL 07
  6     5                   ; search each dice
  7     STO 06
  8     LBL 06
  9         RCL 07          ; outer loop
 10         RCL IND 06      ; inner loop
 11         x=y?            ; is there a match?
 12         GTO 09          ; if so, break out of here
 13     DSE 06
 14     GTO 06
 15     CL Sigma            ; no match, reset sequence counter
 16     LBL 09
 17     x=y?                ; got match
 18     Sigma+              ; inc sequence counter
 19     RCL 16              ; recall n
 20     4
 21     -
 22     x=0?                ; got 4 in a row?
 23     STOP                ; then stop with X = 0
 24 DSE 07
 25 GTO 07
 26 END                     ; X = non-zero
Find all posts by this user
Quote this message in a reply
09-28-2015, 01:29 AM
Post: #24
RE: HHC 2015 RPN programming Contest is now open
Egan,
Very beautiful solutions- I need to study the 40 byte soution.. at first glance, it is a total mystery how it beautifully works!

I have not checked the early termination, but I see you call STOP before the end of the code in a few of your programs?

Do your solutions satisfy rule 7 form the contest description? (e.g. if new data is entered and R/S is pressed it will continue?). I'm not that familiar with the HP 41-- somewhere I read on the 42S if you use a STOP command in a program, execution will resume from that point if R/S is pressed, not return to the beginning of the label?

Quote: "[Contest Rule] 7)...successive runs must work with pressing R/S."

17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b

Find all posts by this user
Quote this message in a reply
09-28-2015, 01:31 AM
Post: #25
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 12:53 AM)Egan Ford Wrote:  HP 41C
40 Bytes
26 Lines

Very nice! This will be hard to beat.

My try set flags indirectly and then checked for sequences (e.g. 3 and 4 must be present). 68 bytes I think.

- Pauli
Find all posts by this user
Quote this message in a reply
09-28-2015, 01:35 AM
Post: #26
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 01:29 AM)Allen Wrote:  Do your solutions satisfy rule 7 form the contest description? (e.g. if new data is entered and R/S is pressed it will continue?). I'm not that familiar with the HP 41-- somewhere I read on the 42S if you use a STOP command in a program, execution will resume from that point if R/S is pressed, not return to the beginning of the label?
Hmmm... Probably not. Back to work.
Find all posts by this user
Quote this message in a reply
09-28-2015, 01:40 AM
Post: #27
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 12:21 AM)Allen Wrote:  Here is what I finished last night. I have not edited it for optimization but nothing is obvious to me without rethinking how it's done: http://www.enterhp.com/images/HHC2015_AllenT.zip
(Zip file includes python test code, and 2 routines to check every possible YATZEE solution, along with a RAW file for Free42.)

Allen,

Nice documentation provided by you and others, unlike my uncommented code. I started only yesterday, late at night. I wasn't thinking of trying this one, so I am glad I came up with a working solution. I hope the lack of comments might be compensated by a QBASIC program which checks every permutation and gives a list of the only 16 permutations that passes three consecutive tests (I had to make sure these three tests would suffice). It is interesting to see the various approaches presented in this thread for the same problem.


Gerson.

Code:

DEFINT A-Z
DIM MS(1500, 5), M(5), D(4)
CLS
CT = 0
FOR A = 1 TO 6
  FOR B = 1 TO 6
    FOR C = 1 TO 6
      FOR D = 1 TO 6
        FOR E = 1 TO 6
          M(1) = A: M(2) = B: M(3) = C: M(4) = D: M(5) = E
          FOR I = 1 TO 4
            FOR J = I + 1 TO 5
              IF M(I) > M(J) THEN SWAP M(I), M(J)
            NEXT J
          NEXT I
          FOR I = 1 TO 4
            D(I) = M(I + 1) - M(I)
          NEXT I
          ZEROS = 0: ONES = 0
          FOR I = 1 TO 4
            IF D(I) = 0 THEN ZEROS = ZEROS + 1
            IF D(I) = 1 THEN ONES = ONES + 1
          NEXT I
          IF ONES > ZEROS THEN IF ONES >= 3 THEN IF (D(2) + D(3)) < 3 THEN CT = CT + 1: MS(CT, 1) = M(1): MS(CT, 2) = M(2): MS(CT, 3) = M(3): MS(CT, 4) = M(4): MS(CT, 5) = M(5)
        NEXT E
      NEXT D
    NEXT C
  NEXT B
NEXT A
PRINT CT: PRINT
FOR I = 1 TO CT - 1
  FOR J = I + 1 TO CT
    IF (MS(I, 1) = MS(J, 1) AND MS(I, 2) = MS(J, 2) AND MS(I, 3) = MS(J, 3) AND MS(I, 4) = MS(J, 4) AND MS(I, 5) = MS(J, 5)) THEN MS(J, 1) = 0
  NEXT J
NEXT I
C2 = 0
FOR I = 1 TO CT
  FOR J = 1 TO 5
    IF MS(I, 1) <> 0 THEN PRINT MS(I, J);
  NEXT J
  IF MS(I, 1) <> 0 THEN PRINT : C2 = C2 + 1
NEXT I
PRINT : PRINT C2
Find all posts by this user
Quote this message in a reply
09-28-2015, 01:42 AM
Post: #28
RE: HHC 2015 RPN programming Contest is now open
Updated version to comply with rule 7.

Bytes: 42
Lines: 27

Code:

  1 LBL RR                  ; 11 SigmaREG required?
  2 CL Sigma                ; count numbers in sequence
  3 6                       ; search for 6, 5, 4, 3, 2, 1 loop
  4 STO 07
  5 LBL 07
  6     5                   ; search each dice
  7     STO 06
  8     LBL 06
  9         RCL 07          ; outer loop
 10         RCL IND 06      ; inner loop
 11         x=y?            ; is there a match?
 12         GTO 09          ; if so, break out of here
 13     DSE 06
 14     GTO 06
 15     CL Sigma            ; no match, reset sequence counter
 16     LBL 09
 17     x=y?                ; got match
 18     Sigma+              ; inc sequence counter
 19     RCL 16              ; recall n
 20     4
 21     -
 22     x=0?                ; got 4 in a row?
 23     GTO 00              ; goto end with X = 0
 24 DSE 07
 25 GTO 07
 26 LBL 00
 27 END                     ; X = non-zero
Find all posts by this user
Quote this message in a reply
09-28-2015, 01:52 AM (This post was last modified: 09-28-2015 02:02 AM by David Hayden.)
Post: #29
RE: HHC 2015 RPN programming Contest is now open
[ Edit: OOPS! Nevermind. I completely discounted a die that rolls 6. In fact the whole thing is wrong]

18 steps and 35 bytes, if I'm counting the bytes right. No registers, no sorting.

Code:

01 LBL RR    # 6 bytes
02 5    # 1 byte

# Set the flag for the register in X
03 LBL 01     # 1 byte
04 RCL IND X  # 2 bytes
05 SF IND X # 2 bytes?
06 X<>Y     # 1 byte
07 DSE X    # 2 bytesG
08 GTO 01    # 2 bytes
# Note that X=0 when you exit this loop

# Now compute the result in flag 01, clear the flags along the way
09 FS?C 05    # 2 bytes
10 SF 01    # 2 bytes   F1 = F1 or F5
11 FC?C 02    # 2 bytes
12 CF 01    # 2 bytes   F1 = (F1 or F5) and F2
13 FC?C 03 # 2 bytes
14 CF 01    # 2 bytes   F1 = (F1 or F5) and F2 and F3
15 FC?C 04 # 2 bytes
16 CF 01    # 2 bytes   F1 = (F1 or F5) and F2 and F3 and F4
   
# X is zero as mentioned above. If F1 is clear, then set it to 1.
17 FC?C 01    # 1 bytes
18 1    # 1 byte
Find all posts by this user
Quote this message in a reply
09-28-2015, 02:12 AM
Post: #30
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 01:52 AM)David Hayden Wrote:  [ Edit: OOPS! Nevermind. I completely discounted a die that rolls 6. In fact the whole thing is wrong]

18 steps and 35 bytes, if I'm counting the bytes right. No registers, no sorting.

I started with a similar attempt, I think you're missing CF 1 - CF 6 to initialize at start?

Too bad we could not use the CX, e.g. X<>F would have been very handy. My first thought on solving this was to or the bits, then and with 3 different masks and check for 15, 30, or 60.

That's mostly what my 64 byte attempt was but with base-10.
Find all posts by this user
Quote this message in a reply
09-28-2015, 02:21 AM
Post: #31
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 01:52 AM)David Hayden Wrote:  
Code:

01 LBL RR    # 6 bytes
02 5    # 1 byte

# Set the flag for the register in X
03 LBL 01     # 1 byte
04 RCL IND X  # 2 bytes
05 SF IND X # 2 bytes?
06 X<>Y     # 1 byte
07 DSE X    # 2 bytesG
08 GTO 01    # 2 bytes
# Note that X=0 when you exit this loop

Dave,

Is this loop assuming the flags are initialized to clear? What happens if they aren't? Or am I missing something here.

Thanks,

Bill
Smithville, NJ
Find all posts by this user
Quote this message in a reply
09-28-2015, 02:22 AM
Post: #32
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 02:12 AM)Egan Ford Wrote:  Too bad we could not use the CX, e.g. X<>F would have been very handy. My first thought on solving this was to or the bits, then and with 3 different masks and check for 15, 30, or 60.

That's mostly what my 64 byte attempt was but with base-10.

I agree! Even with an AND function, one can check for one of the following numbers (binary) [63,62,61,60,47,31,30,15] with out much trouble. x AND x/2 AND x/4 AND x/8.
If the result is >0 then x was one of those numbers.

17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b

Find all posts by this user
Quote this message in a reply
09-28-2015, 02:27 AM (This post was last modified: 09-28-2015 12:41 PM by Bill (Smithville NJ).)
Post: #33
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 02:12 AM)Egan Ford Wrote:  My first thought on solving this was to or the bits, then and with 3 different masks and check for 15, 30, or 60.

That's mostly what my 64 byte attempt was but with base-10.

Egan,

That is similar to what I did. It would be interesting to program this on the HP-16 or even the HP-42S with bit logic. I ended up with a compare with 1111, 11110 and 111100, and used the flags 1-6 to simulate binary. Not the shortest program, but still interesting to program.

Fascinating to see the various methods.

Bill
Smithville, NJ
Find all posts by this user
Quote this message in a reply
09-28-2015, 02:32 AM
Post: #34
RE: HHC 2015 RPN programming Contest is now open
As a side note, I considered using SCI/FIX/ENG commands because you can set up to 6 flags at the same time and read them out independently. One of the rules made this impractical.

17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b

Find all posts by this user
Quote this message in a reply
09-28-2015, 07:25 AM
Post: #35
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 12:53 AM)Egan Ford Wrote:  [code]
17 x=y? ; got match
18 Sigma+ ; inc sequence counter
19 RCL 16 ; recall n

Smile Nested loops and stats - good trick Wink
I need to understand the lines above, btw this is similar to my solution - but far compacted and well engineered.
I need to learn how indirect addressing with ISG/DSE works on 41xx (start/end values for a loop how set on your program) or/and maybe the stat registers used by your loops also...?! I will read the manual first.

Congratulations!

Csaba
Find all posts by this user
Quote this message in a reply
09-28-2015, 11:00 AM
Post: #36
RE: HHC 2015 RPN programming Contest is now open
Egan, Congratulations. Yours is smaller than the other programs!
Thank you for such a compact demonstration of the different registers!

17bii | 32s | 32sii | 41c | 41cv | 41cx | 42s | 48g | 48g+ | 48gx | 50g | 30b

Find all posts by this user
Quote this message in a reply
09-28-2015, 01:27 PM
Post: #37
RE: HHC 2015 RPN programming Contest is now open
Question to Gene and others:

I finally read the complete set of Rules. I know, I should have read them BEFORE I did a program, but who reads rules, manuals, etc.

Rule No. 13 says to "Assume default machine settings" and "program must stop with the default settings in place".

What are considered the "Default machine settings"?

Thanks,

Bill
Smithville, NJ
Find all posts by this user
Quote this message in a reply
09-28-2015, 02:44 PM
Post: #38
RE: HHC 2015 RPN programming Contest is now open
44 bytes including label and END, pure stack solution. I didn't look at any of the others (so far), honest ;-)

Cheers, Werner

41CV†,42S,48GX,49G,DM42,DM41X,17BII,15CE,DM15L,12C,16CE
Find all posts by this user
Quote this message in a reply
09-28-2015, 03:30 PM (This post was last modified: 10-02-2015 12:08 PM by Gerson W. Barbosa.)
Post: #39
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 01:27 PM)Bill (Smithville NJ) Wrote:  I finally read the complete set of Rules. I know, I should have read them BEFORE I did a program, but who reads rules, manuals, etc.

I ought to, but sometimes I don't pay much attention to rules and regulations. Just a glance at the rules and as a result I skipped rule #7 (well, that shouldn't be easy to fix, but I won't). Also, until minutes before I posted my program was labeled simply "S".

(09-28-2015 01:27 PM)Bill (Smithville NJ) Wrote:  Rule No. 13 says to "Assume default machine settings" and "program must stop with the default settings in place".

What are considered the "Default machine settings"?

Aren't these the ones when the calculator is first powered up, or after a reset? "Assume default machine settings" followed by "any default setting that might have been changed during program execution should be restored at program completion", or something like that. This would prevent an obedient programmer from including a line "FIX 4" (or equilavent flag settings), for instance, even if no display mode changing instruction had been used.

Gerson.

Edited to remove somewhat irrelevant part.
Find all posts by this user
Quote this message in a reply
09-28-2015, 04:22 PM
Post: #40
RE: HHC 2015 RPN programming Contest is now open
(09-28-2015 03:30 PM)Gerson W. Barbosa Wrote:  Aren't these the ones when the calculator is first powered up, or after a reset? "Assume default machine settings" followed by "any default setting that might have been changed during program execution should be restored at program completion", or something like that. This would prevent an obedient programmer from including a line "FIX 4" (or equilavent flag settings), for instance, even if no display mode changing instruction had been used.

Then that would mean that there's no need to clear the flags at the start of the program, but would need to clear them when finished to get them back to Default Machine settings.

I'm not sure I'm a fan of this rule. I have would preferred it that there's no assumption made on the calculator initial settings. Other than registers 1-5 containing the test data.

Thanks,

Bill
WD9EQD
Find all posts by this user
Quote this message in a reply
Post Reply 




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