-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-3.2.py
40 lines (31 loc) · 891 Bytes
/
day-3.2.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
import re
data = None
with open("input.txt",encoding="utf-8") as inpf:
data = [x for x in inpf.read().split("\n") if x.strip()]
bit_size = 0
for x in data:
bit_size = max(len(x),bit_size)
oxygen = data.copy()
co2 = data.copy()
i = 0
while(len(oxygen)>1):
temp = [x[i] for x in oxygen]
if temp.count("1") == temp.count("0"):
oxygen = [x for x in oxygen if x[i] == "1"]
elif temp.count("1") > temp.count("0"):
oxygen = [x for x in oxygen if x[i] == "1"]
else:
oxygen = [x for x in oxygen if x[i] == "0"]
i += 1
i = 0
while(len(co2)>1):
temp = [x[i] for x in co2]
if temp.count("1") == temp.count("0"):
co2 = [x for x in co2 if x[i] == "0"]
elif temp.count("1") > temp.count("0"):
co2 = [x for x in co2 if x[i] == "0"]
else:
co2 = [x for x in co2 if x[i] == "1"]
i += 1
if len(oxygen) == len(co2):
print(int(oxygen[0],2)*int(co2[0],2))