2. Teil, Tag 3
This commit is contained in:
parent
447338b9b7
commit
d70a5f5640
|
@ -0,0 +1,58 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
|
||||
line_count = 0
|
||||
lines = []
|
||||
|
||||
def search_elements(mylist, position=0, search='most'):
|
||||
# break if only one element
|
||||
if len(mylist) == 1:
|
||||
return mylist[0]
|
||||
|
||||
list_length = len(mylist)
|
||||
erg = 0
|
||||
OneArray = []
|
||||
NullArray = []
|
||||
|
||||
for elem in mylist:
|
||||
# elem_length = len(elem)
|
||||
if int(elem[position]) == 1:
|
||||
erg = erg + 1
|
||||
OneArray.append(elem)
|
||||
else:
|
||||
NullArray.append(elem)
|
||||
|
||||
if search == 'most':
|
||||
if erg == list_length/2:
|
||||
return search_elements(OneArray, position=position+1, search=search)
|
||||
elif erg > list_length/2:
|
||||
return search_elements(OneArray, position=position+1, search=search)
|
||||
else:
|
||||
return search_elements(NullArray, position=position+1, search=search)
|
||||
|
||||
if search == 'least':
|
||||
if erg == list_length/2:
|
||||
return search_elements(NullArray, position=position+1, search=search)
|
||||
if erg < list_length/2:
|
||||
return search_elements(OneArray, position=position+1, search=search)
|
||||
else:
|
||||
return search_elements(NullArray, position=position+1, search=search)
|
||||
|
||||
|
||||
with open('input.txt', 'r') as myfile:
|
||||
for line in myfile:
|
||||
lines.append(line)
|
||||
|
||||
line_count = len(lines)
|
||||
|
||||
|
||||
most = search_elements(lines, position=0, search='most')
|
||||
least = search_elements(lines, position=0, search='least')
|
||||
print(most)
|
||||
print(least)
|
||||
|
||||
print(int(most, 2) * int(least, 2))
|
||||
|
||||
|
||||
sys.exit()
|
Loading…
Reference in New Issue