Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- from itertools import combinations
- with open("testinput.txt") as f:
- input_lines = f.read().splitlines()
- input_len = len(input_lines)
- idx = 1
- # find all weapons
- input_weapons = []
- for i in range(idx, input_len):
- if input_lines[i] == '':
- idx = i + 2
- break
- else:
- input_weapons.append([int(x) for x in re.findall("\d+", input_lines[i])])
- # find all armors
- # list starts off with [0, 0, 0], representing not picking armor
- input_armors = [[0, 0, 0]]
- for i in range(idx, input_len):
- if input_lines[i] == '':
- idx = i + 2
- break
- else:
- input_armors.append([int(x) for x in re.findall("\d+", input_lines[i])])
- # find all cost, damage and armor values of the rings.
- # [0, 0, 0] represents not picking a ring
- input_rings = [[0, 0, 0], [0, 0, 0]]
- for i in range(idx, input_len):
- if input_lines[i] == '':
- idx = i + 2
- break
- else:
- input_rings.append([int(x) for x in re.findall("\d+", input_lines[i])[1:]])
- # find every combination of two picks in the rings list.
- # this is why it starts off with two [0, 0, 0]. One possible combination is not picking a ring at all.
- input_rings = list(combinations(input_rings, 2))
- # helper function to see who wins with the selected loadout
- def check_victory(player_dmg: int, player_arm: int) -> bool:
- player_hp = 100
- boss_hp, boss_dmg, boss_arm = 100, 8, 2
- while player_hp > 0:
- boss_hp -= player_dmg - boss_arm
- if boss_hp <= 0:
- break
- boss_hit = boss_dmg - player_arm
- if boss_hit <= 0:
- player_hp -= 1
- else:
- player_hp -= boss_hit
- return player_hp > 0
- def cheapest_loadout(weapons: list[list[int]], armors: list[list[int]], rings: list[tuple[list[int]]]):
- output = float('inf')
- # iterate through every possible loadout. Use check_victory to see if it wins
- # if it does and the total_cost is lower than the current output, update output
- for weapon in weapons:
- for armor in armors:
- for ring in rings:
- total_cost = weapon[0] + armor[0] + ring[0][0] + ring[1][0]
- if check_victory(weapon[1] + ring[0][1] + ring[1][1], armor[2] + ring[0][2] + ring[1][2]):
- output = min(output, total_cost)
- return output
- print(cheapest_loadout(input_weapons, input_armors, input_rings))
- # we do the same thing as in cheapest_loadout(), but instead we check for losses and maximize output
- def most_expensive_loadout(weapons: list[list[int]], armors: list[list[int]], rings: list[tuple[list[int]]]):
- output = float('-inf')
- for weapon in weapons:
- for armor in armors:
- for ring in rings:
- total_cost = weapon[0] + armor[0] + ring[0][0] + ring[1][0]
- if not check_victory(weapon[1] + ring[0][1] + ring[1][1], armor[2] + ring[0][2] + ring[1][2]):
- output = max(output, total_cost)
- return output
- print(most_expensive_loadout(input_weapons, input_armors, input_rings))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement