Created
December 7, 2021 23:31
-
-
Save eisterman/533371857833f36673073278ff813b46 to your computer and use it in GitHub Desktop.
Advent Of Code 2021 - 3 wrong
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Tue Dec 7 19:23:24 2021 | |
@author: feder | |
""" | |
import numpy as np | |
file = open('input3.txt') | |
lines = file.readlines() | |
width = len(lines[0].strip()) | |
heigth = len(lines) | |
data = np.zeros((heigth, width), dtype=np.bool8) | |
for h, line in enumerate(lines): | |
for w, b in enumerate(line.strip()): | |
data[h, w] = np.bool8(int(b)) | |
# Part 1 | |
gamma_rate = "" # Most common byte of that column | |
epsilon_rate = "" | |
for i in range(width): | |
var = data[:, i].sum() > heigth/2 | |
gamma_rate += str(int(var)) | |
epsilon_rate += str(int(not var)) | |
gamma = int(gamma_rate, base=2) | |
epsilon = int(epsilon_rate, base=2) | |
print(f"Part 1 result: {gamma*epsilon}") | |
# Part 2 | |
index_oxy = list(range(heigth)) | |
for iteration in range(width): | |
filter_bit = np.bool8(int(gamma_rate[iteration])) | |
new_index = [] | |
for i in index_oxy: | |
if data[i, iteration] == filter_bit: | |
new_index.append(i) | |
index_oxy = new_index | |
if len(index_oxy) <= 1: | |
print(f"BREAK AT {iteration}") | |
break | |
index_co2 = list(range(heigth)) | |
for iteration in range(width): | |
filter_bit = np.bool8(int(epsilon_rate[iteration])) | |
new_index = [] | |
for i in index_co2: | |
if data[i, iteration] == filter_bit: | |
new_index.append(i) | |
index_co2 = new_index | |
if len(index_co2) <= 1: | |
print(f"BREAK AT {iteration}") | |
break | |
oxy = "" | |
co2 = "" | |
for i in range(width): | |
oxy += str(int(data[index_oxy[0], i])) | |
co2 += str(int(data[index_co2[0], i])) | |
oxy_o = int(oxy, base=2) | |
co2_o = int(co2, base=2) | |
print(f"Part 2 result: {oxy_o*co2_o}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment