1import math
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.uranium*self.mass
12
13 def get_mass(self):
14 return self.mass
15
16 def __gt__(self, other):
17 return self.mass > other.mass
18
19
20class Person:
21
22 def __init__(self, position):
23 self.set_position(position)
24
25 def get_position(self):
26 return self.position
27
28 def set_position(self, position):
29 self.position = position
30
31
32class Kid(Person):
33
34 CRITICAL_URANIUM_DOSE = 20
35
36 def __init__(self, position, initiative):
37 super().__init__(position)
38 self.initiative = initiative
39 self.basket = []
40 self.visited_hosts = set()
41
42 def get_initiative(self):
43 return self.initiative
44
45 def add_candy(self, candy):
46 self.basket.append(candy)
47
48 def is_critical(self):
49 sum = 0
50 for candy in self.basket:
51 sum += candy.get_uranium_quantity()
52 return sum > self.CRITICAL_URANIUM_DOSE
53
54 def __lt__(self, other):
55 return self.initiative < other.initiative
56
57 def _sort_hosts(self, host):
58 dist = math.sqrt((self.position[0] - host.position[0]) ** 2 + (self.position[1] - host.position[1]) ** 2)
59 return dist, host.position[0], host.position[1]
60
61 def _go_to_door(self, hosts):
62 unvisited_hosts = [host for host in hosts if host not in self.visited_hosts]
63 if not unvisited_hosts:
64 return False
65 unvisited_hosts.sort(key=lambda host: self._sort_hosts(host))
66 selected_host = unvisited_hosts[0]
67 self.visited_hosts.add(selected_host)
68 self.position=selected_host.position
69 selected_host.visiting_kids.append(self)
70
71
72class Host(Person):
73
74 def __init__(self, position, candies):
75 super().__init__(position)
76 self.basket = []
77 self.visiting_kids = []
78 for candy in candies:
79 self.basket.append(Candy(candy[0], candy[1]))
80
81 def remove_candy(self, func):
82 if not self.basket:
83 return
84 candy_to_remove = func(self.basket)
85 self.basket = [candy for candy in self.basket if candy is not candy_to_remove]
86 return candy_to_remove
87
88 def _give_candy(self):
89 self.visiting_kids.sort(reverse = True)
90 for kid in self.visiting_kids:
91 candy = self.remove_candy(func=lambda basket:sorted(basket)[0])
92 if candy is not None:
93 kid.basket.append(candy)
94 self.visiting_kids.clear()
95
96
97class FluxCapacitor:
98
99 def __init__(self, participants):
100 self.participants = participants
101 self.kids = []
102 self.hosts = []
103 for person in participants:
104 if isinstance(person, Kid):
105 self.kids.append(person)
106 elif isinstance(person, Host):
107 self.hosts.append(person)
108
109 def get_victim(self):
110 kids_with_critical_mass = set()
111 while True:
112 for kid in self.kids:
113 kid._go_to_door(self.hosts)
114 for host in self.hosts:
115 host._give_candy()
116 for kid in self.kids:
117 if kid.is_critical():
118 kids_with_critical_mass.add(kid)
119 if kids_with_critical_mass:
120 return kids_with_critical_mass
121 all_kids_visited_all_hosts = True
122 for kid in self.kids:
123 if not kid.visited_hosts == set(self.hosts):
124 all_kids_visited_all_hosts = False
125 break
126 if all_kids_visited_all_hosts:
127 return None
............
----------------------------------------------------------------------
Ran 12 tests in 0.000s
OK
Георги Кунчев
05.11.2023 21:32Да, долната черта важи и за методи, и за атрибути. Някои от атрибутите ти биха се радвали на една долна черта в името си, но не е нещо, за което държа настоятелно, затова не го споменах изрично.
|
Добромир Пеев
05.11.2023 15:46_ се слага при private или методи и атрибути ли, че вече се обърках след като видях презентацията и последната публикация в форума. има ли смисъл в моя код да указвам кои методи и атрибути са private/protected.
|
f | 1 | import math | f | 1 | import math |
2 | 2 | ||||
3 | 3 | ||||
4 | class Candy: | 4 | class Candy: | ||
5 | 5 | ||||
6 | def __init__(self, mass, uranium): | 6 | def __init__(self, mass, uranium): | ||
7 | self.mass = mass | 7 | self.mass = mass | ||
8 | self.uranium = uranium | 8 | self.uranium = uranium | ||
9 | 9 | ||||
10 | def get_uranium_quantity(self): | 10 | def get_uranium_quantity(self): | ||
11 | return self.uranium*self.mass | 11 | return self.uranium*self.mass | ||
12 | 12 | ||||
13 | def get_mass(self): | 13 | def get_mass(self): | ||
14 | return self.mass | 14 | return self.mass | ||
15 | 15 | ||||
16 | def __gt__(self, other): | 16 | def __gt__(self, other): | ||
17 | return self.mass > other.mass | 17 | return self.mass > other.mass | ||
18 | 18 | ||||
19 | 19 | ||||
20 | class Person: | 20 | class Person: | ||
21 | 21 | ||||
22 | def __init__(self, position): | 22 | def __init__(self, position): | ||
23 | self.set_position(position) | 23 | self.set_position(position) | ||
24 | 24 | ||||
25 | def get_position(self): | 25 | def get_position(self): | ||
26 | return self.position | 26 | return self.position | ||
27 | 27 | ||||
28 | def set_position(self, position): | 28 | def set_position(self, position): | ||
29 | self.position = position | 29 | self.position = position | ||
30 | 30 | ||||
31 | 31 | ||||
32 | class Kid(Person): | 32 | class Kid(Person): | ||
33 | 33 | ||||
34 | CRITICAL_URANIUM_DOSE = 20 | 34 | CRITICAL_URANIUM_DOSE = 20 | ||
35 | 35 | ||||
36 | def __init__(self, position, initiative): | 36 | def __init__(self, position, initiative): | ||
37 | super().__init__(position) | 37 | super().__init__(position) | ||
38 | self.initiative = initiative | 38 | self.initiative = initiative | ||
39 | self.basket = [] | 39 | self.basket = [] | ||
40 | self.visited_hosts = set() | 40 | self.visited_hosts = set() | ||
41 | 41 | ||||
42 | def get_initiative(self): | 42 | def get_initiative(self): | ||
43 | return self.initiative | 43 | return self.initiative | ||
44 | 44 | ||||
45 | def add_candy(self, candy): | 45 | def add_candy(self, candy): | ||
46 | self.basket.append(candy) | 46 | self.basket.append(candy) | ||
47 | 47 | ||||
48 | def is_critical(self): | 48 | def is_critical(self): | ||
49 | sum = 0 | 49 | sum = 0 | ||
50 | for candy in self.basket: | 50 | for candy in self.basket: | ||
51 | sum += candy.get_uranium_quantity() | 51 | sum += candy.get_uranium_quantity() | ||
t | 52 | return sum >= self.CRITICAL_URANIUM_DOSE | t | 52 | return sum > self.CRITICAL_URANIUM_DOSE |
53 | 53 | ||||
54 | def __lt__(self, other): | 54 | def __lt__(self, other): | ||
55 | return self.initiative < other.initiative | 55 | return self.initiative < other.initiative | ||
56 | 56 | ||||
57 | def _sort_hosts(self, host): | 57 | def _sort_hosts(self, host): | ||
58 | dist = math.sqrt((self.position[0] - host.position[0]) ** 2 + (self.position[1] - host.position[1]) ** 2) | 58 | dist = math.sqrt((self.position[0] - host.position[0]) ** 2 + (self.position[1] - host.position[1]) ** 2) | ||
59 | return dist, host.position[0], host.position[1] | 59 | return dist, host.position[0], host.position[1] | ||
60 | 60 | ||||
61 | def _go_to_door(self, hosts): | 61 | def _go_to_door(self, hosts): | ||
62 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | 62 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | ||
63 | if not unvisited_hosts: | 63 | if not unvisited_hosts: | ||
64 | return False | 64 | return False | ||
65 | unvisited_hosts.sort(key=lambda host: self._sort_hosts(host)) | 65 | unvisited_hosts.sort(key=lambda host: self._sort_hosts(host)) | ||
66 | selected_host = unvisited_hosts[0] | 66 | selected_host = unvisited_hosts[0] | ||
67 | self.visited_hosts.add(selected_host) | 67 | self.visited_hosts.add(selected_host) | ||
68 | self.position=selected_host.position | 68 | self.position=selected_host.position | ||
69 | selected_host.visiting_kids.append(self) | 69 | selected_host.visiting_kids.append(self) | ||
70 | 70 | ||||
71 | 71 | ||||
72 | class Host(Person): | 72 | class Host(Person): | ||
73 | 73 | ||||
74 | def __init__(self, position, candies): | 74 | def __init__(self, position, candies): | ||
75 | super().__init__(position) | 75 | super().__init__(position) | ||
76 | self.basket = [] | 76 | self.basket = [] | ||
77 | self.visiting_kids = [] | 77 | self.visiting_kids = [] | ||
78 | for candy in candies: | 78 | for candy in candies: | ||
79 | self.basket.append(Candy(candy[0], candy[1])) | 79 | self.basket.append(Candy(candy[0], candy[1])) | ||
80 | 80 | ||||
81 | def remove_candy(self, func): | 81 | def remove_candy(self, func): | ||
82 | if not self.basket: | 82 | if not self.basket: | ||
83 | return | 83 | return | ||
84 | candy_to_remove = func(self.basket) | 84 | candy_to_remove = func(self.basket) | ||
85 | self.basket = [candy for candy in self.basket if candy is not candy_to_remove] | 85 | self.basket = [candy for candy in self.basket if candy is not candy_to_remove] | ||
86 | return candy_to_remove | 86 | return candy_to_remove | ||
87 | 87 | ||||
88 | def _give_candy(self): | 88 | def _give_candy(self): | ||
89 | self.visiting_kids.sort(reverse = True) | 89 | self.visiting_kids.sort(reverse = True) | ||
90 | for kid in self.visiting_kids: | 90 | for kid in self.visiting_kids: | ||
91 | candy = self.remove_candy(func=lambda basket:sorted(basket)[0]) | 91 | candy = self.remove_candy(func=lambda basket:sorted(basket)[0]) | ||
92 | if candy is not None: | 92 | if candy is not None: | ||
93 | kid.basket.append(candy) | 93 | kid.basket.append(candy) | ||
94 | self.visiting_kids.clear() | 94 | self.visiting_kids.clear() | ||
95 | 95 | ||||
96 | 96 | ||||
97 | class FluxCapacitor: | 97 | class FluxCapacitor: | ||
98 | 98 | ||||
99 | def __init__(self, participants): | 99 | def __init__(self, participants): | ||
100 | self.participants = participants | 100 | self.participants = participants | ||
101 | self.kids = [] | 101 | self.kids = [] | ||
102 | self.hosts = [] | 102 | self.hosts = [] | ||
103 | for person in participants: | 103 | for person in participants: | ||
104 | if isinstance(person, Kid): | 104 | if isinstance(person, Kid): | ||
105 | self.kids.append(person) | 105 | self.kids.append(person) | ||
106 | elif isinstance(person, Host): | 106 | elif isinstance(person, Host): | ||
107 | self.hosts.append(person) | 107 | self.hosts.append(person) | ||
108 | 108 | ||||
109 | def get_victim(self): | 109 | def get_victim(self): | ||
110 | kids_with_critical_mass = set() | 110 | kids_with_critical_mass = set() | ||
111 | while True: | 111 | while True: | ||
112 | for kid in self.kids: | 112 | for kid in self.kids: | ||
113 | kid._go_to_door(self.hosts) | 113 | kid._go_to_door(self.hosts) | ||
114 | for host in self.hosts: | 114 | for host in self.hosts: | ||
115 | host._give_candy() | 115 | host._give_candy() | ||
116 | for kid in self.kids: | 116 | for kid in self.kids: | ||
117 | if kid.is_critical(): | 117 | if kid.is_critical(): | ||
118 | kids_with_critical_mass.add(kid) | 118 | kids_with_critical_mass.add(kid) | ||
119 | if kids_with_critical_mass: | 119 | if kids_with_critical_mass: | ||
120 | return kids_with_critical_mass | 120 | return kids_with_critical_mass | ||
121 | all_kids_visited_all_hosts = True | 121 | all_kids_visited_all_hosts = True | ||
122 | for kid in self.kids: | 122 | for kid in self.kids: | ||
123 | if not kid.visited_hosts == set(self.hosts): | 123 | if not kid.visited_hosts == set(self.hosts): | ||
124 | all_kids_visited_all_hosts = False | 124 | all_kids_visited_all_hosts = False | ||
125 | break | 125 | break | ||
126 | if all_kids_visited_all_hosts: | 126 | if all_kids_visited_all_hosts: | ||
127 | return None | 127 | return None |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import math | f | 1 | import math |
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.uranium*self.mass | 11 | return self.uranium*self.mass | ||
11 | 12 | ||||
12 | def get_mass(self): | 13 | def get_mass(self): | ||
13 | return self.mass | 14 | return self.mass | ||
14 | 15 | ||||
15 | def __gt__(self, other): | 16 | def __gt__(self, other): | ||
16 | return self.mass > other.mass | 17 | return self.mass > other.mass | ||
17 | 18 | ||||
18 | 19 | ||||
19 | class Person: | 20 | class Person: | ||
20 | 21 | ||||
21 | def __init__(self, position): | 22 | def __init__(self, position): | ||
22 | self.set_position(position) | 23 | self.set_position(position) | ||
23 | 24 | ||||
24 | def get_position(self): | 25 | def get_position(self): | ||
25 | return self.position | 26 | return self.position | ||
26 | 27 | ||||
27 | def set_position(self, position): | 28 | def set_position(self, position): | ||
28 | self.position = position | 29 | self.position = position | ||
29 | 30 | ||||
30 | 31 | ||||
31 | class Kid(Person): | 32 | class Kid(Person): | ||
32 | 33 | ||||
n | n | 34 | CRITICAL_URANIUM_DOSE = 20 | ||
35 | |||||
33 | def __init__(self, position, initiative): | 36 | def __init__(self, position, initiative): | ||
34 | super().__init__(position) | 37 | super().__init__(position) | ||
35 | self.initiative = initiative | 38 | self.initiative = initiative | ||
36 | self.basket = [] | 39 | self.basket = [] | ||
37 | self.visited_hosts = set() | 40 | self.visited_hosts = set() | ||
38 | 41 | ||||
39 | def get_initiative(self): | 42 | def get_initiative(self): | ||
40 | return self.initiative | 43 | return self.initiative | ||
41 | 44 | ||||
42 | def add_candy(self, candy): | 45 | def add_candy(self, candy): | ||
43 | self.basket.append(candy) | 46 | self.basket.append(candy) | ||
44 | 47 | ||||
45 | def is_critical(self): | 48 | def is_critical(self): | ||
46 | sum = 0 | 49 | sum = 0 | ||
47 | for candy in self.basket: | 50 | for candy in self.basket: | ||
48 | sum += candy.get_uranium_quantity() | 51 | sum += candy.get_uranium_quantity() | ||
n | 49 | return sum >= 20 | n | 52 | return sum >= self.CRITICAL_URANIUM_DOSE |
50 | 53 | ||||
51 | def __lt__(self, other): | 54 | def __lt__(self, other): | ||
52 | return self.initiative < other.initiative | 55 | return self.initiative < other.initiative | ||
53 | 56 | ||||
54 | def _sort_hosts(self, host): | 57 | def _sort_hosts(self, host): | ||
n | 55 | dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2)) | n | 58 | dist = math.sqrt((self.position[0] - host.position[0]) ** 2 + (self.position[1] - host.position[1]) ** 2) |
56 | return (dist, host.position[1], host.position[0]) | 59 | return dist, host.position[0], host.position[1] | ||
57 | 60 | ||||
58 | def _go_to_door(self, hosts): | 61 | def _go_to_door(self, hosts): | ||
59 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | 62 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | ||
60 | if not unvisited_hosts: | 63 | if not unvisited_hosts: | ||
61 | return False | 64 | return False | ||
n | 62 | unvisited_hosts.sort(key = lambda host : self._sort_hosts(host)) | n | 65 | unvisited_hosts.sort(key=lambda host: self._sort_hosts(host)) |
63 | selected_host = unvisited_hosts[0] | 66 | selected_host = unvisited_hosts[0] | ||
64 | self.visited_hosts.add(selected_host) | 67 | self.visited_hosts.add(selected_host) | ||
n | n | 68 | self.position=selected_host.position | ||
65 | selected_host.visiting_kids.append(self) | 69 | selected_host.visiting_kids.append(self) | ||
66 | 70 | ||||
67 | 71 | ||||
68 | class Host(Person): | 72 | class Host(Person): | ||
69 | 73 | ||||
70 | def __init__(self, position, candies): | 74 | def __init__(self, position, candies): | ||
71 | super().__init__(position) | 75 | super().__init__(position) | ||
n | 72 | self.basket=[] | n | 76 | self.basket = [] |
73 | self.visiting_kids=[] | 77 | self.visiting_kids = [] | ||
74 | for candy in candies: | 78 | for candy in candies: | ||
n | 75 | self.basket.append(Candy(candy[0],candy[1])) | n | 79 | self.basket.append(Candy(candy[0], candy[1])) |
76 | 80 | ||||
77 | def remove_candy(self, func): | 81 | def remove_candy(self, func): | ||
78 | if not self.basket: | 82 | if not self.basket: | ||
79 | return | 83 | return | ||
80 | candy_to_remove = func(self.basket) | 84 | candy_to_remove = func(self.basket) | ||
81 | self.basket = [candy for candy in self.basket if candy is not candy_to_remove] | 85 | self.basket = [candy for candy in self.basket if candy is not candy_to_remove] | ||
82 | return candy_to_remove | 86 | return candy_to_remove | ||
83 | 87 | ||||
84 | def _give_candy(self): | 88 | def _give_candy(self): | ||
85 | self.visiting_kids.sort(reverse = True) | 89 | self.visiting_kids.sort(reverse = True) | ||
86 | for kid in self.visiting_kids: | 90 | for kid in self.visiting_kids: | ||
n | 87 | candy = self.remove_candy(func = lambda basket:sorted(basket)[0]) | n | 91 | candy = self.remove_candy(func=lambda basket:sorted(basket)[0]) |
88 | if candy is not None: | 92 | if candy is not None: | ||
89 | kid.basket.append(candy) | 93 | kid.basket.append(candy) | ||
90 | self.visiting_kids.clear() | 94 | self.visiting_kids.clear() | ||
91 | 95 | ||||
92 | 96 | ||||
93 | class FluxCapacitor: | 97 | class FluxCapacitor: | ||
94 | 98 | ||||
95 | def __init__(self, participants): | 99 | def __init__(self, participants): | ||
96 | self.participants = participants | 100 | self.participants = participants | ||
97 | self.kids = [] | 101 | self.kids = [] | ||
98 | self.hosts = [] | 102 | self.hosts = [] | ||
99 | for person in participants: | 103 | for person in participants: | ||
100 | if isinstance(person, Kid): | 104 | if isinstance(person, Kid): | ||
101 | self.kids.append(person) | 105 | self.kids.append(person) | ||
102 | elif isinstance(person, Host): | 106 | elif isinstance(person, Host): | ||
103 | self.hosts.append(person) | 107 | self.hosts.append(person) | ||
104 | 108 | ||||
105 | def get_victim(self): | 109 | def get_victim(self): | ||
106 | kids_with_critical_mass = set() | 110 | kids_with_critical_mass = set() | ||
t | 107 | while(True): | t | 111 | while True: |
108 | for kid in self.kids: | 112 | for kid in self.kids: | ||
109 | kid._go_to_door(self.hosts) | 113 | kid._go_to_door(self.hosts) | ||
110 | for host in self.hosts: | 114 | for host in self.hosts: | ||
111 | host._give_candy() | 115 | host._give_candy() | ||
112 | for kid in self.kids: | 116 | for kid in self.kids: | ||
113 | if kid.is_critical(): | 117 | if kid.is_critical(): | ||
114 | kids_with_critical_mass.add(kid) | 118 | kids_with_critical_mass.add(kid) | ||
115 | if kids_with_critical_mass: | 119 | if kids_with_critical_mass: | ||
116 | return kids_with_critical_mass | 120 | return kids_with_critical_mass | ||
117 | all_kids_visited_all_hosts = True | 121 | all_kids_visited_all_hosts = True | ||
118 | for kid in self.kids: | 122 | for kid in self.kids: | ||
119 | if not kid.visited_hosts == set(self.hosts): | 123 | if not kid.visited_hosts == set(self.hosts): | ||
120 | all_kids_visited_all_hosts = False | 124 | all_kids_visited_all_hosts = False | ||
121 | break | 125 | break | ||
122 | if all_kids_visited_all_hosts: | 126 | if all_kids_visited_all_hosts: | ||
123 | return None | 127 | return None |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import math | f | 1 | import math |
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.uranium*self.mass | 10 | return self.uranium*self.mass | ||
11 | 11 | ||||
12 | def get_mass(self): | 12 | def get_mass(self): | ||
13 | return self.mass | 13 | return self.mass | ||
14 | 14 | ||||
15 | def __gt__(self, other): | 15 | def __gt__(self, other): | ||
16 | return self.mass > other.mass | 16 | return self.mass > other.mass | ||
17 | 17 | ||||
18 | 18 | ||||
19 | class Person: | 19 | class Person: | ||
20 | 20 | ||||
21 | def __init__(self, position): | 21 | def __init__(self, position): | ||
22 | self.set_position(position) | 22 | self.set_position(position) | ||
23 | 23 | ||||
24 | def get_position(self): | 24 | def get_position(self): | ||
25 | return self.position | 25 | return self.position | ||
26 | 26 | ||||
27 | def set_position(self, position): | 27 | def set_position(self, position): | ||
28 | self.position = position | 28 | self.position = position | ||
29 | 29 | ||||
30 | 30 | ||||
31 | class Kid(Person): | 31 | class Kid(Person): | ||
32 | 32 | ||||
33 | def __init__(self, position, initiative): | 33 | def __init__(self, position, initiative): | ||
34 | super().__init__(position) | 34 | super().__init__(position) | ||
35 | self.initiative = initiative | 35 | self.initiative = initiative | ||
36 | self.basket = [] | 36 | self.basket = [] | ||
37 | self.visited_hosts = set() | 37 | self.visited_hosts = set() | ||
38 | 38 | ||||
39 | def get_initiative(self): | 39 | def get_initiative(self): | ||
40 | return self.initiative | 40 | return self.initiative | ||
41 | 41 | ||||
42 | def add_candy(self, candy): | 42 | def add_candy(self, candy): | ||
43 | self.basket.append(candy) | 43 | self.basket.append(candy) | ||
44 | 44 | ||||
45 | def is_critical(self): | 45 | def is_critical(self): | ||
46 | sum = 0 | 46 | sum = 0 | ||
47 | for candy in self.basket: | 47 | for candy in self.basket: | ||
48 | sum += candy.get_uranium_quantity() | 48 | sum += candy.get_uranium_quantity() | ||
49 | return sum >= 20 | 49 | return sum >= 20 | ||
50 | 50 | ||||
51 | def __lt__(self, other): | 51 | def __lt__(self, other): | ||
52 | return self.initiative < other.initiative | 52 | return self.initiative < other.initiative | ||
53 | 53 | ||||
54 | def _sort_hosts(self, host): | 54 | def _sort_hosts(self, host): | ||
55 | dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2)) | 55 | dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2)) | ||
56 | return (dist, host.position[1], host.position[0]) | 56 | return (dist, host.position[1], host.position[0]) | ||
57 | 57 | ||||
58 | def _go_to_door(self, hosts): | 58 | def _go_to_door(self, hosts): | ||
59 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | 59 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | ||
60 | if not unvisited_hosts: | 60 | if not unvisited_hosts: | ||
61 | return False | 61 | return False | ||
62 | unvisited_hosts.sort(key = lambda host : self._sort_hosts(host)) | 62 | unvisited_hosts.sort(key = lambda host : self._sort_hosts(host)) | ||
63 | selected_host = unvisited_hosts[0] | 63 | selected_host = unvisited_hosts[0] | ||
64 | self.visited_hosts.add(selected_host) | 64 | self.visited_hosts.add(selected_host) | ||
65 | selected_host.visiting_kids.append(self) | 65 | selected_host.visiting_kids.append(self) | ||
66 | 66 | ||||
67 | 67 | ||||
68 | class Host(Person): | 68 | class Host(Person): | ||
69 | 69 | ||||
70 | def __init__(self, position, candies): | 70 | def __init__(self, position, candies): | ||
71 | super().__init__(position) | 71 | super().__init__(position) | ||
72 | self.basket=[] | 72 | self.basket=[] | ||
73 | self.visiting_kids=[] | 73 | self.visiting_kids=[] | ||
74 | for candy in candies: | 74 | for candy in candies: | ||
75 | self.basket.append(Candy(candy[0],candy[1])) | 75 | self.basket.append(Candy(candy[0],candy[1])) | ||
76 | 76 | ||||
77 | def remove_candy(self, func): | 77 | def remove_candy(self, func): | ||
78 | if not self.basket: | 78 | if not self.basket: | ||
79 | return | 79 | return | ||
80 | candy_to_remove = func(self.basket) | 80 | candy_to_remove = func(self.basket) | ||
81 | self.basket = [candy for candy in self.basket if candy is not candy_to_remove] | 81 | self.basket = [candy for candy in self.basket if candy is not candy_to_remove] | ||
82 | return candy_to_remove | 82 | return candy_to_remove | ||
83 | 83 | ||||
84 | def _give_candy(self): | 84 | def _give_candy(self): | ||
85 | self.visiting_kids.sort(reverse = True) | 85 | self.visiting_kids.sort(reverse = True) | ||
86 | for kid in self.visiting_kids: | 86 | for kid in self.visiting_kids: | ||
87 | candy = self.remove_candy(func = lambda basket:sorted(basket)[0]) | 87 | candy = self.remove_candy(func = lambda basket:sorted(basket)[0]) | ||
88 | if candy is not None: | 88 | if candy is not None: | ||
89 | kid.basket.append(candy) | 89 | kid.basket.append(candy) | ||
90 | self.visiting_kids.clear() | 90 | self.visiting_kids.clear() | ||
91 | 91 | ||||
92 | 92 | ||||
93 | class FluxCapacitor: | 93 | class FluxCapacitor: | ||
94 | 94 | ||||
95 | def __init__(self, participants): | 95 | def __init__(self, participants): | ||
96 | self.participants = participants | 96 | self.participants = participants | ||
97 | self.kids = [] | 97 | self.kids = [] | ||
98 | self.hosts = [] | 98 | self.hosts = [] | ||
99 | for person in participants: | 99 | for person in participants: | ||
100 | if isinstance(person, Kid): | 100 | if isinstance(person, Kid): | ||
101 | self.kids.append(person) | 101 | self.kids.append(person) | ||
102 | elif isinstance(person, Host): | 102 | elif isinstance(person, Host): | ||
103 | self.hosts.append(person) | 103 | self.hosts.append(person) | ||
104 | 104 | ||||
105 | def get_victim(self): | 105 | def get_victim(self): | ||
106 | kids_with_critical_mass = set() | 106 | kids_with_critical_mass = set() | ||
107 | while(True): | 107 | while(True): | ||
108 | for kid in self.kids: | 108 | for kid in self.kids: | ||
109 | kid._go_to_door(self.hosts) | 109 | kid._go_to_door(self.hosts) | ||
110 | for host in self.hosts: | 110 | for host in self.hosts: | ||
111 | host._give_candy() | 111 | host._give_candy() | ||
112 | for kid in self.kids: | 112 | for kid in self.kids: | ||
113 | if kid.is_critical(): | 113 | if kid.is_critical(): | ||
114 | kids_with_critical_mass.add(kid) | 114 | kids_with_critical_mass.add(kid) | ||
115 | if kids_with_critical_mass: | 115 | if kids_with_critical_mass: | ||
116 | return kids_with_critical_mass | 116 | return kids_with_critical_mass | ||
117 | all_kids_visited_all_hosts = True | 117 | all_kids_visited_all_hosts = True | ||
118 | for kid in self.kids: | 118 | for kid in self.kids: | ||
119 | if not kid.visited_hosts == set(self.hosts): | 119 | if not kid.visited_hosts == set(self.hosts): | ||
120 | all_kids_visited_all_hosts = False | 120 | all_kids_visited_all_hosts = False | ||
121 | break | 121 | break | ||
122 | if all_kids_visited_all_hosts: | 122 | if all_kids_visited_all_hosts: | ||
123 | return None | 123 | return None | ||
t | 124 | t | |||
125 | |||||
126 | candy = Candy(20, 0.3) | ||||
127 | person = Person((1, 2)) | ||||
128 | kid = Kid((1, 2), 123) | ||||
129 | kid1 = Kid((1, 2), 15) | ||||
130 | kid2 = Kid((1, 2), 65) | ||||
131 | host = Host((2, 0), [(2, 1.0), (20, 0.6)]) | ||||
132 | host1 = Host((0, 2), [(15, 1.0), (15, 3.5)]) | ||||
133 | host2 = Host((2, 1), [(10, 1.0), (266, 3.5)]) | ||||
134 | flux_capacitor = FluxCapacitor({kid,kid1,kid2, host,host1,host2}) | ||||
135 | |||||
136 | print(flux_capacitor.get_victim()) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import math | f | 1 | import math |
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.uranium*self.mass | 10 | return self.uranium*self.mass | ||
11 | 11 | ||||
12 | def get_mass(self): | 12 | def get_mass(self): | ||
13 | return self.mass | 13 | return self.mass | ||
14 | 14 | ||||
n | 15 | def __gt__(self,other): | n | 15 | def __gt__(self, other): |
16 | return self.mass > other.mass | 16 | return self.mass > other.mass | ||
17 | 17 | ||||
18 | 18 | ||||
19 | class Person: | 19 | class Person: | ||
20 | 20 | ||||
21 | def __init__(self, position): | 21 | def __init__(self, position): | ||
22 | self.set_position(position) | 22 | self.set_position(position) | ||
23 | 23 | ||||
24 | def get_position(self): | 24 | def get_position(self): | ||
25 | return self.position | 25 | return self.position | ||
26 | 26 | ||||
27 | def set_position(self, position): | 27 | def set_position(self, position): | ||
28 | self.position = position | 28 | self.position = position | ||
29 | 29 | ||||
30 | 30 | ||||
31 | class Kid(Person): | 31 | class Kid(Person): | ||
32 | 32 | ||||
33 | def __init__(self, position, initiative): | 33 | def __init__(self, position, initiative): | ||
34 | super().__init__(position) | 34 | super().__init__(position) | ||
35 | self.initiative = initiative | 35 | self.initiative = initiative | ||
36 | self.basket = [] | 36 | self.basket = [] | ||
37 | self.visited_hosts = set() | 37 | self.visited_hosts = set() | ||
38 | 38 | ||||
39 | def get_initiative(self): | 39 | def get_initiative(self): | ||
40 | return self.initiative | 40 | return self.initiative | ||
41 | 41 | ||||
42 | def add_candy(self, candy): | 42 | def add_candy(self, candy): | ||
43 | self.basket.append(candy) | 43 | self.basket.append(candy) | ||
44 | 44 | ||||
45 | def is_critical(self): | 45 | def is_critical(self): | ||
46 | sum = 0 | 46 | sum = 0 | ||
47 | for candy in self.basket: | 47 | for candy in self.basket: | ||
48 | sum += candy.get_uranium_quantity() | 48 | sum += candy.get_uranium_quantity() | ||
49 | return sum >= 20 | 49 | return sum >= 20 | ||
50 | 50 | ||||
51 | def __lt__(self, other): | 51 | def __lt__(self, other): | ||
52 | return self.initiative < other.initiative | 52 | return self.initiative < other.initiative | ||
53 | 53 | ||||
54 | def _sort_hosts(self, host): | 54 | def _sort_hosts(self, host): | ||
55 | dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2)) | 55 | dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2)) | ||
56 | return (dist, host.position[1], host.position[0]) | 56 | return (dist, host.position[1], host.position[0]) | ||
57 | 57 | ||||
58 | def _go_to_door(self, hosts): | 58 | def _go_to_door(self, hosts): | ||
59 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | 59 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | ||
60 | if not unvisited_hosts: | 60 | if not unvisited_hosts: | ||
61 | return False | 61 | return False | ||
62 | unvisited_hosts.sort(key = lambda host : self._sort_hosts(host)) | 62 | unvisited_hosts.sort(key = lambda host : self._sort_hosts(host)) | ||
63 | selected_host = unvisited_hosts[0] | 63 | selected_host = unvisited_hosts[0] | ||
64 | self.visited_hosts.add(selected_host) | 64 | self.visited_hosts.add(selected_host) | ||
65 | selected_host.visiting_kids.append(self) | 65 | selected_host.visiting_kids.append(self) | ||
66 | 66 | ||||
67 | 67 | ||||
68 | class Host(Person): | 68 | class Host(Person): | ||
69 | 69 | ||||
70 | def __init__(self, position, candies): | 70 | def __init__(self, position, candies): | ||
71 | super().__init__(position) | 71 | super().__init__(position) | ||
72 | self.basket=[] | 72 | self.basket=[] | ||
73 | self.visiting_kids=[] | 73 | self.visiting_kids=[] | ||
74 | for candy in candies: | 74 | for candy in candies: | ||
75 | self.basket.append(Candy(candy[0],candy[1])) | 75 | self.basket.append(Candy(candy[0],candy[1])) | ||
76 | 76 | ||||
77 | def remove_candy(self, func): | 77 | def remove_candy(self, func): | ||
78 | if not self.basket: | 78 | if not self.basket: | ||
79 | return | 79 | return | ||
80 | candy_to_remove = func(self.basket) | 80 | candy_to_remove = func(self.basket) | ||
81 | self.basket = [candy for candy in self.basket if candy is not candy_to_remove] | 81 | self.basket = [candy for candy in self.basket if candy is not candy_to_remove] | ||
82 | return candy_to_remove | 82 | return candy_to_remove | ||
83 | 83 | ||||
84 | def _give_candy(self): | 84 | def _give_candy(self): | ||
85 | self.visiting_kids.sort(reverse = True) | 85 | self.visiting_kids.sort(reverse = True) | ||
86 | for kid in self.visiting_kids: | 86 | for kid in self.visiting_kids: | ||
87 | candy = self.remove_candy(func = lambda basket:sorted(basket)[0]) | 87 | candy = self.remove_candy(func = lambda basket:sorted(basket)[0]) | ||
88 | if candy is not None: | 88 | if candy is not None: | ||
89 | kid.basket.append(candy) | 89 | kid.basket.append(candy) | ||
90 | self.visiting_kids.clear() | 90 | self.visiting_kids.clear() | ||
91 | 91 | ||||
92 | 92 | ||||
93 | class FluxCapacitor: | 93 | class FluxCapacitor: | ||
94 | 94 | ||||
95 | def __init__(self, participants): | 95 | def __init__(self, participants): | ||
96 | self.participants = participants | 96 | self.participants = participants | ||
97 | self.kids = [] | 97 | self.kids = [] | ||
98 | self.hosts = [] | 98 | self.hosts = [] | ||
99 | for person in participants: | 99 | for person in participants: | ||
100 | if isinstance(person, Kid): | 100 | if isinstance(person, Kid): | ||
101 | self.kids.append(person) | 101 | self.kids.append(person) | ||
102 | elif isinstance(person, Host): | 102 | elif isinstance(person, Host): | ||
103 | self.hosts.append(person) | 103 | self.hosts.append(person) | ||
104 | 104 | ||||
105 | def get_victim(self): | 105 | def get_victim(self): | ||
106 | kids_with_critical_mass = set() | 106 | kids_with_critical_mass = set() | ||
107 | while(True): | 107 | while(True): | ||
108 | for kid in self.kids: | 108 | for kid in self.kids: | ||
109 | kid._go_to_door(self.hosts) | 109 | kid._go_to_door(self.hosts) | ||
110 | for host in self.hosts: | 110 | for host in self.hosts: | ||
111 | host._give_candy() | 111 | host._give_candy() | ||
112 | for kid in self.kids: | 112 | for kid in self.kids: | ||
113 | if kid.is_critical(): | 113 | if kid.is_critical(): | ||
114 | kids_with_critical_mass.add(kid) | 114 | kids_with_critical_mass.add(kid) | ||
115 | if kids_with_critical_mass: | 115 | if kids_with_critical_mass: | ||
116 | return kids_with_critical_mass | 116 | return kids_with_critical_mass | ||
117 | all_kids_visited_all_hosts = True | 117 | all_kids_visited_all_hosts = True | ||
118 | for kid in self.kids: | 118 | for kid in self.kids: | ||
119 | if not kid.visited_hosts == set(self.hosts): | 119 | if not kid.visited_hosts == set(self.hosts): | ||
120 | all_kids_visited_all_hosts = False | 120 | all_kids_visited_all_hosts = False | ||
121 | break | 121 | break | ||
122 | if all_kids_visited_all_hosts: | 122 | if all_kids_visited_all_hosts: | ||
123 | return None | 123 | return None | ||
t | t | 124 | |||
125 | |||||
126 | candy = Candy(20, 0.3) | ||||
127 | person = Person((1, 2)) | ||||
128 | kid = Kid((1, 2), 123) | ||||
129 | kid1 = Kid((1, 2), 15) | ||||
130 | kid2 = Kid((1, 2), 65) | ||||
131 | host = Host((2, 0), [(2, 1.0), (20, 0.6)]) | ||||
132 | host1 = Host((0, 2), [(15, 1.0), (15, 3.5)]) | ||||
133 | host2 = Host((2, 1), [(10, 1.0), (266, 3.5)]) | ||||
134 | flux_capacitor = FluxCapacitor({kid,kid1,kid2, host,host1,host2}) | ||||
135 | |||||
136 | print(flux_capacitor.get_victim()) |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import math | f | 1 | import math |
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.uranium*self.mass | 10 | return self.uranium*self.mass | ||
11 | 11 | ||||
12 | def get_mass(self): | 12 | def get_mass(self): | ||
13 | return self.mass | 13 | return self.mass | ||
14 | 14 | ||||
15 | def __gt__(self,other): | 15 | def __gt__(self,other): | ||
16 | return self.mass > other.mass | 16 | return self.mass > other.mass | ||
17 | 17 | ||||
18 | 18 | ||||
19 | class Person: | 19 | class Person: | ||
20 | 20 | ||||
21 | def __init__(self, position): | 21 | def __init__(self, position): | ||
22 | self.set_position(position) | 22 | self.set_position(position) | ||
23 | 23 | ||||
24 | def get_position(self): | 24 | def get_position(self): | ||
25 | return self.position | 25 | return self.position | ||
26 | 26 | ||||
27 | def set_position(self, position): | 27 | def set_position(self, position): | ||
n | 28 | self.position=position | n | 28 | self.position = position |
29 | 29 | ||||
30 | 30 | ||||
31 | class Kid(Person): | 31 | class Kid(Person): | ||
32 | 32 | ||||
n | 33 | def __init__(self,position, initiative): | n | 33 | def __init__(self, position, initiative): |
34 | super().__init__(position) | 34 | super().__init__(position) | ||
35 | self.initiative = initiative | 35 | self.initiative = initiative | ||
n | 36 | self.basket=[] | n | 36 | self.basket = [] |
37 | self.visited_hosts=set() | 37 | self.visited_hosts = set() | ||
38 | |||||
39 | def __str__(self): | ||||
40 | return f"This kid has {self.initiative} initiative" | ||||
41 | 38 | ||||
42 | def get_initiative(self): | 39 | def get_initiative(self): | ||
43 | return self.initiative | 40 | return self.initiative | ||
44 | 41 | ||||
45 | def add_candy(self, candy): | 42 | def add_candy(self, candy): | ||
46 | self.basket.append(candy) | 43 | self.basket.append(candy) | ||
47 | 44 | ||||
48 | def is_critical(self): | 45 | def is_critical(self): | ||
n | 49 | sum=0 | n | 46 | sum = 0 |
50 | for candy in self.basket: | 47 | for candy in self.basket: | ||
n | 51 | sum+=candy.get_uranium_quantity() | n | 48 | sum += candy.get_uranium_quantity() |
52 | return sum>=20 | 49 | return sum >= 20 | ||
53 | 50 | ||||
n | 54 | def __lt__(self,other): | n | 51 | def __lt__(self, other): |
55 | return self.initiative < other.initiative | 52 | return self.initiative < other.initiative | ||
56 | 53 | ||||
n | 57 | def sort_hosts(self,host): | n | 54 | def _sort_hosts(self, host): |
58 | dist = math.sqrt(math.pow(self.position[0]-host.position[0],2)+math.pow(self.position[1]-host.position[1],2)) | 55 | dist = math.sqrt(math.pow(self.position[0] - host.position[0],2) + math.pow(self.position[1] - host.position[1],2)) | ||
59 | return (dist, host.position[1], host.position[0]) | 56 | return (dist, host.position[1], host.position[0]) | ||
60 | 57 | ||||
n | 61 | def go_to_door(self, hosts): | n | 58 | def _go_to_door(self, hosts): |
62 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | 59 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | ||
63 | if not unvisited_hosts: | 60 | if not unvisited_hosts: | ||
64 | return False | 61 | return False | ||
n | 65 | unvisited_hosts.sort(key=lambda host: self.sort_hosts(host)) | n | 62 | unvisited_hosts.sort(key = lambda host : self._sort_hosts(host)) |
66 | selected_host = unvisited_hosts[0] | 63 | selected_host = unvisited_hosts[0] | ||
67 | self.visited_hosts.add(selected_host) | 64 | self.visited_hosts.add(selected_host) | ||
68 | selected_host.visiting_kids.append(self) | 65 | selected_host.visiting_kids.append(self) | ||
n | n | 66 | |||
69 | 67 | ||||
70 | class Host(Person): | 68 | class Host(Person): | ||
71 | 69 | ||||
72 | def __init__(self, position, candies): | 70 | def __init__(self, position, candies): | ||
73 | super().__init__(position) | 71 | super().__init__(position) | ||
74 | self.basket=[] | 72 | self.basket=[] | ||
75 | self.visiting_kids=[] | 73 | self.visiting_kids=[] | ||
76 | for candy in candies: | 74 | for candy in candies: | ||
77 | self.basket.append(Candy(candy[0],candy[1])) | 75 | self.basket.append(Candy(candy[0],candy[1])) | ||
78 | 76 | ||||
79 | def remove_candy(self, func): | 77 | def remove_candy(self, func): | ||
80 | if not self.basket: | 78 | if not self.basket: | ||
81 | return | 79 | return | ||
n | 82 | candy_to_remove=func(self.basket) | n | 80 | candy_to_remove = func(self.basket) |
83 | self.basket=[candy for candy in self.basket if candy is not candy_to_remove] | 81 | self.basket = [candy for candy in self.basket if candy is not candy_to_remove] | ||
84 | return candy_to_remove | 82 | return candy_to_remove | ||
85 | 83 | ||||
n | 86 | def give_candy(self): | n | 84 | def _give_candy(self): |
87 | self.visiting_kids.sort(reverse=True) | 85 | self.visiting_kids.sort(reverse = True) | ||
88 | for kid in self.visiting_kids: | 86 | for kid in self.visiting_kids: | ||
n | 89 | candy=self.remove_candy(func=lambda basket:sorted(basket)[0]) | n | 87 | candy = self.remove_candy(func = lambda basket:sorted(basket)[0]) |
90 | if candy is not None: | 88 | if candy is not None: | ||
91 | kid.basket.append(candy) | 89 | kid.basket.append(candy) | ||
92 | self.visiting_kids.clear() | 90 | self.visiting_kids.clear() | ||
93 | 91 | ||||
94 | 92 | ||||
95 | class FluxCapacitor: | 93 | class FluxCapacitor: | ||
96 | 94 | ||||
97 | def __init__(self, participants): | 95 | def __init__(self, participants): | ||
n | 98 | self.participants=participants | n | 96 | self.participants = participants |
99 | self.kids=[] | 97 | self.kids = [] | ||
100 | self.hosts=[] | 98 | self.hosts = [] | ||
101 | for person in participants: | 99 | for person in participants: | ||
102 | if isinstance(person, Kid): | 100 | if isinstance(person, Kid): | ||
103 | self.kids.append(person) | 101 | self.kids.append(person) | ||
104 | elif isinstance(person, Host): | 102 | elif isinstance(person, Host): | ||
105 | self.hosts.append(person) | 103 | self.hosts.append(person) | ||
106 | 104 | ||||
107 | def get_victim(self): | 105 | def get_victim(self): | ||
n | 108 | kids_with_critical_mass=set() | n | 106 | kids_with_critical_mass = set() |
109 | while(True): | 107 | while(True): | ||
110 | for kid in self.kids: | 108 | for kid in self.kids: | ||
n | 111 | kid.go_to_door(self.hosts) | n | 109 | kid._go_to_door(self.hosts) |
112 | for host in self.hosts: | 110 | for host in self.hosts: | ||
n | 113 | host.give_candy() | n | 111 | host._give_candy() |
114 | for kid in self.kids: | 112 | for kid in self.kids: | ||
115 | if kid.is_critical(): | 113 | if kid.is_critical(): | ||
116 | kids_with_critical_mass.add(kid) | 114 | kids_with_critical_mass.add(kid) | ||
117 | if kids_with_critical_mass: | 115 | if kids_with_critical_mass: | ||
118 | return kids_with_critical_mass | 116 | return kids_with_critical_mass | ||
119 | all_kids_visited_all_hosts = True | 117 | all_kids_visited_all_hosts = True | ||
n | 120 | n | |||
121 | for kid in self.kids: | 118 | for kid in self.kids: | ||
122 | if not kid.visited_hosts == set(self.hosts): | 119 | if not kid.visited_hosts == set(self.hosts): | ||
123 | all_kids_visited_all_hosts = False | 120 | all_kids_visited_all_hosts = False | ||
124 | break | 121 | break | ||
n | 125 | n | |||
126 | if all_kids_visited_all_hosts: | 122 | if all_kids_visited_all_hosts: | ||
127 | return None | 123 | return None | ||
t | 128 | t | |||
129 | candy = Candy(20, 0.3) | ||||
130 | person = Person((1, 2)) | ||||
131 | kid = Kid((0, 0), 123) | ||||
132 | kid1 = Kid((1, 2), 15) | ||||
133 | kid2 = Kid((6, 5), 65) | ||||
134 | host = Host((2, 0), [(2, 1.0), (20, 0.6)]) | ||||
135 | host1 = Host((0, 2), [(15, 1.0), (15, 3.5)]) | ||||
136 | host2 = Host((2, 1), [(10, 1.0), (266, 3.5)]) | ||||
137 | flux_capacitor = FluxCapacitor({kid,kid1,kid2, host,host1,host2}) | ||||
138 | flux_capacitor.get_victim() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|
f | 1 | import math | f | 1 | import math |
n | 2 | from functools import cmp_to_key | n | ||
3 | 2 | ||||
4 | class Candy: | 3 | class Candy: | ||
5 | 4 | ||||
6 | def __init__(self, mass, uranium): | 5 | def __init__(self, mass, uranium): | ||
7 | self.mass = mass | 6 | self.mass = mass | ||
8 | self.uranium = uranium | 7 | self.uranium = uranium | ||
9 | 8 | ||||
10 | def get_uranium_quantity(self): | 9 | def get_uranium_quantity(self): | ||
11 | return self.uranium*self.mass | 10 | return self.uranium*self.mass | ||
12 | 11 | ||||
13 | def get_mass(self): | 12 | def get_mass(self): | ||
14 | return self.mass | 13 | return self.mass | ||
15 | 14 | ||||
16 | def __gt__(self,other): | 15 | def __gt__(self,other): | ||
17 | return self.mass > other.mass | 16 | return self.mass > other.mass | ||
18 | 17 | ||||
19 | 18 | ||||
20 | class Person: | 19 | class Person: | ||
21 | 20 | ||||
22 | def __init__(self, position): | 21 | def __init__(self, position): | ||
23 | self.set_position(position) | 22 | self.set_position(position) | ||
24 | 23 | ||||
25 | def get_position(self): | 24 | def get_position(self): | ||
26 | return self.position | 25 | return self.position | ||
27 | 26 | ||||
28 | def set_position(self, position): | 27 | def set_position(self, position): | ||
29 | self.position=position | 28 | self.position=position | ||
30 | 29 | ||||
31 | 30 | ||||
32 | class Kid(Person): | 31 | class Kid(Person): | ||
33 | 32 | ||||
34 | def __init__(self,position, initiative): | 33 | def __init__(self,position, initiative): | ||
35 | super().__init__(position) | 34 | super().__init__(position) | ||
36 | self.initiative = initiative | 35 | self.initiative = initiative | ||
37 | self.basket=[] | 36 | self.basket=[] | ||
38 | self.visited_hosts=set() | 37 | self.visited_hosts=set() | ||
39 | 38 | ||||
40 | def __str__(self): | 39 | def __str__(self): | ||
41 | return f"This kid has {self.initiative} initiative" | 40 | return f"This kid has {self.initiative} initiative" | ||
42 | 41 | ||||
43 | def get_initiative(self): | 42 | def get_initiative(self): | ||
44 | return self.initiative | 43 | return self.initiative | ||
45 | 44 | ||||
46 | def add_candy(self, candy): | 45 | def add_candy(self, candy): | ||
47 | self.basket.append(candy) | 46 | self.basket.append(candy) | ||
48 | 47 | ||||
49 | def is_critical(self): | 48 | def is_critical(self): | ||
50 | sum=0 | 49 | sum=0 | ||
51 | for candy in self.basket: | 50 | for candy in self.basket: | ||
52 | sum+=candy.get_uranium_quantity() | 51 | sum+=candy.get_uranium_quantity() | ||
53 | return sum>=20 | 52 | return sum>=20 | ||
54 | 53 | ||||
55 | def __lt__(self,other): | 54 | def __lt__(self,other): | ||
56 | return self.initiative < other.initiative | 55 | return self.initiative < other.initiative | ||
57 | 56 | ||||
58 | def sort_hosts(self,host): | 57 | def sort_hosts(self,host): | ||
n | 59 | return math.sqrt(math.pow(self.position[0]-host.position[0],2)+math.pow(self.position[0]-host.position[0],2)) | n | 58 | dist = math.sqrt(math.pow(self.position[0]-host.position[0],2)+math.pow(self.position[1]-host.position[1],2)) |
59 | return (dist, host.position[1], host.position[0]) | ||||
60 | 60 | ||||
61 | def go_to_door(self, hosts): | 61 | def go_to_door(self, hosts): | ||
62 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | 62 | unvisited_hosts = [host for host in hosts if host not in self.visited_hosts] | ||
63 | if not unvisited_hosts: | 63 | if not unvisited_hosts: | ||
64 | return False | 64 | return False | ||
65 | unvisited_hosts.sort(key=lambda host: self.sort_hosts(host)) | 65 | unvisited_hosts.sort(key=lambda host: self.sort_hosts(host)) | ||
66 | selected_host = unvisited_hosts[0] | 66 | selected_host = unvisited_hosts[0] | ||
67 | self.visited_hosts.add(selected_host) | 67 | self.visited_hosts.add(selected_host) | ||
68 | selected_host.visiting_kids.append(self) | 68 | selected_host.visiting_kids.append(self) | ||
69 | 69 | ||||
70 | class Host(Person): | 70 | class Host(Person): | ||
71 | 71 | ||||
72 | def __init__(self, position, candies): | 72 | def __init__(self, position, candies): | ||
73 | super().__init__(position) | 73 | super().__init__(position) | ||
74 | self.basket=[] | 74 | self.basket=[] | ||
75 | self.visiting_kids=[] | 75 | self.visiting_kids=[] | ||
76 | for candy in candies: | 76 | for candy in candies: | ||
77 | self.basket.append(Candy(candy[0],candy[1])) | 77 | self.basket.append(Candy(candy[0],candy[1])) | ||
78 | 78 | ||||
79 | def remove_candy(self, func): | 79 | def remove_candy(self, func): | ||
80 | if not self.basket: | 80 | if not self.basket: | ||
81 | return | 81 | return | ||
82 | candy_to_remove=func(self.basket) | 82 | candy_to_remove=func(self.basket) | ||
83 | self.basket=[candy for candy in self.basket if candy is not candy_to_remove] | 83 | self.basket=[candy for candy in self.basket if candy is not candy_to_remove] | ||
84 | return candy_to_remove | 84 | return candy_to_remove | ||
85 | 85 | ||||
86 | def give_candy(self): | 86 | def give_candy(self): | ||
87 | self.visiting_kids.sort(reverse=True) | 87 | self.visiting_kids.sort(reverse=True) | ||
88 | for kid in self.visiting_kids: | 88 | for kid in self.visiting_kids: | ||
89 | candy=self.remove_candy(func=lambda basket:sorted(basket)[0]) | 89 | candy=self.remove_candy(func=lambda basket:sorted(basket)[0]) | ||
90 | if candy is not None: | 90 | if candy is not None: | ||
91 | kid.basket.append(candy) | 91 | kid.basket.append(candy) | ||
92 | self.visiting_kids.clear() | 92 | self.visiting_kids.clear() | ||
93 | 93 | ||||
94 | 94 | ||||
95 | class FluxCapacitor: | 95 | class FluxCapacitor: | ||
96 | 96 | ||||
97 | def __init__(self, participants): | 97 | def __init__(self, participants): | ||
98 | self.participants=participants | 98 | self.participants=participants | ||
99 | self.kids=[] | 99 | self.kids=[] | ||
100 | self.hosts=[] | 100 | self.hosts=[] | ||
101 | for person in participants: | 101 | for person in participants: | ||
102 | if isinstance(person, Kid): | 102 | if isinstance(person, Kid): | ||
103 | self.kids.append(person) | 103 | self.kids.append(person) | ||
104 | elif isinstance(person, Host): | 104 | elif isinstance(person, Host): | ||
105 | self.hosts.append(person) | 105 | self.hosts.append(person) | ||
106 | 106 | ||||
107 | def get_victim(self): | 107 | def get_victim(self): | ||
108 | kids_with_critical_mass=set() | 108 | kids_with_critical_mass=set() | ||
109 | while(True): | 109 | while(True): | ||
110 | for kid in self.kids: | 110 | for kid in self.kids: | ||
111 | kid.go_to_door(self.hosts) | 111 | kid.go_to_door(self.hosts) | ||
112 | for host in self.hosts: | 112 | for host in self.hosts: | ||
113 | host.give_candy() | 113 | host.give_candy() | ||
114 | for kid in self.kids: | 114 | for kid in self.kids: | ||
115 | if kid.is_critical(): | 115 | if kid.is_critical(): | ||
116 | kids_with_critical_mass.add(kid) | 116 | kids_with_critical_mass.add(kid) | ||
117 | if kids_with_critical_mass: | 117 | if kids_with_critical_mass: | ||
118 | return kids_with_critical_mass | 118 | return kids_with_critical_mass | ||
119 | all_kids_visited_all_hosts = True | 119 | all_kids_visited_all_hosts = True | ||
120 | 120 | ||||
121 | for kid in self.kids: | 121 | for kid in self.kids: | ||
122 | if not kid.visited_hosts == set(self.hosts): | 122 | if not kid.visited_hosts == set(self.hosts): | ||
123 | all_kids_visited_all_hosts = False | 123 | all_kids_visited_all_hosts = False | ||
124 | break | 124 | break | ||
125 | 125 | ||||
126 | if all_kids_visited_all_hosts: | 126 | if all_kids_visited_all_hosts: | ||
127 | return None | 127 | return None | ||
t | t | 128 | |||
129 | candy = Candy(20, 0.3) | ||||
130 | person = Person((1, 2)) | ||||
131 | kid = Kid((0, 0), 123) | ||||
132 | kid1 = Kid((1, 2), 15) | ||||
133 | kid2 = Kid((6, 5), 65) | ||||
134 | host = Host((2, 0), [(2, 1.0), (20, 0.6)]) | ||||
135 | host1 = Host((0, 2), [(15, 1.0), (15, 3.5)]) | ||||
136 | host2 = Host((2, 1), [(10, 1.0), (266, 3.5)]) | ||||
137 | flux_capacitor = FluxCapacitor({kid,kid1,kid2, host,host1,host2}) | ||||
138 | flux_capacitor.get_victim() |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|