blob: e2d504137f247c2506c651600badf477af888b87 (
plain)
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
|
import fileinput
import re
from copy import deepcopy
L = [list(l.strip()) for l in list(fileinput.input())]
R = len(L)
C = len(L[0])
def solve(L, p1):
t = 0
while True:
t += 1
# print('='*80)
# for r in range(R):
# print(''.join(L[r]))
newL = deepcopy(L)
change = False
for r in range(R):
for c in range(C):
nocc = 0
for dr in [-1, 0, 1]:
for dc in [-1, 0, 1]:
if not (dr == 0 and dc == 0):
rr = r + dr
cc = c + dc
# Only skip floor in part 2
while (
0 <= rr < R
and 0 <= cc < C
and L[rr][cc] == "."
and (not p1)
):
rr = rr + dr
cc = cc + dc
if 0 <= rr < R and 0 <= cc < C and L[rr][cc] == "#":
nocc += 1
if L[r][c] == "L":
if nocc == 0:
newL[r][c] = "#"
change = True
# Threshold changes from 4->5 in part 2
elif L[r][c] == "#" and nocc >= (4 if p1 else 5):
newL[r][c] = "L"
change = True
if not change:
break
L = deepcopy(newL)
print(t)
ans = 0
for r in range(R):
for c in range(C):
if L[r][c] == "#":
ans += 1
return ans
print(solve(L, True))
print(solve(L, False))
|