Advent of Code 2020: Python Solution Day 17
Tags: adventofcode, algorithms, python, challenge
Published: 2020-12-17 07:12:50

I feel ashamed now because I could not understand problem correctly. And I basically copied pasted code from [here](https://topaz.github.io/paste/#XQAAAQBxCQAAAAAAAAAyGUj/T2CE9VryFNMnsYJuiXKzl9PLuR8W0Fg3eLdVHWXy6hwImG+jiKemZUDIgvqj0V7gIgGI4LwI0bEH2gmwEYP8FJOoh2jVAJcPFBvo0oE7X6/nHrrTQfvkNCGGceV8DztXEXXpg7GsvrxNKTg347fIO8VO4ym1Oy8+/Ew6DyUQkNPQTNvDMSLUhSzkiMAJoiyIsW2qAsO9UE3mASOAc7mht7qLWke3yayTIcROsLEjX9YZWgELf28dvaUTRj1bZVqBLXEiwa7eE0qtlfodP5VBv/UAJNGIvuzT2mNF0sAShO5MLBnEUFNqWpRswtjiqRGBi19G4p+BgzfgGc1iSFoZX2FE30YEYKmOv6WNKrqVSf04vMDupTP2x13QLfRENNJaRgOguYWEGzBa8DNBwpVr7Z1y9TN7rmNvlZdsA3nzECpA2947Fe1Tk1iDYcumpYaXDNEFwz8DAlBsmONA3b//eFf/Llrp4AGWWnMd3YxB22wd1EAE/IboOl0GbWQexN63APIOroH7VAbvNxqb4791R/8Us2tJWJap6OiYV82Biec4hKRAy3ddn0g8jMk/5RacpQdP1ZIqaFf8gDmHDTkkwLGZv5SH4kKqfJvfvdFhdio0mLeRTnnFVhAlrlrQL6x80hsRyeKlDs6TgDsPSj4afimebJi9TfX0YT6uPnREpueBmR4/qjpVsPl+5tB/WLYafMrzkCEEPa9TXETPhQaYs8LVjpSguCeFsk4a3hJn1o6cCcwiC/74y/s=) and then refactored a little bit. So all the credit goes to the author.

## Part 1

```python 
def part1():
    with open("day17.txt") as fp:
        lines = [fline.rstrip() for fline in fp.readlines()]
        
    f = lines 
    cubes = {(i, j, 0):lines[i][j] 
             for i in range(len(lines)) 
             for j in range(len(lines[0]))}
    for i in range(6):
        cubes = simulate(cubes)

    print(f"Part 1 Solution: {list(cubes.values()).count('#')}")


def simulate(cubes):
    new_cube = {}
    for c in cubes:
        x = checkNeighbors(c, cubes)
        if cubes[c] == '#':
            if x == 2 or x == 3:
                new_cube[c] = '#'
            else:
                new_cube[c] = '.'
            n = findNeighbors(c)
            for x in n:
                if x not in cubes:
                    k = checkNeighbors(x, cubes)
                    if k == 3:
                        new_cube[x] = '#'
        elif cubes[c] == '.':
            if x == 3:
                new_cube[c] = '#'
            else:
                new_cube[c] = '.'
    return new_cube


def findNeighbors(c):
    neighbors = [(c[0]+i, c[1]+j, c[2]+k) 
                 for i in range(-1, 2) 
                 for j in range(-1, 2) 
                 for k in range(-1, 2) 
                 if not (i == 0 and j == 0 and k == 0)]
    return neighbors


def checkNeighbors(c, cubes):
    n = findNeighbors(c)
    neighbors_count = len([x for x in n if cubes.get(x)=="#"])
    return neighbors_count
part1()
```

```python

def part2():
    with open("day17.txt") as fp:
        lines = [fline.rstrip() for fline in fp.readlines()]
    f = lines 
    cubes = {(i, j, 0, 0):lines[i][j] 
             for i in range(len(lines)) 
             for j in range(len(lines[0]))}
    for i in range(6):
        cubes = simulate4d(cubes)
    
    print(f"Part 2 Solution: {list(cubes.values()).count('#')}")


def simulate4d(cubes):
    new = {}
    for c in cubes:
        x = checkNeighbors4d(c, cubes)
        if cubes[c] == '#':
            if x == 2 or x == 3:
                new[c] = '#'
            else:
                new[c] = '.'
            n = findNeighbors4d(c)
            for x in n:
                if x not in cubes:
                    k = checkNeighbors4d(x, cubes)
                    if k == 3:
                        new[x] = '#'
        elif cubes[c] == '.':
            if x == 3:
                new[c] = '#'
            else:
                new[c] = '.'
    return new


def findNeighbors4d(c):
    neighbors = [(c[0]+i, c[1]+j, c[2]+k, c[3]+w) 
                 for i in range(-1, 2) 
                 for j in range(-1, 2) 
                 for k in range(-1, 2) 
                 for w in range(-1, 2) 
                 if not (i == 0 and j == 0 and k == 0 and w == 0)]
    return neighbors


def checkNeighbors4d(c, cubes):
    n = findNeighbors4d(c)
    neighbors_count = len([x for x in n if cubes.get(x)=="#"])
    return neighbors_count
part2()
```