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: 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 def compare(left, rigth): left = ast.literal_eval(left) rigth = ast.literal_eval(rigth) wrong = '' _for = min(len(left), len(rigth)) for i in range(_for): wrong = comparison(left[i], rigth[i]) if wrong in ['yes', 'no']: return wrong if len(left) > len(rigth): wrong = 'yes' return wrong return 'no' def loop(_item): for q in range(len(sorted)): if compare(_item, sorted[q]) == 'no': sorted.insert(q, _item) return sorted.append(_item) unsorted = [] sorted = [] with open('input13.txt','r') as f: inp = f.read().splitlines(keepends=False) for line in inp: if line != '': unsorted.append(line) unsorted.append('[[2]]') unsorted.append('[[6]]') sorted.append(unsorted[0]) unsorted.pop(0) for item in unsorted: loop(item) for index in range(len(sorted)): if sorted[index] == '[[2]]': divider1 = index + 1 if sorted[index] == '[[6]]': divider2 = index + 1 print('Decoder Key:' ,(divider1 * divider2))