-
Notifications
You must be signed in to change notification settings - Fork 0
/
d3p1.py
32 lines (27 loc) · 868 Bytes
/
d3p1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from collections import Counter
from functools import reduce
if __name__ == '__main__':
count = 0
with open('d3_input.txt', 'r') as f:
counts = Counter()
for row in f:
count += 1
val = int(row.strip(), 2)
idx = 0
mask = 1
while mask <= val:
if mask & val:
counts[idx] += 1
mask <<= 1
idx += 1
gamma, epsilon = [0] * len(counts), [0] * len(counts)
for idx, val in counts.items():
if 2 * val > count:
gamma[idx] = 1
elif 2 * val < count:
epsilon[idx] = 1
else:
raise Exception("DAFOX")
gamma = reduce(lambda x, y: 2 * x + y, gamma[::-1])
epsilon = reduce(lambda x, y: 2 * x + y, epsilon[::-1])
print(gamma, epsilon, gamma * epsilon)