Skip to content

Instantly share code, notes, and snippets.

@maksverver
Created December 26, 2017 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maksverver/ca72a2c9dfc3fcdefba0e9df40fdc535 to your computer and use it in GitHub Desktop.
Save maksverver/ca72a2c9dfc3fcdefba0e9df40fdc535 to your computer and use it in GitHub Desktop.
from collections import defaultdict
import sys
N = int(sys.stdin.readline())
grid = defaultdict(lambda: 0)
grid[0, 0] = 1
x, y = 0, 0
dx, dy = 1, 0
while grid[x, y] <= N:
x += dx
y += dy
grid[x, y] = sum(
grid[nx, ny]
for nx in (x - 1, x, x + 1)
for ny in (y - 1, y, y + 1))
if grid[x - dy, y + dx] == 0:
dx, dy = -dy, dx
print(grid[x, y])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment