f | class Candy: | f | class Candy: |
n | | n | |
| 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: |
n | | n | |
| 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): |
n | | n | |
| URANIUM_SUM_LIMIT = 20 | | URANIUM_SUM_LIMIT = 20 |
| 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): |
| return sum(map(Candy.get_uranium_quantity, self._candies)) > self.URANIUM_SUM_LIMIT | | return sum(map(Candy.get_uranium_quantity, self._candies)) > self.URANIUM_SUM_LIMIT |
| | | |
| | | |
| class Host(Person): | | class Host(Person): |
n | | n | |
| def __init__(self, position, candies): | | def __init__(self, position, candies): |
| super().__init__(position) | | super().__init__(position) |
n | self._candies = [] | n | self._candies = [Candy(*candy) for candy in candies] |
| for candy_tuple in candies: | | |
| self._candies.append(Candy(candy_tuple[0], candy_tuple[1])) | | |
| | | |
| def remove_candy(self, function_predicate): | | def remove_candy(self, function_predicate): |
n | if len(self._candies) == 0: | n | if not self._candies: |
| return None | | return None |
| else: | | else: |
| candy_to_remove = function_predicate(self._candies) | | candy_to_remove = function_predicate(self._candies) |
| self._candies.remove(candy_to_remove) | | self._candies.remove(candy_to_remove) |
| return candy_to_remove | | return candy_to_remove |
| | | |
| @staticmethod | | @staticmethod |
| def get_max_candy(candies): | | def get_max_candy(candies): |
| return max(candies, key=lambda candy: candy._mass) | | return max(candies, key=lambda candy: candy._mass) |
| | | |
| | | |
| class FluxCapacitor: | | class FluxCapacitor: |
n | | n | |
| def __init__(self, participants): | | def __init__(self, participants): |
n | self._participants = participants | n | self._hosts = [] |
| | | self._kids = [] |
| | | for person in participants: |
| | | if type(person) is Host: |
| | | self._hosts.append(person) |
| | | else: |
| | | self._kids.append(person) |
| | | |
| def get_victim(self): | | def get_victim(self): |
n | hosts = [] | n | |
| kids = [] | | |
| for person in self._participants: | | |
| if person.__class__.__name__ == 'Host': | | |
| hosts.append(person) | | |
| else: | | |
| kids.append(person) | | |
| kids_data = list(map(list, map(lambda kid: [kid, hosts.copy()], kids))) | | kids_data = list(map(list, map(lambda kid: [kid, self._hosts.copy()], self._kids))) |
| | | |
| counter_all_hosts_already_visited = 0 | | counter_all_hosts_already_visited = 0 |
n | while counter_all_hosts_already_visited < len(hosts): | n | while counter_all_hosts_already_visited < len(self._hosts): |
| counter_all_hosts_already_visited += 1 | | counter_all_hosts_already_visited += 1 |
n | host_data = list(map(lambda host: [], hosts)) | n | host_data = list(map(lambda host: [], self._hosts)) |
| for kid_data in kids_data: | | for kid_data in kids_data: |
| closest_host = None | | closest_host = None |
| closest_host_dist = -1 | | closest_host_dist = -1 |
| curr_closest_host_index = 0 | | curr_closest_host_index = 0 |
| closet_host_index = 0 | | closet_host_index = 0 |
| for host in kid_data[1]: | | for host in kid_data[1]: |
| if host is None: | | if host is None: |
n | curr_closest_host_index+=1 | n | curr_closest_host_index += 1 |
| continue | | continue |
n | | n | |
| curr_dist_to_host = ((list(host._position)[0] - list(kid_data[0]._position)[0]) ** 2 | | curr_dist_to_host = ((list(host._position)[0] - list(kid_data[0]._position)[0]) ** 2 |
| + (list(host._position)[1] - list(kid_data[0]._position)[1]) ** 2) ** 0.5 | | + (list(host._position)[1] - list(kid_data[0]._position)[1]) ** 2) ** 0.5 |
n | | n | |
| if closest_host_dist == -1 or closest_host_dist > curr_dist_to_host: | | if closest_host_dist == -1 or closest_host_dist > curr_dist_to_host: |
| closest_host = host | | closest_host = host |
| closest_host_dist = curr_dist_to_host | | closest_host_dist = curr_dist_to_host |
| closet_host_index = curr_closest_host_index | | closet_host_index = curr_closest_host_index |
n | elif closest_host_dist == curr_dist_to_host \ | n | elif ((closest_host_dist == curr_dist_to_host) and |
| and (host._position[0] < closest_host._position[0] | | (host._position[0] < closest_host._position[0] or |
| or (host._position[0] == closest_host._position[0] | | (host._position[0] == closest_host._position[0] and |
| and host._position[1] < closest_host._position[1])): | | (host._position[1] < closest_host._position[1])))): |
| closest_host = host | | closest_host = host |
| closest_host_dist = curr_dist_to_host | | closest_host_dist = curr_dist_to_host |
| closet_host_index = curr_closest_host_index | | closet_host_index = curr_closest_host_index |
| curr_closest_host_index+=1 | | curr_closest_host_index+=1 |
| | | |
| kid_data[0]._position = closest_host._position | | kid_data[0]._position = closest_host._position |
| host_data[closet_host_index].append(kid_data[0]) | | host_data[closet_host_index].append(kid_data[0]) |
| kid_data[1][closet_host_index] = None | | kid_data[1][closet_host_index] = None |
| | | |
| counter_curr_host = 0 | | counter_curr_host = 0 |
| for kids_going_to in host_data: | | for kids_going_to in host_data: |
| kids_going_to.sort(reverse=True, key=lambda kid: kid._initiative) | | kids_going_to.sort(reverse=True, key=lambda kid: kid._initiative) |
| for kid in kids_going_to: | | for kid in kids_going_to: |
n | candy_to_add = hosts[counter_curr_host].remove_candy(Host.get_max_candy) | n | candy_to_add = self._hosts[counter_curr_host].remove_candy(Host.get_max_candy) |
| if candy_to_add is not None: | | if candy_to_add is not None: |
| kid.add_candy(candy_to_add) | | kid.add_candy(candy_to_add) |
| counter_curr_host+=1 | | counter_curr_host+=1 |
| kids_above_safe_uranium_level = set() | | kids_above_safe_uranium_level = set() |
n | for kid in kids: | n | for kid in self._kids: |
| if kid.is_critical(): | | if kid.is_critical(): |
| kids_above_safe_uranium_level.add(kid) | | kids_above_safe_uranium_level.add(kid) |
t | if(len(kids_above_safe_uranium_level) != 0): | t | if kids_above_safe_uranium_level: |
| return kids_above_safe_uranium_level | | return kids_above_safe_uranium_level |
| return None | | return None |