PPaste!

Back to the code AI

Home - All the pastes - Authored by Thooms

Raw version

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import sys
import math
from pprint import pformat
from random import randrange
import numpy as np

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

opponent_count = int(input())  # Opponent count
rand = None

def display_carte(carte):
    for l in carte.T:
        print(''.join(l), file=sys.stderr)

def scores(carte):
    return [sum(sum(c==str(i) for c in l) for l in carte) for i in range(opponent_count+1)]

def center_empty(carte):
    return np.average([x for x in range(35) for y in range(20) if carte[x,y]=='.']), np.average([y for x in range(35) for y in range(20) if carte[x,y]=='.'])




def in_game(x,y):
    return 0<=x<35 and 0<=y<20

def voisins_in_game(x,y):
    return [(x,y) for (x,y) in [(x+1,y),(x-1,y),(x,y+1),(x,y-1),] if in_game(x,y)]

def future(carte, me, enemies, my_action, enemy_action):
    carte = np.array(carte)
    #print(my_action, file=sys.stderr)
    if carte[my_action]=='.':
        carte[my_action]='0'
    x,y = me[0],me[1]
    
    for start in [(x+1,y),(x-1,y),(x,y+1),(x,y-1),]:
        to_explore = [start]
        explored = set()
        enclosed = set()
        closed = True
        while to_explore and closed:
            x,y = to_explore.pop()
            if (x,y) not in explored:
                explored.add((x,y))
                if not in_game(x,y):
                    closed = False
                else:
                    if carte[x,y]=='0':
                        pass
                    elif carte[x,y]=='.':
                        enclosed.add((x,y))
                        to_explore += [(x+1,y),(x-1,y),(x,y+1),(x,y-1),]
                    else:
                        closed = False
        #print(closed, file=sys.stderr)
        if closed:
            #print(len(enclosed), file=sys.stderr)
            for i,j in enclosed:
                carte[i,j] = '0'

    return carte
    

# game loop
while 1:
    game_round = int(input())
    print("round : %d" % game_round, file=sys.stderr)
    print(rand, file=sys.stderr)
    
    # x: Your x position
    # y: Your y position
    # back_in_time_left: Remaining back in time
    me = x, y, back_in_time_left = [int(i) for i in input().split()]

    
    enemies = [[int(j) for j in input().split()] for i in range(opponent_count)]
    # opponent_x: X position of the opponent
    # opponent_y: Y position of the opponent
    # opponent_back_in_time_left: Remaining back in time of the opponent
    print(enemies, file=sys.stderr)
    
    carte = np.array([list(input()) for i in range(20)]).T # One line of the map ('.' = free, '0' = you, otherwise the id of the opponent)
    #print(pformat(carte), file=sys.stderr)
    print(scores(carte), file=sys.stderr)
    
    directions = voisins_in_game(x,y)
    print(directions, file=sys.stderr)
    
    
    #total_iter=3
    total_iter = randrange(2,3)
    
    def best_score(carte, me, enemies, max_iter=5):
        if max_iter==total_iter:
            print(me,file=sys.stderr)
        print(max_iter,file=sys.stderr)
        x,y = me
        #####
        
        if carte[x,y]!='.':
            return(0)
        
        #####
        carte = future(carte, (x,y), enemies, (x,y), [])
        if max_iter==0:
            #print("score : %s" % scores(carte), file=sys.stderr)
            return scores(carte)[0]
        else:
            a=max(
                best_score(
                    carte,
                    (i,j),
                    enemies,
                    max_iter=max_iter-1
                )
                for i,j in voisins_in_game(x,y)
            )
            a = max(a,scores(carte)[0])
            if max_iter==total_iter:
                print(me, a,file=sys.stderr)
            return a
            
    
    s = [best_score(carte, (p[0],p[1]), enemies, max_iter=total_iter) for p in directions]

    max_s = max(s)
    i=-1
    while i==-1 or s[i]!=max_s:
        i = randrange(len(directions))
    
    best_play = directions[i]
    
    print(max_s, scores(carte)[0], file=sys.stderr)
    if max_s==scores(carte)[0] or max_s==0:
        """
        if rand is None or (x,y) == rand:
            rand = (randrange(35),randrange(20))
        best_play=rand
        """
        best_play = center_empty(carte)
            
    #best_play = max(directions, key=lambda p:best_score(carte, (p[0],p[1]), enemies, max_iter=total_iter))
    
    
    print("%d %d" % best_play)
    #print("%d %d" % directions[randrange(0,len(directions))])