27
Keeping this challenge short.
You are given 4 numbers: p1, p2, p3 and p4.
The magic sum of the numbers are defined as follows:
magic_sum = |p1 - p2| + |p2 - p3| + |p3 - p4| + |p4 - p1|
You are only allowed to change one of the above integer values (p1, p2, p3 or p4). You need to change the value such that the magic sum of the values attains its minimum value.
For example:
p1, p2, p3, p4 = 17, -6, 15, 33. The value of the magic sum is 78 in this case.
You can change the -6 here to 16, and the value of the magic sum will become 36, which is the minimum attainable value.
Do keep in mind that numbers can be positive or negative integers.
This is code-golf, so least bytes in code wins. Brownie points for using a Practical Language over a Recreational language. May the 4th be with you.
To reiterate:
Sample 1
Input 1
17 -6 15 33
Output 1
36
Explanation 1
The -6 can be replaced with 16 and that gives us the minimum attainable magic sum possible.
Sample 2
Input 2
10 10 10 10
Output 2
0 or 2
either is acceptable
Explanation 2
The minimum attainable magic sum is 0 since the minimum sum of 4 positive integers is 0. If a number has to be changed, then one of the 10's can be changed to a 9 and thus yielding the output 2.
Sample 3
Input 3
1 2 3 4
Output 3
4
Explanation 3
The input by itself yields 6 as its magic sum. Changing the 4 to 1 and the minimum magic sum is attained, which is 4.
10+1, but could do with more examples. – Jonathan Allan – 2019-05-04T20:36:08.990
2A fully worked example and a couple more test cases and it's a
+1
from me. – Shaggy – 2019-05-04T22:47:44.443@Shaggy done. where is my +1? :P – Koishore Roy – 2019-05-05T12:01:11.510
1@KoishoreRoy Wouldn't test case 3 be 6 without the change? – wizzwizz4 – 2019-05-05T13:25:36.487
@wizzwizz4 |1 - 2 | + |2 - 3| + |3 - 4| + |4 - 1| = 1+1+1+3 =6. You are correct. Made the edit. – Koishore Roy – 2019-05-05T13:27:33.693
Also, should the explanations be in blockquotes and not pres? – wizzwizz4 – 2019-05-05T13:28:58.047
@wizzwizz4 kinda new to formatting. can you help make the necessary changes? – Koishore Roy – 2019-05-05T13:31:11.650