-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_11a.py
68 lines (55 loc) · 1.62 KB
/
day_11a.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
#!/usr/bin/env python3
def getOccupiedNeighbours(i, j, seats):
count = 0
for ii in range(i - 1, i + 2):
if ii < 0 or ii >= len(seats):
continue
for jj in range(j - 1, j + 2):
if jj < 0 or jj >= len(seats[ii]):
continue
if ii == i and jj == j:
continue
if seats[ii][jj] == "#":
count += 1
return count
def UpdateSeating(seats, nos):
for i in range(0, len(seats)):
for j in range(0, len(seats[i])):
nos[i][j] = getOccupiedNeighbours(i, j, seats)
changed = False
for i in range(0, len(seats)):
for j in range(0, len(seats[i])):
if seats[i][j] == "L" and nos[i][j] == 0:
changed = True
seats[i][j] = "#"
elif seats[i][j] == "#" and nos[i][j] >= 4:
changed = True
seats[i][j] = "L"
return changed
def CountOccupied(seats):
count = 0
for row in seats:
for col in row:
if col == "#":
count += 1
return count
def printSeats(seats):
for line in seats:
print(line)
def main():
f = open("../input/day_11_input")
seats = list()
for line in f:
line = line.strip().replace("\r", "").replace("\n", "")
seats.append([letter for letter in line])
nos = []
for i in range(0, len(seats)):
nos.append([0] * len(seats[i]))
changed = True
while changed:
changed = UpdateSeating(seats, nos)
count = CountOccupied(seats)
print(count)
return count
if __name__ == "__main__":
main()