-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11.1.py
194 lines (162 loc) · 4.36 KB
/
11.1.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
with open('input/11.txt') as f:
content = ['.' + x.strip() + '.' for x in f.readlines()]
# add boundary floor
content.insert(0, '.' * len(content[0]))
content.append('.' * len(content[0]))
rows = []
for row in content:
rows.append([x for x in row])
for row in rows:
print(row)
class Seat:
def __init__(self, seat):
# Floor by default
self._status = '.'
if seat == 'L':
self.set_empty()
elif seat == '#':
self.set_occupied()
def set_empty(self):
self._status = 'L'
def set_occupied(self):
self._status = '#'
def is_empty(self):
return self._status == 'L'
def is_occupied(self):
return self._status == '#'
def is_floor(self):
return self._status == '.'
def get_status(self):
return self._status
class Grid():
def __init__(self, grid):
self._last_change = 0
self._generation = 1
self._rows = len(grid)
self._columns = len(grid[0])
self._grid = [[Seat(seat) for seat in row] for row in grid]
def last_change(self):
return self._last_change
def get_occupied(self):
count = 0
for row in self._grid:
for column in row:
if column.is_occupied():
count += 1
return count
def get_empty(self):
count = 0
for row in self._grid:
for column in row:
if column.is_empty():
count += 1
return count
def draw(self):
print('\n')
for row in self._grid:
for column in row:
print(column.get_status(), end='')
print()
def check_adjacent(self, x, y):
# depth
search_min = -1
search_max = 2
adjacent = []
for row in range(search_min, search_max):
for column in range(search_min, search_max):
adj_row = x + row
adj_col = y + column
valid = True
if adj_row == x and adj_col == y:
valid = False
elif adj_row < 0 or adj_row >= self._rows:
valid = False
elif adj_col < 0 or adj_col >= self._columns:
valid = False
if valid:
adjacent.append(self._grid[adj_row][adj_col])
return adjacent
def update(self):
becomes_empty = []
becomes_occupied = []
for row in range(len(self._grid)):
for column in range(len(self._grid[0])):
check_adj = self.check_adjacent(row, column)
empty_count = []
occupied_count = []
for adj in check_adj:
if adj.is_empty():
empty_count.append(adj)
elif adj.is_occupied():
occupied_count.append(adj)
seat = self._grid[row][column]
if seat.is_empty():
if len(occupied_count) == 0:
becomes_occupied.append(seat)
elif seat.is_occupied():
if len(occupied_count) >= 4:
becomes_empty.append(seat)
for seat in becomes_empty:
seat.set_empty()
for seat in becomes_occupied:
seat.set_occupied()
self._last_change = len(becomes_empty) + len(becomes_occupied)
self._generation += 1
grid = Grid(rows)
grid.draw()
print(grid.last_change())
# print([x.get_status() for x in Grid(rows).check_adjacent(1,2)])
last = grid
grid.update()
print(grid.last_change())
while grid.last_change() != 0:
last = grid
grid.update()
grid.draw()
print(grid.get_empty())
print(grid.get_occupied())
# def adjacent(grid, x, y):
# rows = len(grid)
# columns = len(grid[0])
# for r in [-1, 0, 1]:
# for c in [-1, 0, 1]:
# if r == c == 0:
# continue
# if 0 <= grid[0]+r < rows and 0 <= grid[1]+c < columns:
# yield grid[x+r][y+c]
# x = 0
# y = 0
# for row in rows:
# for seat in row:
# print([''.join(adjacent(rows, x, y))])
# x += 1
# y += 1
# original = rows.copy()
# j = 0
# print(rows)
# for row in original:
# i = 0
# for seat in row:
# # Ignore floor
# if seat == '.':
# print('.')
# i += 1
# continue
# print(seat)
# empty = 0
# occupied = 0
# for adj in adjacent(original, i, j):
# if adj == 'L':
# empty += 1
# elif adj == '#':
# occupied += 1
# print('seat', seat, 'has', empty, 'empty seats adjacent')
# print('seat', seat, 'has', occupied, 'occupied seats adjacent')
# if seat == 'L' and occupied == 0:
# rows[j][i] = '#'
# elif seat == '#' and occupied > 4:
# rows[j][i] = 'L'
# i += 1
# j += 1
# for row in rows:
# print(row)