File: LifeOnTheSlope.cpp

package info (click to toggle)
golly 2.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 9,560 kB
  • ctags: 5,064
  • sloc: cpp: 38,119; python: 3,203; perl: 1,121; makefile: 58; java: 49; sh: 22
file content (67 lines) | stat: -rw-r--r-- 1,638 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <vector>
#include <map>
#include <string>
#include <cstdio>
using namespace std ;
/**
 *   Set the state count, neighbor count, and put your function here.
 */
const int numStates = 3 ;
const int numNeighbors = 8 ;
/**
    0:  (nothing set)
    1:  \
    2:  /
*/
/* order for nine neighbors is nw, ne, sw, se, n, w, e, s, c */
int f(int *a) {
  int on1 = (a[0] & 1) + (a[3] & 1) + (a[4] >> 1) + (a[5] >> 1) +
    (a[6] >> 1) + (a[7] >> 1) + (a[8] & 1) ;
  int on2 = (a[1] >> 1) + (a[2] >> 1) + (a[4] & 1) + (a[5] & 1) +
    (a[6] & 1) + (a[7] & 1) + (a[8] >> 1) ;
  if (on1 == 2)
    if (on2 == 2)
      return 0 ;
    else
      return 1 ;
  else if (on2 == 2)
    return 2 ;
  else
    return 0 ;
}
const int numParams = numNeighbors + 1 ;
map<string,int> world ;
vector<string> r ;
int nodeSeq, params[9] ;
int getNode(const string &n) {
   map<string,int>::iterator it = world.find(n) ;
   if (it != world.end())
      return it->second ;
   world[n] = nodeSeq ;
   r.push_back(n) ;
   return nodeSeq++ ;
}
int recur(int at) {
   if (at == 0) {
      return f(params) ;
   } else {
      char buf[256*10] ;
      sprintf(buf, "%d", at) ;
      for (int i=0; i<numStates; i++) {
         params[numParams-at] = i ;
         sprintf(buf+strlen(buf), " %d", recur(at-1)) ;
      }
      return getNode(buf) ;
   }
}
void writestring() {
   printf("num_states=%d\n", numStates) ;
   printf("num_neighbors=%d\n", numNeighbors) ;
   printf("num_nodes=%d\n", r.size()) ;
   for (unsigned int i=0; i<r.size(); i++)
      printf("%s\n", r[i].c_str()) ;
}
int main(int argc, char *argv[]) {
   recur(numParams) ;
   writestring() ;
}