From c11457404fd91480c71cd695a014c9de642f11cc Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 27 Aug 2017 19:41:01 +0200 Subject: [PATCH] Solve community cellular automation --- community_cellular.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 community_cellular.py diff --git a/community_cellular.py b/community_cellular.py new file mode 100644 index 0000000..08d3ef5 --- /dev/null +++ b/community_cellular.py @@ -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()