advent_of_code/2021/03/part-001.py

82 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
gamma = []
epsilon = []
lines = 0
with open('input.txt', 'r') as myfile:
for line in myfile:
lines = lines + 1
linelaenge = len(line.strip())
# initialize the arrays
for number in range(linelaenge):
gamma.append(0)
epsilon.append(0)
with open('input.txt', 'r') as myfile:
for line in myfile:
# get most common bit
for number in range(linelaenge):
# print('Nr. {} > {}'.format(number, int(line[number])))
if int(line[number]) == 1:
#print(int(line[number]))
# element does not exists
gamma[number] = gamma[number] + 1
else:
#print(int(line[number]))
epsilon[number] = epsilon[number] + 1
print('Gamma: {}'.format(gamma))
print('Epsilon: {}'.format(epsilon))
gamma_copy = gamma
epsilon_copy = epsilon
ga_number = ''
ep_number = ''
for element in gamma_copy:
if element > lines/2:
ga_number = ga_number + '1'
else:
ga_number = ga_number + '0'
for element in epsilon_copy:
if element > lines/2:
ep_number = ep_number + '1'
else:
ep_number = ep_number + '0'
print('Gamma-Number: {}/{}'.format(ga_number, int(ga_number, 2)))
print('Epsilon-Number: {}/{}'.format(ep_number, int(ep_number, 2)))
print(int(ga_number, 2) * int(ep_number, 2))
sys.exit()
# for line in myfile:
# # get most common bit
# for number in range(linelaenge):
# if int(line[number]) > 0:
# # element does not exists
# if gamma[number] exists:
# gamma.insert[number] = line[number]
print('Number of lines: {}'.format(lines))
print('Line length: {}'.format(linelaenge))
print('Gamma: {}'.format(gamma))
# print('forward: {} depth: {}'.format(forward, depth))
# print('Submit value: {}'.format(forward * abs(depth)))