-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (56 loc) · 1.96 KB
/
main.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import math
from enum import Enum
import random
Direction = Enum('Direction', ['right', 'down', 'left', 'up'])
def set_next_number(x: int, y: int, dir: Direction, number: int, field: list) -> tuple[int, int, list]:
match dir:
case Direction.right:
x += 1
case Direction.down:
y += 1
case Direction.left:
x -= 1
case Direction.up:
y -= 1
field[y][x] = number
return x,y,field
def update_direction(x: int, y: int, dir: Direction, field: list) -> Direction:
match dir:
case Direction.right:
if field[y+1][x] == -1:
return Direction.down
case Direction.down:
if field[y][x-1] == -1:
return Direction.left
case Direction.left:
if field[y-1][x] == -1:
return Direction.up
case Direction.up:
if field[y][x+1] == -1:
return Direction.right
return dir
def main(input: int):
digits = len(str(input)) # We need this to pad the various numbers to form a nice grid
# Calculate the side length of the square matrix
side = math.ceil(math.sqrt(input))
# Initialize the output matrix with -1
output = [[-1 for x in range(side)] for y in range(side)]
# Initialize starting position and number
center = math.ceil(side/2) - 1
x = y = center
current_number = 0
output[y][x] = current_number
current_number += 1
direction = Direction.right
while (current_number < input):
x,y,output = set_next_number(x,y,direction,current_number,output)
direction = update_direction(x,y,direction,output)
current_number += 1
for line in output:
if any(i >= 0 for i in line):
print(' '.join([' ' * digits if n == -1 else str(n).rjust(digits) for n in line]))
for i in range(5):
rand = random.randint(1,100)
print(f'Spiral for random number: {rand}')
main(rand)
print()