AdventOfCode/2021/day3/day3_part2.py

321 lines
4.3 KiB
Python
Raw Normal View History

2023-12-09 22:01:20 +01:00
import numpy as np
filename = open("day3_data.txt","r")
x = []
xr = filename.readline().strip()
while xr:
xr = list(map(int,str(xr)))
x.append(xr)
xr = filename.readline().strip()
x = np.array(x)
x2=x
print(x)
print(x[0][0])
print(x[0,0])
print("---------------")
#for i in range(len(x[:,0])):
# print(x[i][0])
print("------Gamma rate---------")
g=[]
for i in range(len(x[0,:])):
g.append(np.bincount(x[:,i]).argmax())
g = np.array(g)
print("Binary: ",end="")
print(g[:])
print("Decimal: ",end="")
gs=""
gs=''.join(str(n) for n in g)
print(int(gs,2))
print("---------Epsilon rate----------")
e=[]
for i in range(len(x[0,:])):
e.append(np.bincount(x[:,i]).argmin())
e = np.array(e)
print("Binary: ",end=" ")
print(e[:])
print("Decimal: ",end=" ")
es=""
es=''.join(str(n) for n in e)
print(int(es,2))
print("-------power consumption-------")
print("Power Consumption ",end="")
print(str(int(gs,2)*int(es,2)))
print("--------part 2---------------")
a = np.bincount(x[:,0])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 1 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[0] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
a = np.bincount(x[:,1])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 2 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[1] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
a = np.bincount(x[:,2])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 3 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[2] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
a = np.bincount(x[:,3])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 4 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[3] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
a = np.bincount(x[:,4])
print("There are: ")
print("0: " + str(a[0]))
print("1: " + str(a[1]))
keep=0
print("Column 5 keep ", end="")
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
print(str(keep))
new_a = []
for i in x:
if i[4] == keep:
new_a.append(i)
new_a = np.array(new_a)
x = new_a
print("New array is: ")
print(new_a)
g = np.array(x[0])
print("Binary: ",end="")
print(g[:])
print("Decimal: ",end=" ")
ogr=""
ogr=''.join(str(n) for n in g)
print(int(ogr,2))
print("------find OXYGEN-----")
x=x2
for i in range(len(x[0,:])):
a = np.bincount(x[:,i])
keep=0
if a[0] > a[1]:
keep=0
if a[0] < a[1]:
keep=1
if a[0] == a[1]:
keep=1
new_a = []
for j in x:
if j[i] == keep:
new_a.append(j)
new_a = np.array(new_a)
x = new_a
print("new array is: ")
print(new_a)
print("TEST" + str(i) + " LEN: "+ str(len(x[:,0])))
if len(x[:,0]) == 1:
break
g = np.array(x[0])
print("Binary: ",end="")
print(g[:])
print("Decimal: ",end=" ")
og=""
og=''.join(str(n) for n in g)
print(int(og,2))
print("------find CO2-----")
x=x2
for i in range(len(x[0,:])):
a = np.bincount(x[:,i])
keep=0
if a[0] < a[1]:
keep=0
if a[0] > a[1]:
keep=1
if a[0] == a[1]:
keep=0
new_a = []
for j in x:
if j[i] == keep:
new_a.append(j)
new_a = np.array(new_a)
x = new_a
print("new array is: ")
print(new_a)
print("TEST" + str(i) + " LEN: "+ str(len(x[:,0])))
if len(x[:,0]) == 1:
break
g = np.array(x[0])
print("Binary: ",end="")
print(g[:])
print("Decimal: ",end=" ")
co2=""
co2=''.join(str(n) for n in g)
print(int(co2,2))
print("------ RESULT IS: -------")
print(str(int(co2,2)*int(og,2)))