Day 03 (glamorized)
This commit is contained in:
@@ -1,20 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
from collections import Counter
|
||||
|
||||
|
||||
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):
|
||||
zeros = []
|
||||
ones = []
|
||||
for line in lines:
|
||||
zeros.append(line) if line[bit] == '0' else ones.append(line)
|
||||
lines = zeros if (len(zeros) > len(ones)) != is_lowest else ones
|
||||
if len(lines) <= 1:
|
||||
break
|
||||
return lines[0]
|
||||
common = (max if is_lowest else min)(
|
||||
map(lambda x: x[::-1], Counter(s[bit] for s in lines).items())
|
||||
)[1]
|
||||
lines = [s for s in lines if s[bit] == common]
|
||||
return lines
|
||||
|
||||
|
||||
gamma = int(''.join(find(True)), 2)
|
||||
epsilon = int(''.join(find(False)), 2)
|
||||
gamma = int(''.join(find(False)), 2)
|
||||
epsilon = int(''.join(find(True)), 2)
|
||||
|
||||
print(gamma * epsilon)
|
||||
|
||||
Reference in New Issue
Block a user