This commit is contained in:
2020-12-12 13:05:10 +01:00
parent 074ddf2583
commit d858de5d4a
2 changed files with 188 additions and 0 deletions

90
day11/day11.py Normal file
View 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()