a problem to solve

mensanator at aol.com mensanator at aol.com
Fri Mar 24 16:26:19 EST 2006


John Salerno wrote:
> mensanator at aol.com wrote:
>
> > Then you'll want to represent the lights as a 20-bit binary number.
> >
> > Each bit position corresponds to 4 lamps
>
> I'm not sure I understand that. If I use a 20-bit number, wouldn't each
> bit correspond to a single light on each switch? What do you mean that
> each bit is 4 lamps?

You have 4 panels, each with 20 lamps (label them 19 to 0):

panel A 00000000000000000000
panel B 00000000000000000000
panel C 00000000000000000000
panel D 00000000000000000000
        ^                  ^
        |                  |
        lamp 19            lamp 0


There are 4 lamps labeled 19, 4 labeled 18, etc.

The boolean equation I gave you is the solution to a single
lamp position.

If a 0 represents OFF and a 1 represents ON, then this is
equivalent to 4 20-bit binary numbers and you can use
bitwise operators to solve all 20 lamp positions simultaneously.
For each bit position, the boolean equation returns a 1 in the
repective bit if there are exactly 3 1's in that lamp position of
the 4 panels.

The first switch in each panel lights the lamps:

A 11110101111111011100
B 11011101101101111101
C 11110011011110111101
D 01110111111011011011

You can, by hand, figure out which bit positions have exactly
3 1's vertically.

A 11110101111111011100
B 11011101101101111101
C 11110011011110111101
D 01110111111011011011
----------------------
Y 10100100110111000101

So, as you already know, setting switch 1 on each panel is
not a solution, we need a 1 in each bit position. You can
determine this from the decimal representation. A 20-bit binary
number of all 1's would be 2**20 - 1, so if Y = 1048575,
then Y is a solution.

In my example, I used hex to enter the sample values just
because it was easier:

Group the 20 bits into five groups of 4, and convert each
group to a hex character

A 1111 0101 1111 1101 1100
  f    5    f    d    c

Repeating the example (correcting the typo in the first post)

>>> A1 = 0xf5fdc
>>> B1 = 0xddb7d
>>> C1 = 0xf37bd
>>> D1 = 0x77edb
>>> Y = ((C1 & D1) & (A1 ^ B1)) | ((A1 & B1) & (C1 ^ D1))
>>> Y
675269

We can see this is not a solution. Converting Y to binary
allows us to see which lamp positions have three lit and
confirm that the boolean equation matches our hand
calculation.

>>> print gmpy.digits(Y,2)
10100100110111000101

By the way, I have a program that solves the problem.
You said you wanted to do it yourself, so if you need more
help, I can walk you through how to solve it.




More information about the Python-list mailing list