TweetFollow Us on Twitter

Nov 98 Prog Challenge

Volume Number: 14 (1998)
Issue Number: 11
Column Tag: Programmer's Challenge

Nov 98 Challenge

by Bob Boonstra, Westford, MA

Rendezvous and Docking

These are exciting times for space buffs like myself. We've been watching the Russian efforts to keep Mir operating, and the visits of the space shuttle to that space station. We've watched a variety of planetary gravity assist maneuvers to send the Galileo spacecraft to Jupiter and its moons and the Ulysses spacecraft into polar orbit around the sun. A satellite that did not achieve its intended geostationary orbit is being recovered using innovative gravity assist maneuvers around the moon. And over the next few years, the United States and its international partners will be launching and assembling the International Space Station.

Let's imagine that NASA has asked the Programmer's Challenge to develop some efficient software for navigating around the solar system and efficiently completing a rendezvous with another object. The prototype for the code you should write is:

#if defined(__cplusplus)
extern "C" {
#endif

typedef float SimTime;     /* seconds */

typedef struct Vector3D {  /* right-handed coordinate system x-y-z */
   float x;
   float y;
   float z;
} Vector3D;

typedef enum {kDestroyed=0, kShip, kTarget, kOtherMass} BodyType;

typedef struct SimConstants {
   float G;                /* in Newtons*meters/(kg**2) */
   SimTime minTimeStep;    /* simulation timeStep will be >= minTimeStep */
   SimTime maxTimeStep;    /* simulation timeStep will be <= maxTimeStep */
   float maxAcceleration;  /* meters/(sec**2) */
   float timePenalty;      /* meters/sec per millisecond */
} SimConstants;

typedef struct Body {
   float mass;             /* mass in kg */
   float radius;           /* radius in meters */
   Vector3D   position;    /* position vector in meters */
   Vector3D   velocity;    /* velocity vector in meters */
   BodyType   bodyType;    /* (self explanatory) */
} Body;

pascal void InitSim(
   const SimConstants simConstants,    /* simulation constants */
   const SInt8 numBodies,  /* number of bodies in the simulation */
   const Body theBodies[]  /* parameters of simulation bodies */
);

pascal Boolean AdvanceTime( /* return TRUE if docking complete or giving up */
   const SimTime timeStep,  /* propagate forward this amount of time */
   Vector3D *shipAcceleration,  /* return ship acceleration vector applied */
                          /*this step */
   Body theBodies[]  /* return body parameters at the end of this time step */
);

#if defined(__cplusplus)
}
#endif

You will be given a number of Body objects moving through space. One of those objects will be a ship under your control, and another will be a target object. The rest will be objects that exert gravitational influences on one another. Your objective is to guide your ship to the target and complete a rendezvous, minimizing a cost formula that depends on the fuel expended by your ship and the time to complete the rendezvous. First, your InitSim routine will be provided with a number of parameters for the problem, and then your AdvanceTime routine will be called repeatedly to propagate all of the objects in our simulated solar system.

In order to be able to score this Challenge, we'll have to agree on the equations of motion. To the best of my recollection, the equations we will use are the non-relativistic approximations of the versions that govern the real world.

The gravitational force exerted by one object ([j]) on another object ([i]) is given by:

m[i]*r''[i] = - G* m[i]*m[j]*(r[i]-r[j]) / |(r[i]-r[j])|**3, 

where:

  • r[k] is the position vector for object k,
  • r''[k] is the acceleration vector for object k,
  • G is the gravitational constant provided in simConstants
  • m[k] is the mass of object k
  • |v| denotes the magnitude of vector v

The position (r) and velocity (r') vectors for an object at the end of timeStep t are:

   r'new = r' + r''*t
   rnew = r + + r'*t + r''*(t**2)/2

where

  • r, r' are the position and velocity vectors at the start of the timeStep
  • rnew, r'new are the position and velocity vectors at the end of the timeStep
  • t is the duration of the timeStep
  • r'' is the vector sum of all gravitational and ship accelerations acting on the object

Your InitSim routine will be given a set of simConstants that govern the simulation, the number of bodies (numBodies) included in the simulation, and a set of characteristics for each simulated Body, including mass, radius, position vector, velocity vector, and the type of body this is. Exactly one Body (kShip) will be your ship and exactly one will be your target (kTarget).

The simConstants will include the gravitational constant G used to propagate objects, the maximum acceleration (vector magnitude) your ship can endure without breaking up, the maximum and minimum time increments to be used for propagation, and a scaling factor used for scoring.

Each time AdvanceTime is called, you should determine the shipAcceleration you want to apply to your ship, compute the gravitational forces of each object on every other object, compute the new position and velocity of each object, and return the shipAcceleration and the updated Body values in parameters provided.

In the event of a collision, where the object spheres intersect, the smaller object is absorbed by the larger object, increasing its mass, and the velocity of the larger object is changed to conserve the momentum vector (mass * velocity). You only need to worry about collisions at the end of a timeStep. If this means that one of your objects passes through another object undetected, we will attribute that to pseudo quantum mechanical uncertainty in our nonrelativistic universe.

To allow longer simulations to complete, it may be necessary to vary the timeStep value used. Each timeStep will be between minTimeStep and maxTimeStep in duration. The timeStep values will be calculated so that they may increase when the largest gravitational force is smaller and your ship is far from the target, and may decrease when one or more of the gravitational forces becomes large, or when your ship comes close to the target.

Our simulated solar system has a few simplifying characteristics compared with the real world. Fortunately, all of the bodies in the system are perfect spheres of uniform density, so the center of mass is exactly at the center of the sphere, allowing gravity to be modeled as if they were point masses. Our ship has the ability to instantly accelerate in any direction, at any magnitude from zero to maxAcceleration. And for our purposes, a rendezvous is considered accomplished when the ship and the target are within 10 ship radii of intersecting, and their relative velocities differ in magnitude by less than 1 meter/sec or .0001 times the velocity of the ship.

The winner will be the solution that successfully completes the rendezvous at the lowest cost. Cost for this Challenge is primarily determined by the amount of fuel your ship uses, calculated as the sum of the magnitudes of the acceleration vectors you apply during each timeStep. In order to keep execution time reasonable, and in the spirit of the Challenge, the score will be incremented by the total amount of execution time spent in your InitSim and AdvanceTime routines, multiplied by a timePenalty scaling factor. I will not know how large to set the timePenalty scaling factor until I see how long the solutions take to execute, but the same timePenalty factor will be used for everyone. My intent is to structure the problems and the timePenalty value so that most solutions complete in minutes or tens of minutes.

All distance, mass, and time values will be in the mks (meters, kilograms, seconds) system.

This will be a native PowerPC Challenge, using the latest CodeWarrior environment. Solutions may be coded in C, C++, or Pascal.

Three Months Ago Winner

Congratulations to Ernst Munter (Kanata, Ontario) on his return to the Challenge winner's circle, and for submitting the fastest of three entries to the Blockbuster Challenge. Based on the original Soma cube puzzle, the objective was to reassemble a collection of pieces formed from individual cubies into a designated target shape.

Ernst's solution creates a StateMap data structure that represents the goal state, or more accurately, the complement of the goal state. The space representing the goal structure starts out as empty, and is surrounded by a shell of "dummy" cubies that represent the outside boundary of the goal. Each cell contains a set of Flags that indicate the type of surface that exists at a given set of coordinates. Ernst uses the number of corners on a piece as a measure of the complexity of the piece, and I think one of the keys to his success was the fact that he uses this measure to try to place the more complex pieces first.

The three entries to the Blockbuster Challenge were evaluated using a set of seven test cases, including the original Soma cube, some alternative shapes that can be formed using the pieces of the original Soma cube, and some larger puzzles. Unfortunately, the solution time increased very quickly with puzzle size, so I was unable to wait for the entries to solve some of the puzzles I had originally intended to include in the evaluation. I do not think this affected the evaluation results, as the performance advantage of Ernst's winning entry increased as the problem size got larger.

The table below lists, for each of the solutions submitted, the total execution time for the seven test cases, the execution time for the classic Soma cube puzzle, and the execution time for one of the Soma variants. You can see that while the winning entry was not the fastest for the classic puzzle, it was fastest for the variant and fastest overall. The table also includes code size, data size, and programming language used. As usual, the number in parentheses after the entrant's name is the total number of Challenge points earned in all Challenges prior to this one. The entry listed with an asterisk was unable to solve one of the test cases and was therefore ineligible to earn points.

Name Total Time (msec) Soma Time (msec) Soma Time2 (msec) Code Data Lang
Ernst Munter (388) 269.1 11.4 164.9 7752 8424 C++
Sebastian Maurer (30) 3244.6 1.1 256.3 5200 176 C
W.R. (*) 349046.6 1.5 234.1 12204 192 C++

Top Contestants

Both of the contestants earning points this month were already members of our points leader list. Ernst adds to his already dominating points lead, and Sebastian moves up from 10th to 7th place. Listed here are the Top Contestants for the Programmer's Challenge, including everyone who has accumulated 20 or more points during the past two years. The numbers below include points awarded over the 24 most recent contests, including points earned by this month's entrants.

Rank Name Points
1. Munter, Ernst 194
2. Boring, Randy 56
3. Mallett, Jeff 50
4. Saxton, Tom 49
5. Rieken, Willeke 47
6. Cooper, Greg 44
7. Maurer, Sebastian 40
8. Heithcock, JG 37
9. Nicolle, Ludovic 34
10. Lewis, Peter 31
11. Murphy, ACC 24
12. Gregg, Xan 22
13. Hart, Alan 21
14. Antoniewicz, Andy 20
15. Day, Mark 20
16. Higgins, Charles 20
17. Hostetter, Mat 20
18. Studer, Thomas 20

There are three ways to earn points: (1) scoring in the top 5 of any Challenge, (2) being the first person to find a bug in a published winning solution or, (3) being the first person to suggest a Challenge that I use. The points you can win are:

  • 1st place 20 points
  • 2nd place 10 points
  • 3rd place 7 points
  • 4th place 4 points
  • 5th place 2 points
  • Finding bug 2 points
  • Suggesting Challenge 2 points

Here is Tom's winning Elevator solution:

BlockBuster.cp

Copyright © 1998 Ernst Munter

/*

The Problem

Assemble puzzle pieces made from unit-size cubies into the goal shape.

The Solution

The pieces are systematically tried in assembling the goal.

Data Representation

The goal and the cubies are represented in a 3D array space with a 7 bit flag set describing the surfaces of a cubie. The left-down-front bits are stored at the 3D coordinates of the cubie, the right-up-back bits are stored at the next coordinate points in the x, y, and z direction. The seventh bit denotes the presence of a "real" cubie at a 3D location. Simple arithmetic is used to merge a piece into an open goal spot, and to remove it again.

Note: Please reserve enough stack space to handle the recursive solver. The extra stack size required is about 112 bytes per piece, e.g for a 1000 piece puzzle, one should increase the stack size from the 64K compiler default to perhaps 170K. (although this may be a moot point, considering that the algorithm is too slow for any such large number of pieces, unless they are very similar, for example just like bricks.

*/

#include <string.h>
#include <stdlib.h>
#include "BlockBuster.h"

typedef enum {
   kL=1,          // Left
   kR=2,          // Right
   kD=4,          // Down   
   kU=8,          // Up
   kF=16,         // Front
   kB=32,         // Back
   kReal=64,      // Actual Cubie
   kBlocked=kReal,
   kHidden=128,
   kCorner=kL+kD+kF,
   kPack=1} Flags;

typedef UInt16 State;   // could use UInt8 if numPieces < 256

LookupTable
// Lookup for merging/unmerging pieces into the goal
static struct LookupTable {
   UInt8 MergeTable[64][64];
   UInt8 UnmergeTable[64][64];
   LookupTable(){
      for (int goal=0;goal<64;goal++) {
         for (int piece=0;piece<64;piece++) {
            MergeTable[goal][piece] =
               (((goal & (kL+kR)) + 2*(piece & (kL+kR))) % 3) +
               (((goal & (kD+kU)) + 2*(piece & (kD+kU))) % 12) +
               (((goal & (kF+kB)) + 2*(piece & (kF+kB))) % 48);
            UnmergeTable[goal][piece] =
               (((goal & (kL+kR)) + (piece & (kL+kR))) % 3) +
               (((goal & (kD+kU)) + (piece & (kD+kU))) % 12) +
               (((goal & (kF+kB)) + (piece & (kF+kB))) % 48);
         }
      }
   }
} LUT;

Merge/Unmerge
inline int Merge(const int goal,const int piece) {
   return LUT.MergeTable[goal&0x3F][piece&0x3F]
      + ((goal ^ piece) & kReal);
}

inline int Unmerge(const int goal,const int piece) {
   return LUT.UnmergeTable[goal&0x3F][piece&0x3F]
      + ((goal ^ piece) & kReal);
}
// sequence to drive a piece through all 24 rotated positions
static char rotPattern[24] =
   {0,1,1,0, 2,1,0,1, 2,1,0,0, 1,1,1,0, 2,1,0,1, 1,1,2,1};

Spot 
// Spot is a wrapper class for cubies, and is also used for 3D algebra
struct Spot {   
   Cubie   cubie;
#define X cubie.xCoord
#define Y cubie.yCoord
#define Z cubie.zCoord
#define FLAGS cubie.value    
   Spot(){}
   Spot(const int x,const int y,const int z) {X=x;Y=y;Z=z;}
   Spot(const Cubie & C){cubie=C;}
   int CompSpot(const Spot & f) const {
      if (X == f.X) {
         if (Y == f.Y) return Z - f.Z;
         return Y - f.Y;
      }
      return X - f.X;
   }
   int CompSpotFlags(const Spot & f) const {
      if (FLAGS == f.FLAGS) return CompSpot(f);
      return FLAGS - f.FLAGS;
   }
   void LowerBound(const Spot & S) {
      if (S.X < X) X = S.X;
      if (S.Y < Y) Y = S.Y;
      if (S.Z < Z) Z = S.Z;
   }
   void UpperBound(const Spot & S) {
      if (S.X > X) X = S.X;
      if (S.Y > Y) Y = S.Y;
      if (S.Z > Z) Z = S.Z;
   }
   int MaxCoordValue() const {
      if ((X>Y) && (X>Z)) return X;
      if (Y>Z) return Y;
      return Z;
   }
   Spot operator + (const Spot b) const {
      return Spot(X+b.X,Y+b.Y,Z+b.Z);
   }
   Spot operator - (const Spot b) const {
      return Spot(X-b.X,Y-b.Y,Z-b.Z);
   }
   void operator += (const Spot b) {
      X += b.X; Y += b.Y; Z += b.Z;
   }
   void operator -= (const Spot b) {
      X -= b.X; Y -= b.Y; Z -= b.Z;
   }
   void operator |= (const Spot b) {FLAGS |= b.FLAGS;}
   bool IsNegative() const {return ((X|Y|Z) < 0);}
   int Offset(const Spot range) const {
      return (Z*range.Y+Y)*range.X+X;
   }
   void ClearFlags(const int flags){FLAGS&=~flags;}
   void CancelFlags() {
      if ((FLAGS & kL)&&(FLAGS & kR)) ClearFlags(kL+kR);
      if ((FLAGS & kD)&&(FLAGS & kU)) ClearFlags(kD+kU);
      if ((FLAGS & kF)&&(FLAGS & kB)) ClearFlags(kF+kB);
   }
   bool IsHidden() const {return FLAGS & kHidden;}
   bool IsReal() const {return FLAGS & kReal;}
   void SetFlags(const int flags) {FLAGS=flags;}
   void ShiftRight() {X++;}
   void ShiftUp() {Y++;}
   void ShiftBack() {Z++;}
   UInt32 Hash() {return (((X*511)+Y)*2503)+Z;}
};

// Two compare functions for use with quick sort

CompSpot/CompSpotFlags
// Compare coordinates only, (detect duplicates)
static int CompSpot(const void* a,const void* b) {
   return ((const Spot*)b)->CompSpot(*((const Spot*)a));
}

// Compare coordinates and flags (sort real cubies to front)
static int CompSpotFlags(const void* a,const void* b) {
   return ((const Spot*)b)->CompSpotFlags(*((const Spot*)a));
}

StateMap 
// StateMap is a 3D representation of the goal which can be indexed
// as an array.  Each state value represents a number of flags,
// which indicate if a real cubie exists, and which kind of surface
// (left, right, etc) occurs at these coordinates.
//
// Initially, the goal is empty, but surrounded by a shell of cubies
// which define the shape of the goal, and block the placement of
// pieces outside the goal.
//
// As pieces are placed, they effectively shrink the shell, by removing
// some, and adding other, surfaces, and by blocking the space they
// occupy.
//
// After the puzzle is solved, the state array is used to transfer the
// piece identifiers to the Goal of the calling program.
static struct StateMap {
   int      width;
   int      height;
   int       lengthOfState;
   State*   state;
   int      firstGoalIndex;
   int      cacheCorner;
   StateMap(const Spot goalRange,Spot cubies[],const int numCubies) {
// copies spots defining the goal surface into the state map
      width=goalRange.X;
      height=goalRange.Y;
      lengthOfState=width*height*goalRange.Z;
      state=new State[lengthOfState];   
// Initially, all spaces are blocked
      for (int i=0;i<lengthOfState;i++)
         state[i]=kBlocked;    
      Spot* C=cubies;
// Then, the cubies of the goal shape are excavated
      firstGoalIndex=lengthOfState;
      for (int i=0;i<numCubies;i++,C++) {
         int index=C->Offset(goalRange);
         state[index]=Flags(C->FLAGS) ^ kReal;
         if (index<firstGoalIndex) firstGoalIndex=index;
      }
      cacheCorner=firstGoalIndex;
   }
   ~StateMap(){delete[] state;}

   int Corner() {
// Finds a left-lower-front corner in the goal
// should never fail, and should really be better optimized
      if (state[cacheCorner]==(kL+kD+kF)) // probably still good
         return cacheCorner;
      State* C=state+firstGoalIndex-1;
      for (int i=firstGoalIndex;i<lengthOfState;i++) {
         if (*++C == (kL+kD+kF)) {
            cacheCorner=i;
            return i;
         }
      }
      return -1;   // just in case
   }

   bool Collides(const int target,const int sourceFlags) const {
// Checks if piece is compatible
      return (state[target] & sourceFlags & kBlocked);
   }
   void Place(const int target,const int sourceFlags) {
      state[target]=Merge(state[target],sourceFlags);
   }
   void Unplace(const int target,const int sourceFlags) {
      state[target]=Unmerge(state[target],sourceFlags);
   }
   void Copy(const int target,const int id) {
      state[target]=id;
   }
} * goalMap;

CommonPiece
// CommonPiece is the ancestor class for MyPieces and MyGoal
// Creates a temporary set of cubies for computing states and
// surfaces, and tracks the piece-id (value)
struct CommonPiece {
   int      numCubies;
   Spot*      cubies;      // only used during initialization
   int       id;
   void Init(Piece & thePiece,const Spot offset,bool pack) {
// record piece id value just once
      id=thePiece.theCubies[0].value;
      numCubies=thePiece.numCubies;

// Each cubie has potentially 3 virtual neighbors: R,U,B
      int tempNum=4*numCubies;
      Spot* temp=new Spot[tempNum];
      memcpy(temp,thePiece.theCubies,
               sizeof(Spot)*numCubies);
      memcpy(temp+numCubies,thePiece.theCubies,
               sizeof(Spot)*numCubies);
      memcpy(temp+2*numCubies,thePiece.theCubies,
               sizeof(Spot)*numCubies);
      memcpy(temp+3*numCubies,thePiece.theCubies,
               sizeof(Spot)*numCubies);

// setup real cubies and 3 border cubies for each
// each cubie serves to define one or more surfaces, using the flags
      for (int i=0,r=numCubies,u=r+numCubies,b=u+numCubies;
         i<numCubies;i++,r++,u++,b++) {
         temp[i].SetFlags(kL+kD+kF+kReal);
         temp[r].SetFlags(kR);temp[r].ShiftRight();
         temp[u].SetFlags(kU);temp[u].ShiftUp();
         temp[b].SetFlags(kB);temp[b].ShiftBack();
      }

// combine cubies at equal coordinates, and hide unneeded cubies
      qsort(temp,tempNum,sizeof(Spot),CompSpot) ;
      for (int i=1;i<tempNum;i++) {
         if (0==temp[i].CompSpot(temp[i-1])) {
            temp[i] |= (temp[i-1]);
            temp[i-1].SetFlags(kHidden);
         }
      }

// cancel flags, remove hidden cubies, and shift by offset
      for (int i=0;i<tempNum;) {
         if (temp[i].IsHidden()) {
            temp[i]=temp[-tempNum];
         } else {
            temp[i].CancelFlags();
            temp[i] -= offset;
            i++;
         }
      }

// Resort, to bring real cubies to front of array
      qsort(temp,tempNum,sizeof(Spot),CompSpotFlags) ;
// For the goal, we will need placeholders for all spots in 3D space,
// but for the pieces, we can shorten the list by removing all cubies
// which represent hidden internal surfaces.
      if (pack) {
         numCubies=tempNum;
         cubies=new Spot[numCubies];
         memcpy(cubies,temp,sizeof(Spot)*numCubies);
         delete[] temp;         
      } else {
         cubies=temp;
         numCubies=tempNum;
      }
   }
};

Rotate and Utility Functions
// Non-class based utility functions, for rotating the pieces while preparing
// the rotated versions of each piece, and to determine their extreme coords.
typedef void (*RotateFunction)(Piece &);
static void RotateX(Piece & thePiece) {
   Cubie* C=thePiece.theCubies;
   for (int k=0;k<thePiece.numCubies;k++,C++) {
      int save=C->zCoord;C->zCoord=C->yCoord;C->yCoord=-save;
   }
}

static void RotateY(Piece & thePiece) {
   Cubie* C=thePiece.theCubies;
   for (int k=0;k<thePiece.numCubies;k++,C++) {
      int save=C->xCoord;C->xCoord=C->zCoord;C->zCoord=-save;
   }
}

static void RotateZ(Piece & thePiece) {
   Cubie* C=thePiece.theCubies;
   for (int k=0;k<thePiece.numCubies;k++,C++) {
      int save=C->yCoord;C->yCoord=C->xCoord;C->xCoord=-save;
   }
}

static RotateFunction RotatePiece[3] = {RotateX,RotateY,RotateZ};
static Spot Lowest(const Piece & piece) {
   Cubie* C=piece.theCubies;
   Spot low=Spot(C[0]);
   for (int i=1;i<piece.numCubies;i++) {
      low.LowerBound(*++C);
   }
   return low;
}

static Spot Highest(const Piece & piece) {
   Cubie* C=piece.theCubies;
   Spot high=Spot(C[0]);
   for (int i=1;i<piece.numCubies;i++) {
      high.UpperBound(*++C);
   }
   return high;
}

static int Diameter(const Piece & piece) {
   Cubie* C=piece.theCubies;
   Spot low=Spot(C[0]);
   Spot high=Spot(C[0]);
   for (int i=1;i<piece.numCubies;i++) {
      low.LowerBound(*++C);
      high.UpperBound(*C);
   }
   return (high-low).MaxCoordValue();
}

RotPiece 
// RotPiece is one distinct rotational instance of a piece.
// Only the "state plus Index" is stored in 2 fields in a 32-bit variable,
// representing the surface state of the piece (8 bits),
// and the index in global goal map coordinates (24 bits).
struct RotPiece {
   int      lengthOfState;
   int      lengthOfReal;
   int      corner;      // current corner during search
   UInt32*   stateIndex;   // 8+24 bits
   void Init(Spot cubies[],const int numCubies,
                                    const Spot goalRange) {
      lengthOfState=numCubies;
      stateIndex=new UInt32[lengthOfState];
      corner=0;
      lengthOfReal=1;
      Spot* C=cubies;
      for (int i=0;i<numCubies;i++,C++) {
         stateIndex[i] = (C->Offset(goalRange) << 8) | 
                                          Flags(C->FLAGS);
         if (C->FLAGS & kReal) lengthOfReal++;
      }
   }
   void Destroy(){delete[] stateIndex;}

   int NumCorners() {
// a measure of complexity, we try to place more complex pieces first
      int numCorners=0;
      for (int i=0;i<lengthOfReal;i++) {
         if ((stateIndex[i] & kCorner) == kCorner) numCorners++;
         else break;
      }
      return numCorners;
   }
   int GetSpot() {
// Returns the index to the left-lower-front corner spot
// Increments the corner and returns -1 if all corners have been exhausted
      UInt32* SI=stateIndex+corner;
      if ((*SI & kCorner) == kCorner) {
         corner++;
         return (*SI >> 8);
      }
      corner=0;
      return -1;
   }

   bool Fits(int xlate) {
// Returns true if the piece would fit, when translated into the goal map.
      UInt32* SI=stateIndex-1;
// spot[0] always fits, because it was used to define xlate
      for (int i=1;i<lengthOfReal;i++) {
         int target= xlate + (*++SI >> 8);
         int sourceFlags=*SI & 0xFF;
         if (goalMap->Collides(target,sourceFlags)) return false;
      }
      return true;
   }
   void Place(int xlate) {
// Places all cubies and surfaces into the goal map.
      UInt32* SI=stateIndex-1;
      for (int i=0;i<lengthOfState;i++) {
         int target= xlate + (*++SI >> 8);
         int sourceFlags=*SI & 0xFF;
         goalMap->Place(target,sourceFlags);
      }
   }
   void Unplace(int xlate) {
// Removes this piece from the goal map.
      UInt32* SI=stateIndex-1;
      for (int i=0;i<lengthOfState;i++) {
         int target= xlate + (*++SI >> 8);
         int sourceFlags=*SI & 0xFF;
         goalMap->Unplace(target,sourceFlags);
      }
   }
   void Copy(int id,int xlate) {
// Copies the piece id into the goal map, overriding cubie states
      UInt32* SI=stateIndex-1;
      for (int i=0;i<lengthOfReal;i++) {
         int target= xlate + (*++SI >> 8);
         goalMap->Copy(target,id);
      }
   }
};

CommonPiece
// Private representation of the piece.
struct MyPiece:CommonPiece {
   RotPiece   rotPiece[24];   // there can be up to 24 unique versions
   short      numRotations;
   short      curRotation;   
   int      source;         // on the piece
   int      target;         // on the goal
   int      numCorners;
   ~MyPiece() {
      for (int i=0;i<numRotations;i++) {
         rotPiece[i].Destroy();
      }
   }
   void Init(const Spot goalRange,Piece & thePiece) {
      numRotations=0;
      curRotation=0;
      numCorners=0;
      source=0;target=-1;
      CreateRotatedPieces(goalRange,thePiece);
      for (int i=0;i<numRotations;i++) {
         numCorners+=rotPiece[i].NumCorners();
      }   
   }
   void CreateRotatedPieces(const Spot goalRange,Piece & thePiece) {
// Generates all distinct versions by rotation through a cyclic pattern
// Normalizes all pieces by aligning to 0 and sorting, so that equal versions
// can be recognized easily using hash values.
      UInt32 H[24];
      Align(thePiece);
      H[0]=Hash(thePiece);
      CommonPiece::Init(thePiece,Spot(0,0,0),kPack);// make cubies
      rotPiece[numRotations++].Init(cubies,numCubies,goalRange);
      // cubies have done their job
      delete[] cubies;                  
      for (int i=1;i<24;i++) {
         RotatePiece[rotPattern[i]](thePiece);
         Align(thePiece);
         H[i]=Hash(thePiece);               
         for (int k=0;k<i;k++) {
            if (H[k]==H[i]) goto notNew;
         }
         CommonPiece::Init(thePiece,Spot(0,0,0),kPack);
         rotPiece[numRotations++].Init(cubies,numCubies,goalRange);
         delete[] cubies;
notNew:;               
      }                              
   }
   void Align(Piece & thePiece) {
      Spot offset=Lowest(thePiece);
      Cubie* C=thePiece.theCubies;
      for (int i=0;i<thePiece.numCubies;i++,C++) {
         C->xCoord-=offset.X;
         C->yCoord-=offset.Y;
         C->zCoord-=offset.Z;
      }
      qsort(thePiece.theCubies,thePiece.numCubies,
         sizeof(Cubie),CompSpot);
   }
   UInt32 Hash(Piece & thePiece) {
      UInt32 h=1;
      for (int i=0;i<thePiece.numCubies;i++) {
         UInt32 hf=Spot(thePiece.theCubies[i]).Hash();
         h=(h<<9) + ((h>>23)^hf);
      }
      return h;
   }
   int Available(){
// A piece is available for placing if it does not have a target assigned
      return (target < 0);
   }
   bool NextRotation() {
// Cycles through the distinct rotations
      curRotation++;
      if (curRotation>=numRotations) {
         curRotation=0;
         return false;
      }
      return true;
   }
   int GetSpot(int t) {
// Cycles through the left-lower-front corners of a piece
// If a rotation is found, sets target for the piece to t
//    and returns the corresponding source corner in the piece.
      source=rotPiece[curRotation].GetSpot();
      if (source<0) target=-1;
      else target=t;
      return source;
   }
// The next 4 functions are passed on to the current rotational version
   bool Fits() {
      return rotPiece[curRotation].Fits(target-source);
   }
   void Place() {
      rotPiece[curRotation].Place(target-source);
   }
   void Unplace() {
      rotPiece[curRotation].Unplace(target-source);
   }
   void Copy() {
      rotPiece[curRotation].Copy(id,target-source);
   }
   int NumCorners(){return numCorners;}
};

CompPiece
// Comparator function for sorting pieces by complexity (in reverse order)
static int CompPiece(const void* a,const void* b) {
   MyPiece* pa=(MyPiece*)a;
   MyPiece* pb=(MyPiece*)b;
   return pb->NumCorners()-pa->NumCorners();
}

MyGoal
// Private version of the goal which directly and indirectly owns
// all other non-global private data.
struct MyGoal:CommonPiece {
   MyPiece*   pieces;
   int      numPieces;
   int      piecesLeft;
   Spot      goalOffset;   // goal aligned to 0
   Spot      goalRange;
   MyGoal(const long num,Piece thePieces[],Piece theGoal) {
// computes overall dimensions and an offset which define a safe private
// three-dimensional goal space
      goalOffset=Lowest(theGoal);
      int largest=Diameter(thePieces[0]);
      for (int i=1;i<num;i++) {
         int pieceSize=Diameter(thePieces[i]);
         if (pieceSize>largest)
            largest=pieceSize;
      }
      Spot maxPiece=Spot(largest,largest,largest);
      goalOffset-=maxPiece;
// goal cubies are translated into the (0,0,0) based goal range
// of sufficient size to handle the largest misplaced piece.
      CommonPiece::Init(theGoal,goalOffset, ! kPack);
      goalRange=Highest(theGoal)
         - goalOffset + maxPiece + Spot(1,1,1);

// Check for overflow:
      if (goalRange.IsNegative()) {
// Should not happen if diameter of puzzle is < 32K, but if it does,
// we signal this to the Solve() function with a NULL goal map.
         goalMap=0;
         pieces=0;
         return;
      }      
      goalMap = new StateMap(goalRange,cubies,numCubies);   
// these cubies have served their purpose here:
      delete[] cubies;      
// Prepare the pieces
      piecesLeft=numPieces=num;
      pieces=new MyPiece[numPieces];
      for (int i=0;i<numPieces;i++) {
         pieces[i].Init(goalRange,thePieces[i]);
      }
   }
   ~MyGoal() {
      if (pieces) delete[] pieces;
      if (goalMap) delete goalMap;
   }
   bool Solve(Piece theGoal) {   
      if (goalMap==0) // in case of overflow
         return false;   
      qsort(pieces,numPieces,sizeof(MyPiece),CompPiece);
      if (RecursiveSolve()) {
         CopyTheSolution(theGoal);            
         return true;
      } else return false;      
   }
   bool RecursiveSolve() {   
      piecesLeft-;   
                                 
      int target=goalMap->Corner();
      if (target<0) return false;
      MyPiece* P=pieces;
      for (int i=0;i<numPieces;i++,P++) {   // for all pieces   
         if (!P->Available()) continue;   // skip if used already
                               
         do {                        // for all versions
            while (P->GetSpot(target) >= 0)
            {                        // for all LDF corners
               if (P->Fits()) {            
                  P->Place();   
                  if (piecesLeft) {         // recurse until no pieces left
                     if (RecursiveSolve()) return true;
                  } else return true;
                  P->Unplace();                        
               }      
            }
         } while (P->NextRotation());
      }
      piecesLeft++;               
      return false;
   }   
   void CopyTheSolution(Piece theGoal) {
// The solution exists in the translations of the current rotated versions
// of the pieces.  These must be remapped back to the cubies list of theGoal.
// I fill the 3D indexed goal map array to temporarily hold the piece ids,
// and then scan through the cubies list to load these values by indexing
// through the goal map array.
      MyPiece* P=pieces;      
      for (int i=0;i<numPieces;i++,P++) { P->Copy();}
      Cubie* C=theGoal.theCubies;
      for (int i=0;i<theGoal.numCubies;i++,C++) {
         Spot goalSpot=Spot(*C);
         Spot mySpot=goalSpot-goalOffset;
         C->value=goalMap->state[mySpot.Offset(goalRange)];
      }
   }
};

BlockBuster
Boolean   BlockBuster(long numPieces, Piece thePieces[], Piece theGoal) {
      MyGoal* goal=new MyGoal(numPieces,thePieces,theGoal);
      bool rc=goal->Solve(theGoal);
      delete goal;
      return rc;
/*
// The following equivalent style of expression crashes on the second
// call to BlockBuster(...) (CodeWarrior Pro-3, with all updates),
// when it runs the destructor of goal before running Solve().
// However, it works fine for the first call (!?).
      MyGoal goal=MyGoal(numPieces,thePieces,theGoal);
      return goal.Solve(theGoal);
*/
}
 
AAPL
$116.57
Apple Inc.
+0.26
MSFT
$48.70
Microsoft Corpora
+0.00
GOOG
$538.89
Google Inc.
+4.06

MacTech Search:
Community Search:

Software Updates via MacUpdate

Cyberduck 4.6 - FTP and SFTP browser. (F...
Cyberduck is a robust FTP/FTP-TLS/SFTP browser for the Mac whose lack of visual clutter and cleverly intuitive features make it easy to use. Support for external editors and system technologies such... Read more
Maya 2015 - Professional 3D modeling and...
Maya is an award-winning software and powerful, integrated 3D modeling, animation, visual effects, and rendering solution. Because Maya is based on an open architecture, all your work can be scripted... Read more
Evernote 6.0.1 - Create searchable notes...
Evernote allows you to easily capture information in any environment using whatever device or platform you find most convenient, and makes this information accessible and searchable at anytime, from... Read more
calibre 2.11 - Complete e-library manage...
Calibre is a complete e-book library manager. Organize your collection, convert your books to multiple formats, and sync with all of your devices. Let Calibre be your multi-tasking digital... Read more
Herald 5.0.1 - Notification plugin for M...
Note: Versions 2.1.3 (for OS X 10.7), 3.0.6 (for OS X 10.8), and 4.0.8 (for OS X 10.9) are no longer supported by the developer. Herald is a notification plugin for Mail.app, Apple's Mac OS X email... Read more
Firetask 3.7 - Innovative task managemen...
Firetask uniquely combines the advantages of classical priority-and-due-date-based task management with GTD. Stay focused and on top of your commitments - Firetask's "Today" view shows all relevant... Read more
TechTool Pro 7.0.6 - Hard drive and syst...
TechTool Pro is now 7, and this is the most advanced version of the acclaimed Macintosh troubleshooting utility created in its 20-year history. Micromat has redeveloped TechTool Pro 7 to be fully 64... Read more
PhotoDesk 3.0.1 - Instagram client for p...
PhotoDesk lets you view, like, comment, and download Instagram pictures/videos! (NO Uploads! / Image Posting! Instagram forbids that! AND you *need* an *existing* Instagram account). But you can do... Read more
SuperDuper! 2.7.3 - Advanced disk clonin...
SuperDuper! is an advanced, yet easy to use disk copying program. It can, of course, make a straight copy, or "clone" -- useful when you want to move all your data from one machine to another, or do... Read more
MacJournal 6.1.5 - Create, maintain, and...
MacJournal is the world's most popular journaling software for the Mac. MacJournal 6 adds a calendar mode that show entries from any journal, geolocation, word count, and progress tracking, as well... Read more

Latest Forum Discussions

See All

Quest for Revenge (Games)
Quest for Revenge 1.0.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0.0 (iTunes) Description: The great Kingdom of the west has fallen. The gods ignore the prayers of the desperate. A dark warlord has extinguished... | Read more »
Threadz is a New Writing Adventure for Y...
Threadz is a New Writing Adventure for You and Your Friends Posted by Jessica Fisher on November 21st, 2014 [ permalink ] In the tradition of round-robin storytelling, | Read more »
SteelSeries Stratus XL Hardware Review
Made by: SteelSeries Price: $59.99 Hardware/iOS Integration Rating: 4 out of 5 stars Usability Rating: 4.5 out of 5 stars Reuse Value Rating: 4.25 out of 5 stars Build Quality Rating: 4.5 out of 5 stars Overall Rating: 4.31 out of 5 stars | Read more »
ACDSee (Photography)
ACDSee 1.0.0 Device: iOS iPhone Category: Photography Price: $1.99, Version: 1.0.0 (iTunes) Description: Capture, perfect, and share your photos with ACDSee. The ACDSee iPhone app combines an innovative camera, a powerful photo... | Read more »
ProTube for YouTube (Entertainment)
ProTube for YouTube 2.0.2 Device: iOS Universal Category: Entertainment Price: $1.99, Version: 2.0.2 (iTunes) Description: ProTube is the ultimate, fully featured YouTube app. With it's highly polished design, ProTube offers ad-free... | Read more »
Weather or Not - Reports and Forecasts...
Weather or Not - Reports and Forecasts for your Calendar 1.0.0 Device: iOS iPhone Category: Weather Price: $2.99, Version: 1.0.0 (iTunes) Description: Weather or Not is a beautiful and intuitive way to check the weather and... | Read more »
Sago Mini Road Trip (Education)
Sago Mini Road Trip 1.0 Device: iOS Universal Category: Education Price: $2.99, Version: 1.0 (iTunes) Description: Go for a fun-filled drive with Jinja the cat. Pick a destination, select a vehicle and hit the road. What will Jinja... | Read more »
New Tower Defense Game, Kingdom Rush: Or...
New Tower Defense Game, Kingdom Rush: Origins, is Available Today Posted by Jessica Fisher on November 20th, 2014 [ permalink ] iPad Only App - Designed for the iPad | Read more »
Sunburn! (Games)
Sunburn! 1.0.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0.0 (iTunes) Description: Your ship is gone. Your crew is scattered. One option remains. Gather your crew... and jump into the sun. Reunite your... | Read more »
Tapventures Review
Tapventures Review By Jennifer Allen on November 20th, 2014 Our Rating: :: ODDLY COMPELLINGUniversal App - Designed for iPhone and iPad Tapventures is an increasingly hands-off one-tap RPG, but expect it to hook you despite your... | Read more »

Price Scanner via MacPrices.net

64GB iPod touch on sale for $249, save $50
Best Buy has the 64GB iPod touch on sale for $249 on their online store for a limited time. Their price is $50 off MSRP. Choose free shipping or free local store pickup (if available). Sale price for... Read more
15″ 2.2GHz Retina MacBook Pro on sale for $17...
 B&H Photo has the 2014 15″ 2.2GHz Retina MacBook Pro on sale for $1799.99 for a limited time. Shipping is free, and B&H charges NY sales tax only. B&H will also include free copies of... Read more
New Logitech AnyAngle Case/Stand Brings Flexi...
Logitec has announced the newest addition to its suite of tablet products — the Logitech AnyAngle. A protective case with an any-angle stand for iPad Air 2 and all iPad mini models, AnyAngle is the... Read more
2013 15-inch 2.0GHz Retina MacBook Pro availa...
B&H Photo has leftover previous-generation 15″ 2.0GHz Retina MacBook Pros available for $1499 including free shipping plus NY sales tax only. Their price is $500 off original MSRP. B&H will... Read more
16GB Retina iPad mini on sale today for $199,...
 Staples has 2nd generation 16GB Retina iPad minis on sale for $199 on their online store for a limited time. Their price is $100 off MSRP. Choose free shipping or free local store pickup (if... Read more
Developers Start Designing Apps for Apple Wat...
Apple has announced the availability of WatchKit, software that gives developers a set of tools to easily create experiences designed specifically for Apple Watch. Apple’s developer community can now... Read more
C Spire Launches iPad Air 2 and iPad Mini 3 o...
C Spire has announced that iPad Air 2 with Wi-Fi + Cellular and iPad mini 3 with Wi-Fi + Cellular are now available on its 4G LTE network. C Spire offers both new iPads with a range of data plans... Read more
Are You On Your Last PC? – The ‘Book Mystique
Will your current PC be your last? Quite possibly so if you define “personal computer” as a traditional desktop or laptop form factor machine according to some commentators. So then, the upshot that... Read more
Save up to $180 on MacBook Airs with Apple re...
The Apple Store has Apple Certified Refurbished 2014 MacBook Airs available for up to $180 off the cost of new models. An Apple one-year warranty is included with each MacBook, and shipping is free.... Read more
16GB iPad mini available for $219, save $30
Walmart has 16GB iPad minis (1st generation) available for $219 on their online store. Their price is $30 off MSRP. Choose free shipping or free store pickup (if available). Price for online orders... Read more

Jobs Board

*Apple* Solutions Consultant (ASC)- Retail S...
**Job Summary** The ASC is an Apple employee who serves as an Apple brand ambassador and influencer in a Reseller's store. The ASC's role is to grow Apple Read more
Project Manager, *Apple* Financial Services...
**Job Summary** Apple Financial Services (AFS) offers consumers, businesses and educational institutions ways to finance Apple purchases. We work with national and Read more
*Apple* Store Leader Program - College Gradu...
Job Description: Job Summary As an Apple Store Leader Program agent, you can continue your education as you major in the art of leadership at the Apple Store. You'll Read more
*Apple* Retail - Multiple Positions (US) - A...
Sales Specialist - Retail Customer Service and Sales Transform Apple Store visitors into loyal Apple customers. When customers enter the store, you're also the Read more
Senior Event Manager, *Apple* Retail Market...
…This senior level position is responsible for leading and imagining the Apple Retail Team's global event strategy. Delivering an overarching brand story; in-store, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.