Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

6 of 6
Commonmark migration

Python 2, (削除) 174 (削除ここまで) 170

from random import*
k,t=input()
D=[0]*k
E=D+[0]
def U(x):b=D[x];D[x]=0;return b and-~U(x-1)+U(-~x%k)
for x in[0]*t:r=randint(0,k-1);e=U(r);E[e-1]+=1;D[r]=e<1
print E[:-1]

Thanks @Vioz for finding a shorter way to make D, and proving again that not is usually golfable. And also for writing the explanation.

I had tried to make a similar program in Pyth, but there seems to be a scope issue in what I was trying to do. This pretty naively implements the dominoes and the function U propagates earthquakes. The subtraction direction in U doesn't need a mod because it will wrap around naturally. The last element of E counts the number of times a zero is turned into a one, so it is not printed at the end.

Ungolfed + Explanation:

from random import*
k,t=input() # Takes input in form k,t
D = [0]*k # Empty array of size k is made for
 # performing the simulation.
E = D+[0] # Empty array of size k+1 is made for
 # storing the magnitudes.
def U(x): # Define a function U that takes an int x
 b = D[x] # Assign b to the value at x in D
 D[x] = 0 # Clear the value at x in D
 return b and U(x-1)+1 + U((x+1)%k) # Return the sum of U(x-1)+1 and U((x+1)%k)
 # if b is a 1.
for x in[0]*t: # Perform t tests
 r=randint(0,k-1) # Generate a random number between 0 and k-1
 e=U(r) # Assign e to the value of U(r)
 E[e-1]+=1; # Increment the magnitude array at position
 # e-1
 D[r] = e<1 # Set D[r] to be 1 if no earthquake happened.
print E[:-1] # Print the magnitude list
FryAmTheEggman
  • 17.5k
  • 3
  • 42
  • 99

AltStyle によって変換されたページ (->オリジナル) /