Files
advent-of-code/2021/03/part1
2022-01-09 19:41:47 +00:00

29 lines
621 B
Python
Executable File

#!/usr/bin/env python
gamma = 0
epsilon = 0
with open('input') as f:
total = 0
g_counts = [0,0,0,0,0,0,0,0,0,0,0,0]
e_counts = [0,0,0,0,0,0,0,0,0,0,0,0]
for line in f:
total += 1
for idx, chr in enumerate(line[0:12]):
if chr == '1':
g_counts[idx]+=1
else:
e_counts[idx]+=1
target = total / 2
for idx, val in enumerate(g_counts):
if val > target:
gamma += (1 << (11-idx))
for idx, val in enumerate(e_counts):
if val > target:
epsilon += (1 << (11-idx))
print(gamma * epsilon)