Basic colour box and colour well interaction implemented.

This commit is contained in:
Niels Serup
2012-08-08 21:45:05 +02:00
parent a842ad961b
commit b4f53e17f2
2 changed files with 89 additions and 19 deletions

View File

@@ -72,20 +72,20 @@ def generate_random_box(nwells):
r = lambda: random.choice((0, 1))
return [(r(), r(), r()) for _ in range(nwells)]
def get_colours(boxes):
colours = []
for i in range(len(boxes[0])):
r, g, b = boxes[0][i]
for j in range(1, len(boxes)):
r1, g1, b1 = boxes[j][i]
r ^= r1
g ^= g1
b ^= b1
colours.append((r, g, b))
return colours
def makes_all_wells_white(boxes):
"""
Determine if the boxes make all wells white when XOR'ed together.
"""
total = 0
for box in boxes:
n = 0
for r, g, b in box:
n <<= 1
n |= r
n <<= 1
n |= g
n <<= 1
n |= b
total ^= n
return total == 2**(len(boxes[0]) * 3) - 1
return all(c == 1 for c in itertools.chain(*get_colours(boxes)))