There are 4 persons A,B,C and D of which 2 are liars and 2 are knights. Each of these persons has a lucky and an unlucky number. All the 8 numbers are different and they are from 1 to 9 only. It is known that sum of lucky and unlucky numbers is same for all of these 4 persons and also sum of lucky numbers is greater than sum of unlucky numbers. Find the lucky and unlucky numbers for each of them if they made the following statements:
A:
C's lucky number is 7.
The missing number is 5.
B:
C's unlucky number is 4.
D's lucky number is 2.
C:
A is a liar.
B's lucky number is 6.
D:
The product of B's numbers is 24.
The maximum of all our numbers is A's lucky number.
Note: The missing number is the number from 1 to 9 which is not any one of these people's lucky or unlucky number.
The only possible unused (missing) numbers are 1, 5 and 9, as any other missing number would upset the pairing needed to make each person's sum the same.
In the program below, only those missing numbers are tried. Then the lowest is paired with the highest, and the next lowest with the next highest, etc.
In types$, knights are represented as 1 and liars as 0. Within the logic of the program, this is multiplied by 1, as Basic's representation of true is 1 and false is 0, so we can compare against the truth value of the statements as Basic evaluates them.
The code for the permute subroutine is found elsewhere on this site and each time it's called it returns the next permutation of the argument passed to it in lexicographic order, but wrapping around to the original.
DECLARE SUB permute (a$)
CLS
types$ = "1100"
pair$ = "1234"
ht$ = types$
DO
hp$ = pair$
DO
FOR miss = 1 TO 9 STEP 4
st = 1: fin = 9
IF miss = 1 THEN st = 2
IF miss = 9 THEN fin = 8
FOR i = 1 TO 4
low(i) = st
st = st + 1
high(i) = fin
fin = fin  1
NEXT
FOR aLhigh = 0 TO 1
FOR bLhigh = 0 TO 1
FOR cLhigh = 0 TO 1
FOR dLhigh = 0 TO 1
apair = VAL(MID$(pair$, 1, 1))
aluck = aLhigh * high(apair) + (1  aLhigh) * low(apair)
aunluck = aLhigh * low(apair) + (1  aLhigh) * high(apair)
bpair = VAL(MID$(pair$, 2, 1))
bluck = bLhigh * high(bpair) + (1  bLhigh) * low(bpair)
bunluck = bLhigh * low(bpair) + (1  bLhigh) * high(bpair)
cpair = VAL(MID$(pair$, 3, 1))
cluck = cLhigh * high(cpair) + (1  cLhigh) * low(cpair)
cunluck = cLhigh * low(cpair) + (1  cLhigh) * high(cpair)
dpair = VAL(MID$(pair$, 4, 1))
dluck = dLhigh * high(dpair) + (1  dLhigh) * low(dpair)
dunluck = dLhigh * low(dpair) + (1  dLhigh) * high(dpair)
atrue = VAL(MID$(types$, 1, 1))
btrue = VAL(MID$(types$, 2, 1))
ctrue = VAL(MID$(types$, 3, 1))
dtrue = VAL(MID$(types$, 4, 1))
IF atrue = (cluck = 7) AND atrue = (miss = 5) THEN
IF btrue = (cunluck = 4) AND btrue = (dluck = 2) THEN
IF ctrue = (atrue = 0) AND ctrue = (bluck = 6) THEN
IF dtrue = (bluck * bunluck = 24) AND dtrue = (aluck = 9 OR aluck = 8 AND miss = 9) THEN
IF aluck + bluck + cluck + dluck > aunluck + bunluck + cunluck + dunluck THEN
PRINT types$, miss
PRINT aluck; aunluck, bluck; bunluck, cluck; cunluck, dluck; dunluck
END IF
END IF
END IF
END IF
END IF
NEXT
NEXT
NEXT
NEXT
NEXT miss
permute pair$
LOOP UNTIL hp$ = pair$
permute types$
LOOP UNTIL ht$ = types$
produces
1001 5
9 1 4 6 7 3 8 2
The 1001 indicates that A and D are the knights, while B and C are the liars.
The 5 of course indicates that 5 is unused.
The lucky and unlucky numbers are:
lucky unlucky
A: 9 1
B: 4 6
C: 7 3
D: 8 2

Posted by Charlie
on 20090202 10:26:32 