Grocery Store Micromanagement

Python 2, 477 463 453 449 423 402 397 396 393 bytes

t=input()
e=enumerate
q,r=" s"
for i,L in list(e(t))[:0:-1]:
 for j,c in e(L):
	a=t[i-1][j]
	if"0"<c<"6":L[j]="0 1234"[int(c)]
	if(r==a)*q==L[j]:t[i-1][j],L[j]=q+r
	if"e"==a:L[j]=r
	if r==L[j]and"x"==t[i+1][j]:L[j]="5"
 if"Y"in L:x=L.count(r);t[i]=[p.replace(r,q)for p in L]
for i,l in list(e(t))[::-1]:
 for j,c in e(l):
	if"#"==c and(q==l[j+1])*x:x-=1;l[j+1]=r
print"\n".join(map("".join,t))

Try it online!

Still working on golfing this but it solves the problem for now


C++, 898 896 885 841 bytes

Very long to code... but it's there

-2 bytes thanks to Conor O'Brien
-45 byte thanks to Zacharý

#include<vector>
#include<string>
#include<algorithm>
#include<ctime>
#define B begin()
#define L length()
#define C(e)if(i[j].find(e)!=string::npos&&!
#define S's'
#define T size()
#define U i[x][a]
using namespace std;auto g=[](auto&i){int e=i[0].find('e'),n=0,y=0,h=0,o,j,c,x,t=0;for(auto&a:i)t=a.L>t?a.L:t;for_each(i.B,i.end(),[&i,t](string&s){s.resize(t);});srand(time(0));vector<int>s,l;for(j=0;j<i.T;++j){C(S)y)++n;C(89)0)y=j;C(35)h){h=j;for(int d=0;d<i[j].T;++d)if(i[j][d]==35)l.push_back(d+1);s.resize(l.T);}if(h)for(c=0;c<l.T;c++)if(i[j][l[c]]!=32)++s[c];C('x')0)x=j;}--x;for_each(l.B,l.end(),[&i,&x,h](int&a){if(U!=32)--U;if(U==10)U=32;for(int b=x;b>h;--b){if(i[b][a]==32&&i[b-1][a]==S){i[b][a]=S;i[b-1][a]=32;}}if(U==S)U=49+rand()%5;});if(i[y-1][e]==S)i[h][l[min_element(s.B,s.end())-s.B]]=S;for(j=1;j<n+2;++j)if(j<y)i[j][e]=S;};

So... some details :

  • You have to pass a std::vector<std::string> ( they will be resized at the same length the longest string is )

  • All lines of # starts at the same y ( vertical ) coordinates, are the same length, and end at the same y ( vertical ) coordinates

  • Assume that the grid have at least 1 # line or more, have one letter e ( one outlet ) at the top, one letter Y

  • Assume that the input is a valid output so the shoppers waiting to be redirected will always be one after another

Edit : Just saw in the comments of Wheat Wizard's answer that it should support multiple entrances, i will continue to work on that