#!/usr/bin/python def parse_input(lines): ingredients = [] for l in lines: words = l.split() # Rather than label the properties by name, we number them 0-4 props = [] for n in range(5): props.append(int(words[2 + 2 * n].rstrip(","))) name = words[0].rstrip(":") ingredients.append(props) return ingredients def all_recipes(ingredients): recipes = {} for i1 in range(101): for i2 in range(101 - i1): for i3 in range(101 - i1 - i2): i4 = 100 - i1 - i2 - i3 amounts = [i1, i2, i3, i4] # amount of each ingredient props = [0, 0, 0, 0] # score for each property for p in range(len(props)): # Accumulate properties of ingredients for i in range(len(amounts)): prop = amounts[i] * ingredients[i][p] props[p] += prop # Clamp negative totals to zero if props[p] < 0: props[p] = 0 # `recipes' is a dictionary: key is recipe, value is total score recipes[tuple(amounts)] = props[0] * props[1] * props[2] * props[3] return recipes def solve_partn(partn, lines): ingredients = parse_input(lines) recipes = all_recipes(ingredients) if partn == 2: suitable = {} for amounts, score in recipes.items(): calories = 0 for i in range(len(amounts)): calories += amounts[i] * ingredients[i][4] if calories == 500: suitable[amounts] = score recipes = suitable return max(recipes.values()) def main(): # Read ingredient information from input text file with open("input.txt", "r") as f: lines = f.read().splitlines() print("Part 1 solution:", solve_partn(1, lines)) # 222870 for me print("Part 2 solution:", solve_partn(2, lines)) # 117936 for me if __name__ == "__main__": main()