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.

29 lines
762 B
Python

2 years ago
#!/usr/bin/env python3
def main():
content = open('input', 'r').readlines()
input_length = len(content)
# init with first row
popcounts = [int(bit) for bit in content.pop(0).strip()]
#print(popcounts)
for line in content:
popcounts = list(map(int.__add__, popcounts, [int(bit) for bit in line.strip()]))
gamma_rate = ""
for val in popcounts:
if val >= (input_length/2):
gamma_rate += "1"
else:
gamma_rate += "0"
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(gamma_rate * epsilon_rate)
if __name__ == "__main__":
main()