2. Teil, Tag 3

This commit is contained in:
Reiko Kaps 2021-12-03 16:30:22 +01:00
parent 447338b9b7
commit d70a5f5640
1 changed files with 58 additions and 0 deletions

58
2021/03/part-002.py Executable file
View File

@ -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()