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_2.py
1 #!/usr/bin/python3
2
3 import sys
4 import copy
5
6 cubemap={}
7 min_w=0
8 max_w=0
9 min_z=0
10 max_z=0
11 min_x=0
12 max_x=0
13 min_y=0
14 max_y=0
15
16 filename="p1_example.txt"
17 if len(sys.argv) > 1:
18     filename=sys.argv[1]
19
20 def get_active_neighbours(x,y,z,w):
21     global cubemap
22     sum=0
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:
28                         continue
29                     else:
30                         if cur_w in cubemap:
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] == "#":
35                                             sum+=1
36
37     return sum
38
39 def do_iteration():
40     global cubemap,min_w,max_w,min_x,max_x,min_y,max_y,min_z,max_z
41
42     min_x-=1
43     max_x+=1
44     min_y-=1
45     max_y+=1
46     min_z-=1
47     max_z+=1
48     min_w-=1
49     max_w+=1
50
51     new_cubemap=copy.deepcopy(cubemap)
52
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)
58                     if not w in cubemap:
59                         new_cubemap[w]={}
60                         cubemap[w]={}
61                     if not z in cubemap[w]:
62                         new_cubemap[w][z]={}
63                         cubemap[w][z]={}
64                     if not y in cubemap[w][z]:
65                         new_cubemap[w][z][y]={}
66                         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]="#"
74
75     cubemap=new_cubemap
76
77 # read the file
78 w=0
79 z=0
80 y=0
81 cubemap[w]={}
82 cubemap[w][z]={}
83 for line in open(filename, "r"):
84     line=line.rstrip()
85     cubemap[w][z][y]={}
86     x=0
87     for char in line:
88         cubemap[w][z][y][x]=char
89         x+=1
90     max_x=x-1
91     y+=1
92 max_y=y-1
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 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]=="#":
105                     active+=1
106
107 print("Active cubes:", active)