-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_028.py
99 lines (82 loc) · 1.98 KB
/
_028.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
def right(count, pos, n):
xs = []
r, c = pos
while n > 0:
count += 1
r += 1
xs.append((count, (r, c)))
n -= 1
return xs
def down(count, pos, n):
xs = []
r, c = pos
while n > 0:
count += 1
c -= 1
xs.append((count, (r, c)))
n -= 1
return xs
def left(count, pos, n):
xs = []
r, c = pos
while n > 0:
count += 1
r -= 1
xs.append((count, (r, c)))
n -= 1
return xs
def up(count, pos, n):
xs = []
r, c = pos
while n > 0:
count += 1
c += 1
xs.append((count, (r, c)))
n -= 1
return xs
def last(xs):
return xs[len(xs) - 1]
def second_last(xs):
return xs[len(xs) - 2]
def is_diagonal(pos):
r, c = pos
return abs(r) == abs(c)
def spiral(n):
c, times, initial = 1, 1, (0, 0)
s = 0
while True:
ps = right(c, initial, times)
xs = list(map(lambda ps: ps[0], filter(lambda ps: is_diagonal(ps[1]), ps)))
if len(xs) > 0:
s += xs[0]
c, initial = last(ps)
x, _ = second_last(ps)
if n * n == x: break
ps = down(c, initial, times)
s += list(map(lambda ps: ps[0], filter(lambda ps: is_diagonal(ps[1]), ps)))[0]
c, initial = last(ps)
times += 1
ps = left(c, initial, times)
s += list(map(lambda ps: ps[0], filter(lambda ps: is_diagonal(ps[1]), ps)))[0]
c, initial = last(ps)
ps = up(c, initial, times)
s += list(map(lambda ps: ps[0], filter(lambda ps: is_diagonal(ps[1]), ps)))[0]
c, initial = last(ps)
times += 1
return s + 1
# print(spiral(1001))
def simple(n):
s, diff = 1, 2
answer = s
while s != n * n:
s += diff
answer += s
s += diff
answer += s
s += diff
answer += s
s += diff
answer += s
diff += 2
return answer
print(simple(1001))