Домашни > Хелоуин в Припят > Решения > Решението на Виктор Райков

Резултати
10 точки от тестове
0 точки от учител

10 точки общо

12 успешни теста
0 неуспешни теста
Код

 1import math
 2
 3class Candy:
 4    def __init__(self, mass, uranium):
 5        self.mass = mass
 6        self.uranium = uranium
 7
 8    def get_uranium_quantity(self):
 9        return self.mass * self.uranium
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, new_position):
23        self.position = new_position
24
25
26class Kid(Person):
27    CRITICAL_URANIUM_MASS = 20
28
29    def __init__(self, position, initiative):
30        super().__init__(position)
31        self.initiative = initiative
32        self.candies = []
33
34    def get_initiative(self):
35        return self.initiative
36
37    def add_candy(self, candy):
38        self.candies.append(candy)
39
40    def is_critical(self):
41        uranium_mass = sum(candy.get_uranium_quantity() for candy in self.candies)
42        return uranium_mass > Kid.CRITICAL_URANIUM_MASS
43    
44    def move_to_host(self, host):
45        self.set_position(host.get_position())
46
47
48class Host(Person):
49    def __init__(self, position, candies):
50        super().__init__(position)
51        self.candies = [Candy(mass, uranium) for mass, uranium in candies]
52
53    def remove_candy(self, selection_function):
54        if not self.candies:
55            return None
56        selected_candy = selection_function(self.candies)
57        self.candies.remove(selected_candy)
58        return selected_candy
59
60
61class FluxCapacitor:
62    def __init__(self, participants):
63        self.kids = {p for p in participants if isinstance(p, Kid)}
64        self.hosts = {p for p in participants if isinstance(p, Host)}
65        self.victims = set()
66        self.simulate_halloween()
67
68    def get_victim(self):
69        return self.victims if self.victims else None
70
71    def simulate_halloween(self):
72        visited_hosts = {kid: set() for kid in self.kids}
73        while self.hosts and not self.victims:
74            for kid in sorted(self.kids, key=lambda k: k.get_initiative(), reverse=True):
75                closest_host = self.find_closest_host(kid, visited_hosts[kid])
76                if closest_host:
77                    visited_hosts[kid].add(closest_host)
78                    kid.move_to_host(closest_host)
79                    candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda c: c.get_mass()))
80                    if candy:
81                        kid.add_candy(candy)
82            self.victims = {kid for kid in self.kids if kid.is_critical()}
83            if self.victims:
84                break
85            self.hosts = {host for host in self.hosts if host.candies}
86            if not any(visited_hosts[kid] != self.hosts for kid in self.kids):
87                break
88
89    def find_closest_host(self, kid, visited_hosts):
90        unvisited_hosts = [(host, self.calculate_distance(kid.get_position(), host.get_position()))
91                           for host in self.hosts if host not in visited_hosts]
92        unvisited_hosts.sort(key=lambda x: (x[1], x[0].get_position()))
93        return unvisited_hosts[0][0] if unvisited_hosts else None
94
95    def calculate_distance(self, position1, position2):
96        return ((position1[0] - position2[0]) ** 2 + (position1[1] - position2[1]) ** 2) ** 0.5

............
----------------------------------------------------------------------
Ran 12 tests in 0.001s

OK

Дискусия
История

f1import mathf1import math
22
3class Candy:3class Candy:
4    def __init__(self, mass, uranium):4    def __init__(self, mass, uranium):
5        self.mass = mass5        self.mass = mass
6        self.uranium = uranium6        self.uranium = uranium
77
8    def get_uranium_quantity(self):8    def get_uranium_quantity(self):
9        return self.mass * self.uranium9        return self.mass * self.uranium
1010
11    def get_mass(self):11    def get_mass(self):
12        return self.mass12        return self.mass
1313
1414
15class Person:15class Person:
16    def __init__(self, position):16    def __init__(self, position):
17        self.position = position17        self.position = position
1818
19    def get_position(self):19    def get_position(self):
20        return self.position20        return self.position
2121
22    def set_position(self, new_position):22    def set_position(self, new_position):
23        self.position = new_position23        self.position = new_position
2424
2525
26class Kid(Person):26class Kid(Person):
nn27    CRITICAL_URANIUM_MASS = 20
28 
27    def __init__(self, position, initiative):29    def __init__(self, position, initiative):
28        super().__init__(position)30        super().__init__(position)
29        self.initiative = initiative31        self.initiative = initiative
30        self.candies = []32        self.candies = []
3133
32    def get_initiative(self):34    def get_initiative(self):
33        return self.initiative35        return self.initiative
3436
35    def add_candy(self, candy):37    def add_candy(self, candy):
36        self.candies.append(candy)38        self.candies.append(candy)
3739
38    def is_critical(self):40    def is_critical(self):
39        uranium_mass = sum(candy.get_uranium_quantity() for candy in self.candies)41        uranium_mass = sum(candy.get_uranium_quantity() for candy in self.candies)
n40        return uranium_mass > 20n42        return uranium_mass > Kid.CRITICAL_URANIUM_MASS
41    43    
42    def move_to_host(self, host):44    def move_to_host(self, host):
43        self.set_position(host.get_position())45        self.set_position(host.get_position())
4446
4547
46class Host(Person):48class Host(Person):
47    def __init__(self, position, candies):49    def __init__(self, position, candies):
48        super().__init__(position)50        super().__init__(position)
49        self.candies = [Candy(mass, uranium) for mass, uranium in candies]51        self.candies = [Candy(mass, uranium) for mass, uranium in candies]
5052
51    def remove_candy(self, selection_function):53    def remove_candy(self, selection_function):
52        if not self.candies:54        if not self.candies:
53            return None55            return None
54        selected_candy = selection_function(self.candies)56        selected_candy = selection_function(self.candies)
55        self.candies.remove(selected_candy)57        self.candies.remove(selected_candy)
56        return selected_candy58        return selected_candy
5759
5860
59class FluxCapacitor:61class FluxCapacitor:
60    def __init__(self, participants):62    def __init__(self, participants):
61        self.kids = {p for p in participants if isinstance(p, Kid)}63        self.kids = {p for p in participants if isinstance(p, Kid)}
62        self.hosts = {p for p in participants if isinstance(p, Host)}64        self.hosts = {p for p in participants if isinstance(p, Host)}
63        self.victims = set()65        self.victims = set()
64        self.simulate_halloween()66        self.simulate_halloween()
6567
nn68    def get_victim(self):
69        return self.victims if self.victims else None
70 
66    def simulate_halloween(self):71    def simulate_halloween(self):
67        visited_hosts = {kid: set() for kid in self.kids}72        visited_hosts = {kid: set() for kid in self.kids}
68        while self.hosts and not self.victims:73        while self.hosts and not self.victims:
69            for kid in sorted(self.kids, key=lambda k: k.get_initiative(), reverse=True):74            for kid in sorted(self.kids, key=lambda k: k.get_initiative(), reverse=True):
70                closest_host = self.find_closest_host(kid, visited_hosts[kid])75                closest_host = self.find_closest_host(kid, visited_hosts[kid])
71                if closest_host:76                if closest_host:
72                    visited_hosts[kid].add(closest_host)77                    visited_hosts[kid].add(closest_host)
73                    kid.move_to_host(closest_host)78                    kid.move_to_host(closest_host)
74                    candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda c: c.get_mass()))79                    candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda c: c.get_mass()))
75                    if candy:80                    if candy:
76                        kid.add_candy(candy)81                        kid.add_candy(candy)
77            self.victims = {kid for kid in self.kids if kid.is_critical()}82            self.victims = {kid for kid in self.kids if kid.is_critical()}
78            if self.victims:83            if self.victims:
n79                return self.victimsn84                break
80            self.hosts = {host for host in self.hosts if host.candies}85            self.hosts = {host for host in self.hosts if host.candies}
81            if not any(visited_hosts[kid] != self.hosts for kid in self.kids):86            if not any(visited_hosts[kid] != self.hosts for kid in self.kids):
n82                return Nonen87                break
8388
84    def find_closest_host(self, kid, visited_hosts):89    def find_closest_host(self, kid, visited_hosts):
n85        unvisited_hosts = [host for host in self.hosts if host not in visited_hosts]n
86        closest_host = None
87        min_distance = float('inf')
88 
89        for host in unvisited_hosts:
90            distance = self.calculate_distance(kid.get_position(), host.get_position())90        unvisited_hosts = [(host, self.calculate_distance(kid.get_position(), host.get_position()))
91            if distance < min_distance or (distance == min_distance and self.is_closer_by_coordinates(host, closest_host)):91                           for host in self.hosts if host not in visited_hosts]
92                min_distance = distance92        unvisited_hosts.sort(key=lambda x: (x[1], x[0].get_position()))
93                closest_host = host93        return unvisited_hosts[0][0] if unvisited_hosts else None
94        return closest_host
9594
96    def calculate_distance(self, position1, position2):95    def calculate_distance(self, position1, position2):
97        return ((position1[0] - position2[0]) ** 2 + (position1[1] - position2[1]) ** 2) ** 0.596        return ((position1[0] - position2[0]) ** 2 + (position1[1] - position2[1]) ** 2) ** 0.5
t98 t
99    def is_closer_by_coordinates(self, host1, host2):
100        if not host2 or host1.get_position()[0] < host2.get_position()[0]:
101            return True
102        elif host1.get_position()[0] == host2.get_position()[0] and host1.get_position()[1] < host2.get_position()[1]:
103            return True
104        return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op

f1import mathf1import math
22
3class Candy:3class Candy:
4    def __init__(self, mass, uranium):4    def __init__(self, mass, uranium):
5        self.mass = mass5        self.mass = mass
6        self.uranium = uranium6        self.uranium = uranium
77
8    def get_uranium_quantity(self):8    def get_uranium_quantity(self):
9        return self.mass * self.uranium9        return self.mass * self.uranium
1010
11    def get_mass(self):11    def get_mass(self):
12        return self.mass12        return self.mass
1313
1414
15class Person:15class Person:
16    def __init__(self, position):16    def __init__(self, position):
17        self.position = position17        self.position = position
1818
19    def get_position(self):19    def get_position(self):
20        return self.position20        return self.position
2121
22    def set_position(self, new_position):22    def set_position(self, new_position):
23        self.position = new_position23        self.position = new_position
2424
2525
26class Kid(Person):26class 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 = initiative29        self.initiative = initiative
30        self.candies = []30        self.candies = []
3131
32    def get_initiative(self):32    def get_initiative(self):
33        return self.initiative33        return self.initiative
3434
35    def add_candy(self, candy):35    def add_candy(self, candy):
36        self.candies.append(candy)36        self.candies.append(candy)
3737
38    def is_critical(self):38    def is_critical(self):
39        uranium_mass = sum(candy.get_uranium_quantity() for candy in self.candies)39        uranium_mass = sum(candy.get_uranium_quantity() for candy in self.candies)
40        return uranium_mass > 2040        return uranium_mass > 20
41    41    
42    def move_to_host(self, host):42    def move_to_host(self, host):
43        self.set_position(host.get_position())43        self.set_position(host.get_position())
4444
nn45 
45class Host(Person):46class Host(Person):
46    def __init__(self, position, candies):47    def __init__(self, position, candies):
47        super().__init__(position)48        super().__init__(position)
48        self.candies = [Candy(mass, uranium) for mass, uranium in candies]49        self.candies = [Candy(mass, uranium) for mass, uranium in candies]
4950
50    def remove_candy(self, selection_function):51    def remove_candy(self, selection_function):
51        if not self.candies:52        if not self.candies:
52            return None53            return None
53        selected_candy = selection_function(self.candies)54        selected_candy = selection_function(self.candies)
54        self.candies.remove(selected_candy)55        self.candies.remove(selected_candy)
55        return selected_candy56        return selected_candy
5657
5758
58class FluxCapacitor:59class FluxCapacitor:
59    def __init__(self, participants):60    def __init__(self, participants):
60        self.kids = {p for p in participants if isinstance(p, Kid)}61        self.kids = {p for p in participants if isinstance(p, Kid)}
61        self.hosts = {p for p in participants if isinstance(p, Host)}62        self.hosts = {p for p in participants if isinstance(p, Host)}
62        self.victims = set()63        self.victims = set()
63        self.simulate_halloween()64        self.simulate_halloween()
6465
65    def simulate_halloween(self):66    def simulate_halloween(self):
66        visited_hosts = {kid: set() for kid in self.kids}67        visited_hosts = {kid: set() for kid in self.kids}
n67 n
68        while self.hosts and not self.victims:68        while self.hosts and not self.victims:
69            for kid in sorted(self.kids, key=lambda k: k.get_initiative(), reverse=True):69            for kid in sorted(self.kids, key=lambda k: k.get_initiative(), reverse=True):
70                closest_host = self.find_closest_host(kid, visited_hosts[kid])70                closest_host = self.find_closest_host(kid, visited_hosts[kid])
71                if closest_host:71                if closest_host:
72                    visited_hosts[kid].add(closest_host)72                    visited_hosts[kid].add(closest_host)
73                    kid.move_to_host(closest_host)73                    kid.move_to_host(closest_host)
n74 n
75                    candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda c: c.get_mass()))74                    candy = closest_host.remove_candy(lambda candies: max(candies, key=lambda c: c.get_mass()))
76                    if candy:75                    if candy:
77                        kid.add_candy(candy)76                        kid.add_candy(candy)
n78 n
79            self.victims = {kid for kid in self.kids if kid.is_critical()}77            self.victims = {kid for kid in self.kids if kid.is_critical()}
80            if self.victims:78            if self.victims:
81                return self.victims79                return self.victims
n82 n
83            self.hosts = {host for host in self.hosts if host.candies}80            self.hosts = {host for host in self.hosts if host.candies}
84            if not any(visited_hosts[kid] != self.hosts for kid in self.kids):81            if not any(visited_hosts[kid] != self.hosts for kid in self.kids):
85                return None82                return None
8683
87    def find_closest_host(self, kid, visited_hosts):84    def find_closest_host(self, kid, visited_hosts):
88        unvisited_hosts = [host for host in self.hosts if host not in visited_hosts]85        unvisited_hosts = [host for host in self.hosts if host not in visited_hosts]
89        closest_host = None86        closest_host = None
90        min_distance = float('inf')87        min_distance = float('inf')
9188
92        for host in unvisited_hosts:89        for host in unvisited_hosts:
93            distance = self.calculate_distance(kid.get_position(), host.get_position())90            distance = self.calculate_distance(kid.get_position(), host.get_position())
94            if distance < min_distance or (distance == min_distance and self.is_closer_by_coordinates(host, closest_host)):91            if distance < min_distance or (distance == min_distance and self.is_closer_by_coordinates(host, closest_host)):
95                min_distance = distance92                min_distance = distance
96                closest_host = host93                closest_host = host
t97 t
98        return closest_host94        return closest_host
9995
100    def calculate_distance(self, position1, position2):96    def calculate_distance(self, position1, position2):
101        return ((position1[0] - position2[0]) ** 2 + (position1[1] - position2[1]) ** 2) ** 0.597        return ((position1[0] - position2[0]) ** 2 + (position1[1] - position2[1]) ** 2) ** 0.5
10298
103    def is_closer_by_coordinates(self, host1, host2):99    def is_closer_by_coordinates(self, host1, host2):
104        if not host2 or host1.get_position()[0] < host2.get_position()[0]:100        if not host2 or host1.get_position()[0] < host2.get_position()[0]:
105            return True101            return True
106        elif host1.get_position()[0] == host2.get_position()[0] and host1.get_position()[1] < host2.get_position()[1]:102        elif host1.get_position()[0] == host2.get_position()[0] and host1.get_position()[1] < host2.get_position()[1]:
107            return True103            return True
108        return False104        return False
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op