-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_euler15.py
More file actions
executable file
·64 lines (54 loc) · 1.84 KB
/
project_euler15.py
File metadata and controls
executable file
·64 lines (54 loc) · 1.84 KB
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
#!/bin/python3
def main():
max_col = 20
max_row = 20
nodes = {}
for row in range(0, max_row+1):
for col in range(0, max_col+1):
nodes[row, col] = Node(None, None)
for row in range(0, max_row):
for col in range(0, max_col):
if row < max_row:
nodes[row, col].right = nodes[row+1, col]
if col < max_col:
nodes[row, col].down = nodes[row, col+1]
for layer in range(1, max_row+1):
node_layer = []
root_node = nodes[max_row-layer, max_col-layer]
curr_node = root_node
while curr_node.right:
node_layer.append(curr_node)
curr_node = curr_node.right
curr_node = root_node
while curr_node.down:
curr_node = curr_node.down
node_layer.append(curr_node)
for node in node_layer:
queue = []
# top_node = nodes[max_row-layer, max_col-layer]
top_node = node
queue.append(top_node)
count = 0
while queue:
node = queue.pop(0)
if node.paths_found:
count += node.paths_found
print(node.paths_found)
print(count)
else:
if node.right:
queue.append(node.right)
if node.down:
queue.append(node.down)
if not node.right and not node.down:
count += 1
top_node.paths_found = count
print(max_row-layer, max_col-layer, count)
print(nodes[0, 0].paths_found)
class Node():
def __init__(self, right, down, paths_found=None):
self.right = right
self.down = down
self.paths_found = paths_found
if __name__ == '__main__':
main()