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