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.
31 lines
595 B
Python
31 lines
595 B
Python
file = open("in.txt", "r")
|
|
lns = [f.rstrip() for f in file.readlines()]
|
|
|
|
def p1() -> int:
|
|
zero = [0] * 12
|
|
one = [0] * 12
|
|
|
|
for ln in lns:
|
|
for i, ch in enumerate(list(ln)):
|
|
if ch == "0":
|
|
zero[i] += 1
|
|
else:
|
|
one[i] += 1
|
|
|
|
gamma = "0b"
|
|
epsilon = "0b"
|
|
|
|
for i, _ in enumerate(zero):
|
|
if zero[i] > one[i]:
|
|
gamma += "0"
|
|
epsilon += "1"
|
|
else:
|
|
gamma += "1"
|
|
epsilon += "0"
|
|
|
|
return (int(gamma, 2) * int(epsilon, 2))
|
|
|
|
# def p2():
|
|
|
|
print(f"part 1: {p1()}")
|