You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.8 KiB
Python

2 years ago
#!/usr/bin/env python3
def is_bit_set(integer, bit_index):
return bool(integer & (1 << bit_index))
def get_mcv(_input):
input_length = len(_input)
popcounts = [0 for bit in range(len(_input[0].strip()))]
#print(popcounts)
for line in _input:
popcounts = list(map(int.__add__, popcounts, [int(bit) for bit in line.strip()]))
mcv = []
for val in popcounts:
if val >= (input_length/2):
mcv.append(1)
else:
mcv.append(0)
return mcv
def main():
content = open('input', 'r').readlines()
#mcv = get_mcv(content)
# mask = (1 << len(popcounts)) - 1
# gamma_rate = int(gamma_rate, 2)
# epsilon_rate = gamma_rate ^ mask
# print(f'gamma: {bin(gamma_rate)}')
# print(f'epsil: {bin(epsilon_rate)}')
#print(f'mcv: {mcv}')
bit_length = len(content[0].strip())
oxy_input = content
while len(oxy_input) > 1:
offset = 0
while offset < bit_length:
mcv = get_mcv(oxy_input)
print(mcv)
print(f'length {len(oxy_input)} offset {offset} mcv {mcv[offset]} ')
oxy_input = [x for x in oxy_input if int(x[offset]) == mcv[offset]]
if len(oxy_input) == 1:
break
offset += 1
oxygen_value = int(oxy_input[0].strip(), 2)
co_input = content
while len(co_input) > 1:
offset = 0
while offset < bit_length:
print(f'length {len(co_input)} offset {offset} mcv {mcv[offset]} ')
mcv = get_mcv(co_input)
print(mcv)
co_input = [x for x in co_input if int(x[offset]) != mcv[offset]]
if len(co_input) == 1:
break
offset += 1
co_value = int(co_input[0].strip(), 2)
print(co_value * oxygen_value)
if __name__ == "__main__":
main()