-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11.py
More file actions
96 lines (83 loc) · 2.98 KB
/
Copy path11.py
File metadata and controls
96 lines (83 loc) · 2.98 KB
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
with open('11.txt')as f:
seatMatrix=f.read().splitlines()
def getOccupiedNeighbours(matrix,coord):
rows=len(matrix)
columns=len(matrix[0])
padded=["."*columns]+matrix+["."*columns]
padded=["."+p+"."for p in padded]
counter=0
for i in range(-1,2,1):
for j in range(-1,2,1):
if i==0 and j==0:
continue
if padded[coord[0]+i+1][coord[1]+j+1]=="#":
counter+=1
return counter
def partOne(seatMatrix):
hasChanged=True
print("Start")
while hasChanged:
hasChanged = False
newMatrix=seatMatrix.copy()
for i in range (len(seatMatrix)):
for j in range(len(seatMatrix[1])):
numberOfNeigh=getOccupiedNeighbours(seatMatrix,(i,j))
if seatMatrix[i][j]=="L" and numberOfNeigh==0:
newMatrix[i]=newMatrix[i][:j]+"#"+newMatrix[i][j+1:]
hasChanged=True
elif seatMatrix[i][j]=="#" and numberOfNeigh>3:
newMatrix[i]=newMatrix[i][:j]+"L"+newMatrix[i][j+1:]
hasChanged=True
seatMatrix=newMatrix.copy()
occupiedCounter=0
for s in seatMatrix:
for c in s:
if c =="#":
occupiedCounter+=1
print("Answer to part one: ",occupiedCounter)
def getOccupiedNeighbours2(matrix, coord):
rows = len(matrix)
columns = len(matrix[0])
counter = 0
for i in range(-1, 2, 1):
for j in range(-1, 2, 1):
if i == 0 and j == 0:
continue
offset = 1
while True:
try:
if matrix[coord[0] + offset * i][coord[1] + offset * j] == "L":
break
if matrix[coord[0] + offset*i][coord[1] + offset*j] == "#" and -1<(coord[0] + offset * i)<rows and -1<(coord[1] + offset*j)<columns:
counter += 1
break
offset += 1
except IndexError:
break
return counter
pass
def partTwo(seatMatrix):
hasChanged=True
print("Start part Two: ")
while hasChanged:
hasChanged = False
newMatrix=seatMatrix.copy()
for i in range (len(seatMatrix)):
for j in range(len(seatMatrix[1])):
numberOfNeigh=getOccupiedNeighbours2(seatMatrix,(i,j))
if seatMatrix[i][j]=="L" and numberOfNeigh==0:
newMatrix[i]=newMatrix[i][:j]+"#"+newMatrix[i][j+1:]
hasChanged=True
elif seatMatrix[i][j]=="#" and numberOfNeigh>4:
newMatrix[i]=newMatrix[i][:j]+"L"+newMatrix[i][j+1:]
hasChanged=True
seatMatrix=newMatrix.copy()
occupiedCounter=0
for s in seatMatrix:
for c in s:
if c =="#":
occupiedCounter+=1
print("Answer to part two: ",occupiedCounter)
if __name__ == "__main__":
#partOne(seatMatrix)
partTwo(seatMatrix)