-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_11b.py
76 lines (62 loc) · 1.91 KB
/
day_11b.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
#!/usr/bin/env python3
directions = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
def getOccupiedNeighbours(i, j, seats):
count = 0
for dir in directions:
new_point = [i, j]
while True:
new_point = [sum(x) for x in zip(new_point, dir)]
if (
new_point[0] < 0
or new_point[0] >= len(seats)
or new_point[1] < 0
or new_point[1] >= len(seats[i])
):
break
elif seats[new_point[0]][new_point[1]] == "#":
count += 1
break
elif seats[new_point[0]][new_point[1]] == "L":
break
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] >= 5:
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()