42
2
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.
16Quincunx had better answer this. – Calvin's Hobbies – 2015-05-05T04:53:17.347
"some consistent pattern of trailing spaces" - may I assume that the trailing spaces on the Nth line encode the probability for the ball to land in the Nth bucket? – Random832 – 2015-05-05T19:09:55.187
4@Random832 No. (Do you really think that would fly?) – Calvin's Hobbies – 2015-05-05T19:15:07.043
There's a reason I posted it as a comment instead of an answer. I just thought it was interesting how permissive this puzzle was about input - most that I've seen dictate the format of the input and/or output more strictly. – Random832 – 2015-05-05T19:16:18.827
@Random832: The input and output are very unambiguous. Standard loopholes (like encoding the answer in the input) don't need to be addressed in every challenge. – Alex A. – 2015-05-06T00:05:37.620
@Random832 To be clear, I allowed "consistent" trailing spaces because, for example, inputting same sized strings on each line might be easier in some cases. I admit the word "consistent" is a bit vague, but Alex is right that common sense needs to be applied. e.g. I didn't specify that the output be given in Arabic numerals, but somehow everyone knows. – Calvin's Hobbies – 2015-05-06T00:34:31.217
@Calvin'sHobbies: Is there a maximum depth to support? – Mooing Duck – 2015-05-06T16:46:16.063
@MooingDuck The depth should only be limited by computer memory and you languages int maximum. I'd say any depth below 1024 should definitely work. – Calvin's Hobbies – 2015-05-06T17:31:33.483