Skip to content

Instantly share code, notes, and snippets.

@vjeranc
Last active December 14, 2023 14:30
Show Gist options
  • Save vjeranc/a24c49d3946a0a07d07f6618a888fca4 to your computer and use it in GitHub Desktop.
Save vjeranc/a24c49d3946a0a07d07f6618a888fca4 to your computer and use it in GitHub Desktop.
ans = 0
for l in open(0):
P, G = l.split()
G = tuple(map(int, G.split(',')))
P, G = '?'.join([P]*5), G*5 # part 2
P = P+'.'
N, M = len(P), len(G)
cdot = [P[i:].count('.') for i in range(N)]
dp = [[0]*(M+1) for _ in range(N+1)]
dp[N][M] = 1
for i in range(N-1, -1, -1):
for j in range(M, -1, -1):
if P[i] in '.?': dp[i][j] += dp[i+1][j]
if j==M or i+G[j]>=N: continue
if P[i] in '#?' and cdot[i+G[j]]-cdot[i]==0 and P[i+G[j]]!='#':
dp[i][j] += dp[i+G[j]+1][j+1]
ans += dp[0][0]
print(ans)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment