import copy width = 0 height = 0 def nextSeat(x, y, grid): if grid[x][y] == '.': return '.' occupied = 0 forbidden = [-1, height, width] xs = [a for a in [x - 1, x, x + 1] if a not in forbidden] ys = [a for a in [y - 1, y, y + 1] if a not in forbidden] for xz in xs: for yz in ys: if x is xz and y is yz: continue if grid[xz][yz] == '#': occupied += 1 if grid[x][y] == 'L' and occupied == 0: return '#' elif grid[x][y] == '#' and occupied >= 4: return 'L' else: return grid[x][y] def nextRound(grid): next_grid = copy.deepcopy(grid) count = 0 difference = False for x in range(0, width): for y in range(0, height): next_grid[x][y] = nextSeat(x, y, grid) if next_grid[x][y] == '#': count += 1 if next_grid[x][y] != grid[x][y]: difference = True return next_grid, difference, count def part1(): file = open("input.txt") lines = file.readlines() grid = [] for line in lines: row = [] for spot in line.strip(): row.append(spot) grid.append(row) global width, height width = len(grid[0]) height = len(grid) while True: grid, difference, count = nextRound(grid) if not difference: break print(f"Part 1: {count}") def part2(): file = open("input.txt") if __name__ == "__main__": part1() part2()