-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.py
172 lines (158 loc) · 3.46 KB
/
11.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
from collections import defaultdict
from copy import deepcopy
def print_seats(s):
for l in s:
print("".join(l))
def n_occupied(seats,x,y):
n=0
for i in range(x-1,x+2):
for j in range(y-1,y+2):
if i==x and j==y:
continue
if seats[i][j]=='#':
n+=1
return n
def n_occupied_all(seats,x,y):
n=0
#up
i=x-1
while i >0:
if seats[i][y]=='L':
break
if seats[i][y]=='#':
n+=1
break
i-=1
#down
i=x+1
while i <len(seats):
if seats[i][y]=='L':
break
if seats[i][y]=='#':
n+=1
break
i+=1
#left
j=y-1
while j >0:
if seats[x][j]=='L':
break
if seats[x][j]=='#':
n+=1
break
j-=1
#right
j=y+1
while j <len(seats[0]):
if seats[x][j]=='L':
break
if seats[x][j]=='#':
n+=1
break
j+=1
#ul
i=x-1
j=y-1
while i >=0 and j >=0:
if seats[i][j]=='L':
break
if seats[i][j]=='#':
n+=1
break
i-=1
j-=1
#ur
i=x-1
j=y+1
while i >=0 and j <len(seats[0]):
if seats[i][j]=='L':
break
if seats[i][j]=='#':
n+=1
break
i-=1
j+=1
#dl
i=x+1
j=y-1
while i <len(seats) and j >=0:
if seats[i][j]=='L':
break
if seats[i][j]=='#':
n+=1
break
i+=1
j-=1
#dr
i=x+1
j=y+1
while i <len(seats) and j <len(seats[0]):
if seats[i][j]=='L':
break
if seats[i][j]=='#':
n+=1
break
i+=1
j+=1
return n
def count_occupied(s):
n=0
for i in s:
for j in i:
if j == '#':
n+=1
return n
def count_occupied_all(s):
n=0
for i in s:
for j in i:
if j == '#':
n+=1
return n
seats=[]
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)),__file__.replace('.py','.in')),'r',encoding='utf8') as f:
for l in f:
l=l.strip()
seats.append(['.']+[x for x in l]+['.'])
filler_row=['.' for _ in range(len(seats[0]))]
seats=[filler_row]+seats
seats.append(filler_row)
last=seats
while True:
res=deepcopy(last)
for x in range(1,len(seats)-1):
for y in range(1,len(seats[0])-1):
if last[x][y]=='.':
continue
if last[x][y]=='L' and n_occupied(last,x,y)==0 :
res[x][y]="#"
continue
if last[x][y]=='#' and n_occupied(last,x,y)>=4 :
res[x][y]="L"
continue
if last==res:
break
last=res
fst_last=last
last=seats
while True:
res=deepcopy(last)
for x in range(1,len(seats)-1):
for y in range(1,len(seats[0])-1):
if last[x][y]=='.':
continue
if last[x][y]=='L' and n_occupied_all(last,x,y)==0 :
res[x][y]="#"
continue
if last[x][y]=='#' and n_occupied_all(last,x,y)>=5 :
res[x][y]="L"
continue
#if is_identical(last,res):
if last==res:
break
last=res
#print_seats(fst_last)
print(count_occupied(fst_last))
#print_seats(last)
print(count_occupied(last))