32
2
Balancing Act
Overview
Given an input of 3 single-digit positive integers representing a set of weights, output an ASCII representation of a seesaw with the weights placed on it so that it is in balance around a central pivot, taking lever effects into account.
Each number has a weight equal to its value. The torque of each number is the weight multiplied its the distance from the center in characters. For the seesaw to be in balance, the sum torque of the weights on the left of the seesaw must equal that of those on the right, like this.
Input
3 integers in the range of 1-9. You can input the integers however is convenient, e.g. a tuple, 3 comma-separated values etc. However your program must be able to handle numbers input in any order (i.e. no assuming the values will be sorted). Duplicate numbers may be input (e.g. 2,3,2).
The inputs will always mathematically allow for a valid output, otherwise the input is not valid.
Output
The output should be a 2-line ASCII representation of the seesaw with the weights placed on it. On the first line are the digits, spaced in order to balance them on the seesaw.
Numbers may not be placed in the very center of the scale, where the distance and therefore the torque would be zero. Valid distances from the center range from 1-10 characters inclusive to the left or right of the pivot.
In the spaces unoccupied by the numbers are 18 underscore characters (a center underscore and 10 on each side, minus the 3 positions occupied by the numbers). On the last line is a single caret character aligned with the center of the scale, representing the pivot.
Examples
Input:
4,7,2
Output:
________7___42_______
^
7*2 = 4*2 + 2*3
Numbers can be output on either side, for example this would also be valid:
_______24___7________
^
2*3 + 4*2 = 7*2
Numbers can be placed anywhere on the scale as long as they balance, e.g:
Input:
3,1,5
Output:
_____5________1__3___
^
5*5 = 1*4 + 3*7
or
____5________1_____3_
^
5*6 = 1*3 + 3*9
or
____5___________1_3__
^
5*6 = 1*6 + 3*8
etc
Your program only has to output one of the valid outputs. It does not have to output an error if the input is not valid.
Notes
- This is code-golf so the shortest program in bytes wins
- Program may be a stand-alone or a function that accepts the numbers as input and returns a string.
- Trailing newline and white-space on last line is optional
- If you don't know what a seesaw is, it's also known as a teeter-totter or a teeterboard.
Here's a paste showing valid inputs and solutions (with some duplication)
– samgak – 2015-08-06T07:36:55.42711An excellent first challenge! An interesting problem and a thorough spec. – xnor – 2015-08-06T07:58:57.567
2Algorithmically, given a whole-numbered vector, this asks you to find a whole-numbered orthogonal vector with all different entries. – proud haskeller – 2015-08-07T01:34:09.373