34 lines
772 B
Python
34 lines
772 B
Python
![]() |
#!/usr/bin/env python
|
||
|
|
||
|
import copy
|
||
|
|
||
|
code_len=12
|
||
|
input=[]
|
||
|
|
||
|
with open("input01.txt", "r") as f:
|
||
|
for line in f:
|
||
|
input.append(line)
|
||
|
|
||
|
input_oxygen = copy.copy(input)
|
||
|
input_co2 = copy.copy(input)
|
||
|
|
||
|
# oxygen
|
||
|
for pos in range(code_len):
|
||
|
if len(input_oxygen) == 1:
|
||
|
break
|
||
|
tmp = [i for i in input_oxygen if i[pos] == '1']
|
||
|
filter = '1' if (len(tmp) >= len(input_oxygen)/2) else '0'
|
||
|
input_oxygen = [i for i in input_oxygen if i[pos] == filter]
|
||
|
|
||
|
|
||
|
# co2
|
||
|
for pos in range(code_len):
|
||
|
if len(input_co2) == 1:
|
||
|
break
|
||
|
tmp = [i for i in input_co2 if i[pos] == '1']
|
||
|
filter = '1' if (len(tmp) < len(input_co2)/2) else '0'
|
||
|
input_co2 = [i for i in input_co2 if i[pos] == filter]
|
||
|
|
||
|
|
||
|
print(int(input_oxygen[0], 2) * int(input_co2[0],2))
|