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.
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.
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).
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.
Test Inputs and Corresponding Outputs
12 22 -------------------- ^
12 26 -------------------- ^
42 42 ----------- ^
42 42 ----------- ^
3 16 ---------------- ^
14 16 ---------------- ^
1 56 ------------------- ^
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.split()] length = len(lines) pivot = lines.find("^") left_length = pivot right_length = length - 1 - pivot left_torque = weights * left_length right_torque = weights * right_length if left_torque > right_torque: weights = left_torque // right_length elif right_torque > left_torque: weights = right_torque // left_length weights = [str(w) for w in weights] string_gap = " " * (length - sum(len(w) for w in weights)) lines = weights + string_gap + weights print("\n".join(lines)) balance_seesaw(["1 56", "-------------------", " ^ "])
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
- Balance a set of weights on a see-saw - Proposed Aug 2015 by samgak