def add_element(game_state, block_char): game_state = d(game_state) # this is to try out all rotations (and then location) blocks = list("IJLOSTZ") block = blocks.index(block_char) scores = [] moves = [] for rotate in range(4): score, move = move_element(game_state, block, rotate) scores.append(score) moves.append(move) the_rotate = scores.index(min(scores)) the_move = moves[the_rotate] game_state, score = drop_element(d(game_state), block, the_rotate, the_move, update=True) #print("hole_score: ", calculate_holes(game_state)) # game_state = resolve_rows(d(game_state)) # #print("add :", scores) return game_state, 10 * the_rotate + the_move, score
def __init__(self, p_actorColor, p_fromFieldX, p_fromFieldY, p_toFieldX, p_toFieldY, p_friendlyFigure, p_slainFigure): self.actorColor = p_actorColor self.fromFieldX = p_fromFieldX self.fromFieldY = p_fromFieldY self.toFieldX = p_toFieldX self.toFieldY = p_toFieldY self.friendlyFigure = d(p_friendlyFigure) self.slainFigure = d(p_slainFigure) self.isRochade = False self.towerComponentFrom = None self.towerComponentTo = None
def resolve_rows(game_state): game_state = d(game_state) rows_to_remove = [] for i, row in enumerate(game_state): if row == [1] * 10: rows_to_remove.append(i) for row_num in rows_to_remove[::-1]: del game_state[row_num] for _ in range(len(rows_to_remove)): game_state.append([0] * 10) return game_state
def move_element(game_state, block, rotate): game_state = d(game_state) # this is to try out all location given block and rotation scores = [] # ps(game_state) for move in range(10 - widths[block][rotate]): scores.append(drop_element(game_state, block, rotate, move)) move = scores.index(min(scores)) # #print("move :", scores) return min(scores), move
def calculate_drop_height(game_state, block, rotate, move): game_state = d(game_state) block_bases = bases[block][rotate] block_base_line = [point[1] for point in block_bases] # #print(block_base_line) roof = calcuate_roof(game_state) relevant_roof = [roof[i + move] for i in range(len(block_base_line))] # #print(block_base_line) # #print(relevant_roof) difference = [(100 * (x - y) - x) for x, y in zip(relevant_roof, block_base_line)] index = difference.index(max(difference)) return relevant_roof[index] # because do not add to existing space
def solve(s, n): if n == 81: return s for r,c in list(product(range(9), range(9))): if s[r][c] == 0: break row = s[r] col = [s[i][c] for i in range(9)] box = [[s[i+r/3*3][j+c/3*3] for i in range(3)] for j in range(3)] box = box[0]+box[1]+box[2] remain = [i for i in range(1,10) if i not in row+col+box] for i in remain: s[r][c] = i a = solve(d(s), n+1) if a != None: return a
def solve(s, n): if n == 81: return s for r, c in list(product(range(9), range(9))): if s[r][c] == 0: break row = s[r] col = [s[i][c] for i in range(9)] box = [[s[i + r / 3 * 3][j + c / 3 * 3] for i in range(3)] for j in range(3)] box = box[0] + box[1] + box[2] remain = [i for i in range(1, 10) if i not in row + col + box] for i in remain: s[r][c] = i a = solve(d(s), n + 1) if a != None: return a
def the_function(jsoninput): blockchain = jsoninput["tetrominoSequence"] movechain = [] game_state = d(game_state_empty) try: for block in list(blockchain): game_state, action, score = add_element(game_state, block) game_state = resolve_rows(game_state) game_state = resolve_rows(game_state) game_state = resolve_rows(game_state) game_state = resolve_rows(game_state) movechain.append(action) print("score:", score) # pp(game_state) ps(game_state) except: movechain = movechain + [0] * (len(blockchain) - len(movechain)) return {"actions": movechain}
def calculate_holes(game_state): game_state = d(game_state) # #print("calculate_holes") # pp(game_state) difference_state = [] for row_num in range(len(game_state) - 1): row_difference = [ x - y for x, y in zip(game_state[row_num], game_state[row_num + 1]) ] difference_state.append(row_difference) number_of_holes = sum([i.count(-1) for i in difference_state]) # pp(difference_state) # #print(number_of_holes) return number_of_holes
def drop_element(game_state, block, rotate, move, update=False): game_state = d(game_state) # this is to calculate the dropdown and how good it is block_cells = cells[block][rotate] block_bases = bases[block][rotate] # #print(block_cells) # #print(block_bases) height = calculate_drop_height(game_state, block, rotate, move) for point in block_cells: if point[1] + height >= 19: #print("OVERFLOW") pass try: if game_state[point[1] + height][point[0] + move] != 0: # row then column #print("ERROR") #print(point[1]+height, point[0]+move) #print(game_state[point[1]+height][point[0]+move]) # #print(point[1]+height, point[0]+move) # #print(game_state[point[1]+height][point[0]+move]) pass game_state[point[1] + height][point[0] + move] = 1 # if update: # #print([point[1]+height],[point[0]+move]) except: pass score = 100000 * calculate_holes(game_state) + calculate_borders( game_state) + random.randint(0, 3) # discourages (especially) overhang and borders if update: return game_state, score return score
def calculate_borders(game_state): # ps(game_state) game_state = d(game_state) borders = 0 for row in game_state: borders += 10 * [y - x for x, y in zip(row[1:], row[:-1])].count(-1) borders += 10 * [y - x for x, y in zip(row[:-1], row[1:])].count(-1) for i, row in enumerate(game_state[:-1]): borders += [x - y for x, y in zip(row[1:], game_state[i + 1][:-1])].count(-1) borders += [x - y for x, y in zip(row[:-1], game_state[i + 1][1:])].count(-1) for i, row in enumerate(game_state[:-2]): borders += [x - y for x, y in zip(row[1:], game_state[i + 2][:-1])].count(-1) borders += [x - y for x, y in zip(row[:-1], game_state[i + 2][1:])].count(-1) for i, row in enumerate(game_state[:-3]): borders += [x - y for x, y in zip(row[1:], game_state[i + 3][:-1])].count(-1) borders += [x - y for x, y in zip(row[:-1], game_state[i + 3][1:])].count(-1) for i, row in enumerate(game_state[:-4]): borders += 10 * [ x - y for x, y in zip(row[1:], game_state[i + 4][:-1]) ].count(-1) borders += 10 * [ x - y for x, y in zip(row[:-1], game_state[i + 4][1:]) ].count(-1) return borders
def solve(s, n): if n == 81: return s for r, c in list(product(range(9), range(9))): if s[r][c] == 0: break row = s[r] col = [s[i][c] for i in range(9)] box = [[s[i + r / 3 * 3][j + c / 3 * 3] for i in range(3)] for j in range(3)] box = box[0] + box[1] + box[2] remain = [i for i in range(1, 10) if i not in row + col + box] for i in remain: s[r][c] = i a = solve(d(s), n + 1) if a != None: return a #for i in range(len(sudokus)): result = 0 for i in range(50): print i temp = ([[j for j in line if j != 0] for line in sudokus[i]]) nonzero = sum([len(j) for j in temp]) s = solve(d(sudokus[i]), nonzero) result += int(''.join([str(s[0][i]) for i in range(3)])) print result
def calcuate_roof(game_state): game_state = d(game_state) columns = list(zip(*game_state)) # #print(columns) return [-column[::-1].index(max(column)) % 20 for column in columns]
# import matplotlib.pyplot as plt """ def ps(game_state): # plot state plt.figure(figsize=(2,4)) plt.axis([-1, 10, -1, 21]) for i, row in enumerate(game_state): for j, col in enumerate(row): if game_state[i][j] == 1: plt.scatter(j,i) plt.show() """ # In[ ]: game_state = [[0] * 10 for _ in range(20)] game_state_empty = d(game_state) # game_state[0] = [1]*10 # game_state[1][1] = 1 # y then x # game_state[1][2] = 1 # game_state[1][3] = 1 # game_state[2][2] = 1 # game_state[2][3] = 1 # # game_state[5][5] = 1 # # game_state[10] = [1]*10 # # game_state[11] = [1,0,1,0,1,0,1,0,1,0] # game_state_x = d(game_state) # In[ ]:
raw_input() def solve(s, n): if n == 81: return s for r,c in list(product(range(9), range(9))): if s[r][c] == 0: break row = s[r] col = [s[i][c] for i in range(9)] box = [[s[i+r/3*3][j+c/3*3] for i in range(3)] for j in range(3)] box = box[0]+box[1]+box[2] remain = [i for i in range(1,10) if i not in row+col+box] for i in remain: s[r][c] = i a = solve(d(s), n+1) if a != None: return a #for i in range(len(sudokus)): result = 0 for i in range(50): print i temp = ([[j for j in line if j != 0] for line in sudokus[i]]) nonzero = sum([len(j) for j in temp]) s = solve(d(sudokus[i]), nonzero) result += int(''.join([str(s[0][i]) for i in range(3)])) print result