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

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

11 точки общо

12 успешни теста
0 неуспешни теста
Код
Скрий всички коментари

  1from math import dist
  2
  3
  4class Candy:
  5
  6    def __init__(self, mass, uranium):
  7        self.mass = mass
  8        self.uranium = uranium
  9
 10    def get_uranium_quantity(self):
 11        return self.mass * self.uranium
 12
 13    def get_mass(self):
 14        return self.mass
 15
 16    def __str__(self) -> str:
 17        return f"({self.mass}, {self.uranium})"
 18
 19
 20class Person:
 21
 22    def __init__(self, position):
 23        self.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    def __hash__(self) -> int:
 32        return self.position.__hash__()
 33
 34
 35class Kid(Person):
 36
 37    I_am_glowing_in_the_dark_magic_number = 20
 38
 39    def __init__(self, position, initiative):
 40        Person.__init__(self, position)
 41        self.initiative = initiative
 42        self.candies = []
 43        self.visited_hosts = set()
 44
 45    def get_initiative(self):
 46        return self.initiative
 47
 48    def add_candy(self, candy):
 49        if candy is not None:
 50            self.candies.append(candy)
 51
 52    def is_critical(self):
 53        return sum([candy.get_uranium_quantity() for candy in self.candies]) > self.I_am_glowing_in_the_dark_magic_number
 54
 55    def get_closest_host(self, hosts):
 56        available_hosts = [
 57            host for host in hosts if host not in self.visited_hosts]
 58
 59        if not available_hosts:
 60            return None
 61
 62        closest_host = min(available_hosts, key=lambda host:
 63                           (dist(host.get_position(), self.get_position()),
 64                            *host.get_position())
 65                           )
 66
 67        self.visited_hosts.add(closest_host)
 68        self.set_position(closest_host.get_position())
 69        return closest_host
 70
 71    def __str__(self) -> str:
 72        candy_str = ""
 73        for candy in self.candies:
 74            candy_str += str(candy) + " "
 75
 76        hosts_str = ""
 77        for host in self.visited_hosts:
 78            hosts_str += str(host.get_position()) + " "
 79
 80        return f"Kid at {self.position} with initiative {self.initiative} and candies {candy_str}visited hosts {hosts_str}"
 81
 82
 83class Host(Person):
 84
 85    def __init__(self, position, candies):
 86        Person.__init__(self, position)
 87        self.candies = [Candy(*candy) for candy in candies]
 88
 89    def remove_candy(self, func):
 90        if len(self.candies) == 0:
 91            return None
 92
 93        candy = func(self.candies)
 94        self.candies.remove(candy)
 95        return candy
 96
 97    def give_away_happiness(self, kids, func):
 98        for kid in sorted(kids, key=lambda kid: kid.get_initiative(), reverse=True):
 99            kid.add_candy(self.remove_candy(func))
100
101
102class FluxCapacitor:
103
104    def __init__(self, participants):
105        self.kids = [participant for participant in participants if isinstance(
106            participant, Kid)]
107        self.hosts = [participant for participant in participants if isinstance(
108            participant, Host)]
109
110    def get_victim(self):
111
112        # RIP dead_kids_tell_no_tales() 05.11.2023 - 07.11.2023 :pepesad:
113        while True:
114            kids_to_hosts = {
115                host: [] for host in self.hosts
116            }
117
118            for kid in self.kids:
119                host = kid.get_closest_host(self.hosts)
120                if host is not None:
121                    kids_to_hosts[host].append(kid)
122
123            self.are_there_candies = False
124
125            def compare(candies):
126                self.are_there_candies = True
127                return max(candies, key=lambda candy: candy.get_mass())
128
129            for host in self.hosts:
130                host.give_away_happiness(kids_to_hosts[host], compare)
131
132            if not self.are_there_candies:
133                return None
134
135            dead_kids = {kid for kid in self.kids if kid.is_critical()}
136
137            if dead_kids:
138                return dead_kids

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

OK

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

f1from math import distf1from math import dist
22
33
4class Candy:4class Candy:
nn5 
5    def __init__(self, mass, uranium):6    def __init__(self, mass, uranium):
6        self.mass = mass7        self.mass = mass
7        self.uranium = uranium8        self.uranium = uranium
89
9    def get_uranium_quantity(self):10    def get_uranium_quantity(self):
n10        return self.mass*self.uraniumn11        return self.mass * self.uranium
1112
12    def get_mass(self):13    def get_mass(self):
13        return self.mass14        return self.mass
1415
15    def __str__(self) -> str:16    def __str__(self) -> str:
16        return f"({self.mass}, {self.uranium})"17        return f"({self.mass}, {self.uranium})"
1718
1819
19class Person:20class Person:
nn21 
20    def __init__(self, position):22    def __init__(self, position):
21        self.position = position23        self.position = position
2224
23    def get_position(self):25    def get_position(self):
24        return self.position26        return self.position
2527
26    def set_position(self, position):28    def set_position(self, position):
27        self.position = position29        self.position = position
2830
29    def __hash__(self) -> int:31    def __hash__(self) -> int:
30        return self.position.__hash__()32        return self.position.__hash__()
3133
3234
33class Kid(Person):35class Kid(Person):
nn36 
37    I_am_glowing_in_the_dark_magic_number = 20
38 
34    def __init__(self, position, initiative):39    def __init__(self, position, initiative):
35        Person.__init__(self, position)40        Person.__init__(self, position)
36        self.initiative = initiative41        self.initiative = initiative
37        self.candies = []42        self.candies = []
38        self.visited_hosts = set()43        self.visited_hosts = set()
3944
40    def get_initiative(self):45    def get_initiative(self):
41        return self.initiative46        return self.initiative
4247
43    def add_candy(self, candy):48    def add_candy(self, candy):
44        if candy is not None:49        if candy is not None:
45            self.candies.append(candy)50            self.candies.append(candy)
4651
47    def is_critical(self):52    def is_critical(self):
n48        return sum([candy.get_uranium_quantity() for candy in self.candies]) > 20n53        return sum([candy.get_uranium_quantity() for candy in self.candies]) > self.I_am_glowing_in_the_dark_magic_number
4954
50    def get_closest_host(self, hosts):55    def get_closest_host(self, hosts):
51        available_hosts = [56        available_hosts = [
52            host for host in hosts if host not in self.visited_hosts]57            host for host in hosts if host not in self.visited_hosts]
5358
n54        if len(available_hosts) == 0:n59        if not available_hosts:
55            return None60            return None
5661
57        closest_host = min(available_hosts, key=lambda host:62        closest_host = min(available_hosts, key=lambda host:
58                           (dist(host.get_position(), self.get_position()),63                           (dist(host.get_position(), self.get_position()),
59                            *host.get_position())64                            *host.get_position())
60                           )65                           )
6166
62        self.visited_hosts.add(closest_host)67        self.visited_hosts.add(closest_host)
63        self.set_position(closest_host.get_position())68        self.set_position(closest_host.get_position())
64        return closest_host69        return closest_host
6570
66    def __str__(self) -> str:71    def __str__(self) -> str:
67        candy_str = ""72        candy_str = ""
68        for candy in self.candies:73        for candy in self.candies:
69            candy_str += str(candy) + " "74            candy_str += str(candy) + " "
7075
71        hosts_str = ""76        hosts_str = ""
72        for host in self.visited_hosts:77        for host in self.visited_hosts:
73            hosts_str += str(host.get_position()) + " "78            hosts_str += str(host.get_position()) + " "
7479
75        return f"Kid at {self.position} with initiative {self.initiative} and candies {candy_str}visited hosts {hosts_str}"80        return f"Kid at {self.position} with initiative {self.initiative} and candies {candy_str}visited hosts {hosts_str}"
7681
7782
78class Host(Person):83class Host(Person):
nn84 
79    def __init__(self, position, candies):85    def __init__(self, position, candies):
80        Person.__init__(self, position)86        Person.__init__(self, position)
81        self.candies = [Candy(*candy) for candy in candies]87        self.candies = [Candy(*candy) for candy in candies]
8288
83    def remove_candy(self, func):89    def remove_candy(self, func):
84        if len(self.candies) == 0:90        if len(self.candies) == 0:
85            return None91            return None
8692
87        candy = func(self.candies)93        candy = func(self.candies)
88        self.candies.remove(candy)94        self.candies.remove(candy)
89        return candy95        return candy
9096
91    def give_away_happiness(self, kids, func):97    def give_away_happiness(self, kids, func):
92        for kid in sorted(kids, key=lambda kid: kid.get_initiative(), reverse=True):98        for kid in sorted(kids, key=lambda kid: kid.get_initiative(), reverse=True):
93            kid.add_candy(self.remove_candy(func))99            kid.add_candy(self.remove_candy(func))
94100
95101
96class FluxCapacitor:102class FluxCapacitor:
nn103 
97    def __init__(self, participants):104    def __init__(self, participants):
n98        self.participants = participantsn105        self.kids = [participant for participant in participants if isinstance(
106            participant, Kid)]
107        self.hosts = [participant for participant in participants if isinstance(
108            participant, Host)]
99109
100    def get_victim(self):110    def get_victim(self):
n101        kids = [participant for participant in self.participants if isinstance(n
102            participant, Kid)]
103        hosts = [participant for participant in self.participants if isinstance(
104            participant, Host)]
105111
n106        def dead_kids_tell_no_tales():n112        # RIP dead_kids_tell_no_tales() 05.11.2023 - 07.11.2023 :pepesad:
113        while True:
107            kids_to_hosts = {114            kids_to_hosts = {
n108                host: [] for host in hostsn115                host: [] for host in self.hosts
109            }116            }
110117
n111            for kid in kids:n118            for kid in self.kids:
112                host = kid.get_closest_host(hosts)119                host = kid.get_closest_host(self.hosts)
113                if host is not None:120                if host is not None:
114                    kids_to_hosts[host].append(kid)121                    kids_to_hosts[host].append(kid)
115122
n116            are_there_candies = Falsen123            self.are_there_candies = False
117124
118            def compare(candies):125            def compare(candies):
n119                nonlocal are_there_candiesn
120                are_there_candies = True126                self.are_there_candies = True
127                return max(candies, key=lambda candy: candy.get_mass())
121128
n122                return max(n
123                    candies, key=lambda candy: candy.get_mass()
124                )
125 
126            for host in hosts:129            for host in self.hosts:
127                host.give_away_happiness(kids_to_hosts[host], compare)130                host.give_away_happiness(kids_to_hosts[host], compare)
128131
n129            if (not are_there_candies):n132            if not self.are_there_candies:
130                return None133                return None
131134
n132            dead_kids = {n135            dead_kids = {kid for kid in self.kids if kid.is_critical()}
133                kid for kid in kids if kid.is_critical()
134            }
135136
t136            return dead_kids if len(dead_kids) > 0 else dead_kids_tell_no_tales()t137            if dead_kids:
137 138                return dead_kids
138        return dead_kids_tell_no_tales()
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op