-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadventofcodeDay11Part2.py
More file actions
53 lines (38 loc) · 1.44 KB
/
adventofcodeDay11Part2.py
File metadata and controls
53 lines (38 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
seatLayout =[list(line.strip()) for line in open("/home/piyi/Documents/adventofcode2020/input.txt", 'r')]
numberofRows = len(seatLayout)
numberofCols = len(seatLayout[0])
def vis_occupied(x, y, seats, ydir, xdir):
x = x+xdir
y = y+ydir
if ((x > numberofRows-1) or (y> numberofCols-1) or( x<0) or (y <0)):
return 0
while(seats[x][y] == '.'):
x = x+xdir
y = y+ydir
if ((x > numberofRows-1) or (y> numberofCols-1) or( x<0) or (y <0)):
return 0
if seats[x][y] == '.': return 0
elif seats[x][y] == 'L': return 0
elif seats[x][y] == '#': return 1
def num_of_visible(x,y, seats):
return vis_occupied(x,y,seats, -1,0)+vis_occupied(x,y,seats,-1,-1)+vis_occupied(x,y,seats,-1,1) +vis_occupied(x,y,seats,0,-1)+vis_occupied(x,y,seats,0,1)+vis_occupied(x,y,seats,1,-1)+vis_occupied(x,y,seats,1,0)+vis_occupied(x,y,seats,1,1)
newseatLayout = seatLayout.copy()
change = True
while change is True:
change = False
newseatLayout = []
for row in range(numberofRows):
newrow = []
for col in range(numberofCols) :
if seatLayout[row][col] == 'L' and num_of_visible(row,col, seatLayout) == 0:
newrow.append('#')
change = True
elif seatLayout[row][col] == '#' and num_of_visible(row,col, seatLayout) >=5:
newrow.append('L')
change = True
else :
newrow.append(seatLayout[row][col])
newseatLayout.append(newrow)
seatLayout = newseatLayout.copy()
count = sum( [ listElem.count('#') for listElem in seatLayout])
print(count)