14
3
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 code-golf 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 code-golf, so shortest code wins.
1I really don't get the input format. – Fund Monica's Lawsuit – 2016-04-22T23:39:35.943
@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. – a spaghetto – 2016-04-22T23:40:50.467
No, I mean that I just don't get the challenge, really. – Fund Monica's Lawsuit – 2016-04-22T23:41:33.330
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. – trichoplax – 2016-04-23T00:09:16.703
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? – trichoplax – 2016-04-23T00:11:46.997
This sounds fun. Will the lines (#s) always be the same length as each other? Should shoppers always be sent to the queue with the least people? E.g. 5sss is likely slower than 1ssss, by 1 generation on average. Or is the point to simulate the uncertainty of this situation in real life? :) – Chris M – 2017-01-07T14:59:31.690
@ChrisM Very belated but yes they'll be the same length. I didn't want to overcomplicate the line selection procedure even though there's a chance shorter lines could be slower. – a spaghetto – 2017-04-24T21:59:27.520
Do we assume that there will always be 4 lines of #, and, if yes, do we assume that the array are always at the same position ? Also, do we assume that the input will the result of a previous generation ? – HatsuPointerKun – 2017-07-23T22:50:51.637
@HatsuPointerKun There can be an arbitrary number of lines. I can add examples with more lines if necessary. – a spaghetto – 2017-07-24T02:53:14.713