| f | | f | |
| | | |
| class Egg: | | class Egg: |
| def __init__(self): | | def __init__(self): |
| self.upper_colors = [] | | self.upper_colors = [] |
| self.upper_is_broken = False | | self.upper_is_broken = False |
| self.lower_colors = [] | | self.lower_colors = [] |
| self.lower_is_broken = False | | self.lower_is_broken = False |
| self.tournament: None | EggTournament = None | | self.tournament: None | EggTournament = None |
| | | |
| @property | | @property |
| def upper_hardness(self): | | def upper_hardness(self): |
| red = sum([int(color[0:2], 16) * percent / 50 for color, percent in self.upper_colors]) | | red = sum([int(color[0:2], 16) * percent / 50 for color, percent in self.upper_colors]) |
| green = sum([int(color[2:4], 16) * percent / 50 for color, percent in self.upper_colors]) | | green = sum([int(color[2:4], 16) * percent / 50 for color, percent in self.upper_colors]) |
| blue = sum([int(color[4:], 16) * percent / 50 for color, percent in self.upper_colors]) | | blue = sum([int(color[4:], 16) * percent / 50 for color, percent in self.upper_colors]) |
| return red + green + blue | | return red + green + blue |
| | | |
| @property | | @property |
| def lower_hardness(self): | | def lower_hardness(self): |
| red = sum([int(color[0:2], 16) * percent / 50 for color, percent in self.lower_colors]) | | red = sum([int(color[0:2], 16) * percent / 50 for color, percent in self.lower_colors]) |
| green = sum([int(color[2:4], 16) * percent / 50 for color, percent in self.lower_colors]) | | green = sum([int(color[2:4], 16) * percent / 50 for color, percent in self.lower_colors]) |
| blue = sum([int(color[4:], 16) * percent / 50 for color, percent in self.lower_colors]) | | blue = sum([int(color[4:], 16) * percent / 50 for color, percent in self.lower_colors]) |
| return red + green + blue | | return red + green + blue |
| | | |
| @property | | @property |
| def victories(self): | | def victories(self): |
| return list(self.tournament.fights.values()).count(self) if self.tournament else 0 | | return list(self.tournament.fights.values()).count(self) if self.tournament else 0 |
| | | |
| @property | | @property |
| def position(self): | | def position(self): |
| return next(self.tournament.ranking.index(x) for x in self.tournament.ranking if self in x) + 1 if self.tournament is not None else None | | return next(self.tournament.ranking.index(x) for x in self.tournament.ranking if self in x) + 1 if self.tournament is not None else None |
| | | |
| @staticmethod | | @staticmethod |
| def __get_percent(colors): | | def __get_percent(colors): |
| return sum([color[1] for color in colors]) | | return sum([color[1] for color in colors]) |
| | | |
| def paint(self, *colors): | | def paint(self, *colors): |
| if self.__get_percent(self.upper_colors + self.lower_colors + list(colors)) <= 100: | | if self.__get_percent(self.upper_colors + self.lower_colors + list(colors)) <= 100: |
| for color in colors: | | for color in colors: |
| if self.__get_percent(self.upper_colors) == 50: | | if self.__get_percent(self.upper_colors) == 50: |
| self.lower_colors.append(color) | | self.lower_colors.append(color) |
| else: | | else: |
| if self.__get_percent(self.upper_colors + [color]) <= 50: | | if self.__get_percent(self.upper_colors + [color]) <= 50: |
| self.upper_colors.append(color) | | self.upper_colors.append(color) |
| else: | | else: |
| self.lower_colors.append((color[0], color[1] - (50 - self.__get_percent(self.upper_colors)))) | | self.lower_colors.append((color[0], color[1] - (50 - self.__get_percent(self.upper_colors)))) |
| self.upper_colors.append((color[0], 50 - self.__get_percent(self.upper_colors))) | | self.upper_colors.append((color[0], 50 - self.__get_percent(self.upper_colors))) |
| else: | | else: |
| raise ValueError("Can't paint over 100%") | | raise ValueError("Can't paint over 100%") |
| | | |
| def __mul__(self, other): | | def __mul__(self, other): |
| if not self.upper_is_broken and not other.upper_is_broken: | | if not self.upper_is_broken and not other.upper_is_broken: |
| if (self.upper_hardness > other.upper_hardness) or (self.upper_colors and not other.upper_colors): | | if (self.upper_hardness > other.upper_hardness) or (self.upper_colors and not other.upper_colors): |
| other.upper_is_broken = True | | other.upper_is_broken = True |
| if self.tournament == other.tournament and self.tournament: | | if self.tournament == other.tournament and self.tournament: |
| self.tournament.fights[(self, other, "top")] = self | | self.tournament.fights[(self, other, "top")] = self |
| return self | | return self |
| self.upper_is_broken = True | | self.upper_is_broken = True |
| if self.tournament == other.tournament and self.tournament: | | if self.tournament == other.tournament and self.tournament: |
| self.tournament.fights[(self, other, "top")] = other | | self.tournament.fights[(self, other, "top")] = other |
| return other | | return other |
| raise TypeError("An egg is broken on that side") | | raise TypeError("An egg is broken on that side") |
| | | |
| def __matmul__(self, other): | | def __matmul__(self, other): |
| if not self.lower_is_broken and not other.lower_is_broken: | | if not self.lower_is_broken and not other.lower_is_broken: |
| if (self.lower_hardness > other.lower_hardness) or (self.lower_colors and not other.lower_colors): | | if (self.lower_hardness > other.lower_hardness) or (self.lower_colors and not other.lower_colors): |
| other.lower_is_broken = True | | other.lower_is_broken = True |
| if self.tournament == other.tournament and self.tournament: | | if self.tournament == other.tournament and self.tournament: |
| self.tournament.fights[(self, other, "bottom")] = self | | self.tournament.fights[(self, other, "bottom")] = self |
| return self | | return self |
| self.lower_is_broken = True | | self.lower_is_broken = True |
| if self.tournament == other.tournament and self.tournament: | | if self.tournament == other.tournament and self.tournament: |
| self.tournament.fights[(self, other, "bottom")] = other | | self.tournament.fights[(self, other, "bottom")] = other |
| return other | | return other |
| raise TypeError("An egg is broken on that side") | | raise TypeError("An egg is broken on that side") |
| | | |
| # def __repr__(self): | | # def __repr__(self): |
| # return f"upp {self.upper_hardness}|{self.upper_is_broken} - low {self.lower_hardness}|{self.lower_is_broken}" | | # return f"upp {self.upper_hardness}|{self.upper_is_broken} - low {self.lower_hardness}|{self.lower_is_broken}" |
| | | |
| | | |
| class EggTournament: | | class EggTournament: |
| def __init__(self): | | def __init__(self): |
| self.eggs: dict[str, Egg] = {} | | self.eggs: dict[str, Egg] = {} |
| self.fights = {} | | self.fights = {} |
| | | |
| @property | | @property |
| def ranking(self): | | def ranking(self): |
| ranking_points = {egg:egg.victories for egg in self.eggs.values()} | | ranking_points = {egg:egg.victories for egg in self.eggs.values()} |
| ranking = [] | | ranking = [] |
| for egg, points in sorted(ranking_points.items(), key=lambda egg: -egg[1]): | | for egg, points in sorted(ranking_points.items(), key=lambda egg: -egg[1]): |
| if not ranking: | | if not ranking: |
| ranking += [[egg]] | | ranking += [[egg]] |
| continue | | continue |
| if ranking_points[ranking[-1][-1]] == points: | | if ranking_points[ranking[-1][-1]] == points: |
| ranking[-1].append(egg) | | ranking[-1].append(egg) |
| else: | | else: |
| ranking.append([egg]) | | ranking.append([egg]) |
| return ranking | | return ranking |
| | | |
| def register(self, egg:Egg, name: str): | | def register(self, egg:Egg, name: str): |
| if egg.tournament: | | if egg.tournament: |
| raise ValueError("An egg cannot be registered in multiple tournaments") | | raise ValueError("An egg cannot be registered in multiple tournaments") |
| | | |
| try: | | try: |
| t | exec(f"{name} = 0") | t | exec(f"{name} = 0\ndel {name}") |
| except SyntaxError: | | except SyntaxError: |
| raise ValueError("Invalid registration name") | | raise ValueError("Invalid registration name") |
| | | |
| if name in self.eggs.keys(): | | if name in self.eggs.keys(): |
| raise ValueError(f"Egg with name {name} has already been registered") | | raise ValueError(f"Egg with name {name} has already been registered") |
| | | |
| self.eggs[name] = egg | | self.eggs[name] = egg |
| egg.tournament = self | | egg.tournament = self |
| | | |
| def __getitem__(self, key): | | def __getitem__(self, key): |
| if not isinstance(key, slice): | | if not isinstance(key, slice): |
| first, second, side = key | | first, second, side = key |
| else: | | else: |
| first = key.start | | first = key.start |
| second = key.stop | | second = key.stop |
| side = key.step | | side = key.step |
| | | |
| try: | | try: |
| return self.fights[(first, second, side)] | | return self.fights[(first, second, side)] |
| except KeyError: | | except KeyError: |
| pass | | pass |
| | | |
| try: | | try: |
| return self.fights[(second, first, side)] | | return self.fights[(second, first, side)] |
| except KeyError: | | except KeyError: |
| pass | | pass |
| | | |
| raise KeyError("No such fight") | | raise KeyError("No such fight") |
| | | |
| def __rmatmul__(self, other): | | def __rmatmul__(self, other): |
| try: | | try: |
| return set(self.ranking[other - 1]) if len(self.ranking[other - 1]) > 1 else self.ranking[other - 1][0] | | return set(self.ranking[other - 1]) if len(self.ranking[other - 1]) > 1 else self.ranking[other - 1][0] |
| except IndexError: | | except IndexError: |
| raise IndexError("No such position in the tournament") | | raise IndexError("No such position in the tournament") |
| | | |
| def __getattr__(self, name: str): | | def __getattr__(self, name: str): |
| try: | | try: |
| return {"position": self.eggs[name].position, "victories": self.eggs[name].victories} | | return {"position": self.eggs[name].position, "victories": self.eggs[name].victories} |
| except KeyError: | | except KeyError: |
| raise AttributeError("Apologies, there is no such egg registered") | | raise AttributeError("Apologies, there is no such egg registered") |
| | | |
| def __contains__(self, item): | | def __contains__(self, item): |
| return True if item in self.eggs.values() else False | | return True if item in self.eggs.values() else False |
| | | |
15.04.2026 17:11
15.04.2026 17:02
15.04.2026 17:04
15.04.2026 17:05
15.04.2026 17:11