import re import numpy as np f = open('input.txt', 'r') content = f.read() lines = content.splitlines() num_instances = np.ones(len(lines)) for line_count, line in enumerate(lines): card_sum = 0 card = re.split(":", line) card_number = re.findall(r'\d+', card[0]) numbers = re.split("\|", card[1]) my_numbers = [int(x) for x in filter(('').__ne__, re.split(" ", numbers[1]))] winning_numbers = set(int(x) for x in filter(('').__ne__, re.split(" ", numbers[0]))) for num in my_numbers: if num in winning_numbers: card_sum += 1 for x in range(card_sum): num_instances[x+line_count+1] += num_instances[line_count] f.close() print(num_instances) print(np.sum(num_instances))