From: Brett Parker Date: Thu, 17 Dec 2020 20:40:18 +0000 (+0000) Subject: Day 17 - this is neither clean, not pretty, and I couldn't be bothered to do a more... X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2020.git/commitdiff_plain/d8fa220992cb30ff6491df21b6bac912abbf9031?ds=inline;hp=3aa6b63e97b8dac07ff655a8c40595b2cecb6753 Day 17 - this is neither clean, not pretty, and I couldn't be bothered to do a more generic set of looping when only adding one dimension, hence extra file --- 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) diff --git a/day17/conwaycubes_2.py b/day17/conwaycubes_2.py new file mode 100755 index 0000000..bd5c021 --- /dev/null +++ b/day17/conwaycubes_2.py @@ -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) diff --git a/day17/input.txt b/day17/input.txt new file mode 100644 index 0000000..5690ac2 --- /dev/null +++ b/day17/input.txt @@ -0,0 +1,8 @@ +.##...#. +.#.###.. +..##.#.# +##...#.# +#..#...# +#..###.. +.##.#### +..#####. diff --git a/day17/p1_example.txt b/day17/p1_example.txt new file mode 100644 index 0000000..eedd3d2 --- /dev/null +++ b/day17/p1_example.txt @@ -0,0 +1,3 @@ +.#. +..# +###