Solve community cellular automation
This commit is contained in:
parent
5ea278ad40
commit
c11457404f
1 changed files with 31 additions and 0 deletions
31
community_cellular.py
Normal file
31
community_cellular.py
Normal 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()
|
Loading…
Reference in a new issue