Skip to content

Commit c8228d9

Browse files
authored
Add files via upload
1 parent a32e5a0 commit c8228d9

File tree

1 file changed

+36
-52
lines changed

1 file changed

+36
-52
lines changed

3b.py

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#Advent of code 2021
2-
# 12/04/21 day 3b
2+
# 12/05/21 day 3b
33
# Joe McFarland
44
# import sys
55
# import re
@@ -14,56 +14,40 @@
1414
#print(a_list)
1515
maxcols = len(a_list[0])
1616

17-
ogen = None
18-
current_list = copy.deepcopy(a_list)
19-
for col in range(maxcols):
20-
zero_bits = one_bits = 0
21-
for row in current_list:
22-
if row[col] == "0":
23-
zero_bits += 1
24-
elif row[col] == "1":
25-
one_bits += 1
26-
if one_bits >= zero_bits:
27-
common = 1
28-
else:
29-
common = 0
30-
new_list = []
31-
for row in current_list:
32-
if row[col] == str(common):
33-
new_list.append(row)
34-
#print(f"new_list(col={col}):\n{new_list}")
35-
if len(new_list) == 1:
36-
print(f"found entry, {new_list}")
37-
#found_ogen = new_list[0]
38-
ogen = int(new_list[0],2)
39-
break
40-
current_list = copy.deepcopy(new_list)
41-
print(f"ogen = {ogen}")
42-
43-
co2 = None
44-
current_list = copy.deepcopy(a_list)
45-
for col in range(maxcols):
46-
zero_bits = one_bits = 0
47-
for row in current_list:
48-
if row[col] == "0":
49-
zero_bits += 1
50-
elif row[col] == "1":
51-
one_bits += 1
52-
if one_bits < zero_bits: #### TODO: only diff, refactor into one function
53-
common = 1 # least common....
54-
else:
55-
common = 0
56-
new_list = []
57-
for row in current_list:
58-
if row[col] == str(common):
59-
new_list.append(row)
60-
#print(f"new_list(col={col}):\n{new_list}")
61-
if len(new_list) == 1:
62-
print(f"found entry, {new_list}")
63-
#found_co2 = new_list[0]
64-
co2 = int(new_list[0],2)
65-
break
66-
current_list = copy.deepcopy(new_list)
67-
print(f"co2 = {co2}")
17+
def findDiag( isOxygen ):
18+
diag = None
19+
current_list = copy.deepcopy(a_list)
20+
for col in range(maxcols):
21+
zero_bits = one_bits = 0
22+
for row in current_list:
23+
if row[col] == "0":
24+
zero_bits += 1
25+
elif row[col] == "1":
26+
one_bits += 1
27+
if ( isOxygen ):
28+
if one_bits >= zero_bits:
29+
common = 1 # most common
30+
else:
31+
common = 0
32+
else:
33+
if one_bits < zero_bits:
34+
common = 1 # least common
35+
else:
36+
common = 0
37+
new_list = []
38+
for row in current_list:
39+
if row[col] == str(common):
40+
new_list.append(row)
41+
#print(f"new_list(col={col}):\n{new_list}")
42+
if len(new_list) == 1:
43+
print(f"found entry, {new_list}")
44+
#found_val = new_list[0]
45+
diag = int(new_list[0],2)
46+
break
47+
current_list = copy.deepcopy(new_list)
48+
print(f"diag = {diag}")
49+
return diag
6850

51+
ogen = findDiag( True )
52+
co2 = findDiag( False )
6953
print(f"mul = {ogen*co2}")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy