|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | +from typing import List |
| 5 | + |
| 6 | +from adventofcode.util.exceptions import SolutionNotFoundException |
| 7 | +from adventofcode.util.helpers import solution_timer |
| 8 | +from adventofcode.util.input_helpers import get_input_for_day |
| 9 | + |
| 10 | +PATTERN = re.compile(r'(-?\d)') |
| 11 | + |
| 12 | + |
| 13 | +class Ingredient: |
| 14 | + def __init__(self, name: str, capacity: int, durability: int, flavor: int, texture: int, calories: int): |
| 15 | + self.name = name |
| 16 | + self.capacity = capacity |
| 17 | + self.durability = durability |
| 18 | + self.flavor = flavor |
| 19 | + self.texture = texture |
| 20 | + self.calories = calories |
| 21 | + self._quantity = 0 |
| 22 | + |
| 23 | + def __repr__(self): |
| 24 | + return str(self) |
| 25 | + |
| 26 | + def __str__(self): |
| 27 | + return f'{self.name}: capacity: {self.capacity}, durability: {self.durability}, flavor: {self.flavor}, texture: {self.texture}' |
| 28 | + |
| 29 | + @property |
| 30 | + def quantity(self) -> int: |
| 31 | + return self._quantity |
| 32 | + |
| 33 | + def set_quantity(self, quantity: int) -> Ingredient: |
| 34 | + self._quantity = quantity |
| 35 | + return self |
| 36 | + |
| 37 | + def get_capacity(self) -> int: |
| 38 | + return self.capacity * self.quantity |
| 39 | + |
| 40 | + def get_durability(self) -> int: |
| 41 | + return self.durability * self.quantity |
| 42 | + |
| 43 | + def get_flavor(self) -> int: |
| 44 | + return self.flavor * self.quantity |
| 45 | + |
| 46 | + def get_texture(self) -> int: |
| 47 | + return self.texture * self.quantity |
| 48 | + |
| 49 | + def get_calories(self) -> int: |
| 50 | + return self.calories * self.quantity |
| 51 | + |
| 52 | + |
| 53 | +class Cookie: |
| 54 | + def __init__(self, ingredients: List[Ingredient]): |
| 55 | + self.ingredients = ingredients |
| 56 | + |
| 57 | + @property |
| 58 | + def calories(self) -> int: |
| 59 | + return sum([ingredient.get_calories() for ingredient in self.ingredients]) |
| 60 | + |
| 61 | + @property |
| 62 | + def score(self) -> int: |
| 63 | + capacity = sum([ingredient.get_capacity() for ingredient in self.ingredients]) |
| 64 | + durability = sum([ingredient.get_durability() for ingredient in self.ingredients]) |
| 65 | + flavor = sum([ingredient.get_flavor() for ingredient in self.ingredients]) |
| 66 | + texture = sum([ingredient.get_texture() for ingredient in self.ingredients]) |
| 67 | + |
| 68 | + if capacity < 0: |
| 69 | + capacity = 0 |
| 70 | + if durability < 0: |
| 71 | + durability = 0 |
| 72 | + if flavor < 0: |
| 73 | + flavor = 0 |
| 74 | + if texture < 0: |
| 75 | + texture = 0 |
| 76 | + |
| 77 | + return capacity * durability * flavor * texture |
| 78 | + |
| 79 | + |
| 80 | +def parse_ingredients(input_data: List[str]) -> List[Ingredient]: |
| 81 | + ingredients: List[Ingredient] = [] |
| 82 | + |
| 83 | + for line in input_data: |
| 84 | + name, content = line.split(': ') |
| 85 | + capacity, durability, flavor, texture, calories = map(int, PATTERN.findall(content)) |
| 86 | + ingredient = Ingredient(name, capacity, durability, flavor, texture, calories) |
| 87 | + ingredients.append(ingredient) |
| 88 | + |
| 89 | + return ingredients |
| 90 | + |
| 91 | + |
| 92 | +def find_highest_scoring_cookie(input_data: List[str], match_calories: bool = False) -> int: # noqa: C901 |
| 93 | + highest_score = 0 |
| 94 | + ingredients = parse_ingredients(input_data) |
| 95 | + max_ingredients = 100 |
| 96 | + |
| 97 | + if len(ingredients) == 2: |
| 98 | + for a in range(max_ingredients): |
| 99 | + for b in range(max_ingredients - a): |
| 100 | + b = 100 - a |
| 101 | + ingredients[0].set_quantity(a) |
| 102 | + ingredients[1].set_quantity(b) |
| 103 | + |
| 104 | + cookie = Cookie(ingredients) |
| 105 | + |
| 106 | + if match_calories and cookie.calories != 500: |
| 107 | + continue |
| 108 | + |
| 109 | + if cookie.score > highest_score: |
| 110 | + highest_score = cookie.score |
| 111 | + else: |
| 112 | + for a in range(max_ingredients): |
| 113 | + for b in range(max_ingredients - a): |
| 114 | + for c in range(max_ingredients - a - b): |
| 115 | + d = max_ingredients - a - b - c |
| 116 | + ingredients[0].set_quantity(a) |
| 117 | + ingredients[1].set_quantity(b) |
| 118 | + ingredients[2].set_quantity(c) |
| 119 | + ingredients[3].set_quantity(d) |
| 120 | + cookie = Cookie(ingredients) |
| 121 | + |
| 122 | + if match_calories and cookie.calories != 500: |
| 123 | + continue |
| 124 | + |
| 125 | + if cookie.score > highest_score: |
| 126 | + highest_score = cookie.score |
| 127 | + |
| 128 | + return highest_score |
| 129 | + |
| 130 | + |
| 131 | +@solution_timer(2015, 15, 1) |
| 132 | +def part_one(input_data: List[str]): |
| 133 | + answer = find_highest_scoring_cookie(input_data) |
| 134 | + |
| 135 | + if not answer: |
| 136 | + raise SolutionNotFoundException(2015, 15, 1) |
| 137 | + |
| 138 | + return answer |
| 139 | + |
| 140 | + |
| 141 | +@solution_timer(2015, 15, 2) |
| 142 | +def part_two(input_data: List[str]): |
| 143 | + answer = find_highest_scoring_cookie(input_data, match_calories=True) |
| 144 | + |
| 145 | + if not answer: |
| 146 | + raise SolutionNotFoundException(2015, 15, 2) |
| 147 | + |
| 148 | + return answer |
| 149 | + |
| 150 | + |
| 151 | +if __name__ == '__main__': |
| 152 | + data = get_input_for_day(2015, 15) |
| 153 | + part_one(data) |
| 154 | + part_two(data) |
0 commit comments