-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-2.py
124 lines (113 loc) · 2.91 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
import copy
rawdata = open("11-1.input.txt").readlines()
seats = list(["x"*(2+len(rawdata[0].replace('\n','')))])
for item in rawdata:
seats.append(list('x'+item.replace('\n','')+'x') )
seats.append(list("x"*(2+len(rawdata[0].replace('\n','')))))
changes=0
maxx=len(seats[0])
maxy=len(seats)
next=[['.']*len(seats[0])]*len(seats)
for y in range(maxy):
for x in range(maxx):
next[y]=copy.deepcopy(seats[y])
def occupied(x,y):
tally=0
x1=x
y1=y
#up
while (x1==x and y1==y) or seats[y1][x1] not in ['x','L']:
if not (x1==x and y1==y) and seats[y1][x1]=='#':
tally=tally+1
break
y1=y1-1
#up right
x1=x
y1=y
while (x1==x and y1==y) or seats[y1][x1] not in ['x','L']:
if not (x1==x and y1==y) and seats[y1][x1]=='#':
tally=tally+1
break
y1=y1-1
x1=x1+1
#right
x1=x
y1=y
while (x1==x and y1==y) or seats[y1][x1] not in ['x','L']:
if not (x1==x and y1==y) and seats[y1][x1]=='#':
tally=tally+1
break
x1=x1+1
#down right
x1=x
y1=y
while (x1==x and y1==y) or seats[y1][x1] not in ['x','L']:
if not (x1==x and y1==y) and seats[y1][x1]=='#':
tally=tally+1
break
y1=y1+1
x1=x1+1
#down
x1=x
y1=y
while (x1==x and y1==y) or seats[y1][x1] not in ['x','L']:
if not (x1==x and y1==y) and seats[y1][x1]=='#':
tally=tally+1
break
y1=y1+1
#down left
x1=x
y1=y
while (x1==x and y1==y) or seats[y1][x1] not in ['x','L']:
if not (x1==x and y1==y) and seats[y1][x1]=='#':
tally=tally+1
break
y1=y1+1
x1=x1-1
#left
x1=x
y1=y
while (x1==x and y1==y) or seats[y1][x1] not in ['x','L']:
if not (x1==x and y1==y) and seats[y1][x1]=='#':
tally=tally+1
break
x1=x1-1
#up left
x1=x
y1=y
while (x1==x and y1==y) or seats[y1][x1] not in ['x','L']:
if not (x1==x and y1==y) and seats[y1][x1]=='#':
tally=tally+1
break
y1=y1-1
x1=x1-1
return tally
def showseats():
for y in range(maxy):
row=''
for x in range(maxx):
row=row+seats[y][x]
print(row)
def flip():
for y in range(maxy):
for x in range(maxx):
seats[y]=copy.deepcopy(next[y])
changes=1
showseats()
while changes==1:
changes=0
for y in range(1,maxy-1):
for x in range(1,maxx-1):
if seats[y][x]=='L' and occupied(x,y)==0:
next[y][x]='#'
changes=1
if seats[y][x]=='#' and occupied(x,y)>=5:
next[y][x]='L'
changes=1
flip()
totalocc=0
for y in range(maxy):
for x in range(maxx):
if seats[y][x]=='#':
totalocc=totalocc+1
print(totalocc)