Consider this ASCII version of a mechanism similar to a bean machine or plinko/pachinko game:
O
^
\ ^
^ ^ \
\ ^ / ^
U U U U U
1 2 3 4 5
The O
is a ball that falls down.
- When it hits a
^
, there's a 50-50 chance it will go left or right. - When it hits a
/
, it always goes left. - When it hits a
\
, it always goes right.
The ball eventually falls into one of the numbered U
troughs at the bottom. The question is, what is the probability it will end up in each trough?
For this particular case, the probabilities are 0.0
, 0.1875
, 0.5625
, 0.125
, and 0.125
, for troughs 1 through 5 respectively.
Here's another example with 3 troughs instead of 5. The probabilities are 0.5
, 0.5
, and 0.0
:
O
/
^ ^
U U U
1 2 3
In this challenge we will generalize this problem to a mechanism with any number of layers set up in any fashion.
Challenge
Write a program or function that takes in the ASCII representation of the pyramid structure of the mechanism. (Input via stdin/command line/function arg.)
You may either assume it comes in with spaces that put it in the proper shape, e.g.
^
\ ^
^ ^ \
\ ^ / ^
Or you may assume it comes in with no spaces at all, e.g.
^
\^
^^\
\^/^
(If desired, you may assume there is a trailing newline and/or some consistent pattern of trailing spaces.)
The input pyramid structure may have any number of levels (aka lines), including zero. Each level has one more ^
, /
, or \
than the last, and there are levels + 1
troughs at the bottom (which aren't part of the input).
You program/function must print/return the list of the probabilities that the ball lands in each of the troughs (in the order leftmost trough to rightmost trough). These should be floating point values that, when printed, have at least 3 decimal places (superfluous zeros or decimal points are not required; 1
is fine for 1.000
, .5
is fine for 0.500
, etc.). If you wrote a function, you may print the values or return a list/array of the floats.
Any reasonable printed list format is fine. e.g. 0.5 0.5 0.0
, [0.5 0.5 0.0]
, [0.5, 0.5, 0.0]
, {0.5, 0.5, 0.0}
, or 0.5\n0.5\n0.0
would all be alright.
Examples
0 Levels: (boils down to one trivial U
)
Input: [no input/empty string given]
Output: 1.0
1 Level:
Input: ^
Output: 0.5 0.5
Input: /
Output: 1.0 0.0
Input: \
Output: 0.0 1.0
2 Levels: (second example above)
Input:
/
^ ^
Output: 0.5 0.5 0.0
3 Levels:
Input:
^
^ ^
^ ^ ^
Output: 0.125 0.375 0.375 0.125
Input:
\
/ \
/ / \
Output: 0.0 0.0 0.0 1.0
4 Levels: (first example above)
Input:
^
\ ^
^ ^ \
\ ^ / ^
Output: 0.0 0.1875 0.5625 0.125 0.125
7 Levels:
Input:
^
/ ^
^ ^ /
/ \ / \
^ ^ / ^ \
^ \ ^ \ / ^
\ ^ ^ ^ \ ^ /
Output: 0.0 0.09375 0.28125 0.4375 0.1875 0.0 0.0 0.0
Scoring
The shortest answer in bytes wins. Tiebreaker is earlier post.