You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.5 KiB
101 lines
2.5 KiB
|
4 years ago
|
import os
|
||
|
|
import numpy as np
|
||
|
|
|
||
|
|
dir = os.path.dirname(os.path.abspath(__file__))
|
||
|
|
|
||
|
|
def countCommonBit(container, bit):
|
||
|
|
one_count=0
|
||
|
|
zero_count=0
|
||
|
|
for item in container:
|
||
|
|
clean_item = item.strip()
|
||
|
|
char = clean_item[bit]
|
||
|
|
if char == '1':
|
||
|
|
one_count = one_count+1
|
||
|
|
if char == '0':
|
||
|
|
zero_count = zero_count+1
|
||
|
|
return one_count, zero_count
|
||
|
|
|
||
|
|
with open(os.path.join(dir, './input.txt'), 'r') as input_file:
|
||
|
|
|
||
|
|
lines = input_file.readlines()
|
||
|
|
|
||
|
|
line_length = len(lines[0].strip())
|
||
|
|
|
||
|
|
gamma_string = ""
|
||
|
|
epsilon_string = ""
|
||
|
|
for i in range(line_length):
|
||
|
|
ones, zeroes = countCommonBit(lines, i)
|
||
|
|
if ones > zeroes:
|
||
|
|
gamma_string = gamma_string + "1"
|
||
|
|
epsilon_string = epsilon_string + "0"
|
||
|
|
else:
|
||
|
|
gamma_string = gamma_string + "0"
|
||
|
|
epsilon_string = epsilon_string + "1"
|
||
|
|
|
||
|
|
print(gamma_string)
|
||
|
|
gamma = int(gamma_string, 2)
|
||
|
|
print(gamma)
|
||
|
|
|
||
|
|
print(epsilon_string)
|
||
|
|
epsilon = int(epsilon_string, 2)
|
||
|
|
print(epsilon)
|
||
|
|
|
||
|
|
print(gamma*epsilon)
|
||
|
|
|
||
|
|
|
||
|
|
oxygen_candidates = lines.copy()
|
||
|
|
o2_string = ""
|
||
|
|
|
||
|
|
for i in range(line_length):
|
||
|
|
# Calcualte the most common bit at i
|
||
|
|
ones, zeroes = countCommonBit(oxygen_candidates, i)
|
||
|
|
|
||
|
|
if ones >= zeroes:
|
||
|
|
bit = '1'
|
||
|
|
else:
|
||
|
|
bit = '0'
|
||
|
|
|
||
|
|
print(ones, zeroes, bit)
|
||
|
|
|
||
|
|
toremove = []
|
||
|
|
for line in oxygen_candidates:
|
||
|
|
clean_line = line.strip()
|
||
|
|
if clean_line[i] != bit:
|
||
|
|
toremove.append(line)
|
||
|
|
|
||
|
|
oxygen_candidates = [x for x in oxygen_candidates if x not in toremove]
|
||
|
|
if len(oxygen_candidates) == 1:
|
||
|
|
o2_string = oxygen_candidates[0]
|
||
|
|
|
||
|
|
co2_candidates = lines.copy()
|
||
|
|
co2_string = ""
|
||
|
|
for i in range(line_length):
|
||
|
|
# Calcualte the most common bit at i
|
||
|
|
ones, zeroes = countCommonBit(co2_candidates, i)
|
||
|
|
|
||
|
|
if zeroes > ones:
|
||
|
|
bit = '1'
|
||
|
|
else:
|
||
|
|
bit = '0'
|
||
|
|
|
||
|
|
print(ones, zeroes, bit)
|
||
|
|
|
||
|
|
toremove = []
|
||
|
|
for line in co2_candidates:
|
||
|
|
clean_line = line.strip()
|
||
|
|
if clean_line[i] != bit:
|
||
|
|
toremove.append(line)
|
||
|
|
|
||
|
|
co2_candidates = [x for x in co2_candidates if x not in toremove]
|
||
|
|
if len(co2_candidates) == 1:
|
||
|
|
co2_string = co2_candidates[0]
|
||
|
|
|
||
|
|
print(o2_string)
|
||
|
|
o2 = int(o2_string, 2)
|
||
|
|
print(o2)
|
||
|
|
|
||
|
|
print(co2_string)
|
||
|
|
co2 = int(co2_string, 2)
|
||
|
|
print(co2)
|
||
|
|
|
||
|
|
print(o2*co2)
|