66 lines
1.4 KiB
Python
Executable file
66 lines
1.4 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
|
|
# Inspired by descriptive part of oldest answer in
|
|
# https://stackoverflow.com/questions/23308954
|
|
|
|
def try_flips(Outlets, Devices, nDev, l):
|
|
|
|
# Try all the devices in all the outlets
|
|
|
|
# Find the flip configurations in bitwise form
|
|
# that are compatible (present) in all the outlets
|
|
|
|
S = set()
|
|
for j in range(len(Devices)):
|
|
S.add(Outlets[0] ^ Devices[j])
|
|
|
|
for i in range(1, len(Outlets)):
|
|
row = set()
|
|
for j in range(len(Devices)):
|
|
row.add(Outlets[i] ^ Devices[j])
|
|
S = S.intersection(row)
|
|
|
|
# If no common configuration is found, then no flip can
|
|
# solve this case
|
|
|
|
if len(S) == 0:
|
|
return "NOT POSSIBLE"
|
|
|
|
# Otherwise, pick the one with the smallest number of 1s
|
|
# (which represent flips) and return the number of flips
|
|
|
|
m = None
|
|
for s in S:
|
|
b = count_bits(s)
|
|
if m is None or b < m:
|
|
m = b
|
|
return str(m)
|
|
|
|
|
|
def count_bits(x):
|
|
i = 0
|
|
while x != 0:
|
|
i = i + (x & 1)
|
|
x = x >> 1
|
|
return i
|
|
|
|
|
|
def read_switches():
|
|
O = input().split()
|
|
for i in range(len(O)):
|
|
O[i] = int(O[i], 2)
|
|
return O
|
|
|
|
|
|
if __name__ == "__main__":
|
|
t = int(input())
|
|
|
|
for i in range(t):
|
|
line = input().split()
|
|
n, l = int(line[0]), int(line[1])
|
|
|
|
Outlets = read_switches()
|
|
Devices = read_switches()
|
|
|
|
print("Case #" + str(i + 1) + ": " + try_flips(Outlets, Devices, n, l))
|
|
|