X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2020.git/blobdiff_plain/3aa6b63e97b8dac07ff655a8c40595b2cecb6753..d8fa220992cb30ff6491df21b6bac912abbf9031:/day17/conwaycubes.py diff --git a/day17/conwaycubes.py b/day17/conwaycubes.py new file mode 100755 index 0000000..77e1d4b --- /dev/null +++ b/day17/conwaycubes.py @@ -0,0 +1,106 @@ +#!/usr/bin/python3 + +import sys +import copy + +cubemap={} +start_z=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 print_map(): + for z in [z for z in sorted([int(k) for k in cubemap.keys()])]: + print("z = ",z) + for y in [y for y in sorted([int(k) for k in cubemap[z].keys()])]: + print("".join([cubemap[z][y][x] for x in sorted([int(x) for x in cubemap[z][y].keys()])])) + print() + +def get_active_neighbours(x,y,z): + global cubemap + sum=0 + 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 x == cur_x and y == cur_y and z == cur_z: + continue + else: + if cur_z in cubemap: + if cur_y in cubemap[cur_z]: + if cur_x in cubemap[cur_z][cur_y]: + if cubemap[cur_z][cur_y][cur_x] == "#": + sum+=1 + + #print("Location: {},{},{}, Active neighbours: {}".format(x,y,z,sum)) + + return sum + +def do_iteration(): + global cubemap,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 + + new_cubemap=copy.deepcopy(cubemap) + + 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) + if not z in cubemap: + new_cubemap[z]={} + cubemap[z]={} + if not y in cubemap[z]: + new_cubemap[z][y]={} + cubemap[z][y]={} + if not x in cubemap[z][y]: + new_cubemap[z][y][x]="." + cubemap[z][y][x]="." + if cubemap[z][y][x] == "#" and active_neighbours != 2 and active_neighbours != 3: + new_cubemap[z][y][x]="." + elif cubemap[z][y][x] == "." and active_neighbours == 3: + new_cubemap[z][y][x]="#" + + cubemap=new_cubemap + +# read the file +z=0 +y=0 +cubemap[z]={} +for line in open(filename, "r"): + line=line.rstrip() + cubemap[z][y]={} + x=0 + for char in line: + cubemap[z][y][x]=char + x+=1 + max_x=x-1 + y+=1 +max_y=y-1 + +print_map() + +for iteration in range(0,6): + print("Iteration:", iteration+1) + do_iteration() + #print_map() + +active=0 +for z in cubemap.keys(): + for y in cubemap[z].keys(): + for x in cubemap[z][y].keys(): + if cubemap[z][y][x]=="#": + active+=1 + +print("Active cubes:", active)