from itertools import combinations lines = [] with open("9.input") as f: for line in f.readlines(): lines.append(int(line.strip())) queue, rest = lines[0:25], lines[25:] invalid_num = None while rest != []: next = rest.pop(0) valid = next in [a + b for (a, b) in combinations(list(set(queue)), 2)] if not valid: invalid_num = next break queue.pop(0) queue.append(next) print("Answer 1: " + str(invalid_num)) def search_window(size): for i in range(0, len(lines)): slice = lines[i:i+size] if sum(slice) == invalid_num: return min(slice) + max(slice) return None for size in range(2, len(lines)): result = search_window(size) if result != None: print("Answer 2: " + str(result)) break