Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import sys
- import math
- import time
- from enum import Enum
- from typing import Tuple, List, Optional
- MAXLIFE = 100
- def idebug(*args):
- return
- print(*args, file=sys.stderr, flush=True)
- def debug(*args):
- # return
- print(*args, file=sys.stderr, flush=True)
- # Four legendary heroes were fighting for the land of Vindinium
- # Making their way in the dangerous woods
- # Slashing goblins and stealing gold mines
- # And looking for a tavern where to drink their gold
- class Type(Enum):
- AIR = '.'
- WALL = '#'
- TAVERN = 'T'
- MINE = 'M'
- class Tile:
- __slots__ = ['type', 'x', 'y', 'mine']
- def __init__(self, type: Type, x: int, y: int):
- self.type = type
- self.x = x
- self.y = y
- self.mine: Mine = None
- def distance(self, t):
- dx = self.x - t.x
- dy = self.y - t.y
- return math.sqrt(dx * dx + dy * dy)
- def __repr__(self):
- # return f'({self.x},{self.y})'
- return f'{self.type} ({self.x},{self.y})'
- class Mine:
- __slots__ = ['tile', 'owner']
- def __init__(self, tile: Tile):
- self.tile = tile
- self.owner: Hero = None
- def conquer(self, hero):
- self.owner = hero
- def __repr__(self):
- return f'MINE on ({self.tile.x}, {self.tile.y}) belongs to {self.owner}'
- class Hero:
- __slots__ = ['player_id', 'spawn_pos', 'tile', 'life', 'gold']
- def __init__(self, player_id: int, spawn_pos: Tile = None):
- self.player_id = player_id
- self.spawn_pos = spawn_pos
- self.tile = spawn_pos
- self.life = MAXLIFE
- self.gold = 0
- def update(self, x: int, y: int, life: int, gold: int):
- self.tile = [t for t in board if (t.x, t.y) == (x, y)][0]
- self.life, self.gold = life, gold
- def __repr__(self):
- return f'HERO {self.player_id} {self.tile} {self.life} {self.gold}'
- board = []
- players = []
- mines = []
- for i in range(4):
- players.append(Hero(player_id=i))
- size = int(input())
- idebug(size)
- for i in range(size):
- line = input()
- idebug(line)
- for j, c in enumerate(line):
- try:
- c = int(c)
- tile = Tile(type=Type.AIR, x=j, y=i)
- hero = players[c]
- hero.spawn_pos = tile
- hero.tile = tile
- except ValueError:
- if Type(c) == Type.MINE:
- tile = Tile(type=Type.MINE, x=j, y=i)
- mines.append(Mine(tile))
- else:
- tile = Tile(type=Type(c), x=j, y=i)
- finally:
- board.append(tile)
- taverns = [t for t in board if t.type == Type.TAVERN]
- walls = [t for t in board if t.type == Type.WALL]
- my_id = int(input()) # ID of your hero
- idebug(my_id)
- me = [h for h in players if h.player_id == my_id][0]
- # game loop
- while True:
- entity_count = int(input()) # the number of entities
- idebug(entity_count)
- for i in range(entity_count):
- line = input()
- idebug(line)
- inputs = line.split()
- entity_type = inputs[0] # HERO or MINE
- _id = int(inputs[1]) # the ID of a hero or the owner of a mine
- x = int(inputs[2]) # the x position of the entity
- y = int(inputs[3]) # the y position of the entity
- life = int(inputs[4]) # the life of a hero (-1 for mines)
- gold = int(inputs[5]) # the gold of a hero (-1 for mines)
- if entity_type == 'HERO':
- hero = players[_id]
- hero.update(x, y, life, gold)
- else:
- mine = [m for m in mines if (m.tile.x, m.tile.y) == (x, y)][0]
- mine.conquer(_id)
- # Write an action using print
- # To debug: print("Debug messages...", file=sys.stderr, flush=True)
- for hero in players:
- debug(hero)
- for mine in mines:
- debug(mine)
- # WAIT | NORTH | EAST | SOUTH | WEST
- if me.life <= 20:
- taverns_candidates = [(t, me.tile.distance(t)) for t in taverns]
- closest_tavern = min(taverns_candidates, key=lambda x: x[1])[0]
- action = f'MOVE {closest_tavern.x} {closest_tavern.y}'
- else:
- mines_to_conquer_candidates = [(m, me.tile.distance(m.tile) - m.owner / abs(m.owner) * 2) for m in mines if m.owner != my_id]
- if mines_to_conquer_candidates:
- closest_mine_to_conquer = min(mines_to_conquer_candidates, key=lambda x: x[1])[0]
- action = f'MOVE {closest_mine_to_conquer.tile.x} {closest_mine_to_conquer.tile.y}'
- else:
- heroes_to_fight_candidates = [(h, (me.life - h.life) / (1 + me.tile.distance(h.tile))) for h in players and h.player_id != my_id]
- print(action)
Add Comment
Please, Sign In to add comment