Day 03 (glamorized)
This commit is contained in:
@@ -1,13 +1,10 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
lines = (x.strip() for x in open("input.txt"))
|
lines = [x.strip() for x in open("input.txt")]
|
||||||
|
|
||||||
cnt = [0] * 12
|
most_common = (Counter(column).most_common(1)[0][0] for column in zip(*lines))
|
||||||
|
gamma = int("".join(most_common), 2)
|
||||||
for line in lines:
|
epsilon = 2**12-1 ^ gamma
|
||||||
cnt = [x+(int(char)*2-1) for x, char in zip(cnt, line)]
|
|
||||||
|
|
||||||
gamma = int(''.join(['1' if x > 0 else '0' for x in cnt]), 2)
|
|
||||||
epsilon = int(''.join(['1' if x < 0 else '0' for x in cnt]), 2)
|
|
||||||
|
|
||||||
print(gamma * epsilon)
|
print(gamma * epsilon)
|
||||||
@@ -1,20 +1,18 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
from collections import Counter
|
||||||
|
|
||||||
|
|
||||||
def find(is_lowest):
|
def find(is_lowest):
|
||||||
lines = (x.strip() for x in open("input.txt"))
|
lines = [x.strip() for x in open("input.txt")]
|
||||||
for bit in range(12):
|
for bit in range(12):
|
||||||
zeros = []
|
common = (max if is_lowest else min)(
|
||||||
ones = []
|
map(lambda x: x[::-1], Counter(s[bit] for s in lines).items())
|
||||||
for line in lines:
|
)[1]
|
||||||
zeros.append(line) if line[bit] == '0' else ones.append(line)
|
lines = [s for s in lines if s[bit] == common]
|
||||||
lines = zeros if (len(zeros) > len(ones)) != is_lowest else ones
|
return lines
|
||||||
if len(lines) <= 1:
|
|
||||||
break
|
|
||||||
return lines[0]
|
|
||||||
|
|
||||||
|
|
||||||
gamma = int(''.join(find(True)), 2)
|
gamma = int(''.join(find(False)), 2)
|
||||||
epsilon = int(''.join(find(False)), 2)
|
epsilon = int(''.join(find(True)), 2)
|
||||||
|
|
||||||
print(gamma * epsilon)
|
print(gamma * epsilon)
|
||||||
|
|||||||
Reference in New Issue
Block a user