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
diff --git a/day17/conwaycubes_2.py b/day17/conwaycubes_2.py
new file mode 100755 (executable)
index 0000000..bd5c021
--- /dev/null
@@ -0,0 +1,107 @@
+#!/usr/bin/python3
+
+import sys
+import copy
+
+cubemap={}
+min_w=0
+max_w=0
+min_z=0
+max_z=0
+min_x=0
+max_x=0
+min_y=0
+max_y=0
+
+filename="p1_example.txt"
+if len(sys.argv) > 1:
+    filename=sys.argv[1]
+
+def get_active_neighbours(x,y,z,w):
+    global cubemap
+    sum=0
+    for cur_w in range(w-1, w+2):
+        for cur_z in range(z-1, z+2):
+            for cur_y in range(y-1, y+2):
+                for cur_x in range(x-1, x+2):
+                    if w == cur_w and x == cur_x and y == cur_y and z == cur_z:
+                        continue
+                    else:
+                        if cur_w in cubemap:
+                            if cur_z in cubemap[cur_w]:
+                                if cur_y in cubemap[cur_w][cur_z]:
+                                    if cur_x in cubemap[cur_w][cur_z][cur_y]:
+                                        if cubemap[cur_w][cur_z][cur_y][cur_x] == "#":
+                                            sum+=1
+
+    return sum
+
+def do_iteration():
+    global cubemap,min_w,max_w,min_x,max_x,min_y,max_y,min_z,max_z
+
+    min_x-=1
+    max_x+=1
+    min_y-=1
+    max_y+=1
+    min_z-=1
+    max_z+=1
+    min_w-=1
+    max_w+=1
+
+    new_cubemap=copy.deepcopy(cubemap)
+
+    for w in range(min_w, max_w+1):
+        for z in range(min_z, max_z+1):
+            for y in range(min_y, max_y+1):
+                for x in range(min_x, max_x+1):
+                    active_neighbours=get_active_neighbours(x,y,z,w)
+                    if not w in cubemap:
+                        new_cubemap[w]={}
+                        cubemap[w]={}
+                    if not z in cubemap[w]:
+                        new_cubemap[w][z]={}
+                        cubemap[w][z]={}
+                    if not y in cubemap[w][z]:
+                        new_cubemap[w][z][y]={}
+                        cubemap[w][z][y]={}
+                    if not x in cubemap[w][z][y]:
+                        new_cubemap[w][z][y][x]="."
+                        cubemap[w][z][y][x]="."
+                    if cubemap[w][z][y][x] == "#" and active_neighbours != 2 and active_neighbours != 3:
+                        new_cubemap[w][z][y][x]="."
+                    elif cubemap[w][z][y][x] == "." and active_neighbours == 3:
+                        new_cubemap[w][z][y][x]="#"
+
+    cubemap=new_cubemap
+
+# read the file
+w=0
+z=0
+y=0
+cubemap[w]={}
+cubemap[w][z]={}
+for line in open(filename, "r"):
+    line=line.rstrip()
+    cubemap[w][z][y]={}
+    x=0
+    for char in line:
+        cubemap[w][z][y][x]=char
+        x+=1
+    max_x=x-1
+    y+=1
+max_y=y-1
+
+for iteration in range(0,6):
+    print("Iteration:", iteration+1)
+    do_iteration()
+    #print_map()
+
+active=0
+for w in cubemap.keys():
+    for z in cubemap[w].keys():
+        for y in cubemap[w][z].keys():
+            for x in cubemap[w][z][y].keys():
+                if cubemap[w][z][y][x]=="#":
+                    active+=1
+
+print("Active cubes:", active)