Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Supply Line by freixodachamorra
def supply_line(mypos, supply_depots, enemies):
ROWS, COLS = 9, 12
def convert_coords(cords):
col, row = [i for i in cords]
col_number = ord(col) - 65
row_number = int(row) - 1
return (row_number, col_number)
def next_neighbor(row, col):
output = []
nexts_oddeven = [[[+1, 0], [+1, -1], [ 0, -1],
[-1, -1], [-1, 0], [ 0, +1]],
[[+1, +1], [+1, 0], [ 0, -1],
[-1, 0], [-1, +1], [ 0, +1]]]
if col % 2 == 1:
nexts = nexts_oddeven[1]
else:
nexts = nexts_oddeven[0]
for near in nexts:
new_row = row + near[1]
new_col = col + near[0]
if new_col >= 0 and new_col < COLS and new_row >= 0 and new_row < ROWS:
output.append((new_row, new_col))
return output
def flood():
aux = map_war[:]
step = 1
currents = [(i, j) for j in range(COLS) for i in range(ROWS) if aux[i][j] == step ]
while currents:
while currents:
row, col = currents.pop()
nexts = next_neighbor(row, col)
for cell in nexts:
if aux[cell[0]][cell[1]] == 55:
return step
if aux[cell[0]][cell[1]] == 0:
aux[cell[0]][cell[1]] = step + 1
step += 1
currents = [(i, j) for j in range(COLS) for i in range(ROWS) if aux[i][j] == step ]
return None
map_war = [[0 for _ in range(COLS)] for _ in range(ROWS)]
myrow, mycol = convert_coords(mypos)
map_war[myrow][mycol] = 1
for enemy in enemies:
itrow, itcol = convert_coords(enemy)
map_war[itrow][itcol] = 99
enemy_influence = next_neighbor(itrow, itcol)
for it in enemy_influence:
if map_war[it[0]][it[1]] == 0:
map_war[it[0]][it[1]] = 88
for supply in supply_depots:
suprow, supcol = convert_coords(supply)
if map_war[suprow][supcol] not in [88,99]:
map_war[suprow][supcol] = 55
return flood()
if __name__ == '__main__':
assert supply_line("B4", {"F4"}, {"D4"}) == 6, 'simple'
assert supply_line("A3", {"A9", "F5", "G8"}, {"B3", "G6"}) == 11, 'multiple'
assert supply_line("C2", {"B9", "F6"}, {"B7", "E8", "E5", "H6"}) is None, 'None'
assert supply_line("E5", {"C2", "B7", "F8"}, set()) == 4, 'no enemies'
assert supply_line("A5", {"A2", "B9"}, {"B3", "B7", "E3", "E7"}) == 13, '2 depots'
print('"Run" is good. How is "Check"?')
Aug. 5, 2018
Comments: