Skip to content

Instantly share code, notes, and snippets.

@eliasffyksen
Created August 8, 2022 20:01
Show Gist options
  • Save eliasffyksen/6eeee0c068f3d8e5170ca98fa8e27846 to your computer and use it in GitHub Desktop.
Save eliasffyksen/6eeee0c068f3d8e5170ca98fa8e27846 to your computer and use it in GitHub Desktop.
def do(snake: t.Tensor, action: int):
positions = snake.flatten().topk(2)[1]
[pos_cur, pos_prev] = [T(unravel(x, snake.shape)) for x in positions]
rotation = T([[0, -1], [1, 0]]).matrix_power(3 + action)
pos_next = (pos_cur + (pos_cur - pos_prev) @ rotation) % T(snake.shape)
if (snake[tuple(pos_next)] > 0).any():
return (snake[tuple(pos_cur)] - 2).item()
if snake[tuple(pos_next)] == -1:
pos_food = (snake == 0).flatten().to(t.float).multinomial(1)[0]
snake[unravel(pos_food, snake.shape)] = -1
else:
snake[snake > 0] -= 1
snake[tuple(pos_next)] = snake[tuple(pos_cur)] + 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment