26
5
A classic example to introduce people to the concept of a discrete probability distribution is the bean machine. This machine has a large amount of marbles fall from a narrow passageway at the top, after which they hit rows of interlaced pins, where at each pin the marble hits it might fall to the left or the right of the pin. Finally, the pins are collected in vertical bins at the bottom of the machine. A simple diagram of this machine looks like this:
| O |
| ^ |
| ^ ^ |
| ^ ^ ^ |
| ^ ^ ^ ^ |
| ^ ^ ^ ^ ^ |
|_|_|_|_|_|_|
In this diagram, the O
signifies the location from which the marbles fall. Each ^
is a pin at which the marble has a 50% chance to move to the square either to the left or the right of the pin. The marbles then gather at the bins on the bottom of the device, and for a large enough number of marbles, the height of the marble stacks in the bins will resemble a discrete binomial distribution.
Challenge
For this challenge, you will be calculating the resulting probability distribution of bean machines based on diagrams like the above one. The diagrams are interpreted as a two-dimensional 'program' that the marbles pass through, either towards fields at the side or fields below the current field. When the marbles reach the bottom of the machine they are counted for the probability distribution. To keep it interesting, these diagrams will contain a few more fields than just the simple source and pins. An example diagram is:
| O |
| ^ |
| ^ / |
| ^ | ^ |
| <^- = v |
| ^ ^ ^ ^ ^ |
Furthermore, the marbles now each have a rotation direction. This direction is set by some fields and determines to which next field the marble moves in several other fields.
The following fields are defined:
O
: Source. Spawns marbles directly below it. The direction of these marbles is 50% left, 50% right. Each source produces the same amount of marbles.U
: Sink. Any marbles which enter this field are removed from the bean machine.: Empty space. If a marble arrives at this field, it will move to the field below.
-
: Floor. If a marble arrives at this field, it will move to either the field to the left or the field on the right, depending on its current direction.^
: Splitter. If a marble arrives at this field, it has a 50% of moving to the field to the right or the field to the left of the splitter. This also determines the direction of the marble.v
: Join. If a marble arrives at this field, it will move to the field below./
: Slanted pad. If a marble arrives at this field, it will move to the field on the left of the pad, setting the direction of the marble.\
: Same as the previous, but to the right.|
: Reflector. If a marble arrives at this field, it will reverse the direction of the marble, and move the marble to the field to the right or the left, based on this reversed direction.=
: Cannon. If a marble arrives at this field, it will move it to the right or the left in the current direction, until the marble encounters a field that is not,
-
orO
.<
: Same as the previous, but will always set the direction and move towards the left.>
: Same as the previous, but to the right.
The following guarantees are given regarding the diagram.
- Each input row will have exactly the same length in fields.
- The leftmost and rightmost field of each row will always be a
|
. - The diagram will not contain any possible paths through which marbles can get stuck in the machine for an indeterminate amount of iterations, like
\/
or^^
. - The diagram will only contain the above mentioned fields.
- There are one or more sources
Result
Your task will be to generate an 16-line tall ASCII bar graph of the probability distribution in which the marbles exit the bottom side of the graph, scaled so the largest probability covers all 16 characters. So for the following problem:
| O |
| ^ |
| ^ ^ |
| ^ ^ ^ |
| ^ ^ ^ ^ |
| ^ ^ ^ ^ ^ |
Your program should produce the following solution (note that it should have the same width as the input program, including the pipes to the side:
# #
# #
# #
# #
# #
# #
# #
# #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # #
# # # # # #
# # # # # #
Examples
The following is an example that should test the functionality of all different field types:
| O O |
| O ^ / <^\\\ |
| ^ > ^ |
| ^ ^ ^ =|
| ^ ^ | ^ <^ O |
| ^ > ^ | ^ O ^> v |
|| ^U ^ | = ^\ |
| ^ ^ ^ ^U ^\ ---^ |
| = ^ ^ = v |
It should result in the following output:
#
#
#
#
# #
# #
# #
# # # #
# # # #
# # # #
# # # #
## # # #
## # # # #
# ### # # # #
# # ### # # # #
# # ### # # # #
Rules
Both functions and full programs constitute valid answers for this challenge. You will receive the diagram as a newline-separated string, and you should return the output graph in the given format. Default input/output rules apply. While trailing and leading newlines are allowed in the output, each row should have exactly the same width as the input.
As to allow more creative solutions, it is only required that your program outputs the correct result more than 90% of the time for the same diagram. It is a probability simulation after all.
Scoring
This is code-golf, so the lowest score in bytes wins.
Much simpler but related.
– Peter Taylor – 2017-06-28T16:25:19.213Comments are not for extended discussion; this conversation has been moved to chat.
– Dennis – 2017-06-30T18:58:44.267so
v
=[space]
? – l4m2 – 2018-05-16T00:48:56.567@l4m2
v
and[space]
differ in how cannons interact around them. – CensoredUsername – 2018-05-16T14:46:31.470