Балтаев Глеб Иркинович 341 OneDArcade 18547
Горборуков Владимир Владимирович, 522 OneDArcade 18090
f1import asynciof1import asyncio
22
3class Monster:3class Monster:
44
n5    def __init__(self, name, position, interval, strength):n5    def __init__(self, name, position, delay, power):
6        self.name = name6        self.name = name
7        self.position = position7        self.position = position
n8        self.interval = intervaln8        self.delay = delay
9        self.strength = strength9        self.power = power
10        self.is_alive = True10        self.alive = True
1111
n12    async def run(self, start_sync, end_sync):n12    async def run(self, start_barrier, end_barrier):
13        step_counter = 013        counter = 0
14        while True:14        while True:
n15            await start_sync.wait()n15            await start_barrier.wait()
16            step_counter += 116            counter += 1
17            if step_counter == self.interval:17            if counter == self.delay:
18                self.position += 118                self.position += 1
n19                step_counter = 0n19                counter = 0
20            await end_sync.wait()20            await end_barrier.wait()
2121
n22async def game(monsters, start_sync, end_sync, rounds):n22async def game(monsters, start_barrier, end_barrier, epochs):
23    for _ in range(rounds):23    for epoch in range(epochs):
24        await start_sync.wait()24        await start_barrier.wait()
25        await end_sync.wait()25        await end_barrier.wait()
26        active_monsters = [m for m in monsters if m.is_alive]26        alive_monsters = [m for m in monsters if m.alive]
27        if not active_monsters:27        if not alive_monsters:
28            return 'All dead'28            return 'All dead'
n29        collision = Nonen29        fighters = None
30        for i, monster1 in enumerate(active_monsters[:-1]):30        for idxfighter1 in enumerate(alive_monsters[:-1]):
31            if collision:31            if fighters:
32                break32                break
n33            for monster2 in active_monsters[i + 1:]:n
34                if monster1.position == monster2.position:
35                    collision = [monster1, monster2]
36                    break
37        if collision:
38            monster1, monster2 = collision
39            if monster1.strength == monster2.strength:
40                monster1.is_alive = False
41                monster2.is_alive = False
42            elif monster1.strength > monster2.strength:
43                monster1.strength -= monster2.strength
44                monster2.is_alive = False
45            else:33            else:
n46                monster2.strength -= monster1.strengthn34                for fighter2 in alive_monsters[idx + 1:]:
35                    if fighter1.position == fighter2.position:
36                        fighters = [fighter1, fighter2]
37                        break
38        if fighters:
39            fighter1, fighter2 = fighters
40            if fighter1.power == fighter2.power:
47                monster1.is_alive = False41                fighter1.alive = False
42                fighter2.alive = False
43            elif fighter1.power > fighter2.power:
44                fighter1.power -= fighter2.power
45                fighter2.alive = False
46            else:
47                fighter2.power -= fighter1.power
48                fighter1.alive = False
48        if len(active_monsters) < 2:49        if len(alive_monsters) < 2:
49            break50            break
n50    survivors = [m for m in monsters if m.is_alive]n51    alive_monsters = [m for m in monsters if m.alive]
51    if not survivors:52    if not alive_monsters:
52        return 'All dead'53        return 'All dead'
t53    elif survivors != monsters:t54    elif alive_monsters != monsters:
54        return ', '.join([m.name for m in survivors])55        return ', '.join([m.name for m in alive_monsters])
55    else:56    else:
56        return 'All flee'57        return 'All flee'
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op