1from math import sqrt
2
3
4class Candy:
5
6 def __init__(self, mass, uranium):
7 self.mass = mass
8 self.uranium = uranium
9
10 def get_uranium_quantity(self):
11 return self.mass * self.uranium
12
13 def get_mass(self):
14 return self.mass
15
16
17class Person:
18
19 def __init__(self, position):
20 self.position = position
21
22 def get_position(self):
23 return self.position
24
25 def set_position(self, new_position):
26 self.position = new_position
27
28
29class Kid(Person):
30
31
32 def __init__(self, position, initiative):
33 super().__init__(position)
34 self.initiative = initiative
35 self.basket = []
36 self.CRITICAL_LEVEL_URANIUM = 20
37
38 def get_initiative(self):
39 return self.initiative
40
41 def add_candy(self, candy):
42 print(candy.mass, candy.uranium)
43 self.basket.append(candy)
44
45 def is_critical(self):
46 return sum(candy.get_uranium_quantity() for candy in self.basket) > self.CRITICAL_LEVEL_URANIUM
47
48
49class Host(Person):
50 def __init__(self, position, candies):
51 super().__init__(position)
52 self.candies = [Candy(mass, uranium) for mass,uranium in candies]
53
54 def remove_candy(self, func):
55 if not self.candies:
56 return None
57
58 selected_candy = func(self.candies)
59 self.candies.remove(selected_candy)
60 return selected_candy
61
62
63class FluxCapacitor:
64 def __init__(self, participants):
65 self.kids = []
66 self.hosts = []
67
68 for participant in participants:
69 if type(participant) == Kid:
70 self.kids.append(participant)
71 else:
72 self.hosts.append(participant)
73
74 self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True)
75
76 def get_victim(self):
77 critical_kids = set()
78 closest_host = None
79 round = 0
80 visited = [[False for _ in self.kids] for _ in self.hosts]
81
82 while len(critical_kids) == 0 and round < len(self.hosts):
83 for kid in self.kids:
84 min_distance = float('inf')
85 same_distance_hosts = []
86 for host in self.hosts:
87 if visited[self.hosts.index(host)][self.kids.index(kid)]:
88 continue
89 distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2)
90 if distance < min_distance:
91 min_distance = distance
92 closest_host = host
93 same_distance_hosts = [host]
94 elif distance == min_distance:
95 same_distance_hosts.append(host)
96 if len(same_distance_hosts) > 1:
97 closest_host = min(same_distance_hosts, key=lambda host: host.get_position())
98
99 visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True
100 kid.set_position(closest_host.get_position())
101
102 if len(closest_host.candies) != 0:
103 kid.add_candy(closest_host.remove_candy(FluxCapacitor.find_max_element_in_list_by_criteria))
104
105 if kid.is_critical():
106 critical_kids.add(kid)
107 round += 1
108
109 return critical_kids or None
110
111
112 def find_max_element_in_list_by_criteria(list):
113 return max(list, key=lambda candy: candy.get_mass())
............
----------------------------------------------------------------------
Ran 12 tests in 0.001s
OK
f | 1 | from math import sqrt | f | 1 | from math import sqrt |
n | n | 2 | |||
2 | 3 | ||||
3 | class Candy: | 4 | class Candy: | ||
4 | 5 | ||||
5 | def __init__(self, mass, uranium): | 6 | def __init__(self, mass, uranium): | ||
6 | self.mass = mass | 7 | self.mass = mass | ||
7 | self.uranium = uranium | 8 | self.uranium = uranium | ||
8 | 9 | ||||
9 | def get_uranium_quantity(self): | 10 | def get_uranium_quantity(self): | ||
10 | return self.mass * self.uranium | 11 | return self.mass * self.uranium | ||
11 | 12 | ||||
12 | def get_mass(self): | 13 | def get_mass(self): | ||
13 | return self.mass | 14 | return self.mass | ||
14 | 15 | ||||
n | n | 16 | |||
15 | class Person: | 17 | class Person: | ||
16 | 18 | ||||
17 | def __init__(self, position): | 19 | def __init__(self, position): | ||
18 | self.position = position | 20 | self.position = position | ||
19 | 21 | ||||
20 | def get_position(self): | 22 | def get_position(self): | ||
21 | return self.position | 23 | return self.position | ||
22 | 24 | ||||
23 | def set_position(self, new_position): | 25 | def set_position(self, new_position): | ||
24 | self.position = new_position | 26 | self.position = new_position | ||
25 | 27 | ||||
n | n | 28 | |||
26 | class Kid(Person): | 29 | class Kid(Person): | ||
27 | 30 | ||||
n | n | 31 | |||
28 | def __init__(self, position, initiative): | 32 | def __init__(self, position, initiative): | ||
29 | super().__init__(position) | 33 | super().__init__(position) | ||
30 | self.initiative = initiative | 34 | self.initiative = initiative | ||
31 | self.basket = [] | 35 | self.basket = [] | ||
n | n | 36 | self.CRITICAL_LEVEL_URANIUM = 20 | ||
32 | 37 | ||||
33 | def get_initiative(self): | 38 | def get_initiative(self): | ||
34 | return self.initiative | 39 | return self.initiative | ||
35 | 40 | ||||
36 | def add_candy(self, candy): | 41 | def add_candy(self, candy): | ||
37 | print(candy.mass, candy.uranium) | 42 | print(candy.mass, candy.uranium) | ||
38 | self.basket.append(candy) | 43 | self.basket.append(candy) | ||
39 | 44 | ||||
40 | def is_critical(self): | 45 | def is_critical(self): | ||
n | 41 | return sum(candy.get_uranium_quantity() for candy in self.basket) > 20 | n | 46 | return sum(candy.get_uranium_quantity() for candy in self.basket) > self.CRITICAL_LEVEL_URANIUM |
47 | |||||
42 | 48 | ||||
43 | class Host(Person): | 49 | class Host(Person): | ||
44 | def __init__(self, position, candies): | 50 | def __init__(self, position, candies): | ||
45 | super().__init__(position) | 51 | super().__init__(position) | ||
46 | self.candies = [Candy(mass, uranium) for mass,uranium in candies] | 52 | self.candies = [Candy(mass, uranium) for mass,uranium in candies] | ||
47 | 53 | ||||
48 | def remove_candy(self, func): | 54 | def remove_candy(self, func): | ||
49 | if not self.candies: | 55 | if not self.candies: | ||
50 | return None | 56 | return None | ||
51 | 57 | ||||
52 | selected_candy = func(self.candies) | 58 | selected_candy = func(self.candies) | ||
53 | self.candies.remove(selected_candy) | 59 | self.candies.remove(selected_candy) | ||
54 | return selected_candy | 60 | return selected_candy | ||
55 | 61 | ||||
56 | 62 | ||||
57 | class FluxCapacitor: | 63 | class FluxCapacitor: | ||
58 | def __init__(self, participants): | 64 | def __init__(self, participants): | ||
59 | self.kids = [] | 65 | self.kids = [] | ||
60 | self.hosts = [] | 66 | self.hosts = [] | ||
61 | 67 | ||||
62 | for participant in participants: | 68 | for participant in participants: | ||
63 | if type(participant) == Kid: | 69 | if type(participant) == Kid: | ||
64 | self.kids.append(participant) | 70 | self.kids.append(participant) | ||
n | n | 71 | else: | ||
72 | self.hosts.append(participant) | ||||
65 | 73 | ||||
n | 66 | self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True) | n | 74 | self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True) |
67 | |||||
68 | for participant in participants: | ||||
69 | if type(participant) == Host: | ||||
70 | self.hosts.append(participant) | ||||
71 | 75 | ||||
72 | def get_victim(self): | 76 | def get_victim(self): | ||
73 | critical_kids = set() | 77 | critical_kids = set() | ||
74 | closest_host = None | 78 | closest_host = None | ||
n | 75 | same_distance_hosts = [] | n | ||
76 | round = 0 | 79 | round = 0 | ||
77 | visited = [[False for _ in self.kids] for _ in self.hosts] | 80 | visited = [[False for _ in self.kids] for _ in self.hosts] | ||
78 | 81 | ||||
79 | while len(critical_kids) == 0 and round < len(self.hosts): | 82 | while len(critical_kids) == 0 and round < len(self.hosts): | ||
80 | for kid in self.kids: | 83 | for kid in self.kids: | ||
81 | min_distance = float('inf') | 84 | min_distance = float('inf') | ||
n | n | 85 | same_distance_hosts = [] | ||
82 | for host in self.hosts: | 86 | for host in self.hosts: | ||
83 | if visited[self.hosts.index(host)][self.kids.index(kid)]: | 87 | if visited[self.hosts.index(host)][self.kids.index(kid)]: | ||
84 | continue | 88 | continue | ||
85 | distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2) | 89 | distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2) | ||
86 | if distance < min_distance: | 90 | if distance < min_distance: | ||
87 | min_distance = distance | 91 | min_distance = distance | ||
88 | closest_host = host | 92 | closest_host = host | ||
89 | same_distance_hosts = [host] | 93 | same_distance_hosts = [host] | ||
90 | elif distance == min_distance: | 94 | elif distance == min_distance: | ||
91 | same_distance_hosts.append(host) | 95 | same_distance_hosts.append(host) | ||
92 | if len(same_distance_hosts) > 1: | 96 | if len(same_distance_hosts) > 1: | ||
93 | closest_host = min(same_distance_hosts, key=lambda host: host.get_position()) | 97 | closest_host = min(same_distance_hosts, key=lambda host: host.get_position()) | ||
94 | 98 | ||||
n | 95 | if len(closest_host.candies) == 0: | n | 99 | visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True |
96 | continue | 100 | kid.set_position(closest_host.get_position()) | ||
97 | 101 | ||||
n | n | 102 | if len(closest_host.candies) != 0: | ||
98 | kid.add_candy(closest_host.remove_candy(find_max_element_in_list_by_criteria)) | 103 | kid.add_candy(closest_host.remove_candy(FluxCapacitor.find_max_element_in_list_by_criteria)) | ||
99 | kid.set_position(closest_host.get_position()) | ||||
100 | visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True | ||||
101 | same_distance_hosts = [] | ||||
102 | 104 | ||||
103 | if kid.is_critical(): | 105 | if kid.is_critical(): | ||
104 | critical_kids.add(kid) | 106 | critical_kids.add(kid) | ||
105 | round += 1 | 107 | round += 1 | ||
106 | 108 | ||||
n | 107 | if len(critical_kids) == 0: | n | ||
108 | return None | ||||
109 | return critical_kids | 109 | return critical_kids or None | ||
110 | 110 | ||||
t | t | 111 | |||
111 | def find_max_element_in_list_by_criteria(list): | 112 | def find_max_element_in_list_by_criteria(list): | ||
112 | return max(list, key=lambda candy: candy.get_mass()) | 113 | return max(list, key=lambda candy: candy.get_mass()) | ||
113 | 114 | ||||
114 | 115 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | from math import sqrt | f | 1 | from math import sqrt |
2 | 2 | ||||
3 | class Candy: | 3 | class Candy: | ||
4 | 4 | ||||
5 | def __init__(self, mass, uranium): | 5 | def __init__(self, mass, uranium): | ||
6 | self.mass = mass | 6 | self.mass = mass | ||
7 | self.uranium = uranium | 7 | self.uranium = uranium | ||
8 | 8 | ||||
9 | def get_uranium_quantity(self): | 9 | def get_uranium_quantity(self): | ||
10 | return self.mass * self.uranium | 10 | return self.mass * self.uranium | ||
11 | 11 | ||||
12 | def get_mass(self): | 12 | def get_mass(self): | ||
13 | return self.mass | 13 | return self.mass | ||
14 | 14 | ||||
15 | class Person: | 15 | class Person: | ||
16 | 16 | ||||
17 | def __init__(self, position): | 17 | def __init__(self, position): | ||
18 | self.position = position | 18 | self.position = position | ||
19 | 19 | ||||
20 | def get_position(self): | 20 | def get_position(self): | ||
21 | return self.position | 21 | return self.position | ||
22 | 22 | ||||
23 | def set_position(self, new_position): | 23 | def set_position(self, new_position): | ||
24 | self.position = new_position | 24 | self.position = new_position | ||
25 | 25 | ||||
26 | class Kid(Person): | 26 | class Kid(Person): | ||
27 | 27 | ||||
28 | def __init__(self, position, initiative): | 28 | def __init__(self, position, initiative): | ||
29 | super().__init__(position) | 29 | super().__init__(position) | ||
30 | self.initiative = initiative | 30 | self.initiative = initiative | ||
31 | self.basket = [] | 31 | self.basket = [] | ||
32 | 32 | ||||
33 | def get_initiative(self): | 33 | def get_initiative(self): | ||
34 | return self.initiative | 34 | return self.initiative | ||
35 | 35 | ||||
36 | def add_candy(self, candy): | 36 | def add_candy(self, candy): | ||
37 | print(candy.mass, candy.uranium) | 37 | print(candy.mass, candy.uranium) | ||
38 | self.basket.append(candy) | 38 | self.basket.append(candy) | ||
39 | 39 | ||||
40 | def is_critical(self): | 40 | def is_critical(self): | ||
41 | return sum(candy.get_uranium_quantity() for candy in self.basket) > 20 | 41 | return sum(candy.get_uranium_quantity() for candy in self.basket) > 20 | ||
42 | 42 | ||||
43 | class Host(Person): | 43 | class Host(Person): | ||
44 | def __init__(self, position, candies): | 44 | def __init__(self, position, candies): | ||
45 | super().__init__(position) | 45 | super().__init__(position) | ||
46 | self.candies = [Candy(mass, uranium) for mass,uranium in candies] | 46 | self.candies = [Candy(mass, uranium) for mass,uranium in candies] | ||
47 | 47 | ||||
48 | def remove_candy(self, func): | 48 | def remove_candy(self, func): | ||
49 | if not self.candies: | 49 | if not self.candies: | ||
50 | return None | 50 | return None | ||
51 | 51 | ||||
52 | selected_candy = func(self.candies) | 52 | selected_candy = func(self.candies) | ||
53 | self.candies.remove(selected_candy) | 53 | self.candies.remove(selected_candy) | ||
54 | return selected_candy | 54 | return selected_candy | ||
55 | 55 | ||||
56 | 56 | ||||
57 | class FluxCapacitor: | 57 | class FluxCapacitor: | ||
58 | def __init__(self, participants): | 58 | def __init__(self, participants): | ||
59 | self.kids = [] | 59 | self.kids = [] | ||
60 | self.hosts = [] | 60 | self.hosts = [] | ||
61 | 61 | ||||
62 | for participant in participants: | 62 | for participant in participants: | ||
63 | if type(participant) == Kid: | 63 | if type(participant) == Kid: | ||
64 | self.kids.append(participant) | 64 | self.kids.append(participant) | ||
65 | 65 | ||||
66 | self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True) | 66 | self.kids = sorted(self.kids, key=lambda kid: kid.initiative, reverse=True) | ||
67 | 67 | ||||
68 | for participant in participants: | 68 | for participant in participants: | ||
69 | if type(participant) == Host: | 69 | if type(participant) == Host: | ||
70 | self.hosts.append(participant) | 70 | self.hosts.append(participant) | ||
71 | 71 | ||||
72 | def get_victim(self): | 72 | def get_victim(self): | ||
73 | critical_kids = set() | 73 | critical_kids = set() | ||
74 | closest_host = None | 74 | closest_host = None | ||
75 | same_distance_hosts = [] | 75 | same_distance_hosts = [] | ||
76 | round = 0 | 76 | round = 0 | ||
77 | visited = [[False for _ in self.kids] for _ in self.hosts] | 77 | visited = [[False for _ in self.kids] for _ in self.hosts] | ||
78 | 78 | ||||
79 | while len(critical_kids) == 0 and round < len(self.hosts): | 79 | while len(critical_kids) == 0 and round < len(self.hosts): | ||
80 | for kid in self.kids: | 80 | for kid in self.kids: | ||
n | 81 | print(f"Current kid: {kid.position}\n") | n | ||
82 | min_distance = float('inf') | 81 | min_distance = float('inf') | ||
83 | for host in self.hosts: | 82 | for host in self.hosts: | ||
n | 84 | print(f"Current host: {host.position}") | n | ||
85 | if visited[self.hosts.index(host)][self.kids.index(kid)]: | 83 | if visited[self.hosts.index(host)][self.kids.index(kid)]: | ||
n | 86 | print("skipped:", self.hosts.index(host), self.kids.index(kid)) | n | ||
87 | continue | 84 | continue | ||
88 | distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2) | 85 | distance = sqrt((kid.get_position()[0] - host.get_position()[0])**2 + (kid.get_position()[1] - host.get_position()[1])**2) | ||
89 | if distance < min_distance: | 86 | if distance < min_distance: | ||
90 | min_distance = distance | 87 | min_distance = distance | ||
91 | closest_host = host | 88 | closest_host = host | ||
92 | same_distance_hosts = [host] | 89 | same_distance_hosts = [host] | ||
93 | elif distance == min_distance: | 90 | elif distance == min_distance: | ||
94 | same_distance_hosts.append(host) | 91 | same_distance_hosts.append(host) | ||
95 | if len(same_distance_hosts) > 1: | 92 | if len(same_distance_hosts) > 1: | ||
96 | closest_host = min(same_distance_hosts, key=lambda host: host.get_position()) | 93 | closest_host = min(same_distance_hosts, key=lambda host: host.get_position()) | ||
97 | 94 | ||||
n | 98 | print(f"Move to host: {closest_host.position}") | n | ||
99 | if len(closest_host.candies) == 0: | 95 | if len(closest_host.candies) == 0: | ||
n | 100 | print("candy problem") | n | ||
101 | continue | 96 | continue | ||
102 | 97 | ||||
103 | kid.add_candy(closest_host.remove_candy(find_max_element_in_list_by_criteria)) | 98 | kid.add_candy(closest_host.remove_candy(find_max_element_in_list_by_criteria)) | ||
104 | kid.set_position(closest_host.get_position()) | 99 | kid.set_position(closest_host.get_position()) | ||
105 | visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True | 100 | visited[self.hosts.index(closest_host)][self.kids.index(kid)] = True | ||
106 | same_distance_hosts = [] | 101 | same_distance_hosts = [] | ||
107 | 102 | ||||
n | 108 | print("visited:", self.hosts.index(host), self.kids.index(kid)) | n | ||
109 | if kid.is_critical(): | 103 | if kid.is_critical(): | ||
110 | critical_kids.add(kid) | 104 | critical_kids.add(kid) | ||
111 | round += 1 | 105 | round += 1 | ||
112 | 106 | ||||
113 | if len(critical_kids) == 0: | 107 | if len(critical_kids) == 0: | ||
114 | return None | 108 | return None | ||
115 | return critical_kids | 109 | return critical_kids | ||
116 | 110 | ||||
117 | def find_max_element_in_list_by_criteria(list): | 111 | def find_max_element_in_list_by_criteria(list): | ||
118 | return max(list, key=lambda candy: candy.get_mass()) | 112 | return max(list, key=lambda candy: candy.get_mass()) | ||
119 | 113 | ||||
t | t | 114 |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|