Advertisement
k98kurz

rule110.py

Aug 28th, 2021 (edited)
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. from secrets import token_bytes
  2. from sys import argv
  3. from time import sleep
  4.  
  5.  
  6. rules = {
  7.     '111': '0',
  8.     '110': '1',
  9.     '101': '1',
  10.     '100': '0',
  11.     '011': '1',
  12.     '010': '1',
  13.     '001': '1',
  14.     '000': '0',
  15. }
  16.  
  17.  
  18. def prettify(tape: str) -> str:
  19.     return tape.replace('0', ' ').replace('1', '.')
  20.  
  21.  
  22. def next_generation(tape: str) -> str:
  23.     tape = tape
  24.     new_tape = rules[tape[-1:] + tape[:2]]
  25.     for i in range(0, len(tape) - 2):
  26.         code = tape[i:i+3]
  27.         new_tape += rules[code]
  28.     new_tape += rules[tape[-2:] + tape[0]]
  29.     return new_tape
  30.  
  31.  
  32. def simulate(tape: str, number_of_generations: int, delay: int, use_pretty: bool = False):
  33.     if number_of_generations == 0:
  34.         number_of_generations = -1
  35.     while number_of_generations != 0:
  36.         print(prettify(tape) if use_pretty else tape)
  37.         sleep(delay/1000)
  38.         tape = next_generation(tape)
  39.         number_of_generations -= 1
  40.  
  41.  
  42. def setup_tape(tape_len: int, starting_string: str) -> str:
  43.     string = ''
  44.     if starting_string != '':
  45.         while len(string) < tape_len:
  46.             string += starting_string
  47.     else:
  48.         while len(string) < tape_len:
  49.             string += "{0:b}".format(int.from_bytes(token_bytes(), 'little'))
  50.     return string[:tape_len]
  51.  
  52.  
  53. def interactive():
  54.     val = 20
  55.     prevval = val
  56.     while val != 0:
  57.         print(f'example tape_len={val}')
  58.         print(setup_tape(int(val), ''))
  59.         prevval = val
  60.         val = input('next int len to try (or non-int to stop): ')
  61.         try:
  62.             val = int(val)
  63.         except:
  64.             val = 0
  65.  
  66.     tape = setup_tape(prevval, '')
  67.     simulate(tape, 0, 42, True)
  68.  
  69.  
  70. def usage(script):
  71.     print(f'usage: python {script} [tape_len|interactive|i] [number_of_generations] [delay in ms] [prettify] [starting_string]')
  72.     print('\ttape_len is the integer number of elements in the tape')
  73.     print('\fif interactive or i is chosen, interactively determine the best length')
  74.     print('\tif number_of_generations is omitted or is 0, the simulation will continue until interrupted')
  75.     print('\tif delay is omitted or is 0, a 42ms delay is used as default')
  76.     print('\tif prettify is set to 1, the prettifier will be used')
  77.     print('\tif starting_string has len less than tape_len, it will be repeated to fill out the rest')
  78.  
  79.  
  80. def main(args):
  81.     if len(args) == 1 or args[1] in ('h', '?', '-h', '-?', '--help'):
  82.         usage(args[0])
  83.         exit()
  84.  
  85.     starting_string = args[5] if len(args) == 6 else ''
  86.     use_pretty = (args[4] in ('1', 'True', 'true')) if len(args) >= 5 else False
  87.     delay = int(args[3]) if len(args) >= 4 else 42
  88.     delay = delay if delay > 0 else 42
  89.     number_of_generations = int(args[2]) if len(args) >= 3 else 0
  90.  
  91.     if args[1] in ('interactive', 'i'):
  92.         return interactive()
  93.  
  94.     tape_len = int(args[1])
  95.  
  96.     tape = setup_tape(tape_len, starting_string)
  97.     simulate(tape, number_of_generations, delay, use_pretty)
  98.  
  99.  
  100. if __name__ == '__main__':
  101.     main(argv)
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement