aoc2021/3/a.py
2021-12-02 21:29:11 -08:00

33 lines
638 B
Python

with open("input.txt") as f:
lines = [i.strip() for i in f.readlines()]
def count_digits(ar):
zeros = [0] * len(ar[0])
ones = [0] * len(ar[0])
for row in ar:
for pos, c in enumerate(row.strip()):
(zeros if c == "0" else ones)[pos] += 1
return zeros, ones
zeros, ones = count_digits(lines)
print(zeros)
print(ones)
# lol
gamma_bits = []
epsi_bits = []
for pos, zero in enumerate(zeros):
gamma_bits.append("1" if zero < ones[pos] else "0")
epsi_bits.append("1" if zero > ones[pos] else "0")
gamma = int("".join(gamma_bits), 2)
epsi = int("".join(epsi_bits), 2)
print(gamma * epsi)