#!/usr/bin/env python3 import sys from functools import cmp_to_key from copy import deepcopy def compare(x,y): x = deepcopy(x) y = deepcopy(y) while True: if len(y) == 0 and len(x) == 0: return 0 elif len(y) == 0: return -1 elif len(x) == 0: return 1 x0 = x.pop(0) y0 = y.pop(0) if isinstance(x0,int) and type(y0) == list: x0 = [x0] elif isinstance(y0,int) and type(x0) == list: y0 = [y0] if isinstance(x0,int) and isinstance(y0,int): if x0 > y0: return -1 elif x0 < y0: return 1 elif type(x0) == list and type(y0) == list: r = compare(x0,y0) if r == 1: return 1 if r == -1: return -1 return -1 if __name__ == '__main__': pairs = [tuple(pair.split('\n')) for pair in open(sys.argv[1]).read().split('\n\n')] # challenge 1 res = 0 for i, pair in enumerate(pairs): x = eval(pair[0]) y = eval(pair[1]) if compare(x,y) == 1: res = res + 1 + i res1 = str(res) print("challenge 1:" + "\n" + res1 + "\n") # challenge 2 big_list = [] for pair in pairs: big_list.append(eval(pair[0])) big_list.append(eval(pair[1])) big_list.append([[2]]) big_list.append([[6]]) big_list.sort(key=cmp_to_key(compare),reverse=True) i1 = big_list.index([[2]]) + 1 i2 = big_list.index([[6]]) + 1 res2 = str(i1*i2) print("challenge 2:" + "\n" + res2 + "\n")