f | class Candy: | f | 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): |
| uranium_quantity = self.mass * self.uranium | | uranium_quantity = self.mass * self.uranium |
| return uranium_quantity | | return uranium_quantity |
| | | |
| 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 |
n | self.candy_basket = [] # to store candies | n | self.candy_basket = [] |
| | | |
| def get_initiative(self): | | def get_initiative(self): |
| return self.initiative | | return self.initiative |
| | | |
| def add_candy(self, candy_instance): | | def add_candy(self, candy_instance): |
| self.candy_basket.append(candy_instance) | | self.candy_basket.append(candy_instance) |
| | | |
| def is_critical(self): | | def is_critical(self): |
| total_uranium = sum(candy.get_uranium_quantity() for candy in self.candy_basket) | | total_uranium = sum(candy.get_uranium_quantity() for candy in self.candy_basket) |
| | | |
| return total_uranium > 20 | | return total_uranium > 20 |
| | | |
| | | |
| class Host(Person): | | class Host(Person): |
| def __init__(self, position, candies): | | def __init__(self, position, candies): |
| super().__init__(position) | | super().__init__(position) |
n | self.candy_basket = [] # to store candies | n | self.candy_basket = [] |
| | | |
| for candy_args in candies: | | for candy_args in candies: |
| candy_instance = Candy(*candy_args) | | candy_instance = Candy(*candy_args) |
| self.candy_basket.append(candy_instance) | | self.candy_basket.append(candy_instance) |
| | | |
| def remove_candy(self, selection_function): | | def remove_candy(self, selection_function): |
| if not self.candy_basket: | | if not self.candy_basket: |
| return None | | return None |
| else: | | else: |
| selected_candy = selection_function(self.candy_basket) | | selected_candy = selection_function(self.candy_basket) |
| | | |
| self.candy_basket.remove(selected_candy) | | self.candy_basket.remove(selected_candy) |
| | | |
| return selected_candy | | return selected_candy |
| | | |
| def max_candy_mass(candies): | | def max_candy_mass(candies): |
| return max(candies, key=lambda candy: candy.get_mass()) | | return max(candies, key=lambda candy: candy.get_mass()) |
n | | n | |
| 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): |
| while True: | | while True: |
| next_round_kids = set() | | next_round_kids = set() |
| victims = set() | | victims = set() |
| all_visited_hosts = set() | | all_visited_hosts = set() |
| | | |
n | # Process kids' interactions with hosts | n | |
| for kid in self.participants: | | for kid in self.participants: |
| if isinstance(kid, Kid): | | if isinstance(kid, Kid): |
| if kid in all_visited_hosts: | | if kid in all_visited_hosts: |
n | # kids that have visited all hosts - skip | n | |
| continue | | continue |
| | | |
| closest_host = None | | closest_host = None |
| closest_distance = float('inf') | | closest_distance = float('inf') |
| | | |
| for host in self.participants: | | for host in self.participants: |
| if isinstance(host, Host) and host not in all_visited_hosts: | | if isinstance(host, Host) and host not in all_visited_hosts: |
| distance = abs(kid.get_position()[0] - host.get_position()[0]) + abs(kid.get_position()[1] - host.get_position()[1]) | | distance = abs(kid.get_position()[0] - host.get_position()[0]) + abs(kid.get_position()[1] - host.get_position()[1]) |
| if distance < closest_distance: | | if distance < closest_distance: |
| closest_host = host | | closest_host = host |
| closest_distance = distance | | closest_distance = distance |
| | | |
| if closest_host is not None: | | if closest_host is not None: |
| candy = closest_host.remove_candy(max_candy_mass) | | candy = closest_host.remove_candy(max_candy_mass) |
| if candy: | | if candy: |
| kid.add_candy(candy) | | kid.add_candy(candy) |
| if kid.is_critical(): | | if kid.is_critical(): |
| victims.add(kid) | | victims.add(kid) |
| all_visited_hosts.add(closest_host) | | all_visited_hosts.add(closest_host) |
| next_round_kids.add(kid) | | next_round_kids.add(kid) |
| else: | | else: |
n | all_visited_hosts = set(self.participants) # all hosts visited | n | all_visited_hosts = set(self.participants) |
| | | |
| if not next_round_kids: | | if not next_round_kids: |
n | # all kids visited all hosts; no one reached critical mass | n | |
| return None | | return None |
| | | |
| if victims: | | if victims: |
| return victims | | return victims |
| else: | | else: |
| self.participants = next_round_kids | | self.participants = next_round_kids |
| | | |
| | | |
| candy = Candy(20, 0.3) | | candy = Candy(20, 0.3) |
| person = Person((1, 2)) | | person = Person((1, 2)) |
| kid = Kid((0, 0), 123) | | kid = Kid((0, 0), 123) |
| host = Host((3, 4), [(1, 1.0), (2, 0.5)]) | | host = Host((3, 4), [(1, 1.0), (2, 0.5)]) |
| flux_capacitor = FluxCapacitor({kid, host}) | | flux_capacitor = FluxCapacitor({kid, host}) |
| | | |
t | # get victims | t | |
| victims = flux_capacitor.get_victim() | | victims = flux_capacitor.get_victim() |
| | | |
| if victims: | | if victims: |
| print("Victims found:") | | print("Victims found:") |
| for victim in victims: | | for victim in victims: |
| print(victim.get_position()) | | print(victim.get_position()) |
| else: | | else: |
| print("No victims found.") | | print("No victims found.") |
| | | |
07.11.2023 09:27
07.11.2023 09:27
07.11.2023 09:28
07.11.2023 09:28
07.11.2023 09:28
07.11.2023 09:29
07.11.2023 09:31