#x86-16 Assembly, IBM PC DOS, 56 41 bytes
Binary:
00000000: b103 33ed ac8b fe51 f2ae 7503 45eb f995 ..3....Q..u.E...
00000010: 3c03 7504 80c2 4d43 03d8 59e2 e592 85db <.u...MC..Y.....
00000020: 7502 040a 3c64 7201 43 u...<dr.C
Unassembled:
B1 03 MOV CL, 3 ; set up loop counter for 4 digits
DIGIT_LOOP:
33 ED XOR BP, BP ; clear digit counter
AC LODSB ; next digit char into AL
8B FE MOV DI, SI ; start search at next char
51 PUSH CX ; save outer loop counter
MATCH_LOOP:
F2/ AE REPNZ SCASB ; search until digit AL is found
75 03 JNZ CHECK_FOUR ; end of search string found
45 INC BP ; otherwise a match was found, increment count
EB F9 JMP MATCH_LOOP ; continue looping
CHECK_FOUR:
95 XCHG BP, AX ; match results to AX for shorter compare
3C 03 CMP AL, 3 ; is four of a kind?
75 04 JNE SCORE_DIGIT ; if not, add number of new 1UPs
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 D8 ADD BX, AX ; Add number of matches to total
59 POP CX ; restore outer loop position
E2 E5 LOOP DIGIT_LOOP ; keep looping
92 XCHG DX, AX ; coin count to AX for shorter compare
85 DB TEST BX, BX ; was ending score 0?
75 02 JNZ FINAL_SCORE ; if not, go to 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 array of "icons" (which can be '1'
-'5'
, or any byte value). Output number of lives in BX
.
Explanation:
The input of four bytes are iterated and for each digit, the remaining digits to the right are compared and the number of matches counted. The scores for each type of match are awarded and add up to the total according to the following:
- 3 matches (Four-of-a-kind) = 4 1UPs + 77 coins
- 2 matches (Three-of-a-kind) = 2 1UPs
- 1 match (One pair) = 1 1UP
Example: [2, 2, 2, 2]
(four-of-a-kind) - 7 1UPs + 77 coins
2 [2, 2, 2] == 3 matches (4 1UPs + 77 coins)
2 [2, 2] == 2 matches (2 1UPs)
2 [2] == 1 match (1 1UP)
Example: [2, 5, 2, 2]
(three-of-a-kind) - 3 1UPs
2 [5, 2, 2] == 2 matches (2 1UPs)
5 [2, 2] == 0 matches
2 [2] == 1 match (1 1UP)
Example: [4, 5, 5, 4]
(two pair) - 2 1UPs
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 1UPs is 0 at the end, 10 coins are awarded, and if coins are over 100, an additional 1UP is added.
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.