-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3b.py
53 lines (49 loc) · 1.36 KB
/
3b.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#Advent of code 2021
# 12/05/21 day 3b
# Joe McFarland
# import sys
# import re
import copy
filename = "data3.txt"
file = open(filename)
filestr = file.read()
a_list = filestr.split("\n")
maxrows = len(a_list)
#print(a_list)
maxcols = len(a_list[0])
def findDiag( isOxygen ):
diag = None
current_list = copy.deepcopy(a_list)
for col in range(maxcols):
zero_bits = one_bits = 0
for row in current_list:
if row[col] == "0":
zero_bits += 1
elif row[col] == "1":
one_bits += 1
if ( isOxygen ):
if one_bits >= zero_bits:
common = 1 # most common
else:
common = 0
else:
if one_bits < zero_bits:
common = 1 # least common
else:
common = 0
new_list = []
for row in current_list:
if row[col] == str(common):
new_list.append(row)
#print(f"new_list(col={col}):\n{new_list}")
if len(new_list) == 1:
print(f"found entry, {new_list}")
#found_val = new_list[0]
diag = int(new_list[0],2)
break
current_list = copy.deepcopy(new_list)
print(f"diag = {diag}")
return diag
ogen = findDiag( True )
co2 = findDiag( False )
print(f"mul = {ogen*co2}")