Created
August 17, 2023 22:39
-
-
Save Jaakkonen/b1e511e495ef704137d64805923da09d to your computer and use it in GitHub Desktop.
Antimatter dimensions challenge 9 script to avoid price conflicts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| increments = { | |
| "Tickspeed": 1, | |
| "1st": 3, | |
| "2nd": 4, | |
| "3rd": 5, | |
| "4th": 6, | |
| "5th": 8, | |
| "6th": 10, | |
| "7th": 12, | |
| "8th": 15, | |
| } | |
| prices = { | |
| "Tickspeed": 3, | |
| "1st": 1, | |
| "2nd": 2, | |
| "3rd": 4, | |
| "4th": 6, | |
| "5th": 9, | |
| "6th": 13, | |
| "7th": 18, | |
| "8th": 24, | |
| } | |
| unlocked = { | |
| "Tickspeed": False, | |
| "1st": True, | |
| "2nd": False, | |
| "3rd": False, | |
| "4th": False, | |
| "5th": False, | |
| "6th": False, | |
| "7th": False, | |
| "8th": False, | |
| } | |
| """ | |
| Purchasing 1st dimension unlocks 2nd dimension | |
| Purchasing 2nd dimension unlocks 3rd dimension and tickspeed | |
| Purchasing 3rd dimension unlocks 4th dimension | |
| Purchasing 4th dimension unlocks 5th dimension | |
| Purchasing 5th dimension unlocks 6th dimension | |
| Purchasing 6th dimension unlocks 7th dimension | |
| Purchasing 7th dimension unlocks 8th dimension | |
| After purchasing a dimension/tickspeed its price goes up by `increment` plus its previous price. | |
| After purchasing and increasing the prices & handling unlocks, there should be no items with the same price. | |
| """ | |
| # GOAL: Create an algorithm that purchases the cheapest item that is unlocked | |
| # and doesn't cause there to be two items with the same price. | |
| def find_new_prices( | |
| to_purchase: str, prices: dict[str, int], unlocked: dict[str, bool] | |
| ): | |
| """ | |
| Find the new prices after purchasing `to_purchase`. | |
| """ | |
| if to_purchase not in prices: | |
| raise ValueError(f"Item {to_purchase} is not in prices.") | |
| new_prices = prices.copy() | |
| new_unlocked = unlocked.copy() | |
| for item, price in prices.items(): | |
| if price == prices[to_purchase]: | |
| # All items with the same price have their price increased | |
| # by their corresponding increment | |
| new_prices[item] += increments[item] | |
| # Handle new unlocks | |
| match to_purchase: | |
| case "1st": | |
| new_unlocked["2nd"] = True | |
| case "2nd": | |
| new_unlocked["3rd"] = True | |
| new_unlocked["Tickspeed"] = True | |
| case "3rd": | |
| new_unlocked["4th"] = True | |
| case "4th": | |
| new_unlocked["5th"] = True | |
| case "5th": | |
| new_unlocked["6th"] = True | |
| case "6th": | |
| new_unlocked["7th"] = True | |
| case "7th": | |
| new_unlocked["8th"] = True | |
| return new_prices, new_unlocked | |
| def are_new_prices_legal(prices: dict[str, int]) -> bool: | |
| """ | |
| Checks if there is a duplicate price in the new prices. | |
| """ | |
| return len(prices) == len(set(prices.values())) | |
| def find_cheapest_and_update_prices( | |
| prices: dict[str, int], unlocked: dict[str, bool] | |
| ) -> tuple[str, dict[str, int], dict[str, bool]]: | |
| """ | |
| Finds the cheapest item to purchase that'd result in a legal set of prices. | |
| """ | |
| purchaseables = {name for name, unlocked in unlocked.items() if unlocked} | |
| # Sort the purchaseables by price in ascending order | |
| purchaseables = sorted(purchaseables, key=lambda x: prices[x]) | |
| for purchaseable in purchaseables: | |
| new_prices, new_unlocked = find_new_prices(purchaseable, prices, unlocked) | |
| if are_new_prices_legal(new_prices): | |
| return purchaseable, new_prices, new_unlocked | |
| else: | |
| raise ValueError("No legal purchaseables found.") | |
| first_items = [ | |
| "1st", | |
| "2nd", | |
| "3rd", # Forced buy (2nd upgrade to 1 same price as initial 3) | |
| "4th", # Forced buy (2nd upgrade to 2 same price as initial 4) | |
| "5th", # Forced buy (2nd upgrade to 3 same price as initial 5) | |
| ] | |
| def print_purchase_line(purchaseable: str, prices: dict[str, int]) -> None: | |
| # price = prices[purchaseable] | |
| print( | |
| f"\033[4m\033[1m{purchaseable:9}\033[0m \033[3m{prices[purchaseable]:4}\033[0m", | |
| end=" -- ", | |
| ) | |
| for item, price in prices.items(): | |
| print(f"{item} \033[3m{price:<7}\033[0m", end=" ") | |
| print() | |
| def print_purchase_line_waiting( | |
| prices: dict[str, int], unlocked: dict[str, bool], initial_purchases: list[str] = first_items | |
| ): | |
| """ | |
| Pretty-prints the item to-be purchased, the price and a list of all item prices | |
| using control codes. | |
| Format: | |
| [underline][bold]item[/underline] [italic]price[/italic][/bold] -- item [italic]item price[/italic] ... | |
| """ | |
| prices = prices.copy() | |
| for item in initial_purchases: | |
| print_purchase_line(item, prices) | |
| prices, unlocked = find_new_prices(item, prices, unlocked) | |
| while max(prices.values()) < 300: | |
| try: | |
| purchaseable, new_prices, new_unlocked = find_cheapest_and_update_prices( | |
| prices, | |
| unlocked | |
| ) | |
| except ValueError as e: | |
| print("===== WARNING: NO LEGAL PURCHASEABLES FOUND =====") | |
| print_purchase_line("1st", prices) | |
| raise e | |
| print_purchase_line(purchaseable, prices) | |
| prices = new_prices | |
| unlocked = new_unlocked | |
| # Wait for user to press enter | |
| if purchaseable != "Tickspeed": | |
| input() | |
| print("\033[F\033[K", end="") | |
| #print() | |
| # Remove the previous line | |
| print_purchase_line_waiting(prices, unlocked) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment