-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent_3_2.py
82 lines (66 loc) · 1.4 KB
/
advent_3_2.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
from math import sqrt, ceil, floor
MOVE_START = 0
MOVE_UP = 1
MOVE_DOWN = 2
MOVE_LEFT = 3
MOVE_RIGHT = 4
target, number = 361527, 361527
#target, number = 23, 23
# setup matrix
w = int(ceil(sqrt(number))) # w = h
matrix = [[0 for i in range(w)] for j in range(w)]
x0 = int(round(w/2.0)) - 1
y0 = w/2 + 1 - 1
x = x0
y = y0
matrix[y][x] = 1
move = MOVE_START
for i in range(2, number + 1):
if move == MOVE_START:
move = MOVE_RIGHT
x = x + 1
elif move == MOVE_RIGHT:
if matrix[y-1][x] == 0:
y = y -1
move = MOVE_UP
else:
x = x + 1
elif move == MOVE_UP:
if matrix[y][x-1] == 0:
x = x -1
move = MOVE_LEFT
else:
y = y - 1
elif move == MOVE_LEFT:
if matrix[y+1][x] == 0:
y = y + 1
move = MOVE_DOWN
else:
x = x - 1
elif move == MOVE_DOWN:
if matrix[y][x+1] == 0:
x = x + 1
move = MOVE_RIGHT
else:
y = y + 1
value = 0
if y - 1 >= 0 and x - 1 >= 0:
value = value + matrix[y-1][x-1]
if y - 1 >= 0:
value = value + matrix[y-1][x]
if y - 1 >= 0 and x + 1 < w:
value = value + matrix[y-1][x+1]
if x - 1 >= 0:
value = value + matrix[y][x-1]
if x + 1 < w:
value = value + matrix[y][x+1]
if y + 1 < w and x - 1 >= 0:
value = value + matrix[y+1][x-1]
if y + 1 < w:
value = value + matrix[y+1][x]
if y + 1 < w and x + 1 < w:
value = value + matrix[y+1][x+1]
if value > 361527:
print 'answer is', value
break
matrix[y][x] = value