Created
August 8, 2022 20:01
-
-
Save eliasffyksen/6eeee0c068f3d8e5170ca98fa8e27846 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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