15 filename="p1_example.txt"
20 for z in [z for z in sorted([int(k) for k in cubemap.keys()])]:
22 for y in [y for y in sorted([int(k) for k in cubemap[z].keys()])]:
23 print("".join([cubemap[z][y][x] for x in sorted([int(x) for x in cubemap[z][y].keys()])]))
26 def get_active_neighbours(x,y,z):
29 for cur_z in range(z-1, z+2):
30 for cur_y in range(y-1, y+2):
31 for cur_x in range(x-1, x+2):
32 if x == cur_x and y == cur_y and z == cur_z:
36 if cur_y in cubemap[cur_z]:
37 if cur_x in cubemap[cur_z][cur_y]:
38 if cubemap[cur_z][cur_y][cur_x] == "#":
41 #print("Location: {},{},{}, Active neighbours: {}".format(x,y,z,sum))
46 global cubemap,min_x,max_x,min_y,max_y,min_z,max_z
55 new_cubemap=copy.deepcopy(cubemap)
57 for z in range(min_z, max_z+1):
58 for y in range(min_y, max_y+1):
59 for x in range(min_x, max_x+1):
60 active_neighbours=get_active_neighbours(x,y,z)
64 if not y in cubemap[z]:
67 if not x in cubemap[z][y]:
68 new_cubemap[z][y][x]="."
70 if cubemap[z][y][x] == "#" and active_neighbours != 2 and active_neighbours != 3:
71 new_cubemap[z][y][x]="."
72 elif cubemap[z][y][x] == "." and active_neighbours == 3:
73 new_cubemap[z][y][x]="#"
81 for line in open(filename, "r"):
94 for iteration in range(0,6):
95 print("Iteration:", iteration+1)
100 for z in cubemap.keys():
101 for y in cubemap[z].keys():
102 for x in cubemap[z][y].keys():
103 if cubemap[z][y][x]=="#":
106 print("Active cubes:", active)