Exemplo n.º 1
0
void sow(uint8_t* gameBoard, uint8_t row, uint8_t column) {
	uint8_t* orig_pit_ptr =  gameBoard_at(gameBoard, row, column);
	uint8_t* pit_ptr = NULL;
	uint8_t startRow = row, startColumn = column;
	int seedsInHand = *orig_pit_ptr;
	int rowshift = 0;
	
	if (*orig_pit_ptr == 0) return;
	*orig_pit_ptr = 0;
	while (seedsInHand > 0) {
		seedsInHand--;
		/*row + 1 if at (0,0) or (2,0)*/
		/*row - 1 if at (1,7) or (3,7)*/
		rowshift = (!(row%2) && column == 0);
		rowshift -= (!rowshift) * (row%2 && (column == GAME_BOARD_COLUMNS - 1)); 
		row += rowshift;
		
		/*don't change column when row just changed*/
		/*column + 1 if in row 1 or 3*/
		/*column - 1 if in row 0 or 2*/
		printf("row: %i, column: %i, rowshift: %i\n", row, column, rowshift);
		column = column + (!rowshift) * (1 - 2 * !(row%2));
		
		pit_ptr = gameBoard_at(gameBoard, row, column);
		*pit_ptr += 1;
	}
	
	if (*pit_ptr == 1) /*if we ended in an empty pit*/
		return;
	
	/*if there already were seeds in the pit we put the last seed into*/
	/*if at row 1, check 2 and 3; if at row 2, check 0 and 1*/
	else if (row == 1 || row == 2) {
		rowshift = 3 - 2*row; /*+1 if row == 1; -1 if row == 2*/
		uint8_t* targetPit1_ptr = gameBoard_at(gameBoard, row+rowshift, column);
		uint8_t* targetPit2_ptr = gameBoard_at(gameBoard, row+2*rowshift, column);
		if (*targetPit1_ptr > 0 && *targetPit2_ptr > 0) {
			*orig_pit_ptr += *targetPit1_ptr + *targetPit2_ptr;
			*targetPit1_ptr = 0;
			*targetPit2_ptr = 0;
			sow(gameBoard, startRow, startColumn);
			return;
		}
	} 
	/*if we ended in a pit with seeds in it, but the opposing pits aren't filled or we're not in the offensive row*/
	sow(gameBoard, row, column);
}
Exemplo n.º 2
0
int main() {
  std::vector<int> things(1000000);
  for(int& i: things) {
    i = rand();
  }

  auto finder = Chaff::MaxFinder<int,int>::byCount(3);
  for(int i: things) {
    finder.sow(i, i);
  }

  std::cout << "The best three things are:\n";
  for(int i: finder.reap()) {
    std::cout << " - " << i << '\n';
  }

  std::cout << "Aren't they amazing?\n\n";
  return 0;
}
Exemplo n.º 3
0
Arquivo: snget.c Projeto: liu-chong/sn
int main (int argc, char **argv)
{
   char optdebugbuf[7];
   int n, mark;
   char *cp;

   progname = ((cp = strrchr(argv[0], '/')) ? cp + 1 : argv[0]);

   while ((n = opt_get(argc, argv, "pthcmo")) > -1)
      switch (n)
      {
         case 'd':
            if (++debug < 6)
	    {
               if (!optdebug)
                  strcpy(optdebug = optdebugbuf, "-d");
               else
                  strcat(optdebug, "d");
            }
            break;
         case 'V': version(); _exit(0);
         case 't':
            if (!opt_arg)
               usage();
            LOG("option \"-t %s\" no longer supported", opt_arg);
            break;
         case 'p':
            if (!opt_arg)
               usage();
            concurrency = strtoul(opt_arg, &cp, 10);
            if (concurrency > MAX_CONCURRENCY || concurrency <= 0 || *cp)
               fail(1, "Bad value for concurrency option -p");
            break;
         case 'h':
            if (!opt_arg)
               usage();
            throttlerate = strtoul(opt_arg, &cp, 10);
            if (throttlerate < 0)
               fail(1, "Bad value for throttle option -h");
            break;
         case 'c':
            if (!(optpipelining = opt_arg))
               usage();
            if (strtoul(optpipelining, &cp, 10) < 0 || *cp)
               fail(1, "Bad value for pipeline option -c");
            break;
         case 'm':
            if (!(optmax = opt_arg))
               usage();
            if (strtoul(optmax, &cp, 10) <= 0 || *cp)
               fail(1, "Bad value for max-prime-articles option -m");
            break;
         case 'P':
            optlogpid = TRUE;
            log_with_pid();
            break;
         default:
            usage();
      }
   close(0);
   open("/dev/null", O_RDONLY);
   /* snag 6 and 7 so we can dup onto them */
   if (-1 == dup2(2, 6) || -1 == dup2(2, 7) || -1 == dup2(2, 1))
      fail(2, "Unable to dup standard error:%m");

   parameters(TRUE);
   if (-1 == chdir(snroot))
      fail(2, "chdir(%s):%m", snroot);
   init();
   if (-1 == set_path_var())
      fail(2, "No memory");

   n = 0;
   if (opt_ind == argc)
   {
      DIR *dir;
      struct dirent *dp;
      struct stat st;
      char ch;

      if (!(dir = opendir(".")))
         fail(2, "opendir(%s):%m", snroot);
      while ((dp = readdir(dir)))
         if (is_valid_group(dp->d_name))
            if (-1 == readlink(dp->d_name, &ch, 1)) /* no symlinks */
               if (0 == statf(&st, "%s/.outgoing", dp->d_name))
                  if (S_ISDIR(st.st_mode))
                     if (add(dp->d_name) > -1)   /* NB: add() from get.c */
                        n++;
      closedir(dir);
   }
   else
   {
      debug++;
      for (; opt_ind < argc; opt_ind++)
         if (add(argv[opt_ind]) > -1)
            n++;
      debug--;
   }

   if (n == 0)
      fail(0, "No groups to fetch");

   for (mark = 0; jobs_not_done(); mark++)
   {
      struct timeval tv;
      fd_set rset;
      int max;

      while (sow() == 0)   /* Start some jobs */
         ;
      FD_ZERO(&rset);
      if (throttlerate)
      {
         max = throttle_setfds(&rset);
         if (sigusr)
         {
            sigusr = FALSE;
            LOG("throttling at %d bytes/sec", throttlerate);
         }
      }
      else
         max = -1;
      
      tv.tv_sec = 1;
      tv.tv_usec = 0;
      if (select(max + 1, &rset, NULL, NULL, &tv) > 0)
         if (throttlerate)
            throttle(&rset);
      
      if (sigchld || 1 == mark % 10)
      {
         sigchld = FALSE;
         while (reap() == 0)
            ;
      }
   }
   quit();
   _exit(0);
}