[PYTHON] Get complex results with short code-one-dimensional cellular automaton-

What is this?

I wanted to get complex (interesting) output with as simple code as possible. Automata are convenient in such a case. Just execute the parameters in the chaos area and it will draw a mysterious pattern. Reference: Rule 30

Code (with explanation)

#1D cellular automaton

# [Rule 30](https://ja.wikipedia.org/wiki/%E3%83%AB%E3%83%BC%E3%83%AB30)
#  88 :The internal state of Rule 30 is reversed left and right
# 135 :Inverted state bits
# 149 :The internal state is reversed left and right, and the state bit is also inverted.

RULE_ID = 30 # [0, 0, 0, 1, 1, 1, 1, 0]
rule = list(map(int, f'{RULE_ID:08b}'))

N_CELL = 80  #Cell size
T_CNT = 100  #Execution time

#Initial state of cell
INIT_ID = 12345678987654321 #suitable
cell_c = list(map(int, f'{INIT_ID:b}'.zfill(N_CELL)[-N_CELL:]))

for t in range(T_CNT):
    print(''.join(map(lambda n:'*' if n else ' ', cell_c))) #1 to "*Output with

    #Next state
    cell_n = []
    for i in range(N_CELL):
        #State of adjacent 3 cells =[0,0,0]...[1,1,1]=0...8 ways of 7
        #The next state is the value of that bit position(0 or 1)Adopt
        #The cells shall be connected in a ring shape
        pos = cell_c[i-1]*4 + cell_c[i]*2 + cell_c[(i+i) % N_CELL]
        cell_n.append( rule[pos])

    cell_c = cell_n #Change of state

Code (short version)

RULE_ID = 30 # [0, 0, 0, 1, 1, 1, 1, 0]
rule = list(map(int, f'{RULE_ID:08b}'))

N_CELL, T_CNT = 80, 100  #Cell size,Execution time

INIT_ID = 12345678987654321 #Initial cell state. suitable
cell_c = list(map(int, f'{INIT_ID:b}'.zfill(N_CELL)[-N_CELL:]))

for t in range(T_CNT):
    print(''.join(map(lambda n:'*' if n else ' ', cell_c))) #1 to "*Output with
    cell_n = [ rule[cell_c[i-1]*4 + cell_c[i]*2 + cell_c[(i+i) % N_CELL]] for i in range(N_CELL)] #Next state
    cell_c = cell_n #Change of state

result

                          * * **** ***   * * *   **   * *  *   ***** *  * **   *
*                          * **  *** **   * * *  ***   * *  *  *   ** *  ****
 *                          **** * *****   * * * * **   * *  *  *  *** * *  **
  *                         *  ** **   **   * * * ****   * *  *  * * ** * * ***
   *                         * ******  ***   * * **  **   * *  *  * **** * ** **
*   *                         **    ** * **   * **** ***   * *  *  **  ** ******
**   *                        ***   *** ****   **  *** **   * *  * *** ****
***   *                       * **  * ***  **  *** * *****   * *  ** ***  **
* **   *                       ****  ** ** *** * ** **   **   * * **** ** ***
 ****   *                      *  ** ******* ** *******  ***   * **  ****** **
 *  **   *                      * ****     ******     ** * **   **** *    *****
  * ***   *                      **  **    *    **    *** ****  *  ** *   *   **
*  ** **   *                     *** ***    *   ***   * ***  **  * *** *   *  **
** ******   *                    * *** **    *  * **   ** ** ***  ** ** *   * *
****    **   *                    ** *****    *  ****  ******* ** ****** *   * *
   **   ***   *                   ****   **    * *  ** *     ******    ** *   **
*  ***  * **   *                  *  **  ***    * * *** *    *    **   *** *  **
** * **  ****   *                  * *** * **    * ** ** *    *   ***  * ** * *
*** **** *  **   *                  ** ** ****    ******* *    *  * **  **** * *
  ***  ** * ***   *                 *******  **   *     ** *    *  **** *  ** **
* * ** *** ** **   *                *     ** ***   *    *** *    * *  ** * *****
** ***** ********   *                *    **** **   *   * ** *    * * *** **
****   ***      **   *                *   *  *****   *   **** *    * ** *****
*  **  * **     ***   *                *   * *   **   *  *  ** *    *****   **
 * ***  ****    * **   *                *   * *  ***   *  * *** *   *   **  ***
  ** ** *  **    ****   *                *   * * * **   *  ** ** *   *  *** * **
* ****** * ***   *  **   *                *   * * ****   * ****** *   * * ** ***
***    ** ** **   * ***   *                *   * **  **   **    ** *   * *****
* **   *********   ** **   *                *   **** ***  ***   *** *   **   **
 ****  *       **  ******   *                *  *  *** ** * **  * ** *  ***  ***
**  **  *      *** *    **   *                *  * * ***** ****  **** * * ** * *
 ** ***  *     * ** *   ***   *                *  * **   ***  ** *  ** * **** **
***** **  *     **** *  * **   *                *  ****  * ** *** * *** **  ****
    *****  *    *  ** *  ****   *                * *  **  ***** ** ** ***** *
    *   **  *    * *** * *  **   *                * * *** *   *********   ** *
     *  ***  *    ** ** * * ***   *                * ** ** *  *       **  *** *
      * * **  *   ****** * ** **   *                ******* *  *      *** * ** *
*      * ****  *  *    ** *******   *               *     ** *  *     * ** ****
 *      **  **  *  *   ****     **   *               *    *** *  *     *****  **
* *     *** ***  *  *  *  **    ***   *               *   * ** *  *    *   ** **
** *    * *** **  *  *  * ***   * **   *               *   **** *  *    *  ****
*** *    ** *****  *  *  ** **   ****   *               *  *  ** *  *    * *  **
  ** *   ****   **  *  * ******  *  **   *               *  * *** *  *    * * *
  *** *  *  **  ***  *  **    **  * ***   *               *  ** ** *  *    * * *
* * ** *  * *** * **  * ***   ***  ** **   *               * ****** *  *    * *
 * **** *  ** ** ****  ** **  * ** ******   *               **    ** *  *    * *
* **  ** * *******  ** ******  *****    **   *              ***   *** *  *    *
 **** *** **     ** ****    ** *   **   ***   *             * **  * ** *  *    *
**  *** *****    ****  **   *** *  ***  * **   *             ****  **** *  *
*** * ***   **   *  ** ***  * ** * * **  ****   *            *  ** *  ** *  *
* ** ** **  ***   * **** **  **** * **** *  **   *            * *** * *** *  *
 ********** * **   **  ***** *  ** **  ** * ***   *            ** ** ** ** *  *
 *        ** ****  *** *   ** * ****** *** ** **   *           ************ *  *
* *       ****  ** * ** *  *** **    *** ********   *          *          ** *
 * *      *  ** *** **** * * *****   * ***      **   *          *         *** *
  * *      * **** ***  ** * **   **   ** **     ***   *          *        * ** *
*  * *      **  *** ** *** ****  ***  ******    * **   *          *        ****
 *  * *     *** * ****** ***  ** * ** *    **    ****   *          *       *  **
* *  * *    * ** **    *** ** *** **** *   ***   *  **   *          *       * **
** *  * *    *******   * ****** ***  ** *  * **   * ***   *          *       **
*** *  * *   *     **   **    *** ** *** *  ****   ** **   *          *      ***
  ** *  * *   *    ***  ***   * ****** ** * *  **  ******   *          *     *
  *** *  * *   *   * ** * **   **    ***** * * *** *    **   *          *     *
  * ** *  * *   *   **** ****  ***   *   ** * ** ** *   ***   *          *     *
*  **** *  * *   *  *  ***  ** * **   *  *** ******* *  * **   *          *
 * *  ** *  * *   *  * * ** *** ****   * * ***     ** *  ****   *          *
  * * *** *  * *   *  * ***** ***  **   * ** **    *** * *  **   *          *
   * ** ** *  * *   *  **   *** ** ***   *******   * ** * * ***   *          *
    ******* *  * *   * ***  * ****** **  *     **   **** * ** **   *          *
    *     ** *  * *   ** **  **    *****  *    ***  *  ** *******   *          *
*    *    *** *  * *  ****** ***   *   **  *   * **  * ****     **   *
 *    *   * ** *  * * *    *** **   *  ***  *   ****  **  **    ***   *
  *    *   **** *  * * *   * *****   * * **  *  *  ** *** ***   * **   *
   *    *  *  ** *  * * *   **   **   * ****  *  * **** *** **   ****   *
    *    *  * *** *  * * *  ***  ***   **  **  *  **  *** *****  *  **   *
     *    *  ** ** *  * * * * ** * **  *** ***  * *** * ***   **  * ***   *
      *    * ****** *  * * * **** **** * *** **  ** ** ** **  ***  ** **   *
       *    **    ** *  * * **  ***  ** ** ***** ************ * ** ******   *
        *   ***   *** *  * **** * ** *******   ***          ** *****    **   *
         *  * **  * ** *  **  ** *****     **  * **         ****   **   ***   *
          *  ****  **** * *** ****   **    ***  ****        *  **  ***  * **   *
*          * *  ** *  ** ** ***  **  ***   * ** *  **        * *** * **  ****
 *          * * *** * ******* ** *** * **   **** * ***        ** ** **** *  **
  *          * ** ** **     ****** ** ****  *  ** ** **       *******  ** * ***
   *          **********    *    ******  **  * *********      *     ** *** ** **
*   *         *        **    *   *    ** ***  **       **      *    **** *******
**   *         *       ***    *   *   **** ** ***      ***      *   *  ***
***   *         *      * **    *   *  *  ****** **     * **      *   * * **
* **   *         *      ****    *   *  * *    *****     ****      *   * ****
 ****   *         *     *  **    *   *  * *   *   **    *  **      *   **  **
 *  **   *         *     * ***    *   *  * *   *  ***    * ***      *  *** ***
  * ***   *         *     ** **    *   *  * *   * * **    ** **      * * *** **
   ** **   *         *    ******    *   *  * *   * ****   ******      * ** *****
*  ******   *         *   *    **    *   *  * *   **  **  *    **      *****   *
** *    **   *         *   *   ***    *   *  * *  *** ***  *   ***     *   **  *
 ** *   ***   *         *   *  * **    *   *  * * * *** **  *  * **     *  *** *
**** *  * **   *         *   *  ****    *   *  * * ** *****  *  ****     * * **
*  ** *  ****   *         *   * *  **    *   *  * *****   **  * *  **     * ****
** *** * *  **   *         *   * * ***    *   *  **   **  ***  * * ***     **
**** ** * * ***   *         *   * ** **    *   * ***  *** * **  * ** **    ***

Recommended Posts

Get complex results with short code-one-dimensional cellular automaton-
Get complex results with short code-one-dimensional cellular automaton, saved version with images-