day11p1
This commit is contained in:
90
day11/day11.py
Normal file
90
day11/day11.py
Normal file
@@ -0,0 +1,90 @@
|
||||
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)
|
||||
|
||||
for x in range(0, width):
|
||||
for y in range(0, height):
|
||||
next_grid[x][y] = nextSeat(x, y, grid)
|
||||
|
||||
return next_grid, grid
|
||||
|
||||
|
||||
def countSeats(grid1):
|
||||
count = 0
|
||||
for x in range(0, width):
|
||||
for y in range(0, height):
|
||||
if grid1[x][y] == '#':
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
def equals(grid1, grid2):
|
||||
for x in range(0, width):
|
||||
for y in range(0, height):
|
||||
if grid1[x][y] != grid2[x][y]:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
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, oldGrid = nextRound(grid)
|
||||
if equals(grid, oldGrid):
|
||||
seats = countSeats(grid)
|
||||
break
|
||||
|
||||
print(f"Part 1: {seats}")
|
||||
|
||||
|
||||
def part2():
|
||||
file = open("input.txt")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part1()
|
||||
part2()
|
||||
Reference in New Issue
Block a user