From 850c66cd8d2d9ab8eb54b4aaccc76235f8e17922 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Sat, 20 Jan 2024 17:57:37 +0100 Subject: [PATCH] 2015 day 21. --- src/holt59/aoc/2015/day21.py | 66 +++++++++++++++++++++ src/holt59/aoc/inputs/holt59/2015/day21.txt | 3 + 2 files changed, 69 insertions(+) create mode 100644 src/holt59/aoc/2015/day21.py create mode 100644 src/holt59/aoc/inputs/holt59/2015/day21.txt diff --git a/src/holt59/aoc/2015/day21.py b/src/holt59/aoc/2015/day21.py new file mode 100644 index 0000000..cf71059 --- /dev/null +++ b/src/holt59/aoc/2015/day21.py @@ -0,0 +1,66 @@ +import itertools +import sys +from math import ceil +from typing import TypeAlias + +Modifier: TypeAlias = tuple[str, int, int, int] + +WEAPONS: list[Modifier] = [ + ("Dagger", 8, 4, 0), + ("Shortsword", 10, 5, 0), + ("Warhammer", 25, 6, 0), + ("Longsword", 40, 7, 0), + ("Greataxe", 74, 8, 0), +] + +ARMORS: list[Modifier] = [ + ("", 0, 0, 0), + ("Leather", 13, 0, 1), + ("Chainmail", 31, 0, 2), + ("Splintmail", 53, 0, 3), + ("Bandedmail", 75, 0, 4), + ("Platemail", 102, 0, 5), +] + +RINGS: list[Modifier] = [ + ("", 0, 0, 0), + ("Damage +1", 25, 1, 0), + ("Damage +2", 50, 2, 0), + ("Damage +3", 100, 3, 0), + ("Defense +1", 20, 0, 1), + ("Defense +2", 40, 0, 2), + ("Defense +3", 80, 0, 3), +] + + +lines = sys.stdin.read().splitlines() + +player_hp = 100 + +boss_attack = int(lines[1].split(":")[1].strip()) +boss_armor = int(lines[2].split(":")[1].strip()) +boss_hp = int(lines[0].split(":")[1].strip()) + + +min_cost, max_cost = 1_000_000, 0 +for equipments in itertools.product(WEAPONS, ARMORS, RINGS, RINGS): + if equipments[-1][0] != "" and equipments[-2] == equipments[-1]: + continue + + cost, player_attack, player_armor = ( + sum(equipment[1:][k] for equipment in equipments) for k in range(3) + ) + + if ceil(boss_hp / max(1, player_attack - boss_armor)) <= ceil( + player_hp / max(1, boss_attack - player_armor) + ): + min_cost = min(cost, min_cost) + else: + max_cost = max(cost, max_cost) + + +answer_1 = min_cost +print(f"answer 1 is {answer_1}") + +answer_2 = max_cost +print(f"answer 2 is {answer_2}") diff --git a/src/holt59/aoc/inputs/holt59/2015/day21.txt b/src/holt59/aoc/inputs/holt59/2015/day21.txt new file mode 100644 index 0000000..db43742 --- /dev/null +++ b/src/holt59/aoc/inputs/holt59/2015/day21.txt @@ -0,0 +1,3 @@ +Hit Points: 100 +Damage: 8 +Armor: 2