16 filename="p1_example.txt"
20 def get_active_neighbours(x,y,z,w):
23 for cur_w in range(w-1, w+2):
24 for cur_z in range(z-1, z+2):
25 for cur_y in range(y-1, y+2):
26 for cur_x in range(x-1, x+2):
27 if w == cur_w and x == cur_x and y == cur_y and z == cur_z:
31 if cur_z in cubemap[cur_w]:
32 if cur_y in cubemap[cur_w][cur_z]:
33 if cur_x in cubemap[cur_w][cur_z][cur_y]:
34 if cubemap[cur_w][cur_z][cur_y][cur_x] == "#":
40 global cubemap,min_w,max_w,min_x,max_x,min_y,max_y,min_z,max_z
51 new_cubemap=copy.deepcopy(cubemap)
53 for w in range(min_w, max_w+1):
54 for z in range(min_z, max_z+1):
55 for y in range(min_y, max_y+1):
56 for x in range(min_x, max_x+1):
57 active_neighbours=get_active_neighbours(x,y,z,w)
61 if not z in cubemap[w]:
64 if not y in cubemap[w][z]:
65 new_cubemap[w][z][y]={}
67 if not x in cubemap[w][z][y]:
68 new_cubemap[w][z][y][x]="."
69 cubemap[w][z][y][x]="."
70 if cubemap[w][z][y][x] == "#" and active_neighbours != 2 and active_neighbours != 3:
71 new_cubemap[w][z][y][x]="."
72 elif cubemap[w][z][y][x] == "." and active_neighbours == 3:
73 new_cubemap[w][z][y][x]="#"
83 for line in open(filename, "r"):
88 cubemap[w][z][y][x]=char
94 for iteration in range(0,6):
95 print("Iteration:", iteration+1)
100 for w in cubemap.keys():
101 for z in cubemap[w].keys():
102 for y in cubemap[w][z].keys():
103 for x in cubemap[w][z][y].keys():
104 if cubemap[w][z][y][x]=="#":
107 print("Active cubes:", active)