-
Notifications
You must be signed in to change notification settings - Fork 0
/
p28.py
100 lines (77 loc) · 1.54 KB
/
p28.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import math
import numpy as np
#import matplotlib.pyplot as plt
size = 1001
def up(a, b):
return a-1, b
def right(a,b):
return a, b+1
def left (a, b):
return a, b-1
def down (a, b):
return a+1, b
matrix = np.zeros((size, size))
#print matrix
#plt.axis([0, size, 0, size])
#plt.ion()
#for i in range(10):
# y = np.random.random()
# plt.scatter(i, y)
# plt.pause(0.05)
#while True:
# plt.pause(0.05)
lengths = []
for i in range(1, size):
lengths.append(i)
lengths.append(i)
lengths.append(size-1)
#print lengths
x = (size - 1)/2
y = (size - 1)/2
counter = 1
number = 1
while (counter < 2*size):
#print counter
matrix[x, y] = number
#plt.scatter(x, y)
#plt.pause(0.05)
number += 1
if counter % 4 == 1:
for i in range(0, lengths[counter-1]):
x,y = right(x,y)
matrix[x,y] = number
#plt.scatter(x, y)
#plt.pause(0.05)
number += 1
elif counter % 4 == 2:
for i in range(0, lengths[counter-1]):
x,y = down(x,y)
matrix[x,y] = number
#plt.scatter(x, y)
#plt.pause(0.05)
number += 1
elif counter % 4 == 3:
for i in range(0, lengths[counter-1]):
x,y = left(x,y)
matrix[x,y] = number
#plt.scatter(x, y)
#plt.pause(0.05)
number += 1
else:
for i in range(0, lengths[counter-1]):
x,y = up(x,y)
matrix[x,y] = number
#plt.scatter(x, y)
#plt.pause(0.05)
number += 1
#print matrix
number -=1
counter += 1
#print matrix
#plt.pause(5)
output = 0
for i in range(0, size):
for j in range(0, size):
if i == j or i + j == size - 1:
output += matrix[i][j]
print int(output)