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 nextSeatPart2(x, y, grid): if grid[x][y] == '.': return '.' occupied = 0 # go left i = x - 1 while width > i >= 0: if grid[i][y] == '#': occupied += 1 break if grid[i][y] == 'L': break i -= 1 # go right i = x + 1 while width > i >= 0: if grid[i][y] == '#': occupied += 1 break if grid[i][y] == 'L': break i += 1 # go up j = y + 1 while height > j >= 0: if grid[x][j] == '#': occupied += 1 break if grid[x][j] == 'L': break j += 1 # go down j = y - 1 while height > j >= 0: if grid[x][j] == '#': occupied += 1 break if grid[x][j] == 'L': break j -= 1 # go left/up i = x - 1 j = y - 1 while height > j >= 0 and width > i >= 0: if grid[i][j] == '#': occupied += 1 break if grid[i][j] == 'L': break i -= 1 j -= 1 i = x - 1 j = y + 1 while height > j >= 0 and width > i >= 0: if grid[i][j] == '#': occupied += 1 break if grid[i][j] == 'L': break i -= 1 j += 1 i = x + 1 j = y + 1 while height > j >= 0 and width > i >= 0: if grid[i][j] == '#': occupied += 1 break if grid[i][j] == 'L': break i += 1 j += 1 i = x + 1 j = y - 1 while height > j >= 0 and width > i >= 0: if grid[i][j] == '#': occupied += 1 break if grid[i][j] == 'L': break i += 1 j -= 1 if grid[x][y] == 'L' and occupied == 0: return '#' elif grid[x][y] == '#' and occupied >= 5: 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 nextRound2(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] = nextSeatPart2(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") 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 = nextRound2(grid) if not difference: break print(f"Part 2: {count}") if __name__ == "__main__": part1() part2()