# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
with open('input.txt') as t:
    text = t.read().splitlines()


# -

# vloer toevoegen aan alle randen
l_rand =len(text[0])
rand = '.' * l_rand
[text.insert(x, rand) for x in range(0,l_rand)]
[text.insert(len(text), rand) for _ in range(l_rand)]
wachtruimte = [rand + rij + rand for rij in text]

# **Deel 1**

# +
# def checkstoelen(wr, count):
#     count += 1
#     nwr = []
#     o = 0
#     for i,v in enumerate(wr):
#         nrij = ''
#         if (i < 5) | (i > (len(wr)-5)):
#             nrij = v
#         else:
#             for j,s in enumerate(v):
#                 if (j < 5) | (j > (len(v)-5)):
#                     nrij += s
#                 else:
#                     ad = [wr[i-1][j-1], wr[i-1][j], wr[i-1][j+1],
#                           wr[i][j-1], wr[i][j+1],
#                           wr[i+1][j-1], wr[i+1][j], wr[i+1][j+1]
#                          ]
#                     if s == 'L':
#                         if sum(x != '#' for x in ad) == 8:
#                             nrij += '#'
#                             o += 1
#                         else:
#                             nrij += s
#                     elif s == '#':
#                         if sum(x == s for x in ad) >= 4:
#                             nrij += 'L'
#                         else:
#                             nrij += s
#                             o += 1
#                     else:
#                         nrij += s
#         nwr.append(nrij)
#         veranderd = set(wr) != set(nwr)
#     return(nwr, veranderd, count, o) 

# +
# check = wachtruimte
# v = True
# i = 0
# while v == True:
#     check, v, i, bezet = checkstoelen(check, i)

# +
# print(bezet)
# -

# **Deel 2**




def checkaangrenzend(d, m, n, l):
    ll =[]
    r = list(range(1,l))
    for x in r:
        cellen = [d[m-x][n-x], d[m-x][n], d[m-x][n+x], d[m][n-x], d[m][n+x], d[m+x][n-x], d[m+x][n], d[m+x][n+x]]
        ll += [cellen]
    ll_list =  [[row[z] for row in ll] for z in range(len(cellen))]
    ad = []
    for richting in ll_list:
        maxr = len(richting)
        if 'L' in richting:
            maxr = richting.index('L')
        if '#' in richting[:maxr]:
            ad.append(True)
        else:
            ad.append(False)
    return(ad)


def checkstoelen2(wr, count, l):
    count += 1
    nwr = []
    o = 0
    for i,v in enumerate(wr):
        nrij = ''
        if (i < l) | (i >= (len(wr)-l)):
            nrij = v
        else:
            for j,s in enumerate(v):
                if (j < l) | (j >= (len(v)-l)):
                    nrij += s
                else:
                    omgeving = checkaangrenzend(wr, i, j, l)
                    if s == 'L':
                        if sum(omgeving) == 0:
                            nrij += '#'
                            o += 1
                        else:
                            nrij += s
                    elif s == '#':
                        if sum(omgeving) >= 5:
                            nrij += 'L'
                        else:
                            nrij += s
                            o += 1
                    else:
                        nrij += s
        nwr.append(nrij)
    veranderd = set(wr) != set(nwr)
    return(nwr, veranderd, count, o) 


check = wachtruimte
v = True
i = 0
checks = [wachtruimte]
while v == True:
    check, v, i, bezet = checkstoelen2(check, i, l_rand)

print(bezet)

StarcoderdataJupyterScriptsDedupFiltered