#x86-16 Assembly, 56 41 39 bytes
Binary:
00000000: b103 33ed ac8b fe51 f2ae 7503 45eb f983 ..3....Q..u.E...
00000010: fd03 7504 80c2 4d43 03dd 59e2 e592 7502 ..u...MC..Y...u.
00000020: 040a 3c64 7201 43 ..<dr.C
Unassembled:
B1 03 MOV CL, 3 ; set up loop counter for 3 digits
DIGIT_LOOP:
33 ED XOR BP, BP ; clear digit match counter
AC LODSB ; next digit char into AL
8B FE MOV DI, SI ; start search at next char
51 PUSH CX ; save outer digit loop counter
MATCH_LOOP:
F2/ AE REPNZ SCASB ; search until digit in AL is found
75 03 JNZ CHECK_FOUR ; is end of search?
45 INC BP ; if not, a match was found, increment count
EB F9 JMP MATCH_LOOP ; continue looping
CHECK_FOUR:
83 FD 03 CMP BP, 3 ; is four of a kind?
75 04 JNE SCORE_DIGIT ; if not, add number of matches to 1UP's
80 C2 4D ADD DL, 77 ; add 77 to coin count
43 INC BX ; +1 1UP extra for four-of-a-kind
SCORE_DIGIT:
03 DD ADD BX, BP ; Add number of matches to total, set ZF if 0
59 POP CX ; restore outer digit loop position
E2 E5 LOOP DIGIT_LOOP ; keep looping
92 XCHG DX, AX ; coin count to AX for shorter compare
75 02 JNZ FINAL_SCORE ; if 1UPs are 0, calculate score
04 0A ADD AL, 10 ; consolation prize of 10
FINAL_SCORE:
3C 64 CMP AL, 100 ; is coin score over 100?
72 01 JB DONE ; if not, no extra 1UP
43 INC BX ; otherwise, increment 1UP
DONE:
Input starting coin count in DX
, SI
pointing to start of "icon" bytes (which can be '1'
-'5'
, or any byte value). Output the number of 1UP's in BX
.
Explanation:
The input of four bytes is iterated and compared to the remaining bytes to the right, counting the number of matches. The scores for each type of match are awarded and add up to the total. Since a four-of-a-kind is also three-of-a-kind and also a one-pair, the value of each score type can be decomposed as follows:
- 3 matches = 4 1UP's + 77 coins
- 2 matches = 2 1UP's
- 1 match = 1 1UP
Example: [2, 2, 2, 2]
(four-of-a-kind) = 7 1UP's + 77 coins
2 [2, 2, 2] = 3 matches = 4 1UP's + 77 coins
2 [2, 2] = 2 matches = 2 1UP's
2 [2] = 1 match = 1 1UP
Example: [2, 5, 2, 2]
(three-of-a-kind) = 3 1UP's
2 [5, 2, 2] = 2 matches = 2 1UP's
5 [2, 2] = 0 matches
2 [2] = 1 match = 1 1UP
Example: [4, 5, 5, 4]
(two pair) = 2 1UP's
4 [5, 5, 4] = 1 match = 1 1UP
5 [5, 4] = 1 match = 1 1UP
5 [4] = 0 matches
Example: [2, 3, 4, 3]
(one pair) = 1 1UP
2 [3, 4, 3] = 0 matches
3 [4, 3] = 1 match = 1 1UP
4 [3] = 0 matches
If number of earned 1UP's is 0 at the end, 10 coins are awarded. If total coins are greater than 100, an additional 1UP is awarded.
Here is a test program for PC DOS that includes extra routines to handle the integer value I/O:
Download and test LUCKY.COM for DOS.