Manoeuvre the grid!

Briefing

You are a bot, in a 2D grid that extends infinitely in all four directions, north, south, east and west. When given a number, you must move the bot so that you get to the target number.

Here's how the grid works:

You can move in 4 directions: north, south, east or west. Once you move off a cell, you are not allowed to go back to that cell again (so effectively, it's been wiped off the map).

There is a "counter", which goes 1234567890 (so it goes from 1 to 2... all the way to 9, then to 0, then back to 1 again), which changes every time you move.

You also have a "value", which starts off at 0.

Once you move in any direction, a mathematical operation occurs, depending on what direction you move:

• North: Your value is increased by counter (value += counter).
• East: Your value is decremented by counter (value -= counter).
• South: Your value is multiplied by counter (value *= counter).
• West: Your value is divided by counter (value /= counter).
• Division is integer division, so 5/2 -> 2.
• You are not allowed to divide by 0.

Example:

If the bot moves north 3 times:

• The first "north" move increments the counter to 1, and adds that to the value (which is now 1).
• The second "north" move increments the counter to 2, and adds that to the value (which is now 3).
• The third "north" move increments the counter to 3, and adds that to the value (which is now 6).

The final value is 6.

Move north, then south again:

• The first "north" move increments the counter to 1, and adds that to the value (which is now 1).
• The second "south" move errors, because the cell the bot is trying to move on is removed (from the first move).

There is no final value, because the bot errored.

Challenge

Your challenge is to write a program when, given a number, produce the suitable directions for the bot to go in so that the final value of the bot is equal to that number.

So if the number is 6, a valid solution to that would be:

nnn


(The bot moves north 3 times in a row).

49445094, 71259604, 78284689, 163586986, 171769219, 211267178, 222235492, 249062828, 252588742, 263068669, 265657839, 328787447, 344081398, 363100288, 363644732, 372642304, 374776630, 377945535, 407245889, 467229432, 480714605, 491955034, 522126455, 532351066, 542740616, 560336635, 563636122, 606291383, 621761054, 648274119, 738259135, 738287367, 748624287, 753996071, 788868538, 801184363, 807723631, 824127368, 824182796, 833123975, 849666906, 854952292, 879834610, 890418072, 917604533, 932425141, 956158605, 957816726, 981534928, 987717553


(These are 50 random numbers from 1 to 1 billion.)

Your score is the total amount of moves made for all 50 numbers - the fewer moves, the better. In case of a tie, the person who submitted their code earlier wins.

Specs

• You are guaranteed receive a positive integer for input.
• Your value variable must not go above 2^31-1 or below -2^31 at any point for your generated paths.
• Your final program must fit in an answer (so, < 30,000 bytes).
• You may only hard-code 10 numbers.
• Your program must run within 5 minutes on a reasonable laptop for any test case.
• The results MUST be the same every time the program is run for each number.
• Shouldn't this be code-challenge instead of atomic-code-golf? You're not scoring by the size of the submitted program, not even as a tie-breaker, you're scoring by size of the output, which makes this an algorithmic challenge and not a code golf problem. Commented Nov 14, 2016 at 7:50
• @marinus Fixed. I thought atomic code golf was for the program - I must have gotten confused. Commented Nov 14, 2016 at 7:57
• Is there a proof this is possible? Commented Nov 14, 2016 at 9:18
• 1. I think you should add a rule that submissions have to be runnable on a computer with specs X and a time frame Y. One of the current answers claims a perfect score, but I doubt it could actually calculate it. 2. You are not allowed to calculate values above [...] That refers to the variable value, yes? At least to me, it sounds like a restriction imposed on the implementation, not the actual algorithm. Commented Nov 14, 2016 at 21:07
• @Dennis Do you think 10 minutes is enough for all 50 test cases? Commented Nov 15, 2016 at 7:15

C++: score = 453,324,048

OK, needed some time to rework this, but here's the way I solved it.

After studying the solution space, I decided that my strategy would be:

1. Uses the south steps to get as close to the target number
1. if the target is positive, follow this path: nnnesssssessssssss
2. if the target is negative, follow this path: esssssssseessssss c. if the target is between 0 and 20, solve it "the old fashion way" (trail and error over every possible path til we reach it).
3. Once we have our "best place" (get as close to the target, without going "over"), we may be able to get closer by multiplying by 2 or 3; so take between 0 to 9 steps east, and then one step south. keep the path that gets us the closest to the target.
4. "Run" north, or east til we are within 45 points of the target (every 10 steps north, add 45 points to the score, like wise, every 10 steps east, reduces the score by 45).
2. Take a few more steps in the same direction, til we are within 10 points of the target
3. Do "the old fashion way" from this point, it shouldn't be that hard now.

Here's my result: total score is 453324048

And the paths:

  0) to reach   49445094, it takes   1311037 steps, by doing: nnnesssssesssssseeeeese(n *     1311010)enen
1) to reach   71259604, it takes   1320313 steps, by doing: nnnesssssesssssseeeeeese(n *     1320280)nnnnnneee
2) to reach   78284689, it takes   1956998 steps, by doing: nnnesssssesssssseeeeeees(e *     1956970)eeee
3) to reach  163586986, it takes   2483885 steps, by doing: nnnesssssessssssse(n *     2483860)nnnnnnn
4) to reach  171769219, it takes   4302163 steps, by doing: nnnesssssessssssse(n *     4302130)nnnnnnnnnnennnn
5) to reach  211267178, it takes  13079485 steps, by doing: nnnesssssessssssse(n *    13079460)nnnnnen
6) to reach  222235492, it takes  15516886 steps, by doing: nnnesssssessssssse(n *    15516860)nnnnnnnn
7) to reach  249062828, it takes  12390325 steps, by doing: nnnesssssessssssseeees(e *    12390290)eeeeenenneene
8) to reach  252588742, it takes  11606785 steps, by doing: nnnesssssessssssseeees(e *    11606760)een
9) to reach  263068669, it takes   9277915 steps, by doing: nnnesssssessssssseeees(e *     9277880)eeeeenennneee
10) to reach  265657839, it takes   8702543 steps, by doing: nnnesssssessssssseeees(e *     8702510)eeeeenennee
11) to reach  328787447, it takes   5326312 steps, by doing: nnnesssssessssssseeeese(n *     5326280)nnnnennnn
12) to reach  344081398, it takes   8724966 steps, by doing: nnnesssssessssssseeeese(n *     8724940)enn
13) to reach  363100288, it takes  12951386 steps, by doing: nnnesssssessssssseeeese(n *    12951360)enn
14) to reach  363644732, it takes  13072373 steps, by doing: nnnesssssessssssseeeese(n *    13072340)nnnnnnnnen
15) to reach  372642304, it takes  15071833 steps, by doing: nnnesssssessssssseeeese(n *    15071800)nnnnnnnenn
16) to reach  374776630, it takes  15546133 steps, by doing: nnnesssssessssssseeeese(n *    15546100)nnnnnenene
17) to reach  377945535, it takes  16250331 steps, by doing: nnnesssssessssssseeeese(n *    16250300)nnnnennn
18) to reach  407245889, it takes  11107325 steps, by doing: nnnesssssessssssseeeees(e *    11107300)ne
19) to reach  467229432, it takes   2222403 steps, by doing: nnnesssssessssssseeeeese(n *     2222370)nnnnnnnee
20) to reach  480714605, it takes   5219109 steps, by doing: nnnesssssessssssseeeeese(n *     5219080)neenn
21) to reach  491955034, it takes   7716983 steps, by doing: nnnesssssessssssseeeeese(n *     7716950)nnnnennnn
22) to reach  522126455, it takes  14421745 steps, by doing: nnnesssssessssssseeeeese(n *    14421710)nnnnnneneee
23) to reach  532351066, it takes  16693875 steps, by doing: nnnesssssessssssseeeeese(n *    16693850)n
24) to reach  542740616, it takes  14866179 steps, by doing: nnnesssssessssssseeeeees(e *    14866150)eeeen
25) to reach  560336635, it takes  10955953 steps, by doing: nnnesssssessssssseeeeees(e *    10955920)eeeeennen
26) to reach  563636122, it takes  10222731 steps, by doing: nnnesssssessssssseeeeees(e *    10222700)eeeeene
27) to reach  606291383, it takes    743785 steps, by doing: nnnesssssessssssseeeeees(e *      743760)e
28) to reach  621761054, it takes   2693968 steps, by doing: nnnesssssessssssseeeeeese(n *     2693940)nnn
29) to reach  648274119, it takes   8585761 steps, by doing: nnnesssssessssssseeeeeese(n *     8585730)nnnnnn
30) to reach  738259135, it takes   5286413 steps, by doing: nnnesssssessssssseeeeeees(e *     5286380)eeneneee
31) to reach  738287367, it takes   5280141 steps, by doing: nnnesssssessssssseeeeeees(e *     5280110)nneenn
32) to reach  748624287, it takes   2983042 steps, by doing: nnnesssssessssssseeeeeees(e *     2983010)eeeenee
33) to reach  753996071, it takes   1789313 steps, by doing: nnnesssssessssssseeeeeees(e *     1789280)eeeennee
34) to reach  788868538, it takes   5960183 steps, by doing: nnnesssssessssssseeeeeeese(n *     5960150)nnenene
35) to reach  801184363, it takes   8697033 steps, by doing: nnnesssssessssssseeeeeeese(n *     8697000)nnenene
36) to reach  807723631, it takes  10150197 steps, by doing: nnnesssssessssssseeeeeeese(n *    10150170)n
37) to reach  824127368, it takes  13795475 steps, by doing: nnnesssssessssssseeeeeeese(n *    13795440)nnnnnnnne
38) to reach  824182796, it takes  13807795 steps, by doing: nnnesssssessssssseeeeeeese(n *    13807760)nnnnnenee
39) to reach  833123975, it takes  15794722 steps, by doing: nnnesssssessssssseeeeeeese(n *    15794690)nennnn
40) to reach  849666906, it takes  14397917 steps, by doing: nnnesssssessssssseeeeeeees(e *    14397880)eeeeeeeenee
41) to reach  854952292, it takes  13223389 steps, by doing: nnnesssssessssssseeeeeeees(e *    13223350)eeeeeeeeneeen
42) to reach  879834610, it takes   7693981 steps, by doing: nnnesssssessssssseeeeeeees(e *     7693950)eeenn
43) to reach  890418072, it takes   5342102 steps, by doing: nnnesssssessssssseeeeeeees(e *     5342070)eeennn
44) to reach  917604533, it takes    699395 steps, by doing: nnnesssssessssssseeeeeeeese(n *      699360)nnnneene
45) to reach  932425141, it takes   3992863 steps, by doing: nnnesssssessssssseeeeeeeese(n *     3992830)nennnn
46) to reach  956158605, it takes   9266963 steps, by doing: nnnesssssessssssseeeeeeeese(n *     9266930)nnnnen
47) to reach  957816726, it takes   9635434 steps, by doing: nnnesssssessssssseeeeeeeese(n *     9635400)nnnennn
48) to reach  981534928, it takes  14906145 steps, by doing: nnnesssssessssssseeeeeeeese(n *    14906110)nnnnnnnn
49) to reach  987717553, it takes  16280059 steps, by doing: nnnesssssessssssseeeeeeeese(n *    16280030)nn


I'm sure there's a way to improve on this by doing some cleaver "south/west" moves (dividing by 4 and multiplying by 5; for example); but coding it, and making sure you don't over lap or get trapped, is tricky.

Another solution path, may be to try to go back down from the target, to a "reasonable" number, and then, just find a path to that smaller number; but you'll have to "aim" it right, so that the step number will match. tricky, but might be the best way to solve this.

Anyway, here is my code code:

#include <stdio.h>
#include <vector>
#include <queue>;

using namespace std;

long long upperLimit;
long long lowerLimit;
bool bDebugInfo = false;
//bool bDebugInfo = true;

//  a point struct (x and y)
struct point
{
int x;
int y;

point():x(0),y(0)
{
}

bool operator ==(const point& other)
{
return (x==other.x) && (y==other.y);
}

void ApplyDirection(char direction)
{
switch (direction)
{
case 'n':
y++;
break;
case 'w':
x--;
break;
case 'e':
x++;
break;
case 's':
y--;
break;
}
}
};

// each state is of this formate
struct botState
{
int nStep;
long long number;
vector<char> path;

botState()
:nStep(0),
number(0)
{
}

botState* clone()
{
botState* tmp = new botState();
tmp->nStep = nStep;
tmp->number = number;
tmp->path = path;
return tmp;
}

void clone(botState* other)
{
nStep = other->nStep;
number = other->number;
path = other->path;
}

};

bool changeNumberWithDirection(long long &number, char direction, int step)
{
switch (direction)
{
case 'n':
number += (step%10);
break;
case 'w':
if (step%10)
number /= (step%10);
else
return false;
break;
case 'e':
number -= (step%10);
break;
case 's':
number *= (step%10);
break;

default:
return false;
}

return true;
}

bool tryToAddStep(queue<botState*>& queueOfStates, const botState* pState, char direction, char cStarDirection)
{
botState* pTmpState;
long long newNumber;
int newStep = pState->nStep+1;

newNumber = pState->number;
if (!changeNumberWithDirection(newNumber, direction, newStep))
return false;

if (newNumber > upperLimit)
return false;

if (newNumber < lowerLimit)
return false;

if ((newNumber == 0) && (newStep%10 == 0))
return false;                // no need to return back to 0 after 10 or more steps, we already have better ways to do this.

// build the x,y points of the path up to this point
point tmpPoint;
vector<point> pointsInPath;
pointsInPath.push_back(tmpPoint);

for (int i=0; i<pState->path.size(); i++)
{
if (pState->path.at(i) == '*')
{
for (int j=0; j<100; j++)
{
tmpPoint.ApplyDirection(cStarDirection);
pointsInPath.push_back(tmpPoint);
}
}
else
{
tmpPoint.ApplyDirection(pState->path.at(i));
pointsInPath.push_back(tmpPoint);
}
}

tmpPoint.ApplyDirection(direction);

// check for over lap
for (int i=0; i<pointsInPath.size(); i++)
{
if (tmpPoint == (pointsInPath.at(i)))
return false;
}

pTmpState = new botState();
pTmpState->nStep = newStep;
pTmpState->number= newNumber;
pTmpState->path  = pState->path;

pTmpState->path.push_back(direction);

queueOfStates.push(pTmpState);

return true;
}

bool isBetterNum(long long newNum, long long oldBest, long long target)
{
long long newDiff = (newNum  > target) ? newNum  - target : target - newNum ;
long long oldDiff = (oldBest > target) ? oldBest - target : target - oldBest;

return (newDiff < oldDiff);
}

bool tryToJumpDown(long long num, botState* pState, int& nTimes)
{
// if where the bot is, we have a clear path to go as far east as we could ever want, we can just do as many sets of eeeeeeeeee (e*10) as needed, til we are close enough to the target
point tmpPoint;
vector<point> pointsInPath;
pointsInPath.push_back(tmpPoint);

for (int i=0; i<pState->nStep; i++)
{
tmpPoint.ApplyDirection(pState->path.at(i));
pointsInPath.push_back(tmpPoint);
}

for (int i=0; i<pointsInPath.size(); i++)
{
if ((pointsInPath.at(i).x > tmpPoint.x) && (pointsInPath.at(i).y == tmpPoint.y))
return false;  // we have a point blocking our path up!
}

long long tmpTimes = (pState->number - num)/45;
if ((tmpTimes>1) && (tmpTimes<upperLimit))
{
tmpTimes--;
tmpTimes*=10;
nTimes = (int)tmpTimes;
pState->nStep+=nTimes;
pState->number-=(tmpTimes/10)*45;
pState->path.push_back('*');
return true;
}

return false;
}

bool tryToJumpUp(long long num, botState* pState, int& nTimes)
{
// if where the bot is, we have a clear path to go as far north as we could ever want, we can just do as many sets of nnnnnnnnnn (n*10) as needed, til we are close enough to the target
point tmpPoint;
vector<point> pointsInPath;
pointsInPath.push_back(tmpPoint);

for (int i=0; i<pState->nStep; i++)
{
tmpPoint.ApplyDirection(pState->path.at(i));
pointsInPath.push_back(tmpPoint);
}

for (int i=0; i<pointsInPath.size(); i++)
{
if ((pointsInPath.at(i).x == tmpPoint.x) && (pointsInPath.at(i).y > tmpPoint.y))
return false;  // we have a point blocking our path up!
}

long long tmpTimes = (num - pState->number)/45;
if ((tmpTimes>1) && (tmpTimes<upperLimit))
{
tmpTimes--;
tmpTimes*=10;
nTimes = (int)tmpTimes;
pState->nStep+=nTimes;
pState->number+=(tmpTimes/10)*45;
pState->path.push_back('*');
return true;
}

return false;
}

typedef char* PChar;

bool buildPath(long long num, PChar& str, int& nLen, int& nScore, botState* startState, int nTimes)
{
long long nBest = 0;
int nMaxSteps = 0;
long long nMax = 0;
long long nMin = 0;
int nCleanUpOnStep= 12;
long long nFromLastCleanUp = 0;
bool bInCleanUp = false;
char cDirection = ' ';

if (nTimes>0)
cDirection = 'n';
else if (nTimes<0)
{
cDirection = 'e';
nTimes*=-1;
}

if (startState->nStep >= nCleanUpOnStep)
nCleanUpOnStep= startState->nStep+10;

str  = NULL;
nLen = 0;
botState* bestState = new botState();
bestState->clone(startState);
queue<botState*> queueOfStates;
queueOfStates.push(bestState);  // put the starting state into the queue

while (!queueOfStates.empty())       // while we still have states in the queue, process them
{
botState* pState = queueOfStates.front();
queueOfStates.pop();             // take a state out of the queue

if (!str)                        // no solution yet
{
if (pState->number == num)   // check if this is a solution
{
// we solved it!
int nOffset=0;
nLen = pState->nStep - nTimes + 17;
str = new char[nLen+1];
if (bDebugInfo)
printf("solved!\n");
nScore = pState->nStep;
for (int i=0; i<pState->path.size(); i++)
{
if (pState->path.at(i)=='*')
{
sprintf(str+i, "(%c * %11d)", cDirection, nTimes);
if (bDebugInfo)
printf("(%c * %11d)", cDirection, nTimes);
nOffset=16;
}
else
{
str[i+nOffset] = pState->path.at(i);
if (bDebugInfo)
printf("%c", str[i+nOffset]);// print solution while making the string
}
}
if (bDebugInfo)
printf("\n");
str[nLen]='\0';
}
else
{                            // no solution yet, we need to go deeper
if (pState->number < nMin)
nMin = pState->number;

if (pState->number > nMax)
nMax = pState->number;

if ((!bInCleanUp) && (queueOfStates.size()>1000000))
{
nCleanUpOnStep=nMaxSteps+10;
bInCleanUp = true;
}
if (pState->nStep > nMaxSteps)
{                        // a little tracing, so we can see progress
nMaxSteps = pState->nStep;
//                    printf("current states have %d steps, reached a max of %lld, and a min of %lld\n", nMaxSteps, nMax, nMin);
if (nMaxSteps >= nCleanUpOnStep)
{
nCleanUpOnStep+=10;
bInCleanUp = true;
}
}

if (isBetterNum(pState->number, nBest, num))
{                        // a little tracing, so we can see progress
nBest = pState->number;
if (bDebugInfo)
printf("Got closer to the target, %lld, with %d steps (target is %lld, diff is %lld)\n", nBest, pState->nStep, num, num-nBest);
if (bestState != pState)
delete bestState;
bestState = pState;
}

if (!bInCleanUp)
{

if (!nTimes)  // once we did the "long walk in one direction" don't do the west or south moves any more
{
}
}
}
}
if (pState!=bestState)
delete pState;                  // this is not java, we need to keep the memory clear.

if ((bInCleanUp) && (queueOfStates.empty()))
{
queueOfStates.push(bestState);  // put the starting state into the queue
bInCleanUp = false;
long long diff = nFromLastCleanUp-bestState->number;
if (!nTimes)
{
if ((diff>0) && (diff<100))
if (tryToJumpDown(num, bestState, nTimes))
cDirection = 'e';
if ((diff<0) && (diff>-100))
if (tryToJumpUp(num, bestState, nTimes))
cDirection = 'n';

if (nTimes)
nCleanUpOnStep = bestState->nStep;
}
nFromLastCleanUp = bestState->number;
}
}

delete bestState;                  // this is not java, we need to keep the memory clear.
return str!=NULL;
}

char* positiveSpine = "nnnesssssessssssss";
char* negativeSpine = "esssssssseessssss";

bool canReachNumber(long long num, PChar& str, int& nLen, int& nScore)
{
int nTimes = 0;
botState tmpState;
if ((num>=0) && (num<=20))
return buildPath(num, str, nLen, nScore, &tmpState, nTimes);

botState bestState;
bestState.clone(&tmpState);

char* spine = NULL;
if (num>0)
{
spine = positiveSpine;
}
else
{
spine = negativeSpine;
}

for (int i=0; spine[i]; i++)
{
tmpState.nStep++;
tmpState.path.push_back(spine[i]);
if (!changeNumberWithDirection(tmpState.number, spine[i], tmpState.nStep))
return false;

if ((num>0) && (tmpState.number<num))
{
bestState.clone(&tmpState);
}
else if ((num<0) && (tmpState.number>num))
{
bestState.clone(&tmpState);
}
}

if (bestState.number == num)
return buildPath(num, str, nLen, nScore, &bestState, nTimes);

botState tryPath;
tmpState.clone(&bestState);
for (int i=0; i<9; i++)
{
tryPath.clone(&tmpState);
bool pathOK = true;
for (int j=0; j<i; j++)
{
tryPath.nStep++;
tryPath.path.push_back('e');
if (!changeNumberWithDirection(tryPath.number, 'e', tryPath.nStep))
{
pathOK = false;
break;
}
}
tryPath.nStep++;
tryPath.path.push_back('s');
if (!changeNumberWithDirection(tryPath.number, 's', tryPath.nStep))
{
pathOK = false;
break;
}

if ((pathOK) && (isBetterNum(tryPath.number, bestState.number, num)))
{
bestState.clone(&tryPath);
}
}

// in case we'll need to add, but last step was south, move one to the east.
if ((bestState.path.at(bestState.path.size()-1) == 's') && (bestState.number<num))
{
bestState.nStep++;
bestState.path.push_back('e');
if (!changeNumberWithDirection(bestState.number, 'e', bestState.nStep))
return false;
}

if (bestState.number<num)
{
long long diff = num - bestState.number;
diff/=45;
nTimes = (int)diff*10;
bestState.nStep += nTimes;
bestState.path.push_back('*');
bestState.number += 45*diff;
while (num - bestState.number > 10)
{
bestState.nStep++;
bestState.path.push_back('n');
if (!changeNumberWithDirection(bestState.number, 'n', bestState.nStep))
return false;
}
return buildPath(num, str, nLen, nScore, &bestState, nTimes);
}
else
{
long long diff = bestState.number - num;
diff/=45;
nTimes = (int)diff*10;
bestState.nStep += nTimes;
bestState.path.push_back('*');
bestState.number -= 45*diff;
while (bestState.number - num > 10)
{
bestState.nStep++;
bestState.path.push_back('e');
if (!changeNumberWithDirection(bestState.number, 'e', bestState.nStep))
return false;
}
return buildPath(num, str, nLen, nScore, &bestState, -nTimes);
}

return false;
}
long long aVals[] = {49445094, 71259604, 78284689, 163586986, 171769219, 211267178, 222235492, 249062828, 252588742, 263068669, 265657839, 328787447, 344081398, 363100288, 363644732, 372642304, 374776630, 377945535, 407245889, 467229432, 480714605, 491955034, 522126455, 532351066, 542740616, 560336635, 563636122, 606291383, 621761054, 648274119, 738259135, 738287367, 748624287, 753996071, 788868538, 801184363, 807723631, 824127368, 824182796, 833123975, 849666906, 854952292, 879834610, 890418072, 917604533, 932425141, 956158605, 957816726, 981534928, 987717553};

void main(void)
{
upperLimit =     2147483647;       //  2^31 - 1
lowerLimit =-1;       // -2^31
lowerLimit *=2147483648;       // -2^31
long long num=0;
char* str=NULL;
int nLen = 0;
int nItems = sizeof(aVals)/sizeof(aVals[0]);
int nScore = 0;
long long nTotalScore = 0;
//  nItems=1;

for(int i=0; i<nItems; i++)
{
if (canReachNumber(aVals[i], str, nLen, nScore))  //try to reach it
{
printf("%3d) to reach %10lld, it takes %9d steps, by doing: %s\n", i, aVals[i], nScore, str);

nTotalScore+=nScore;
delete str;
}
else
{
if (aVals[i]>0)
printf("Failed to reach %lld, use nenenenenenen..... ('n', followed by %lld pairs of 'en')\n", aVals[i], aVals[i]-1);
else
printf("Failed to reach %lld, use enenenenenene..... ('e', followed by %lld pairs of 'ne')\n", aVals[i], aVals[i]-1);
nTotalScore+=2*aVals[i]-1;
}
}

printf("done, total score is %lld\n", nTotalScore);
return;
}

• In esssssssseessssss are you sure variable not overflow? If v=1 t=1 that string mean (1*2*3*4*5*6*7-8)*1*2*3*4*5 etc or something as that
– user58988
Commented Nov 27, 2016 at 21:59
• @RosLuP that's not -8. it is more like this : ((-1)*(2*3*4*5*6*7*8*9)-0-1)*2*3*4*5*6*7 which is -1828920240 which is a about -2^30.7683 so it doesn't pass -2^31 Commented Nov 28, 2016 at 11:57

Python, Score = 56068747912

def move(n):
print("n" + "en" * (n - 1))


Just prints nenenenenenenen... for every number.

• Off by 1, isn't it? nen is 2 Commented Nov 14, 2016 at 11:21
• @edc65 Fixed it. Commented Nov 14, 2016 at 20:31
• did you get that score by actually running this code, or is this your "guess", if everything works right? Commented Nov 17, 2016 at 12:25
• @EyalLev The latter. It should work as expected, anyway - every "en" after the initial "n" should increment the value by 1 (since the value goes "-2+3-4+5...-0+1-2+3" after the initial "+1"). Commented Nov 17, 2016 at 20:39
• the problem is, that the requirement is that it takes 10 min'. not sure if your "let's print them all" way will meet that constraint. Commented Nov 20, 2016 at 9:03

Rust, score = 1758 (optimal among paths with no west)

Runs in about 7 seconds total for 50 numbers, using a bidirectional search.

use std::collections::HashSet;
use std::io::{self, prelude::*};

#[derive(Debug, Eq, Clone, Copy, Hash, Ord, PartialEq, PartialOrd)]
enum Dir {
N,
E,
S,
}
use Dir::{E, N, S};

fn dir_char(dir: Dir) -> char {
match dir {
N => 'n',
E => 'e',
S => 's',
}
}

#[derive(Debug, Eq, Clone, Hash, Ord, PartialEq, PartialOrd)]
struct State {
counter: i32,
value: i32,
next: Dir,
}

fn step(s: &State) -> impl Iterator<Item = State> {
let (values, nexts): (_, &[Dir]) = match s.next {
E => (s.value.checked_sub(s.counter), &[N, E, S]),
S => (
if s.counter != 0 {
s.value.checked_mul(s.counter)
} else {
None
},
&[E, S],
),
};
let counter = (s.counter + 1) % 10;
values.into_iter().flat_map(move |value| {
nexts.iter().map(move |&next| State {
counter,
value,
next,
})
})
}

fn unstep(s: &State) -> impl Iterator<Item = State> {
let counter = (s.counter + 9) % 10;
(match s.next {
N | E => s.value.checked_sub(counter).map(|value| State {
counter,
value,
next: N,
}),
_ => None,
}).into_iter()
counter,
value,
next: E,
}))
.chain(match s.next {
E | S if counter != 0 && s.value % counter == 0 => {
s.value.checked_div(counter).map(|value| State {
counter,
value,
next: S,
})
}
_ => None,
})
}

fn search(value: i32) -> String {
let mut lefts: Vec<HashSet<State>> = Vec::new();
let mut left = [N, E, S]
.iter()
.map(|&next| State {
counter: 1,
value: 0,
next,
})
.collect::<HashSet<_>>();
let mut rights: Vec<HashSet<State>> = Vec::new();
let mut right = (0..10)
.map(|counter| State {
counter,
value,
next: E,
})
.collect::<HashSet<_>>();
loop {
if let Some(mid) = left.intersection(&right).min() {
let mut path = Vec::new();
let mut mid1 = mid.clone();
for left in lefts.into_iter().rev() {
let mid2 = unstep(&mid1)
.filter(|mid2| left.contains(mid2))
.next()
.unwrap();
mid1 = mid2;
path.push(mid1.next);
}
path.reverse();
let mut mid1 = mid.clone();
for right in rights.into_iter().rev() {
let mid2 = step(&mid1)
.filter(|mid2| right.contains(mid2))
.next()
.unwrap();
path.push(mid1.next);
mid1 = mid2;
}
return path.into_iter().map(dir_char).collect::<String>();
}
if left.len() <= right.len() {
let left1 = left.iter().flat_map(step).collect::<HashSet<_>>();
lefts.push(left);
left = left1;
} else {
let right1 = right.iter().flat_map(unstep).collect::<HashSet<_>>();
rights.push(right);
right = right1;
}
}
}

fn main() {
let stdin = io::stdin();
let values = stdin
.lock()
.lines()
.flat_map(|line| {
line.unwrap()
.split(", ")
.map(|s| s.parse().unwrap())
.collect::<Vec<i32>>()
})
.collect::<Vec<_>>();

for value in values {
println!("{} {}", value, search(value));
}
}


Try it online!

Output

49445094 nennesseseenenesseseeseseeeeseess
71259604 nnnnnnessennnessseeesssenesenesses
78284689 ennnesssseeeneenesenesssseeesese
163586986 ennnesesseneeeeessennesseeseseeneesen
171769219 ennnessenessssessseesesseeseenesee
211267178 sennnnneseeenessssenessssenenneseseee
222235492 ennnnnesseeeneseesseeesseseneesseesee
249062828 nnnnnesseneneseesssenennesseenesse
252588742 nennnessenneeeessesesesseseeseseeseee
263068669 nennnesseessseeessseesseeenesesssen
265657839 nnnesssseneesesssennneenesseeeses
328787447 eennnesssenesseesssesennnneeseenese
344081398 sennnnesennnesesessesesssseeseennnn
363100288 sennnnesseeneseesssenneesessennenee
363644732 nnnesssenneessesseeesseseseesenees
372642304 nnnnesseneseneseesseneneesssennesese
374776630 sennnnesseseesseneseeeseseessenesen
377945535 nnnesssseneeennesseesseeessseeses
407245889 nnnesseneesessseseseeeeessessenenee
467229432 nnnnesesennnnnesessesessesseeneess
480714605 nnnnessennneseesssenenesenesseesesen
491955034 nnnnnessseeneeeessseeeseenesseseeee
522126455 nnnnesssseeneeesesseesesseeeenese
532351066 nennnessenneeenesesesesessessesenesen
542740616 sennnnesseeneenesssesseenesseesesesen
560336635 nnnesssesesesssseeennessseseeneee
563636122 sennnnnesennneseseennesesssesenesenes
606291383 nnnessssenneeeseseseeseseeeeseesese
621761054 nnnessseennessesssenneeseseseess
648274119 nnnnessseneesseseeseenessseeneseeese
738259135 eennnnnesenennnesseneessssssennnees
738287367 nnnessesseessseseseneeesesseennen
748624287 nennnesseesseeenneseessseseeseneseseese
753996071 nnnnessseneeeseesssenesesenennnesesen
788868538 nnnessesseeseeeneeseesesseesseseeseee
801184363 ennnesseseeseeeeseseeeeseeseseessse
807723631 nnnessessessssesseennnnesssen
824127368 nnnnessesenessseseennnessseesesennnnn
824182796 nnnnessesenesssseenesssesssenesee
833123975 ennnnnneseeeennnessesssessseennnneeesse
849666906 sennnessseeeeseesesesssenesseneeeesen
854952292 nnnnnnesenenesssseeneeessessseseeeeeeee
879834610 nennnnesseessseneeseeesessseseneee
890418072 nnnesssennnnessesesennnesessennnnees
917604533 ennnnesseneeseeesesenennesesseeneesse
932425141 ennnnesssesseesesenesssessseeneesen
956158605 nnnnesseseeeeesesssennneseseenesseee
957816726 enennnesseseeseesseessessssenesss
981534928 eennnessennessseesseesessseenessseenn
987717553 nnnessseeneeesssesseesssesennessee

• You can never return to a cell so every ns, sn, ew and we is immediately illegal in addition to any loops in the path Commented Jul 23, 2018 at 22:14
• @Veskah Thanks for pointing that out. Fixed by disallowing w, ns, and sn, which leaves only legal paths at the expense of ignoring legal paths with w. Commented Jul 24, 2018 at 18:22

PHP, Score=1391462099

function manoeuvre($n){$i=0;
$c=0;$char='';
while($i!=$n){
$c=($c+1)%10;
if($char!='n' and$c>0 and $i>0 and$i*$c<=$n){
$char='s';$i=$i*$c;
}
else if($char!='s' and$i+$c<=$n and ($i-$c<=0 or ($i-$c)*max(($c+1)%10,2)>$n or $c==9)){$char='n';
$i=$i+$c; } else{$char='e';
$i=$i-$c; } echo$char;
}
}


A quick attempt, never goes west to simplify path checking and has few rules to decide direction at each step.