From d0b8f3142ef77324d773a3ddf48b5e0eb2c57bcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Jos=C3=A9=20Guti=C3=A9rrez=20de=20Quevedo=20P=C3=A9?= =?UTF-8?q?rez?= Date: Thu, 17 Dec 2020 10:06:02 +0100 Subject: [PATCH] day 17, not optimizing this one --- 17/1.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 17/2.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 17/einput | 3 ++ 17/input | 8 +++++ 4 files changed, 186 insertions(+) create mode 100644 17/1.py create mode 100644 17/2.py create mode 100644 17/einput create mode 100644 17/input diff --git a/17/1.py b/17/1.py new file mode 100644 index 0000000..3fe4c6d --- /dev/null +++ b/17/1.py @@ -0,0 +1,90 @@ +with open("input") as f: + content = [x.strip() for x in f] + +occupied = "#" +free = "." +dimension = 21 +initial = int(dimension/3) +x = y = z = initial + +grid = [free] * dimension +for z in range(len(grid)): + grid[z] = [free] * dimension + for y in range(len(grid[z])): + grid[z][y] = [free] * dimension + +def count_2d_neighbours(grid, z, y, x): + points = [] + points.append(grid[z][y-1][x-1]) + points.append(grid[z][y-1][x]) + points.append(grid[z][y-1][x+1]) + points.append(grid[z][y][x-1]) + points.append(grid[z][y][x]) + points.append(grid[z][y][x+1]) + points.append(grid[z][y+1][x-1]) + points.append(grid[z][y+1][x]) + points.append(grid[z][y+1][x+1]) + + return points.count(occupied) + +def count_neighbours(grid, z, y, x): + count = 0 + count += count_2d_neighbours(grid, z, y, x) + count += count_2d_neighbours(grid, z-1, y, x) + count += count_2d_neighbours(grid, z+1, y, x) + + if grid[z][y][x] == occupied: + count -= 1 + + return count + + +def count_occupied(grid): + count = 0 + for z in range(len(grid)): + for y in range(len(grid[z])): + count += grid[z][y].count(occupied) + + return count + +def print_grid(grid): + for z in range(len(grid)): + count = 0 + for y in range(len(grid[z])): + count += grid[z][y].count(occupied) + if count: + print(f"z={z}") + for y in range(len(grid[z])): + print("".join(grid[z][y])) + + +for x in range(initial, initial + len(content)): + for y in range(initial, initial + len(content)): + grid[initial][x][y] = content[x-initial][y-initial] + +print_grid(grid) +#print(count_neighbours(grid, initial, initial)) + +for pass_ in range(6): + newgrid = [free] * dimension + for z in range(len(newgrid)): + newgrid[z] = [free] * dimension + for y in range(len(newgrid[z])): + newgrid[z][y] = [free] * dimension + + for z in range(1, len(grid) - 1): + for y in range(1, len(grid[z]) - 1): + for x in range(1, len(grid[z][y]) - 1): + neighbours = count_neighbours(grid, z, y, x) + if grid[z][y][x] == occupied and neighbours not in [2, 3]: + newgrid[z][y][x] = free + elif grid[z][y][x] == free and neighbours == 3: + newgrid[z][y][x] = occupied + else: + newgrid[z][y][x] = grid[z][y][x] + + print(f"pass: {pass_}\n-----------") + print_grid(newgrid) + grid = newgrid.copy() + +print(count_occupied(grid)) diff --git a/17/2.py b/17/2.py new file mode 100644 index 0000000..673b480 --- /dev/null +++ b/17/2.py @@ -0,0 +1,85 @@ +with open("input") as f: + content = [x.strip() for x in f] + +occupied = "#" +free = "." +dimension = 21 +initial = int(dimension/3) +x = y = z = w = initial + +def count_4d_neighbours(grid, w, z, y, x): + count = 0 + for w_ in range(w-1, w +2): + for z_ in range(z-1, z+2): + for y_ in range(y-1, y+2): + for x_ in range(x-1, x+2): + if grid[w_][z_][y_][x_] == occupied: + count +=1 + + if grid[w][z][y][x] == occupied: + count -= 1 + + return count + + +def count_occupied(grid): + count = 0 + for w in range(len(grid)): + for z in range(len(grid[w])): + for y in range(len(grid[w][z])): + count += grid[w][z][y].count(occupied) + + return count + +def print_grid(grid): + for w in range(len(grid)): + for z in range(len(grid[w])): + count = 0 + for y in range(len(grid[w][z])): + count += grid[w][z][y].count(occupied) + if count: + print(f"w={w} z={z}") + for y in range(len(grid[w][z])): + print("".join(grid[w][z][y])) + +grid = [None] * dimension +for w in range(len(grid)): + grid[w] = [None] * dimension + for z in range(len(grid[w])): + grid[w][z] = [None] * dimension + for y in range(len(grid[w][z])): + grid[w][z][y] = [free] * dimension + +for x in range(initial, initial + len(content)): + for y in range(initial, initial + len(content)): + grid[initial][initial][x][y] = content[x-initial][y-initial] + +print_grid(grid) +#print(count_neighbours(grid, initial, initial)) + +for pass_ in range(6): + newgrid = [free] * dimension + for w in range(len(newgrid)): + newgrid[w] = [free] * dimension + for z in range(len(newgrid[w])): + newgrid[w][z] = [free] * dimension + for y in range(len(newgrid[w][z])): + newgrid[w][z][y] = [free] * dimension + + for w in range(1, len(grid) - 1): + for z in range(1, len(grid[w]) - 1): + for y in range(1, len(grid[w][z]) - 1): + for x in range(1, len(grid[w][z][y]) - 1): + neighbours = count_4d_neighbours(grid, w, z, y, x) + if grid[w][z][y][x] == occupied and neighbours not in [2, 3]: + newgrid[w][z][y][x] = free + elif grid[w][z][y][x] == free and neighbours == 3: + newgrid[w][z][y][x] = occupied + else: + newgrid[w][z][y][x] = grid[w][z][y][x] + + print(f"pass: {pass_}\n-----------") + print_grid(newgrid) + grid = newgrid.copy() + +print(count_occupied(grid)) diff --git a/17/einput b/17/einput new file mode 100644 index 0000000..17630fd --- /dev/null +++ b/17/einput @@ -0,0 +1,3 @@ +.#. +..# +### \ No newline at end of file diff --git a/17/input b/17/input new file mode 100644 index 0000000..04e8187 --- /dev/null +++ b/17/input @@ -0,0 +1,8 @@ +..#..##. +#.....## +##.#.#.# +..#...#. +.###.... +######.. +.###..#. +..#..##.