-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
30 lines (20 loc) · 848 Bytes
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from itertools import cycle
def spiralize(size: int) -> list[list[int]]:
def collision_in_two_steps() -> bool:
step_inside_bounds = y + y_inc * 2 in range(size) and x + x_inc * 2 in range(size)
return step_inside_bounds and grid[y + y_inc * 2][x + x_inc * 2] == 1
directions = cycle(((1, 0), (0, 1), (-1, 0), (0, -1)))
grid = [[0] * size for _ in range(size)]
x, y = 0, 0
direction_changes = 0
# Observed that the spiral pattern has a total of size - 1 direction changes
while direction_changes < size:
x_inc, y_inc = next(directions)
while y + y_inc in range(size) and x + x_inc in range(size):
grid[y][x] = 1
if collision_in_two_steps():
break
y += y_inc
x += x_inc
direction_changes += 1
return grid