-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay03.py
31 lines (24 loc) · 880 Bytes
/
Day03.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 sys import argv, stdin
from collections import Counter
ns = [[int(c) for c in s.strip()] for s in stdin.readlines()]
cols = len(ns[0])
bit_counts = lambda nums: [Counter(col) for col in zip(*nums)]
most_common = lambda bitc: [1 if col[1] >= col[0] else 0 for col in bitc]
least_common = lambda mostc: [1 - x for x in mostc]
as_dec = lambda l: int("".join(map(str, l)), 2)
if argv[1] == "1":
t = most_common(bit_counts(ns))
gamma = as_dec(t)
epsilon = as_dec(least_common(t))
print(gamma * epsilon)
else:
# Part two
def go(init, post):
for i in range(cols):
if len(init) > 1:
t = post(most_common(bit_counts(init)))
init = list(filter(lambda x: x[i] == t[i], init))
return init[0]
oxygen = as_dec(go(ns, lambda x: x))
co2 = as_dec(go(ns, least_common))
print(oxygen * co2)