Balancing Act
A see-saw (supposedly from the French 'ci-ça', meaning 'this-that') forms a third of the holy trinity of playground equipment, along with the similarly ubiquitous slide and swing. A see-saw is in perfect balance if, and only if, the sum of the moments on each side are equivalent. A see-saw can therefore be balanced by adding a specific quantity of weight to the side with the lower moment sum; achieving this is your goal for this challenge.
Challenge
Your challenge is to take a depiction of a see-saw as input and output it again, with weight added to one end of the see-saw to balance it.
Input
Your program must take, in any reasonable format, an ASCII see-saw such as the following:
100 100
-------------------
^
The first line contains two numbers, each representing weights on the see-saw. Exactly one weight is present on each side, each acting on the very end of its side of the plank. Weights are guaranteed to be integers, and always align with their corresponding end of the plank. These numbers will never overlap the fulcrum (^
).
The second line represents the 'plank' of the see-saw. Each dash (-
) represents an equal length to each other dash, with the sole exception of the dash directly over the fulcrum (^
), which has no length.
The third line represents the fulcrum of the see-saw. This fulcrum is marked by the only character that is not a space on this line, a circumflex ('^'). The fulcrum can be positioned anywhere along the length of the plank in a valid input so long as enough space is left so that the numbers representing weights do not overlap the fulcrum in either the input or the output.
The input is guaranteed to have three lines, and have no white-space prior to or after the characters that constitute the see-saw (excepting, of course, the third line, which requires it).
Output
For output, the same see-saw depiction should be printed to stdout, but with one (and only one) of the weights replaced with a larger weight, so as to balance the see-saw. Inputs are guaranteed to make this possible using integers alone. Therefore, weights must be shown without decimal points or any other similar notations. If your language does not use stdout you should go by community / meta consensus on output. Trailing newlines are fine but any other changes to the depiction format are probably not OK.
Exemplification
Test Inputs and Corresponding Outputs
Input 1
12 22
--------------------
^
Output 1
12 26
--------------------
^
Input 2
42 42
-----------
^
Output 2
42 42
-----------
^
Input 3
3 16
----------------
^
Output 3
14 16
----------------
^
Input 4
1 56
-------------------
^
Output 4
196 56
-------------------
^
Reference Implementation - Python 3
# Takes a list of strings as input
def balance_seesaw(lines):
weights = [int(w.strip()) for w in lines[0].split()]
length = len(lines[1])
pivot = lines[2].find("^")
left_length = pivot
right_length = length - 1 - pivot
left_torque = weights[0] * left_length
right_torque = weights[1] * right_length
if left_torque > right_torque:
weights[1] = left_torque // right_length
elif right_torque > left_torque:
weights[0] = right_torque // left_length
weights = [str(w) for w in weights]
string_gap = " " * (length - sum(len(w) for w in weights))
lines[0] = weights[0] + string_gap + weights[1]
print("\n".join(lines))
balance_seesaw(["1 56",
"-------------------",
" ^ "])
Rules
This is code-golf, so the shortest code wins counted in bytes. Check meta if counting bytes is awkward in your language.
Standard rules/loopholes apply.
Input must be taken in a reasonable format. A non-exhaustive list of appropriate formats are given as follows:
- A single string with lines separated by newline characters
- A list of strings, each string represented a line
- A 2D Array or Matrix of characters
Related Challenges
- Balance a set of weights on a see-saw - Proposed Aug 2015 by samgak