diff --git a/bit_difference.py b/bit_difference.py old mode 100644 new mode 100755 diff --git a/charging_chaos.py b/charging_chaos.py new file mode 100755 index 0000000..b248d54 --- /dev/null +++ b/charging_chaos.py @@ -0,0 +1,66 @@ +#!/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)) + diff --git a/charging_chaos.txt b/charging_chaos.txt new file mode 100644 index 0000000..e25f941 --- /dev/null +++ b/charging_chaos.txt @@ -0,0 +1,10 @@ +3 +3 2 +01 11 10 +11 00 10 +2 3 +101 111 +010 001 +2 2 +01 10 +10 01 diff --git a/modular_exponentiation.py b/modular_exponentiation.py old mode 100644 new mode 100755