from lib import get_data from lib import Grid2D from lib import ints from collections import defaultdict data = """ 190: 10 19 3267: 81 40 27 83: 17 5 156: 15 6 7290: 6 8 6 15 161011: 16 10 13 192: 17 8 14 21037: 9 7 18 13 292: 11 6 16 20 """ data = get_data(__file__) def comp(acc, rest, part_2=False): if rest == []: return acc nacc = [] for a in acc: nacc.append(a + rest[0]) nacc.append(a * rest[0]) if part_2: nacc.append(int(str(a) + str(rest[0]))) return comp(nacc, rest[1:], part_2) t1, t2 = 0, 0 for line in data.splitlines(): xs = ints(line) expected = xs[0] if xs[0] in comp([xs[1]], xs[2:]): t1 += xs[0] if xs[0] in comp([xs[1]], xs[2:], True): t2 += xs[0] print(t1) print(t2)