f | import math | f | import math |
n | | n | |
| | | |
| class Candy: | | class Candy: |
| def __init__(self, mass, uranium): | | def __init__(self, mass, uranium): |
| self.mass = mass | | self.mass = mass |
| self.uranium = uranium | | self.uranium = uranium |
| | | |
| def get_uranium_quantity(self): | | def get_uranium_quantity(self): |
| return self.mass * self.uranium | | return self.mass * self.uranium |
| | | |
| def get_mass(self): | | def get_mass(self): |
| return self.mass | | return self.mass |
| | | |
| | | |
| class Person: | | class Person: |
| def __init__(self, position): | | def __init__(self, position): |
| self.position = position | | self.position = position |
| | | |
| def get_position(self): | | def get_position(self): |
| return self.position | | return self.position |
| | | |
| def set_position(self, new_position): | | def set_position(self, new_position): |
| self.position = new_position | | self.position = new_position |
| | | |
| | | |
| class Kid(Person): | | class Kid(Person): |
| def __init__(self, position, initiative): | | def __init__(self, position, initiative): |
| super().__init__(position) | | super().__init__(position) |
| self.initiative = initiative | | self.initiative = initiative |
| self.candies = [] | | self.candies = [] |
| | | |
| def get_initiative(self): | | def get_initiative(self): |
| return self.initiative | | return self.initiative |
| | | |
| def add_candy(self, candy): | | def add_candy(self, candy): |
| self.candies.append(candy) | | self.candies.append(candy) |
| | | |
| def is_critical(self): | | def is_critical(self): |
| uranium_quantity = sum(candy.get_uranium_quantity() for candy in self.candies) | | uranium_quantity = sum(candy.get_uranium_quantity() for candy in self.candies) |
| return uranium_quantity > 20 | | return uranium_quantity > 20 |
| | | |
| | | |
| class Host(Person): | | class Host(Person): |
| def __init__(self, position, candies): | | def __init__(self, position, candies): |
| super().__init__(position) | | super().__init__(position) |
| self.candies = [Candy(mass, uranium) for mass, uranium in candies] | | self.candies = [Candy(mass, uranium) for mass, uranium in candies] |
| | | |
| def remove_candy(self, choose_candy): | | def remove_candy(self, choose_candy): |
| if not self.candies: | | if not self.candies: |
| return None | | return None |
| candy_to_remove = choose_candy(self.candies) | | candy_to_remove = choose_candy(self.candies) |
| self.candies.remove(candy_to_remove) | | self.candies.remove(candy_to_remove) |
| return candy_to_remove | | return candy_to_remove |
| | | |
| | | |
| class FluxCapacitor: | | class FluxCapacitor: |
| def __init__(self, participants): | | def __init__(self, participants): |
| self.participants = participants | | self.participants = participants |
| | | |
| def get_victim(self): | | def get_victim(self): |
| kids = [participant for participant in self.participants if isinstance(participant, Kid)] | | kids = [participant for participant in self.participants if isinstance(participant, Kid)] |
| hosts = [participant for participant in self.participants if isinstance(participant, Host)] | | hosts = [participant for participant in self.participants if isinstance(participant, Host)] |
| critical_kids = set() | | critical_kids = set() |
| while hosts and kids: | | while hosts and kids: |
| for kid in sorted(kids, key=lambda kid: kid.get_initiative(), reverse=True): | | for kid in sorted(kids, key=lambda kid: kid.get_initiative(), reverse=True): |
| min_distance = float('inf') | | min_distance = float('inf') |
| closest_host = None | | closest_host = None |
| for host in hosts: | | for host in hosts: |
| distance = math.sqrt((host.get_position()[0] - kid.get_position()[0])**2 + (host.get_position()[1] - kid.get_position()[1])**2) | | distance = math.sqrt((host.get_position()[0] - kid.get_position()[0])**2 + (host.get_position()[1] - kid.get_position()[1])**2) |
| if distance < min_distance: | | if distance < min_distance: |
| min_distance = distance | | min_distance = distance |
| closest_host = host | | closest_host = host |
| if closest_host: | | if closest_host: |
| selected_candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda candy: candy.get_mass())) | | selected_candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda candy: candy.get_mass())) |
| if selected_candy: | | if selected_candy: |
| kid.add_candy(selected_candy) | | kid.add_candy(selected_candy) |
| kid.set_position(closest_host.get_position()) | | kid.set_position(closest_host.get_position()) |
| if kid.is_critical(): | | if kid.is_critical(): |
| critical_kids.add(kid) | | critical_kids.add(kid) |
| hosts.remove(closest_host) | | hosts.remove(closest_host) |
n | if len(hosts) == 0: | n | if not hosts: |
| break | | break |
t | if critical_kids: | t | return critical_kids if critical_kids else None |
| return critical_kids | | |
| return None | | |