f | import math | f | import math |
| | | |
| | | |
| class Candy: | | class Candy: |
n | def __init__(self, mass, uranium ): | n | 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 |
| self.basket_of_sweets = [] | | self.basket_of_sweets = [] |
| | | |
| 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 |
| return self.position | | return self.position |
| | | |
| | | |
| class Kid(Person): | | class Kid(Person): |
n | def __init__ (self, position, initiative): | n | def __init__(self, position, initiative): |
| super().__init__(position) | | super().__init__(position) |
| self.initiative = initiative | | self.initiative = initiative |
| self.history_of_positions = [] | | self.history_of_positions = [] |
| | | |
| def get_initiative(self): | | def get_initiative(self): |
| return self.initiative | | return self.initiative |
| | | |
n | def add_candy(self, sweet ): | n | def add_candy(self, sweet): |
| self.basket_of_sweets.append(sweet) | | self.basket_of_sweets.append(sweet) |
| | | |
| def is_critical(self): | | def is_critical(self): |
n | self.history_of_positions.append(self.get_position()) | n | |
| uranium_q_ty = 0 | | uranium_q_ty = 0 |
n | | n | critical_uranium_q_ty = 20 |
| | | |
| for sweet in self.basket_of_sweets: | | for sweet in self.basket_of_sweets: |
| uranium_q_ty += sweet.get_uranium_quantity() | | uranium_q_ty += sweet.get_uranium_quantity() |
n | if uranium_q_ty > 20: | n | return uranium_q_ty > critical_uranium_q_ty |
| return True | | |
| else: | | |
| return False | | |
| | | |
n | | n | |
| def biggest_sweet(basket_of_sweets): | | |
| sweet_mass = 0 | | |
| biggest_sweet = 0 | | |
| | | |
| for candy in basket_of_sweets: | | |
| if candy.mass > sweet_mass: | | |
| sweet_mass = candy.mass | | |
| biggest_sweet = candy | | |
| | | |
| return biggest_sweet | | |
| | | |
| class Host(Person): | | class Host(Person): |
| def __init__(self, position, candies): | | def __init__(self, position, candies): |
| super().__init__(position) | | super().__init__(position) |
| self.candies = candies | | self.candies = candies |
| | | |
| for candy in self.candies: | | for candy in self.candies: |
n | self.basket_of_sweets.append(Candy(candy[0], candy[1])) | n | self.basket_of_sweets.append(Candy(*candy)) |
| | | |
n | | n | |
| def remove_candy (self, func): | | def remove_candy(self, func): |
| if not self.basket_of_sweets: | | if not self.basket_of_sweets: |
| return None | | return None |
| candy = func(self.basket_of_sweets) | | candy = func(self.basket_of_sweets) |
n | | n | |
| self.basket_of_sweets.remove(candy) | | self.basket_of_sweets.remove(candy) |
| return candy | | return candy |
| | | |
n | | n | |
| def distance_between_two_points(point_A, point_B): | | |
| return math.sqrt(math.pow(point_B[0] - point_A[0], 2) + math.pow(point_B[1] - point_A[1], 2)) | | |
| | | |
| | | |
| class FluxCapacitor: | | class FluxCapacitor: |
| def __init__(self, participants): | | def __init__(self, participants): |
| self.participants = participants | | self.participants = participants |
| self.kids = [] | | self.kids = [] |
| self.hosts = [] | | self.hosts = [] |
| | | |
n | while participants: | n | |
| person = participants.pop() | | for person in participants: |
| if type(person) is Kid: | | if type(person) is Kid: |
| self.kids.append(person) | | self.kids.append(person) |
| elif type(person) is Host: | | elif type(person) is Host: |
| self.hosts.append(person) | | self.hosts.append(person) |
| | | |
| def set_new_position(self): | | def set_new_position(self): |
| for kid in self.kids: | | for kid in self.kids: |
| distance = 0 | | distance = 0 |
n | min_distance = float('inf') | n | |
| new_position = () | | new_position = () |
n | | n | list_of_distances_and_positions = [] |
| for host in self.hosts: | | for host in self.hosts: |
| if host.get_position() in kid.history_of_positions: | | if host.get_position() in kid.history_of_positions: |
| continue | | continue |
n | distance = distance_between_two_points(kid.get_position(), host.get_position()) | n | distance = math.dist(kid.get_position(), host.get_position()) |
| if min_distance > distance: | | list_of_distances_and_positions.append((distance, host.get_position())) |
| min_distance = distance | | |
| new_position = host.get_position() | | new_position = min(list_of_distances_and_positions)[1] |
| elif min_distance == distance: | | kid.set_position(new_position) |
| if new_position[0] > host.get_position()[0]: | | kid.history_of_positions.append(kid.get_position()) |
| new_position = host.get_position() | | |
| elif new_position[1] > host.get_position()[1]: | | |
| new_position = host.get_position() | | |
| | | |
n | kid.set_position(new_position) | n | @staticmethod |
| | | def biggest_sweet(basket_of_sweets): |
| | | biggest_sweet = max(basket_of_sweets, key=lambda x: x.mass) |
| | | return biggest_sweet |
| | | |
| def recieve_candy(self): | | def recieve_candy(self): |
| for host in self.hosts: | | for host in self.hosts: |
| kids_with_this_host = [] | | kids_with_this_host = [] |
| | | |
| for kid in self.kids: | | for kid in self.kids: |
| if kid.get_position() == host.get_position(): | | if kid.get_position() == host.get_position(): |
| kids_with_this_host.append(kid) | | kids_with_this_host.append(kid) |
| | | |
n | kids_with_this_host.sort(key = lambda x: x.initiative, reverse = True) | n | kids_with_this_host.sort(key=lambda x: x.initiative, reverse = True) |
| | | |
| for kid in kids_with_this_host: | | for kid in kids_with_this_host: |
n | sweet = host.remove_candy(biggest_sweet) | n | sweet = host.remove_candy(self.biggest_sweet) |
| if sweet: | | if sweet: |
| kid.add_candy(sweet) | | kid.add_candy(sweet) |
| | | |
| def take_kids_to_heaven(self): | | def take_kids_to_heaven(self): |
| kids_in_heaven = set() | | kids_in_heaven = set() |
| | | |
| for kid in self.kids: | | for kid in self.kids: |
| if kid.is_critical(): | | if kid.is_critical(): |
| kids_in_heaven.add(kid) | | kids_in_heaven.add(kid) |
| | | |
| return kids_in_heaven | | return kids_in_heaven |
| | | |
n | | n | |
| def get_victim(self): | | def get_victim(self): |
| for host in self.hosts: | | for host in self.hosts: |
| self.set_new_position() | | self.set_new_position() |
| self.recieve_candy() | | self.recieve_candy() |
| kids_in_heaven = self.take_kids_to_heaven() | | kids_in_heaven = self.take_kids_to_heaven() |
| | | |
| if kids_in_heaven: | | if kids_in_heaven: |
| return kids_in_heaven | | return kids_in_heaven |
| | | |
t | return None | t | return None |