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.
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.
17 -6 15 33
The -6 can be replaced with 16 and that gives us the minimum attainable magic sum possible.
10 10 10 10
0 or 2
either is acceptable
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.
1 2 3 4
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.