day11p2
This commit is contained in:
136
day11/day11.py
136
day11/day11.py
@@ -29,6 +29,105 @@ def nextSeat(x, y, grid):
|
||||
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)
|
||||
|
||||
@@ -46,6 +145,23 @@ def nextRound(grid):
|
||||
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()
|
||||
@@ -72,6 +188,26 @@ def part1():
|
||||
|
||||
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__":
|
||||
|
||||
Reference in New Issue
Block a user