Created
December 4, 2021 20:13
-
-
Save Solaxun/347fd14c31d0dcc7cbea0f304eda9987 to your computer and use it in GitHub Desktop.
AoC 2021: Python Day 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
data = open('day3.txt').read().splitlines() | |
def common_bits(bits,most=True): | |
data = list(zip(*bits)) | |
mc = [Counter(d).most_common() for d in data] | |
mc = [d[0] if most else d[-1] for d in mc] | |
mc = [d[0] for d in mc] | |
return int("".join(mc),2) | |
most_common = common_bits(data,most=True) | |
least_common = common_bits(data,most=False) | |
print(most_common * least_common) | |
## part 2 | |
def get_common(data,most=True): | |
c = Counter(data) | |
zero,one = c['0'],c['1'] | |
if most: | |
return '1' if one >= zero else '0' | |
return '0' if zero <= one else '1' | |
def rating(data,most=True): | |
i = 0 | |
while len(data) > 1: | |
most_common = get_common([d[i] for d in data],most) | |
data = [d for d in data if d[i] == most_common] | |
i += 1 | |
return int("".join(data),2) | |
oxygen = rating(data,most=True) | |
co2 = rating(data,most=False) | |
print(oxygen * co2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment