-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-2.py
147 lines (132 loc) · 4.38 KB
/
11-2.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
import os
import copy
printOut = False
printIter = False
#part2
lines = []
def checkAdjacent(row, col):
if lines[row][col] == ".":
return 0
count = 0
#print("************")
#check left
checkR = row
checkC = col - 1
while checkC > 0 and lines[checkR][checkC] == ".":
checkC = checkC - 1
if checkC >= 0 and lines[checkR][checkC] == "#":
if printOut:
print(row, col, checkR, checkC, "left")
count = count + 1
#check right
checkR = row
checkC = col + 1
while checkC < len(lines[checkR]) and lines[checkR][checkC] == ".":
checkC = checkC + 1
if checkC < len(lines[checkR]) and lines[checkR][checkC] == "#":
if printOut:
print(row, col, checkR, checkC, "right")
count = count + 1
#check up
checkR = row - 1
checkC = col
while checkR > 0 and lines[checkR][checkC] == ".":
checkR = checkR - 1
if checkR >= 0 and lines[checkR][checkC] == "#":
if printOut:
print(row, col, checkR, checkC, "up")
count = count + 1
#check down
checkR = row + 1
checkC = col
while checkR < len(lines) and lines[checkR][checkC] == ".":
checkR = checkR + 1
if checkR < len(lines) and lines[checkR][checkC] == "#":
if printOut:
print(row, col, checkR, checkC, "down")
count = count + 1
#check left-up
checkR = row - 1
checkC = col - 1
while checkC > 0 and checkR > 0 and lines[checkR][checkC] == ".":
checkC = checkC - 1
checkR = checkR - 1
if checkC >= 0 and checkR >= 0 and lines[checkR][checkC] == "#":
if printOut:
print(row, col, checkR, checkC, "left-up")
count = count + 1
#check right-up
checkR = row - 1
checkC = col + 1
while checkC < len(lines[checkR]) and checkR > 0 and lines[checkR][checkC] == ".":
checkC = checkC + 1
checkR = checkR - 1
if checkC < len(lines[checkR]) and checkR >= 0 and lines[checkR][checkC] == "#":
if printOut:
print(row, col, checkR, checkC, "right-up")
count = count + 1
#check left-down
checkR = row + 1
checkC = col - 1
while checkC > 0 and checkR < len(lines) and lines[checkR][checkC] == ".":
checkC = checkC - 1
checkR = checkR + 1
if checkC >= 0 and checkR < len(lines) and lines[checkR][checkC] == "#":
if printOut:
print(row, col, checkR, checkC, "left-down")
count = count + 1
#check right-down
checkR = row + 1
checkC = col + 1
while checkR < len(lines) and checkC < len(lines[checkR]) and lines[checkR][checkC] == ".":
checkC = checkC + 1
checkR = checkR + 1
#print(row, col, checkR, checkC, len(lines),len(lines[checkR]))
if checkR < len(lines) and checkC < len(lines[checkR]) and lines[checkR][checkC] == "#":
if printOut:
print(row, col, checkR, checkC, "right-down")
count = count + 1
if printOut:
print(row, col, "Count", count)
return count
def printSeats():
for line in lines:
print(line)
with open("11.txt") as file:
lines = file.readlines()
for i in range(len(lines)):
if lines[i][-1] == "\n":
lines[i] = lines[i][0:-1]
iteration = 0
changed = True
if printIter:
print("---------",iteration)
printSeats()
while changed == True:
workingSeats = copy.deepcopy(lines)
changed = False
iteration = iteration + 1
for r in range(len(lines)):
colLen = len(lines[r])
for c in range(colLen):
count = checkAdjacent(r, c)
myLine = workingSeats[r]
if count == 0 and myLine[c] == "L":
myLine = myLine[:c] + "#" + myLine[c + 1:]
workingSeats[r] = myLine
changed = True
if count > 4 and myLine[c] == "#":
myLine = myLine[:c] + "L" + myLine[c + 1:]
workingSeats[r] = myLine
changed = True
c = c + 1
r = r + 1
lines = copy.deepcopy(workingSeats)
if printIter:
print("---------",iteration)
printSeats()
print(str(changed))
occupiedCount = 0
for line in lines:
occupiedCount = occupiedCount + line.count("#")
print(occupiedCount)