init
[CYBERHEART] / TESTS / ca.c
1 #include <time.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <limits.h>
5
6 #define bit(x) (1 << (x))
7
8 #define RULE    30 // 22 // 
9 #define LEN     8
10 #define STEPS   64      // default steps
11
12 typedef unsigned char uint8_t;
13 typedef unsigned long long uint64_t;
14
15 //                         *    *    * ****   **  * **  *  *  ***** *** *   **   ***  ** 
16 //                       001000010000101111000110010110010010011111011101000110001110011        
17 // uint8_t uni[ LEN ] = { 0b00100001, 0b00001011, 0b11000110, 0b01011001, 0b00100111, 0b11011101, 0b00011000, 0b11100110 };
18
19 uint8_t uni[ LEN ] = { 0b00011100, 0b00010011, 0b01010001, 0b11011110, 0b11100001, 0b01111000, 0b01110011, 0b10010110 };
20
21 void shift_left( uint8_t shift) {
22
23         while (shift--) {
24
25                 uint8_t carry = 0;
26                 uint8_t i = LEN - 1;
27
28                 do {
29                         uint8_t next = (uni[i] & 0x80) ? 1 : 0;
30                         uni[i] = carry | (uni[i] << 1);
31                         carry = next;
32
33                 } while ( i-- > 0 );
34
35                 uni[ LEN - 1 ] |= carry ;
36         }
37 }  
38
39
40 int main( int argc, char *argv[] ){
41
42         uint64_t        j = STEPS;
43         uint8_t         i, tmp, triade;
44         uint8_t         cnt=0, cntMax=0;
45         uint8_t         stat[ 8*LEN ];
46
47         if ( argc < 2 ) {
48                 printf("CyberHeart evolution statistical analyzer. (◎)2021 by Dmitry Shalnoff, MIT\n");
49                 printf("Usage: %s [number of steps] [p]\n", argv[0]);
50                 printf("p:\tprint output state instead of statistics\n");
51                 return 0;
52         } 
53
54         j = atoi( argv[1] );
55
56         // ------------ cell automata -------------
57
58         if ( argc == 2 ){
59                 printf("Steps: %llu, about %llu min (>%llu realtime days with 8 sec epoch delay) \n", j, (j*8)/(60), (j*8)/(60*60*24) );
60         }
61
62         while( j-- ) {
63
64                 tmp = uni[ 0 ];
65
66                 if ( argc > 2  && argv[2][0] == 'p' ){
67                         for (i = 0; i < LEN*8; i++) putchar( uni[ i/8 ] & bit( 7-(i%8) ) ? '*' : ' ');
68 //                      for (i = 0; i < LEN*8; i++) printf("%s", uni[ i/8 ] & bit( 7-(i%8) ) ? "▒▒" : "  ");
69                         putchar('\n');
70                 }
71
72                 for (i=0; i < LEN*8; i++){
73
74 //                      if ( argv[2] == 'p' ){
75 //                              put_char( uni[ i/8 ] & bit( 7-(i%8) ) );
76 //                              putchar( uni[ i/8 ] & bit( 7-(i%8) ) ? '*' : ' ');
77 //                      }
78
79                         if ( i > LEN * 8 - 4 ) triade = (tmp >> (LEN*8 - i) ); else triade = uni[ 0 ];
80                         if ( RULE & bit(7 & triade) ) uni[ 0 ] |= bit(2); else uni[ 0 ] &= ~bit(2);
81
82                         if ( (( uni[ 0 ] >> 2) & 0b11) == 0b00 ) { 
83
84                                 cnt++;
85
86                                 if ( cnt > cntMax ) {
87                                         cntMax = cnt;
88                                 }
89                         
90                         } else {
91                                 stat[cnt]++;
92                                 cnt = 0;
93                         }
94                         
95                         shift_left( 1 );
96                 }
97
98                 shift_left( LEN*8-1 );
99         }
100
101         if ( argc == 2){
102                 printf("cnt max: %d\n", cntMax );
103                 printf("stat: \n");
104                 for (i=0; i<=cntMax; i++) printf("%d: \t%d\n", i, stat[i]);
105         }
106
107 }
Contact me: dev (at) shalnoff (dot) com
PGP fingerprint: A6B8 3B23 6013 F18A 0C71 198B 83D8 C64D 917A 5717