Solve community cellular automation

This commit is contained in:
Gabriel Augendre 2017-08-27 19:41:01 +02:00
parent 5ea278ad40
commit c11457404f
No known key found for this signature in database
GPG key ID: F360212F958357D4

31
community_cellular.py Normal file
View file

@ -0,0 +1,31 @@
def to_bin(dot_at_str):
return str(dot_at_str).replace('@', '1').replace('.', '0')
def to_dot_at(bin_str):
return str(bin_str).replace('1', '@').replace('0', '.')
def evolve(pattern, neighborhood):
evolution = ''
for i in range(len(pattern)):
key = pattern[i-1] + pattern[i] + pattern[(i+1) % len(pattern)]
evolution += neighborhood[key]
return evolution
def main():
rule = format(int(input()), 'b').zfill(8)
neighborhood = {}
for i, value in enumerate(rule[::-1]):
neighborhood[format(i, 'b').zfill(3)] = value
n_lines = int(input())
start_pattern = to_bin(input())
for _ in range(n_lines):
print(to_dot_at(start_pattern))
start_pattern = evolve(start_pattern, neighborhood)
if __name__ == '__main__':
main()