1CRITICAL_MASS = 20
2
3class Candy:
4 def __init__(self, mass, uranium):
5 self.mass = mass
6 self.uranium = uranium
7
8 def get_uranuium_quantity(self):
9 return self.uranium * self.mass
10
11 def get_mass(self):
12 return self.mass
13
14
15class Person:
16 def __init__(self, position):
17 self.position = position
18
19 def get_position(self):
20 return self.position
21
22 def set_position(self, position):
23 self.position = position
24
25
26class Kid(Person):
27 def __init__(self, position, initiative):
28 super().__init__(position)
29 self.initiative = initiative
30 self.basket = []
31
32 def get_initiative(self):
33 return self.initiative
34
35 def add_candy(self, new_candy):
36 self.basket.append(new_candy)
37
38 def is_critical(self):
39 return sum(self.basket) > CRITICAL_MASS
40
41
42class Host(Person):
43 def __init__(self, position, candies):
44 super().__init__(position)
45 self.candies = candies
46
47 def remove_candy(self, func):
48 if len(self.candies) == 0:
49 return None
50
51 return func(self.candies)
52
53 def get_candies_count(self, candies):
54 return len(candies)
55
56 def greater_distance(self, other_person):
57 return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2)
58
59
60class FluxCapacitor:
61 def __init__(self, participants):
62 self.participants = participants
63 self.kids = []
64 self.hosts = []
65 for participant in self.participants:
66 if participant is Kid:
67 self.kids.append(participant)
68 elif participant is Host:
69 self.hosts.append(participant)
70
71 self.kids.sort(key=lambda x: x.get_initiative(), reverse = True)
72
73 every_kid_hosts = {}
74 for kid in self.kids:
75 every_kid_hosts[kid] = self.hosts
76
77 def remove_function(self, current_candies):
78 current_candies.sort(key=lambda x: x.mass, reverse = True)
79 best_candy = current_candies.pop(0)
80 return best_candy
81
82 def get_min_host(self, kid, hosts):
83 return min(hosts, key=(lambda host:(host.greater_distance(kid), host.get_position()[0], host.get_position()[1])))
84
85 def get_victim(self):
86 victims = []
87
88 while len(self.hosts) != 0:
89 for kid in self.kids:
90 best_host = self.get_min_host(kid, self.every_kid_hosts[kid])
91
92 best_candy = best_host.remove_candy(self.remove_function(best_host.candies))
93 kid.add_candy(best_candy)
94
95 if kid.is_critical():
96 victims.append(kid)
97
98 kid.set_position(best_host.get_position())
99
100 for host in self.hosts:
101 if host.get_candies_count() == 0:
102 self.hosts.discard(host)
103
104 self.every_kid_hosts[kid].discard(best_host)
105
106 if len(victims) != 0:
107 return victims
108
109 return None
E....FE..E..
======================================================================
ERROR: test_basic_usage (test.CandyTest)
Test basic usage of Candy class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 15, in test_basic_usage
self.assertEqual(candy.get_uranium_quantity(), 2.5)
AttributeError: 'Candy' object has no attribute 'get_uranium_quantity'
======================================================================
ERROR: test_basic_usage (test.HostTest)
Test basic usage of Host class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 65, in test_basic_usage
candy = host.remove_candy(compare_fun)
File "/tmp/solution.py", line 51, in remove_candy
return func(self.candies)
File "/tmp/test.py", line 63, in <lambda>
compare_fun = lambda candies: min(candies, key=lambda candy: candy.get_mass())
File "/tmp/test.py", line 63, in <lambda>
compare_fun = lambda candies: min(candies, key=lambda candy: candy.get_mass())
AttributeError: 'tuple' object has no attribute 'get_mass'
======================================================================
ERROR: test_candies (test.KidTest)
Test basic usage of candies in the Kid class.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/test.py", line 48, in test_candies
self.assertFalse(kid.is_critical())
File "/tmp/solution.py", line 39, in is_critical
return sum(self.basket) > CRITICAL_MASS
TypeError: unsupported operand type(s) for +: 'int' and 'Candy'
======================================================================
FAIL: test_real_case (test.FluxCapacitorTest)
Test with real case.
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/timeout_decorator/timeout_decorator.py", line 82, in new_function
return function(*args, **kwargs)
File "/tmp/test.py", line 115, in test_real_case
self.assertEqual(FluxCapacitor({kid1, kid2, host1, host2}).get_victim(), {kid1, kid2})
AssertionError: None != {<solution.Kid object at 0x7f6ca47cce80>,[36 chars]4f0>}
----------------------------------------------------------------------
Ran 12 tests in 0.001s
FAILED (failures=1, errors=3)
| f | 1 | CRITICAL_MASS = 20 | f | 1 | CRITICAL_MASS = 20 |
| 2 | 2 | ||||
| 3 | class Candy: | 3 | class Candy: | ||
| 4 | def __init__(self, mass, uranium): | 4 | def __init__(self, mass, uranium): | ||
| 5 | self.mass = mass | 5 | self.mass = mass | ||
| 6 | self.uranium = uranium | 6 | self.uranium = uranium | ||
| 7 | 7 | ||||
| 8 | def get_uranuium_quantity(self): | 8 | def get_uranuium_quantity(self): | ||
| 9 | return self.uranium * self.mass | 9 | return self.uranium * self.mass | ||
| 10 | 10 | ||||
| 11 | def get_mass(self): | 11 | def get_mass(self): | ||
| 12 | return self.mass | 12 | return self.mass | ||
| 13 | 13 | ||||
| 14 | 14 | ||||
| 15 | class Person: | 15 | class Person: | ||
| 16 | def __init__(self, position): | 16 | def __init__(self, position): | ||
| 17 | self.position = position | 17 | self.position = position | ||
| 18 | 18 | ||||
| 19 | def get_position(self): | 19 | def get_position(self): | ||
| 20 | return self.position | 20 | return self.position | ||
| 21 | 21 | ||||
| 22 | def set_position(self, position): | 22 | def set_position(self, position): | ||
| 23 | self.position = position | 23 | self.position = position | ||
| 24 | 24 | ||||
| 25 | 25 | ||||
| 26 | class Kid(Person): | 26 | class Kid(Person): | ||
| 27 | def __init__(self, position, initiative): | 27 | def __init__(self, position, initiative): | ||
| 28 | super().__init__(position) | 28 | super().__init__(position) | ||
| 29 | self.initiative = initiative | 29 | self.initiative = initiative | ||
| 30 | self.basket = [] | 30 | self.basket = [] | ||
| 31 | 31 | ||||
| 32 | def get_initiative(self): | 32 | def get_initiative(self): | ||
| 33 | return self.initiative | 33 | return self.initiative | ||
| 34 | 34 | ||||
| 35 | def add_candy(self, new_candy): | 35 | def add_candy(self, new_candy): | ||
| 36 | self.basket.append(new_candy) | 36 | self.basket.append(new_candy) | ||
| 37 | 37 | ||||
| 38 | def is_critical(self): | 38 | def is_critical(self): | ||
| 39 | return sum(self.basket) > CRITICAL_MASS | 39 | return sum(self.basket) > CRITICAL_MASS | ||
| 40 | 40 | ||||
| 41 | 41 | ||||
| 42 | class Host(Person): | 42 | class Host(Person): | ||
| 43 | def __init__(self, position, candies): | 43 | def __init__(self, position, candies): | ||
| 44 | super().__init__(position) | 44 | super().__init__(position) | ||
| 45 | self.candies = candies | 45 | self.candies = candies | ||
| 46 | 46 | ||||
| 47 | def remove_candy(self, func): | 47 | def remove_candy(self, func): | ||
| 48 | if len(self.candies) == 0: | 48 | if len(self.candies) == 0: | ||
| 49 | return None | 49 | return None | ||
| 50 | 50 | ||||
| 51 | return func(self.candies) | 51 | return func(self.candies) | ||
| 52 | 52 | ||||
| 53 | def get_candies_count(self, candies): | 53 | def get_candies_count(self, candies): | ||
| 54 | return len(candies) | 54 | return len(candies) | ||
| 55 | 55 | ||||
| 56 | def greater_distance(self, other_person): | 56 | def greater_distance(self, other_person): | ||
| 57 | return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2) | 57 | return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2) | ||
| 58 | 58 | ||||
| 59 | 59 | ||||
| 60 | class FluxCapacitor: | 60 | class FluxCapacitor: | ||
| 61 | def __init__(self, participants): | 61 | def __init__(self, participants): | ||
| 62 | self.participants = participants | 62 | self.participants = participants | ||
| 63 | self.kids = [] | 63 | self.kids = [] | ||
| 64 | self.hosts = [] | 64 | self.hosts = [] | ||
| 65 | for participant in self.participants: | 65 | for participant in self.participants: | ||
| 66 | if participant is Kid: | 66 | if participant is Kid: | ||
| 67 | self.kids.append(participant) | 67 | self.kids.append(participant) | ||
| t | 68 | elif parcticipant is Host: | t | 68 | elif participant is Host: |
| 69 | self.hosts.append(participant) | 69 | self.hosts.append(participant) | ||
| 70 | 70 | ||||
| 71 | self.kids.sort(key=lambda x: x.get_initiative(), reverse = True) | 71 | self.kids.sort(key=lambda x: x.get_initiative(), reverse = True) | ||
| 72 | 72 | ||||
| 73 | every_kid_hosts = {} | 73 | every_kid_hosts = {} | ||
| 74 | for kid in self.kids: | 74 | for kid in self.kids: | ||
| 75 | every_kid_hosts[kid] = self.hosts | 75 | every_kid_hosts[kid] = self.hosts | ||
| 76 | 76 | ||||
| 77 | def remove_function(self, current_candies): | 77 | def remove_function(self, current_candies): | ||
| 78 | current_candies.sort(key=lambda x: x.mass, reverse = True) | 78 | current_candies.sort(key=lambda x: x.mass, reverse = True) | ||
| 79 | best_candy = current_candies.pop(0) | 79 | best_candy = current_candies.pop(0) | ||
| 80 | return best_candy | 80 | return best_candy | ||
| 81 | 81 | ||||
| 82 | def get_min_host(self, kid, hosts): | 82 | def get_min_host(self, kid, hosts): | ||
| 83 | return min(hosts, key=(lambda host:(host.greater_distance(kid), host.get_position()[0], host.get_position()[1]))) | 83 | return min(hosts, key=(lambda host:(host.greater_distance(kid), host.get_position()[0], host.get_position()[1]))) | ||
| 84 | 84 | ||||
| 85 | def get_victim(self): | 85 | def get_victim(self): | ||
| 86 | victims = [] | 86 | victims = [] | ||
| 87 | 87 | ||||
| 88 | while len(self.hosts) != 0: | 88 | while len(self.hosts) != 0: | ||
| 89 | for kid in self.kids: | 89 | for kid in self.kids: | ||
| 90 | best_host = self.get_min_host(kid, self.every_kid_hosts[kid]) | 90 | best_host = self.get_min_host(kid, self.every_kid_hosts[kid]) | ||
| 91 | 91 | ||||
| 92 | best_candy = best_host.remove_candy(self.remove_function(best_host.candies)) | 92 | best_candy = best_host.remove_candy(self.remove_function(best_host.candies)) | ||
| 93 | kid.add_candy(best_candy) | 93 | kid.add_candy(best_candy) | ||
| 94 | 94 | ||||
| 95 | if kid.is_critical(): | 95 | if kid.is_critical(): | ||
| 96 | victims.append(kid) | 96 | victims.append(kid) | ||
| 97 | 97 | ||||
| 98 | kid.set_position(best_host.get_position()) | 98 | kid.set_position(best_host.get_position()) | ||
| 99 | 99 | ||||
| 100 | for host in self.hosts: | 100 | for host in self.hosts: | ||
| 101 | if host.get_candies_count() == 0: | 101 | if host.get_candies_count() == 0: | ||
| 102 | self.hosts.discard(host) | 102 | self.hosts.discard(host) | ||
| 103 | 103 | ||||
| 104 | self.every_kid_hosts[kid].discard(best_host) | 104 | self.every_kid_hosts[kid].discard(best_host) | ||
| 105 | 105 | ||||
| 106 | if len(victims) != 0: | 106 | if len(victims) != 0: | ||
| 107 | return victims | 107 | return victims | ||
| 108 | 108 | ||||
| 109 | return None | 109 | return None |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | CRITICAL_MASS = 20 | f | 1 | CRITICAL_MASS = 20 |
| 2 | 2 | ||||
| 3 | class Candy: | 3 | class Candy: | ||
| 4 | def __init__(self, mass, uranium): | 4 | def __init__(self, mass, uranium): | ||
| 5 | self.mass = mass | 5 | self.mass = mass | ||
| 6 | self.uranium = uranium | 6 | self.uranium = uranium | ||
| 7 | 7 | ||||
| 8 | def get_uranuium_quantity(self): | 8 | def get_uranuium_quantity(self): | ||
| 9 | return self.uranium * self.mass | 9 | return self.uranium * self.mass | ||
| 10 | 10 | ||||
| 11 | def get_mass(self): | 11 | def get_mass(self): | ||
| 12 | return self.mass | 12 | return self.mass | ||
| 13 | 13 | ||||
| 14 | 14 | ||||
| 15 | class Person: | 15 | class Person: | ||
| 16 | def __init__(self, position): | 16 | def __init__(self, position): | ||
| 17 | self.position = position | 17 | self.position = position | ||
| 18 | 18 | ||||
| 19 | def get_position(self): | 19 | def get_position(self): | ||
| 20 | return self.position | 20 | return self.position | ||
| 21 | 21 | ||||
| 22 | def set_position(self, position): | 22 | def set_position(self, position): | ||
| 23 | self.position = position | 23 | self.position = position | ||
| 24 | 24 | ||||
| 25 | 25 | ||||
| 26 | class Kid(Person): | 26 | class Kid(Person): | ||
| 27 | def __init__(self, position, initiative): | 27 | def __init__(self, position, initiative): | ||
| 28 | super().__init__(position) | 28 | super().__init__(position) | ||
| 29 | self.initiative = initiative | 29 | self.initiative = initiative | ||
| 30 | self.basket = [] | 30 | self.basket = [] | ||
| 31 | 31 | ||||
| 32 | def get_initiative(self): | 32 | def get_initiative(self): | ||
| 33 | return self.initiative | 33 | return self.initiative | ||
| 34 | 34 | ||||
| 35 | def add_candy(self, new_candy): | 35 | def add_candy(self, new_candy): | ||
| 36 | self.basket.append(new_candy) | 36 | self.basket.append(new_candy) | ||
| 37 | 37 | ||||
| 38 | def is_critical(self): | 38 | def is_critical(self): | ||
| 39 | return sum(self.basket) > CRITICAL_MASS | 39 | return sum(self.basket) > CRITICAL_MASS | ||
| 40 | 40 | ||||
| 41 | 41 | ||||
| 42 | class Host(Person): | 42 | class Host(Person): | ||
| 43 | def __init__(self, position, candies): | 43 | def __init__(self, position, candies): | ||
| 44 | super().__init__(position) | 44 | super().__init__(position) | ||
| 45 | self.candies = candies | 45 | self.candies = candies | ||
| 46 | 46 | ||||
| 47 | def remove_candy(self, func): | 47 | def remove_candy(self, func): | ||
| 48 | if len(self.candies) == 0: | 48 | if len(self.candies) == 0: | ||
| 49 | return None | 49 | return None | ||
| 50 | 50 | ||||
| 51 | return func(self.candies) | 51 | return func(self.candies) | ||
| 52 | 52 | ||||
| 53 | def get_candies_count(self, candies): | 53 | def get_candies_count(self, candies): | ||
| 54 | return len(candies) | 54 | return len(candies) | ||
| 55 | 55 | ||||
| 56 | def greater_distance(self, other_person): | 56 | def greater_distance(self, other_person): | ||
| 57 | return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2) | 57 | return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2) | ||
| 58 | 58 | ||||
| 59 | 59 | ||||
| 60 | class FluxCapacitor: | 60 | class FluxCapacitor: | ||
| 61 | def __init__(self, participants): | 61 | def __init__(self, participants): | ||
| 62 | self.participants = participants | 62 | self.participants = participants | ||
| 63 | self.kids = [] | 63 | self.kids = [] | ||
| 64 | self.hosts = [] | 64 | self.hosts = [] | ||
| 65 | for participant in self.participants: | 65 | for participant in self.participants: | ||
| 66 | if participant is Kid: | 66 | if participant is Kid: | ||
| 67 | self.kids.append(participant) | 67 | self.kids.append(participant) | ||
| 68 | elif parcticipant is Host: | 68 | elif parcticipant is Host: | ||
| 69 | self.hosts.append(participant) | 69 | self.hosts.append(participant) | ||
| 70 | 70 | ||||
| 71 | self.kids.sort(key=lambda x: x.get_initiative(), reverse = True) | 71 | self.kids.sort(key=lambda x: x.get_initiative(), reverse = True) | ||
| 72 | 72 | ||||
| 73 | every_kid_hosts = {} | 73 | every_kid_hosts = {} | ||
| 74 | for kid in self.kids: | 74 | for kid in self.kids: | ||
| 75 | every_kid_hosts[kid] = self.hosts | 75 | every_kid_hosts[kid] = self.hosts | ||
| 76 | 76 | ||||
| 77 | def remove_function(self, current_candies): | 77 | def remove_function(self, current_candies): | ||
| 78 | current_candies.sort(key=lambda x: x.mass, reverse = True) | 78 | current_candies.sort(key=lambda x: x.mass, reverse = True) | ||
| 79 | best_candy = current_candies.pop(0) | 79 | best_candy = current_candies.pop(0) | ||
| 80 | return best_candy | 80 | return best_candy | ||
| 81 | 81 | ||||
| 82 | def get_min_host(self, kid, hosts): | 82 | def get_min_host(self, kid, hosts): | ||
| t | 83 | return min(hosts, key=(lamda host: host.greater_distance(kid), host.get_position()[0], host.get_position()[1])) | t | 83 | return min(hosts, key=(lambda host:(host.greater_distance(kid), host.get_position()[0], host.get_position()[1]))) |
| 84 | 84 | ||||
| 85 | def get_victim(self): | 85 | def get_victim(self): | ||
| 86 | victims = [] | 86 | victims = [] | ||
| 87 | 87 | ||||
| 88 | while len(self.hosts) != 0: | 88 | while len(self.hosts) != 0: | ||
| 89 | for kid in self.kids: | 89 | for kid in self.kids: | ||
| 90 | best_host = self.get_min_host(kid, self.every_kid_hosts[kid]) | 90 | best_host = self.get_min_host(kid, self.every_kid_hosts[kid]) | ||
| 91 | 91 | ||||
| 92 | best_candy = best_host.remove_candy(self.remove_function(best_host.candies)) | 92 | best_candy = best_host.remove_candy(self.remove_function(best_host.candies)) | ||
| 93 | kid.add_candy(best_candy) | 93 | kid.add_candy(best_candy) | ||
| 94 | 94 | ||||
| 95 | if kid.is_critical(): | 95 | if kid.is_critical(): | ||
| 96 | victims.append(kid) | 96 | victims.append(kid) | ||
| 97 | 97 | ||||
| 98 | kid.set_position(best_host.get_position()) | 98 | kid.set_position(best_host.get_position()) | ||
| 99 | 99 | ||||
| 100 | for host in self.hosts: | 100 | for host in self.hosts: | ||
| 101 | if host.get_candies_count() == 0: | 101 | if host.get_candies_count() == 0: | ||
| 102 | self.hosts.discard(host) | 102 | self.hosts.discard(host) | ||
| 103 | 103 | ||||
| 104 | self.every_kid_hosts[kid].discard(best_host) | 104 | self.every_kid_hosts[kid].discard(best_host) | ||
| 105 | 105 | ||||
| 106 | if len(victims) != 0: | 106 | if len(victims) != 0: | ||
| 107 | return victims | 107 | return victims | ||
| 108 | 108 | ||||
| 109 | return None | 109 | return None |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| n | n | 1 | CRITICAL_MASS = 20 | ||
| 2 | |||||
| 1 | class Candy: | 3 | class Candy: | ||
| 2 | def __init__(self, mass, uranium): | 4 | def __init__(self, mass, uranium): | ||
| 3 | self.mass = mass | 5 | self.mass = mass | ||
| 4 | self.uranium = uranium | 6 | self.uranium = uranium | ||
| 5 | 7 | ||||
| 6 | def get_uranuium_quantity(self): | 8 | def get_uranuium_quantity(self): | ||
| 7 | return self.uranium * self.mass | 9 | return self.uranium * self.mass | ||
| 8 | 10 | ||||
| 9 | def get_mass(self): | 11 | def get_mass(self): | ||
| 10 | return self.mass | 12 | return self.mass | ||
| 11 | 13 | ||||
| 12 | 14 | ||||
| 13 | class Person: | 15 | class Person: | ||
| 14 | def __init__(self, position): | 16 | def __init__(self, position): | ||
| 15 | self.position = position | 17 | self.position = position | ||
| 16 | 18 | ||||
| 17 | def get_position(self): | 19 | def get_position(self): | ||
| 18 | return self.position | 20 | return self.position | ||
| 19 | 21 | ||||
| 20 | def set_position(self, position): | 22 | def set_position(self, position): | ||
| 21 | self.position = position | 23 | self.position = position | ||
| 22 | 24 | ||||
| 23 | 25 | ||||
| 24 | class Kid(Person): | 26 | class Kid(Person): | ||
| 25 | def __init__(self, position, initiative): | 27 | def __init__(self, position, initiative): | ||
| 26 | super().__init__(position) | 28 | super().__init__(position) | ||
| 27 | self.initiative = initiative | 29 | self.initiative = initiative | ||
| 28 | self.basket = [] | 30 | self.basket = [] | ||
| 29 | 31 | ||||
| 30 | def get_initiative(self): | 32 | def get_initiative(self): | ||
| 31 | return self.initiative | 33 | return self.initiative | ||
| 32 | 34 | ||||
| 33 | def add_candy(self, new_candy): | 35 | def add_candy(self, new_candy): | ||
| 34 | self.basket.append(new_candy) | 36 | self.basket.append(new_candy) | ||
| 35 | 37 | ||||
| n | 36 | def is_critical(self): | n | 38 | def is_critical(self): |
| 37 | mass_sum = 0 | 39 | return sum(self.basket) > CRITICAL_MASS | ||
| 38 | for current_candy in self.basket: | ||||
| 39 | mass_sum += current_candy.get_mass() | ||||
| 40 | |||||
| 41 | if mass_sum > 20: | ||||
| 42 | return True | ||||
| 43 | |||||
| 44 | return False | ||||
| 45 | 40 | ||||
| n | 46 | n | |||
| 47 | def remove_function(current_candies): | ||||
| 48 | current_candies.sort(key = lambda x: x.mass, reverse = True) | ||||
| 49 | best_candy = current_candies.pop(0) | ||||
| 50 | return best_candy | ||||
| 51 | 41 | ||||
| 52 | class Host(Person): | 42 | class Host(Person): | ||
| 53 | def __init__(self, position, candies): | 43 | def __init__(self, position, candies): | ||
| 54 | super().__init__(position) | 44 | super().__init__(position) | ||
| 55 | self.candies = candies | 45 | self.candies = candies | ||
| 56 | 46 | ||||
| 57 | def remove_candy(self, func): | 47 | def remove_candy(self, func): | ||
| 58 | if len(self.candies) == 0: | 48 | if len(self.candies) == 0: | ||
| 59 | return None | 49 | return None | ||
| 60 | 50 | ||||
| 61 | return func(self.candies) | 51 | return func(self.candies) | ||
| 62 | 52 | ||||
| 63 | def get_candies_count(self, candies): | 53 | def get_candies_count(self, candies): | ||
| 64 | return len(candies) | 54 | return len(candies) | ||
| 65 | 55 | ||||
| n | 66 | def distance(self, other_host): | n | 56 | def greater_distance(self, other_person): |
| 67 | return ((self.get_position()[0] - other_host.get_position()[0]) ** 2 + (self.get_position()[1] - other_host.get_position()[1]) ** 2) | 57 | return ((self.get_position()[0] - other_person.get_position()[0]) ** 2 + (self.get_position()[1] - other_person.get_position()[1]) ** 2) | ||
| 68 | 58 | ||||
| 69 | 59 | ||||
| 70 | class FluxCapacitor: | 60 | class FluxCapacitor: | ||
| 71 | def __init__(self, participants): | 61 | def __init__(self, participants): | ||
| 72 | self.participants = participants | 62 | self.participants = participants | ||
| n | 73 | kids = [] | n | 63 | self.kids = [] |
| 64 | self.hosts = [] | ||||
| 74 | for participant in self.participants: | 65 | for participant in self.participants: | ||
| 75 | if participant is Kid: | 66 | if participant is Kid: | ||
| n | 76 | kids.append(participant) | n | 67 | self.kids.append(participant) |
| 68 | elif parcticipant is Host: | ||||
| 69 | self.hosts.append(participant) | ||||
| 77 | 70 | ||||
| n | 78 | kids.sort(key = lambda x: x.get_initiative(), reverse = True) | n | 71 | self.kids.sort(key=lambda x: x.get_initiative(), reverse = True) |
| 79 | |||||
| 80 | hosts = [] | ||||
| 81 | for participant in self.participants: | ||||
| 82 | if participant is Host: | ||||
| 83 | hosts.append(participant) | ||||
| 84 | 72 | ||||
| 85 | every_kid_hosts = {} | 73 | every_kid_hosts = {} | ||
| n | 86 | for kid in kids: | n | 74 | for kid in self.kids: |
| 87 | every_kid_hosts[kid] = hosts | 75 | every_kid_hosts[kid] = self.hosts | ||
| 76 | |||||
| 77 | def remove_function(self, current_candies): | ||||
| 78 | current_candies.sort(key=lambda x: x.mass, reverse = True) | ||||
| 79 | best_candy = current_candies.pop(0) | ||||
| 80 | return best_candy | ||||
| 81 | |||||
| 82 | def get_min_host(self, kid, hosts): | ||||
| 83 | return min(hosts, key=(lamda host: host.greater_distance(kid), host.get_position()[0], host.get_position()[1])) | ||||
| 88 | 84 | ||||
| 89 | def get_victim(self): | 85 | def get_victim(self): | ||
| 90 | victims = [] | 86 | victims = [] | ||
| 91 | 87 | ||||
| 92 | while len(self.hosts) != 0: | 88 | while len(self.hosts) != 0: | ||
| 93 | for kid in self.kids: | 89 | for kid in self.kids: | ||
| n | 94 | def find_best_host(host): | n | 90 | best_host = self.get_min_host(kid, self.every_kid_hosts[kid]) |
| 95 | curr_distance = host.greater_distance(kid) | ||||
| 96 | return(curr_distance, host.get_position()[0], host.get_position()[1]) | ||||
| 97 | |||||
| 98 | best_host = min(self.every_kid_hosts[kid], key = find_best_host) | ||||
| 99 | 91 | ||||
| n | 100 | best_candy = best_host.remove_candy(remove_function) | n | 92 | best_candy = best_host.remove_candy(self.remove_function(best_host.candies)) |
| 101 | kid.add_candy(best_candy) | 93 | kid.add_candy(best_candy) | ||
| 102 | 94 | ||||
| t | 103 | if(kid.is_critical()): | t | 95 | if kid.is_critical(): |
| 104 | victims.append(kid) | 96 | victims.append(kid) | ||
| 105 | 97 | ||||
| 106 | kid.set_position(best_host.get_position()) | 98 | kid.set_position(best_host.get_position()) | ||
| 107 | 99 | ||||
| 108 | for host in self.hosts: | 100 | for host in self.hosts: | ||
| 109 | if host.get_candies_count() == 0: | 101 | if host.get_candies_count() == 0: | ||
| 110 | self.hosts.discard(host) | 102 | self.hosts.discard(host) | ||
| 111 | 103 | ||||
| 112 | self.every_kid_hosts[kid].discard(best_host) | 104 | self.every_kid_hosts[kid].discard(best_host) | ||
| 113 | 105 | ||||
| 114 | if len(victims) != 0: | 106 | if len(victims) != 0: | ||
| 115 | return victims | 107 | return victims | ||
| 116 | 108 | ||||
| 117 | return None | 109 | return None |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||
| f | 1 | class Candy: | f | 1 | class Candy: |
| 2 | def __init__(self, mass, uranium): | 2 | def __init__(self, mass, uranium): | ||
| 3 | self.mass = mass | 3 | self.mass = mass | ||
| 4 | self.uranium = uranium | 4 | self.uranium = uranium | ||
| 5 | 5 | ||||
| 6 | def get_uranuium_quantity(self): | 6 | def get_uranuium_quantity(self): | ||
| 7 | return self.uranium * self.mass | 7 | return self.uranium * self.mass | ||
| 8 | 8 | ||||
| 9 | def get_mass(self): | 9 | def get_mass(self): | ||
| 10 | return self.mass | 10 | return self.mass | ||
| 11 | 11 | ||||
| 12 | 12 | ||||
| 13 | class Person: | 13 | class Person: | ||
| 14 | def __init__(self, position): | 14 | def __init__(self, position): | ||
| 15 | self.position = position | 15 | self.position = position | ||
| 16 | 16 | ||||
| 17 | def get_position(self): | 17 | def get_position(self): | ||
| 18 | return self.position | 18 | return self.position | ||
| 19 | 19 | ||||
| 20 | def set_position(self, position): | 20 | def set_position(self, position): | ||
| 21 | self.position = position | 21 | self.position = position | ||
| 22 | 22 | ||||
| 23 | 23 | ||||
| 24 | class Kid(Person): | 24 | class Kid(Person): | ||
| 25 | def __init__(self, position, initiative): | 25 | def __init__(self, position, initiative): | ||
| 26 | super().__init__(position) | 26 | super().__init__(position) | ||
| 27 | self.initiative = initiative | 27 | self.initiative = initiative | ||
| 28 | self.basket = [] | 28 | self.basket = [] | ||
| 29 | 29 | ||||
| 30 | def get_initiative(self): | 30 | def get_initiative(self): | ||
| 31 | return self.initiative | 31 | return self.initiative | ||
| 32 | 32 | ||||
| 33 | def add_candy(self, new_candy): | 33 | def add_candy(self, new_candy): | ||
| 34 | self.basket.append(new_candy) | 34 | self.basket.append(new_candy) | ||
| 35 | 35 | ||||
| 36 | def is_critical(self): | 36 | def is_critical(self): | ||
| 37 | mass_sum = 0 | 37 | mass_sum = 0 | ||
| 38 | for current_candy in self.basket: | 38 | for current_candy in self.basket: | ||
| 39 | mass_sum += current_candy.get_mass() | 39 | mass_sum += current_candy.get_mass() | ||
| 40 | 40 | ||||
| 41 | if mass_sum > 20: | 41 | if mass_sum > 20: | ||
| 42 | return True | 42 | return True | ||
| 43 | 43 | ||||
| 44 | return False | 44 | return False | ||
| 45 | 45 | ||||
| 46 | 46 | ||||
| 47 | def remove_function(current_candies): | 47 | def remove_function(current_candies): | ||
| n | 48 | current_candies.sort(key = lambda obj: obj.mass, reverse = True) | n | 48 | current_candies.sort(key = lambda x: x.mass, reverse = True) |
| 49 | best_candy = current_candies.pop(0) | 49 | best_candy = current_candies.pop(0) | ||
| 50 | return best_candy | 50 | return best_candy | ||
| 51 | 51 | ||||
| 52 | class Host(Person): | 52 | class Host(Person): | ||
| 53 | def __init__(self, position, candies): | 53 | def __init__(self, position, candies): | ||
| 54 | super().__init__(position) | 54 | super().__init__(position) | ||
| 55 | self.candies = candies | 55 | self.candies = candies | ||
| 56 | 56 | ||||
| 57 | def remove_candy(self, func): | 57 | def remove_candy(self, func): | ||
| 58 | if len(self.candies) == 0: | 58 | if len(self.candies) == 0: | ||
| 59 | return None | 59 | return None | ||
| 60 | 60 | ||||
| 61 | return func(self.candies) | 61 | return func(self.candies) | ||
| 62 | 62 | ||||
| 63 | def get_candies_count(self, candies): | 63 | def get_candies_count(self, candies): | ||
| 64 | return len(candies) | 64 | return len(candies) | ||
| 65 | 65 | ||||
| 66 | def distance(self, other_host): | 66 | def distance(self, other_host): | ||
| 67 | return ((self.get_position()[0] - other_host.get_position()[0]) ** 2 + (self.get_position()[1] - other_host.get_position()[1]) ** 2) | 67 | return ((self.get_position()[0] - other_host.get_position()[0]) ** 2 + (self.get_position()[1] - other_host.get_position()[1]) ** 2) | ||
| 68 | 68 | ||||
| 69 | 69 | ||||
| 70 | class FluxCapacitor: | 70 | class FluxCapacitor: | ||
| 71 | def __init__(self, participants): | 71 | def __init__(self, participants): | ||
| 72 | self.participants = participants | 72 | self.participants = participants | ||
| n | 73 | kids = set() | n | 73 | kids = [] |
| 74 | for participant in self.participants: | 74 | for participant in self.participants: | ||
| 75 | if participant is Kid: | 75 | if participant is Kid: | ||
| n | 76 | kids.add(participant) | n | 76 | kids.append(participant) |
| 77 | 77 | ||||
| n | 78 | kids = kids.sort(key = lambda obj: obj.get_initiative(), reverse = True) | n | 78 | kids.sort(key = lambda x: x.get_initiative(), reverse = True) |
| 79 | 79 | ||||
| n | 80 | hosts = set() | n | 80 | hosts = [] |
| 81 | for participant in self.participants: | 81 | for participant in self.participants: | ||
| 82 | if participant is Host: | 82 | if participant is Host: | ||
| n | 83 | hosts.add(participant) | n | 83 | hosts.append(participant) |
| 84 | 84 | ||||
| 85 | every_kid_hosts = {} | 85 | every_kid_hosts = {} | ||
| 86 | for kid in kids: | 86 | for kid in kids: | ||
| 87 | every_kid_hosts[kid] = hosts | 87 | every_kid_hosts[kid] = hosts | ||
| 88 | 88 | ||||
| 89 | def get_victim(self): | 89 | def get_victim(self): | ||
| n | 90 | victims = set() | n | 90 | victims = [] |
| 91 | 91 | ||||
| 92 | while len(self.hosts) != 0: | 92 | while len(self.hosts) != 0: | ||
| 93 | for kid in self.kids: | 93 | for kid in self.kids: | ||
| 94 | def find_best_host(host): | 94 | def find_best_host(host): | ||
| 95 | curr_distance = host.greater_distance(kid) | 95 | curr_distance = host.greater_distance(kid) | ||
| 96 | return(curr_distance, host.get_position()[0], host.get_position()[1]) | 96 | return(curr_distance, host.get_position()[0], host.get_position()[1]) | ||
| 97 | 97 | ||||
| 98 | best_host = min(self.every_kid_hosts[kid], key = find_best_host) | 98 | best_host = min(self.every_kid_hosts[kid], key = find_best_host) | ||
| 99 | 99 | ||||
| 100 | best_candy = best_host.remove_candy(remove_function) | 100 | best_candy = best_host.remove_candy(remove_function) | ||
| 101 | kid.add_candy(best_candy) | 101 | kid.add_candy(best_candy) | ||
| 102 | 102 | ||||
| 103 | if(kid.is_critical()): | 103 | if(kid.is_critical()): | ||
| t | 104 | victims.add(kid) | t | 104 | victims.append(kid) |
| 105 | 105 | ||||
| 106 | kid.set_position(best_host.get_position()) | 106 | kid.set_position(best_host.get_position()) | ||
| 107 | 107 | ||||
| 108 | for host in self.hosts: | 108 | for host in self.hosts: | ||
| 109 | if host.get_candies_count() == 0: | 109 | if host.get_candies_count() == 0: | ||
| 110 | self.hosts.discard(host) | 110 | self.hosts.discard(host) | ||
| 111 | 111 | ||||
| 112 | self.every_kid_hosts[kid].discard(best_host) | 112 | self.every_kid_hosts[kid].discard(best_host) | ||
| 113 | 113 | ||||
| 114 | if len(victims) != 0: | 114 | if len(victims) != 0: | ||
| 115 | return victims | 115 | return victims | ||
| 116 | 116 | ||||
| 117 | return None | 117 | return None |
| Legends | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
| |||||||||