-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc_11a.py
60 lines (48 loc) · 1.11 KB
/
aoc_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
import copy
fin = open('input_11.txt')
boat = []
for line in fin:
line = ' '+line.strip()+' '
boat.append(list(line))
fin.close()
l = len(boat[0])
emptyrow = list(' '*l)
boat.insert(0,emptyrow)
boat.append(emptyrow)
def iterate():
global boat
newboat = copy.deepcopy(boat)
rnum = len(boat)-2
cnum = len(boat[0])-2
occ = 0
for r in range(1,rnum+1):
for c in range(1,cnum+1):
n = neighborcount(r,c)
if n == 0 and boat[r][c]=="L":
newboat[r][c]='#'
elif n >= 4 and boat[r][c]=="#":
newboat[r][c]="L"
occ += newboat[r].count('#')
boat = newboat
return occ
def neighborcount(r,c):
global boat
n = 0
for dr in [-1,0,1]:
for dc in [-1,0,1]:
if dr==0 and dc==0:
continue
n += (boat[r+dr][c+dc] == '#')
return n
def printboat():
global boat
for line in boat:
for char in line:
print(char,end="")
print()
print()
printboat()
for i in range(100):
occseats = iterate()
# printboat()
print(occseats)