<jupyter_start><jupyter_text>MIT LicenseCopyright (c) 2020 <NAME>Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.<jupyter_code>input = '''..#....#
##.#..##
.###....
#....#.#
#.######
##.#....
#.......
.#......'''

lines = input.split('\n')

active = []
active4d = []

x = len(lines)
y = len(lines[0])
z = 1
w = 1

for i in range(len(lines)):
    for j in range(len(lines[i])):
        if lines[i][j] == '#':
            active.append((i, j, 0))
            active4d.append((i, j, 0, 0))
from itertools import product

neighbors = list(product([-1, 0, 1], repeat=3))
neighbors.remove((0, 0, 0))

def count_neighbors_active(active, x, y, z):
    count = 0
    for (dx, dy, dz) in neighbors:
        if (x + dx, y + dy, z + dz) in active:
            if z == 0 and dz == 1:
                count += 2
            else:
                count += 1
    return count
    
def part1(active, xrange, yrange, zrange, cycles=6):
    xs = 0
    ys = 0
    for c in range(cycles):
        xrange += 1
        yrange += 1
        zrange += 1
        xs -= 1
        ys -= 1
        
        new_active = []
        for x in range(xs, xrange):
            for y in range(ys, yrange):
                for z in range(0, zrange):
                    count = count_neighbors_active(active, x, y, z)
                    if (x, y, z) in active:
                        if count == 2 or count == 3:
                            new_active.append((x, y, z))
                    elif count == 3:
                        new_active.append((x, y, z))
        active = new_active
    
    count = 0
    for (x, y, z) in active:
        if z == 0:
            count += 1
        else:
            count += 2
            
    return count

part1(active, x, y, z)
from itertools import product

neighbors4d = list(product([-1, 0, 1], repeat=4))
neighbors4d.remove((0, 0, 0, 0))

def count_neighbors_active4d(active, x, y, z, w):
    count = 0
    for (dx, dy, dz, dw) in neighbors4d:
        if (x + dx, y + dy, z + dz, w + dw) in active:
            dcount = 1
            if z == 0 and dz == 1:
                dcount *= 2
            if w == 0 and dw == 1:
                dcount *= 2
            count += dcount
    return count
    
def part2(active, xrange, yrange, zrange, wrange, cycles=6):
    xs = 0
    ys = 0
    for c in range(cycles):
        xrange += 1
        yrange += 1
        zrange += 1
        wrange += 1
        xs -= 1
        ys -= 1
        
        new_active = []
        for x in range(xs, xrange):
            for y in range(ys, yrange):
                for z in range(0, zrange):
                    for w in range(0, wrange):
                        count = count_neighbors_active4d(active, x, y, z, w)
                        if (x, y, z, w) in active:
                            if count == 2 or count == 3:
                                new_active.append((x, y, z, w))
                        elif count == 3:
                            new_active.append((x, y, z, w))
        active = new_active
        
    count = 0
    for (x, y, z, w) in active:
        dcount = 1
        if z != 0:
            dcount *= 2
        if w != 0:
            dcount *= 2
        count += dcount
        
    return count

part2(active4d, x, y, z, w)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11528466808410111, 'fraction_numerical': 0.020316560359083392, 'mean_word_length': 1.698534098151689, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '355281', 'license': 'MIT', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1274, 'n_words': 489, 'path': 'day17.ipynb', 'repo_name': 'sohrak/aoc2020', 'size': '4241'}