adventofcode/2021/3/1.py
2022-12-01 21:13:55 +01:00

29 lines
762 B
Python
Executable File

#!/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()