2015 day 15.
This commit is contained in:
		
							
								
								
									
										57
									
								
								src/holt59/aoc/2015/day15.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/holt59/aoc/2015/day15.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,57 @@ | ||||
| import math | ||||
| import sys | ||||
| from typing import Sequence, cast | ||||
|  | ||||
| import parse  # type: ignore | ||||
|  | ||||
|  | ||||
| def score(ingredients: list[list[int]], teaspoons: Sequence[int]) -> int: | ||||
|     return math.prod( | ||||
|         max( | ||||
|             0, | ||||
|             sum( | ||||
|                 ingredient[prop] * teaspoon | ||||
|                 for ingredient, teaspoon in zip(ingredients, teaspoons) | ||||
|             ), | ||||
|         ) | ||||
|         for prop in range(len(ingredients[0]) - 1) | ||||
|     ) | ||||
|  | ||||
|  | ||||
| lines = sys.stdin.read().splitlines() | ||||
|  | ||||
| ingredients: list[list[int]] = [] | ||||
| for line in lines: | ||||
|     _, *scores = cast( | ||||
|         tuple[str, int, int, int, int, int], | ||||
|         parse.parse(  # type: ignore | ||||
|             "{}: capacity {:d}, durability {:d}, flavor {:d}, " | ||||
|             "texture {:d}, calories {:d}", | ||||
|             line, | ||||
|         ), | ||||
|     ) | ||||
|     ingredients.append(scores) | ||||
|  | ||||
| total_teaspoons = 100 | ||||
| calories: list[int] = [] | ||||
| scores: list[int] = [] | ||||
|  | ||||
| for a in range(total_teaspoons + 1): | ||||
|     for b in range(total_teaspoons + 1 - a): | ||||
|         for c in range(total_teaspoons + 1 - a - b): | ||||
|             teaspoons = (a, b, c, total_teaspoons - a - b - c) | ||||
|  | ||||
|             scores.append(score(ingredients, teaspoons)) | ||||
|             calories.append( | ||||
|                 sum( | ||||
|                     ingredient[-1] * teaspoon | ||||
|                     for ingredient, teaspoon in zip(ingredients, teaspoons) | ||||
|                 ) | ||||
|             ) | ||||
|  | ||||
|  | ||||
| answer_1 = max(scores) | ||||
| print(f"answer 1 is {answer_1}") | ||||
|  | ||||
| answer_2 = max(score for score, calory in zip(scores, calories) if calory == 500) | ||||
| print(f"answer 2 is {answer_2}") | ||||
		Reference in New Issue
	
	Block a user