Day 17 - this is neither clean, not pretty, and I couldn't be bothered to do a more...
[advent-of-code-2020.git] / day17 / conwaycubes.py
1 #!/usr/bin/python3
2
3 import sys
4 import copy
5
6 cubemap={}
7 start_z=0
8 min_z=0
9 max_z=0
10 min_x=0
11 max_x=0
12 min_y=0
13 max_y=0
14
15 filename="p1_example.txt"
16 if len(sys.argv) > 1:
17     filename=sys.argv[1]
18
19 def print_map():
20     for z in [z for z in sorted([int(k) for k in cubemap.keys()])]:
21         print("z = ",z)
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()])]))
24         print()
25
26 def get_active_neighbours(x,y,z):
27     global cubemap
28     sum=0
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:
33                     continue
34                 else:
35                     if cur_z in cubemap:
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] == "#":
39                                     sum+=1
40
41     #print("Location: {},{},{}, Active neighbours: {}".format(x,y,z,sum))
42
43     return sum
44
45 def do_iteration():
46     global cubemap,min_x,max_x,min_y,max_y,min_z,max_z
47
48     min_x-=1
49     max_x+=1
50     min_y-=1
51     max_y+=1
52     min_z-=1
53     max_z+=1
54
55     new_cubemap=copy.deepcopy(cubemap)
56
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)
61                 if not z in cubemap:
62                     new_cubemap[z]={}
63                     cubemap[z]={}
64                 if not y in cubemap[z]:
65                     new_cubemap[z][y]={}
66                     cubemap[z][y]={}
67                 if not x in cubemap[z][y]:
68                     new_cubemap[z][y][x]="."
69                     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]="#"
74
75     cubemap=new_cubemap
76
77 # read the file
78 z=0
79 y=0
80 cubemap[z]={}
81 for line in open(filename, "r"):
82     line=line.rstrip()
83     cubemap[z][y]={}
84     x=0
85     for char in line:
86         cubemap[z][y][x]=char
87         x+=1
88     max_x=x-1
89     y+=1
90 max_y=y-1
91
92 print_map()
93
94 for iteration in range(0,6):
95     print("Iteration:", iteration+1)
96     do_iteration()
97     #print_map()
98
99 active=0
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]=="#":
104                 active+=1
105
106 print("Active cubes:", active)