Last active
December 3, 2021 15:45
-
-
Save bclavie/28ee51898a445134e1b1ef3a4a44140b to your computer and use it in GitHub Desktop.
AoC 2021 Day3
This file contains hidden or 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 | |
# Part 1 | |
inputs = [x.strip() for x in open('inputs.txt', 'r').readlines()] | |
gamma_str = '' | |
epsilon_str = '' | |
for i in range(len(inputs[0])): | |
col_count = Counter([x[i] for x in inputs]) | |
if col_count['1'] > col_count['0']: | |
gamma_str += '1' | |
epsilon_str += '0' | |
else: | |
gamma_str += '0' | |
epsilon_str += '1' | |
gamma, epsilon = int(gamma_str, 2), int(epsilon_str, 2) | |
p1 = gamma * epsilon | |
print(f'Part 1: {p1}') | |
# Part 2 | |
ox_str = '' | |
co2_str = '' | |
ox_lst = co2_lst = inputs | |
for i in range(len(inputs[0])): | |
ox_cnt = Counter([x[i] for x in ox_lst]) | |
co2_cnt = Counter([x[i] for x in co2_lst]) | |
if ox_lst: | |
if ox_cnt['0'] > ox_cnt['1']: | |
ox_lst = [x for x in ox_lst if x[i] == '0'] | |
else: | |
ox_lst = [x for x in ox_lst if x[i] == '1'] | |
try: | |
ox_str = ox_lst[0] | |
except: | |
break | |
if co2_lst: | |
if co2_cnt['0'] > co2_cnt['1']: | |
co2_lst = [x for x in co2_lst if x[i] == '1'] | |
else: | |
co2_lst = [x for x in co2_lst if x[i] == '0'] | |
try: | |
co2_str = co2_lst[0] | |
except: | |
break | |
ox, co2 = int(ox_str ,2), int(co2_str ,2) | |
p2 = ox * co2 | |
print(f'Part 2: {p2}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment