One person comes up to another person beside his bike. "Can I use your bike?" he asks. The person by the bike replies, "Only if you figure out the combination to my bike lock, which is made up of 4 different numbers from 1 through 8. You can guess 3 numbers."
He guessed 1235, 4721, and 3862. All three were answered with "One number in the combination is in the wrong place, and another is in the right place. The other two aren't in the combination."
The guesser was puzzled and asked "Is the number divisible by 7?" The person with the bike answered this question and after thinking for a while, the guesser told him the combination. What is the combination?
(In reply to
Solution  the original problem is fine as it is! by SilverKnight)
DECLARE SUB check (str1$, str2$, bl!, wh!)
PRINT
FOR n = 1234 TO 8765
n$ = LTRIM$(STR$(n))
check n$, "1235", black, white
IF black = 1 AND white = 1 THEN
check n$, "4721", black, white
IF black = 1 AND white = 1 THEN
check n$, "3862", black, white
IF black = 1 AND white = 1 THEN
good = 1
FOR i = 1 TO LEN(n$)
IF MID$(n$, i, 1) < \\"1\\" OR MID$(n$, i, 1) > "8" THEN
good = 0: EXIT FOR
END IF
IF INSTR(MID$(n$, i + 1), MID$(n$, i, 1)) THEN
good = 0: EXIT FOR
END IF
NEXT
IF good THEN
PRINT n$, n MOD 7
END IF
END IF
END IF
END IF
NEXT
SUB check (str1$, str2$, bl, wh)
s1$ = str1$: s2$ = str2$
bl = 0: wh = 0
FOR i = 1 TO LEN(s1$)
IF MID$(s1$, i, 1) = MID$(s2$, i, 1) THEN
bl = bl + 1
END IF
NEXT
FOR i = 1 TO LEN(s1$)
ix = INSTR(s2$, MID$(s1$, i, 1))
IF ix > 0 THEN
wh = wh + 1
s2$ = LEFT$(s2$, ix  1) + MID$(s2$, ix + 1)
END IF
NEXT
wh = wh  bl
END SUB
with results:
1763 6
2765 0
7825 6
7831 5
with 2765 uniquely divisible by 7.
(The terminology black and white comes from MasterMind, which uses black and white pegs to indicate such matches.)
Edited on November 18, 2003, 9:42 am

Posted by Charlie
on 20031118 09:40:19 