-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdayElevenB.py
67 lines (57 loc) · 1.85 KB
/
dayElevenB.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
from copy import deepcopy
space = []
with open("dayEleven.txt") as file:
for line in file.readlines():
temp = []
for ch in line:
if ch != "\n":
temp.append(ch)
space.append(temp)
def no_occupied_seat(a, b, grid, x, y):
if a < 0 or a >= len(grid) or b < 0 or b >= len(grid[0]):
return True
if grid[a][b] == "L":
return True
if grid[a][b] == "#":
return False
return no_occupied_seat(a+x, b +y, grid, x, y)
def is_occupied_seat(a, b, grid, x, y):
if a < 0 or a >= len(grid) or b < 0 or b >= len(grid[0]):
return False
if grid[a][b] == "L":
return False
if grid[a][b] == "#":
return True
return is_occupied_seat(a+x, b+y, grid, x, y)
def free_adjascent(grid, i, j):
axes = [(0, 1), (0, -1), (-1, 0), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
count = 0
for x,y in axes:
if no_occupied_seat(i+x, j+y, grid, x, y):
count += 1
return count == 8
def occupied_adjascent(grid, i, j):
axes = [(0, 1), (0, -1), (-1, 0), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
count = 0
for x,y in axes:
if is_occupied_seat(i+x, j+y, grid, x, y):
count += 1
return count >= 5
for _ in range(100):
curr_state = deepcopy(space)
for i in range(len(space)):
for j in range(len(space[0])):
if space[i][j] == "#":
if occupied_adjascent(curr_state, i, j):
space[i][j] = "L"
elif space[i][j] == "L":
if free_adjascent(curr_state, i, j):
space[i][j] = "#"
# print('\n'.join(map(''.join, space)))
# print()
num_occupied = 0
for i in range(len(space)):
for j in range(len(space[0])):
if space[i][j] == "#":
num_occupied += 1
print(num_occupied)