8000 Add files via upload · jrmdev1/adventofcode2021@92b21fd · GitHub
[go: up one dir, main page]

Skip to content

Commit 92b21fd

Browse files
authored
Add files via upload
1 parent c3b140a commit 92b21fd

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

3b.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# import re
66
import copy
77

8-
filename = "data3_short.txt"
8+
filename = "data3.txt"
99

1010
file = open(filename)
1111
filestr = file.read()
@@ -31,26 +31,24 @@
3131

3232
maxcols = len(a_list[0])
3333

34-
ogen = c02 = None
34+
ogen = None
3535
current_list = copy.deepcopy(a_list)
3636
for col in range(maxcols):
3737
zero_bits = one_bits = 0
38-
#col = 0
3938
for row in current_list:
4039
if row[col] == "0":
4140
zero_bits += 1
4241
elif row[col] == "1":
4342
one_bits += 1
4443
if one_bits >= zero_bits:
45-
most_common = 1
44+
common = 1
4645
else:
47-
most_common = 0
48-
#col = 0
46+
common = 0
4947
new_list = []
5048
for row in current_list:
51-
if row[col] == str(most_common):
49+
if row[col] == str(common):
5250
new_list.append(row)
53-
print(f"new_list(col={col}):\n{new_list}")
51+
#print(f"new_list(col={col}):\n{new_list}")
5452
if len(new_list) == 1:
5553
print(f"found entry, {new_list}")
5654
#found_ogen = new_list[0]
@@ -59,6 +57,36 @@
5957
current_list = copy.deepcopy(new_list)
6058
print(f"ogen = {ogen}")
6159

60+
co2 = None
61+
current_list = copy.deepcopy(a_list)
62+
for col in range(maxcols):
63+
zero_bits = one_bits = 0
64+
for row in current_list:
65+
if row[col] == "0":
66+
zero_bits += 1
67+
elif row[col] == "1":
68+
one_bits += 1
69+
if one_bits < zero_bits: #### TODO: only diff, refactor into one function
70+
common = 1 # least common....
71+
else:
72+
common = 0
73+
new_list = []
74+
for row in current_list:
75+
if row[col] == str(common):
76+
new_list.append(row)
77+
#print(f"new_list(col={col}):\n{new_list}")
78+
if len(new_list) == 1:
79+
print(f"found entry, {new_list}")
80+
#found_co2 = new_list[0]
81+
co2 = int(new_list[0],2)
82+
break
83+
current_list = copy.deepcopy(new_list)
84+
print(f"co2 = {co2}")
85+
86+
print(f"mul = {ogen*co2}")
87+
88+
89+
6290
# print(f"gamma={gamma}, epsilon={epsilon}")
6391
# g1 = arraytostr(gamma)
6492
# e1 = arraytostr(epsilon)

0 commit comments

Comments
 (0)
0