You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
1.9 KiB
74 lines
1.9 KiB
import ast |
|
|
|
def comparison(_left, _rigth): |
|
_wrong = '' |
|
if type(_left) is list and type(_rigth) is list: |
|
__for = min(len(_left), len(_rigth)) |
|
for j in range(__for): |
|
_wrong = comparison(_left[j], _rigth[j]) |
|
if _wrong in ['yes', 'no']: |
|
return _wrong |
|
if len(_left) > len(_rigth): |
|
_wrong = 'yes' |
|
if len(_left) < len(_rigth): |
|
_wrong = 'no' |
|
elif type(_left) is list: |
|
if _left == []: |
|
_wrong = 'no' |
|
return _wrong |
|
_wrong = comparison(_left[0], _rigth) |
|
if _wrong == '': |
|
_wrong = 'yes' |
|
elif type(_rigth) is list: |
|
print(_left, _rigth) |
|
if _rigth == []: |
|
_wrong = 'yes' |
|
return _wrong |
|
_wrong = comparison(_left, _rigth[0]) |
|
if _wrong == '': |
|
_wrong = 'no' |
|
else: |
|
if _left > _rigth: |
|
_wrong = 'yes' |
|
if _rigth > _left: |
|
_wrong = 'no' |
|
return _wrong |
|
|
|
class pair(): |
|
def __init__(self, left, rigth): |
|
self.left = ast.literal_eval(left) |
|
self.rigth = ast.literal_eval(rigth) |
|
|
|
def compare(self): |
|
self.wrong = '' |
|
_for = min(len(self.left), len(self.rigth)) |
|
for i in range(_for): |
|
self.wrong = comparison(self.left[i], self.rigth[i]) |
|
if self.wrong in ['yes', 'no']: |
|
return self.wrong |
|
if len(self.left) > len(self.rigth): |
|
self.wrong = 'yes' |
|
return self.wrong |
|
return 'no' |
|
|
|
def testPrint(self): |
|
print(self.left) |
|
print(self.rigth) |
|
print('') |
|
|
|
pairs = [] |
|
sum = 0 |
|
|
|
with open('input13.txt','r') as f: |
|
inp = f.read().splitlines(keepends=False) |
|
|
|
tempa = [] |
|
for i in range(0, len(inp), 3): |
|
pairs.append(pair(inp[i], inp[i+1])) |
|
|
|
for pairN in range(len(pairs)): |
|
print(pairN) |
|
if pairs[pairN].compare() == 'no': |
|
sum += (pairN + 1) |
|
|
|
print('the sum of indices is:', sum) |