You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

141 lines
2.9 KiB

from copy import deepcopy
def test(array, x, y):
test = []
if not x-1 < 0:
try:
test.append(array[x-1][y])
except IndexError:
pass
try:
test.append(array[x - 1][y + 1])
except IndexError:
pass
try:
test.append(array[x - 1][y - 1])
except IndexError:
pass
try:
test.append(array[x][y-1])
except IndexError:
pass
try:
test.append(array[x + 1][y - 1])
except IndexError:
pass
try:
test.append(array[x + 1][y])
except IndexError:
pass
try:
test.append(array[x][y + 1])
except IndexError:
pass
try:
test.append(array[x + 1][y + 1])
except IndexError:
pass
return test
def occupy_seats(array):
second = deepcopy(array)
changes = 0
for x in range(0, len(array)):
for y in range(0, len(array[x])):
tests = test(array, x, y)
d = tests.count('#')
if array[x][y] == 'L' and d == 0:
second[x][y] = '#'
changes += 1
if array[x][y] == "#" and d > 3:
second[x][y] = 'L'
changes += 1
return changes, second
def oob(space, y, x):
if y < 0: return True
if y >= len(space): return True
if x < 0: return True
if x >= len(space[y]): return True
return False
def visible_seats(space, y, x):
dirs = [(y, x) for x in [0, 1, -1] for y in [0, 1, -1]]
neighbors = []
for a, b in dirs:
if a == 0 and b == 0: continue
step = 1
def pos():
return (step * a + y, step * b + x)
while (not oob(space, *pos())):
loc = space[pos()[0]][pos()[1]]
if loc == "L" or loc == "#":
neighbors.append(loc)
break
step += 1
return neighbors
def occupy_seats2(array):
second = deepcopy(array)
changes = 0
for x in range(0, len(array)):
for y in range(0, len(array[x])):
d = visible_seats(array, x, y).count('#')
if array[x][y] == 'L' and d == 0:
second[x][y] = '#'
changes += 1
if array[x][y] == "#" and d > 4:
second[x][y] = 'L'
changes += 1
return changes, second
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input11") as f:
lines = f.readlines()
for i in range(0, len(lines)):
lines[i] = list(lines[i])
lines2 = deepcopy(lines)
while True:
c, lines = occupy_seats(lines)
if c == 0:
break
count = 0
for line in lines:
count += line.count('#')
print(count)
while True:
c, lines2 = occupy_seats2(lines2)
print(c)
if c == 0:
break
count2 = 0
for line in lines2:
count2 += line.count('#')
print(count2)