14
\$\begingroup\$

You are an employee at the hip new grocery store Half Foods, and it's the day before Thanksgiving Christmas Easter. Since the store will be packed with customers rushing to get their foodstuffs, the store needs a traffic manager to send everyone to the appropriate lines. Being lazy, you'd like to automate this so that you can go hit the deli before everyone takes all the turkey ham whatever. However, all you have with you is your phone, and coding long programs on it is a real pain -- so you need to bust out your ninja skills.

Challenge

Let's visualize the grocery store on a two-dimensional grid. Here's a sample grid to dissect:

                             e
                             s
                             s
                             s
                               Y

#           #                #s           #
#s          #                #s           #
#s          #                #s           #
#s          #s               #s           #
#3          #1               #4           #
 x           x                x            x

The grid starts out with an e, which represents an "outlet" to the rest of the store. Every generation, all of the outlets in the grid spawn a shopper (s) directly below. The shoppers move downward each generation until they reach you (Y). When a shopper reaches the same row as you, you must teleport the shopper to the beginning of the line with the least amount of shoppers in it. A shopper immediately moves to the line when they would move into the row with the Y, there is no generation in between. The lines are represented by the #s -- the column after the #s is a line. The shoppers go down to the end of the line (represented by an exit x), and then turn into a random number between 1 and 5. Each generation, you must decrement numbered shoppers by 1 -- when a shopper would reach 0, they're done checking out and they leave the store.

Given an input of a grid like this, output the next generation of the grocery store (move all the shoppers down simultaneously, redirect shoppers, and have them leave if they are done).

Samples

Input:

                e

                     Y


#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
 x           x                x            x

Output:

                e
                s 
                     Y


#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
 x           x                x            x

Input:

                e
                s 
                     Y


#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
 x           x                x            x

Output

                e
                s 
                     Y


#s          #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
 x           x                x            x

Input:

                e

                     Y


#           #                #            #
#           #                #            #
#           #                #            #
#s          #                #            #
#           #                #            #
 x           x                x            x

(Possible) Output:

                e
                s 
                     Y


#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
#3          #                #            #
 x           x                x            x

Input:

                e
                s 
                     Y


#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
#3          #                #            #
 x           x                x            x

Output:

                e
                s 
                     Y


#           #s               #            #
#           #                #            #
#           #                #            #
#           #                #            #
#2          #                #            #
 x           x                x            x

Input:

                e

                     Y


#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
#1          #                #            #
 x           x                x            x

Output:

                e
                s 
                     Y


#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
#           #                #            #
 x           x                x            x

This is , so shortest code wins.

\$\endgroup\$
  • 1
    \$\begingroup\$ I really don't get the input format. \$\endgroup\$ – Nic Hartley Apr 22 '16 at 23:39
  • \$\begingroup\$ @QPaysTaxes Input can be either a multi-line string or an array of single line strings as per our defaults for taking multiple lines of input. \$\endgroup\$ – a spaghetto Apr 22 '16 at 23:40
  • \$\begingroup\$ No, I mean that I just don't get the challenge, really. \$\endgroup\$ – Nic Hartley Apr 22 '16 at 23:41
  • \$\begingroup\$ Rather than rely on the examples, it might be worth stating explicitly that the shopper can never be on the same row as Y, as moving down to the Y row and being teleported to the top of the relevant queue happen in a single step. \$\endgroup\$ – trichoplax Apr 23 '16 at 0:09
  • \$\begingroup\$ It would also help to have a description of how a queue moves, with a test case as an example. If there are 3 shoppers vertically adjacent in a queue, and the lowest can move down, do all 3 move down together in a single step or does the available space move up one row at a time as each shopper moves into it? \$\endgroup\$ – trichoplax Apr 23 '16 at 0:11
4
+25
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ You can remove excess indentation and line breaks (single line blocks can go on the same line as the start of the block) \$\endgroup\$ – Solomon Ucko Jul 23 '17 at 15:02
  • \$\begingroup\$ @SolomonUcko Where are you talking about? \$\endgroup\$ – Wheat Wizard Jul 23 '17 at 15:06
  • \$\begingroup\$ 1. Are tabs 8 spaces to python? 2. I think you can remove the line breaks after the last 2 for loops. \$\endgroup\$ – Solomon Ucko Jul 23 '17 at 19:38
  • 1
    \$\begingroup\$ 1. Tabs are their own thing in Python. 2. You can't remove that line break. \$\endgroup\$ – Wheat Wizard Jul 23 '17 at 19:42
  • \$\begingroup\$ 1. Does python just count the first indentation level in a block as the indentation level for that block? 2. Do you know why not? I tested it and it doesn't work. \$\endgroup\$ – Solomon Ucko Jul 23 '17 at 20:07
4
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Maybe you could make that C macro be #define C(e)i[j].find(e)!=string::npos? \$\endgroup\$ – Conor O'Brien Jul 24 '17 at 20:16
  • \$\begingroup\$ My answer supports multiple entrances as a side effect, of it being golfy. Quartata said it would be required, but I don't see it in the question, so as far as I'm concerned you are free to only support a single entrance. \$\endgroup\$ – Wheat Wizard Jul 28 '17 at 13:07
  • \$\begingroup\$ @WheatWizard Well, if i read the question, it says : "The grid starts out with an e, which represents an outlet" and "all of the outlets", so that suggest it can have several entrances \$\endgroup\$ – HatsuPointerKun Jul 28 '17 at 13:54
  • \$\begingroup\$ You can change the definition of C(e) to be #define C(e)if(i[j].find(e)!=string::npos and change the calls accordingly. \$\endgroup\$ – Zacharý Sep 4 '17 at 16:49
  • \$\begingroup\$ And since length() is only applied on a, you can change L to be defined as a.length(), modifying calls accordingly. In addition, you can move the using namespace std; to the bottom, saving a byte by removing the newline \$\endgroup\$ – Zacharý Sep 4 '17 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.