-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpuzzle_11_b.py
138 lines (120 loc) · 2.76 KB
/
puzzle_11_b.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
import sys
seats = []
i = 0
for line in sys.stdin:
line = line.strip()
seats.append([])
seats[i] = list(line)
i+=1
print seats
def count_visible_occupied(i,j):
count = 0
x = i-1
while x >= 0:
if seats[x][j] == '#':
count +=1
break
if seats[x][j] == 'L':
break
x -=1
x = i+1
while x < len(seats):
if seats[x][j] == '#':
count +=1
break
if seats[x][j] == 'L':
break
x +=1
y = j-1
while y >= 0:
if seats[i][y] == '#':
count +=1
break
if seats[i][y] == 'L':
break
y -=1
y = j+1
while y < len(seats[i]):
if seats[i][y] == '#':
count +=1
break
if seats[i][y] == 'L':
break
y +=1
x = i-1
y = j-1
while x >= 0 and y >= 0:
if seats[x][y] == '#':
count +=1
break
if seats[x][y] == 'L':
break
x -=1
y -=1
x = i-1
y = j+1
while x >= 0 and y < len(seats[x]):
if seats[x][y] == '#':
count +=1
break
if seats[x][y] == 'L':
break
x -=1
y +=1
x = i+1
y = j-1
while x < len(seats) and y >= 0:
if seats[x][y] == '#':
count +=1
break
if seats[x][y] == 'L':
break
x +=1
y -=1
x = i+1
y = j+1
while x < len(seats) and y < len(seats[x]):
if seats[x][y] == '#':
count +=1
break
if seats[x][y] == 'L':
break
x +=1
y +=1
return count
def get_value(i,j):
if seats[i][j] == '.':
return '.'
visible = count_visible_occupied(i,j)
#print i,j,visible
if seats[i][j] == 'L' and visible == 0:
return '#'
if seats[i][j] == '#' and visible >=5:
return 'L'
return seats[i][j]
changed = 1
while changed:
newseats =[]
changed = 0
for i in range(len(seats)):
newseats.append([])
for j in range(len(seats[i])):
newseats[i].append(get_value(i,j))
#print "======="
#print newseats
for i in range(len(seats)):
for j in range(len(seats[i])):
if seats[i][j] != newseats[i][j]:
#print "Changed", i, j, seats[i][j], newseats[i][j]
changed = 1
seats = []
for i in range(len(newseats)):
seats.append([])
for j in range(len(newseats[i])):
seats[i].append(newseats[i][j])
Total = 0
for i in range(len(seats)):
for j in range(len(seats[i])):
if seats[i][j] == '#':
Total +=1
print Total