# Move my liquid to make me equal

You are given 4 positive integers: volume of the first container (v1), volume of the second container (v2), volume of the liquid in the first container (l1), and volume of the liquid in the second container (l2). Your task is to move some of the liquid from container 1 to container 2, making the amount of empty space in the containers equal to each other, outputting how much liquid should be moved.

## An example

Here is an example of some possible input (formatted for the ease of test cases. The input isn't formatted.):

8 11
6  5

Here, you need to make sure that the differences between the volumes and the containers are equal. Currently the difference is not equal:

8 11
- 6  5
======
6 2

So we need to try to make them equal, by taking some of the values in one container to another container:

8 11
- 4  7
======
4  4

After you have succeeded, output how much liquid you need to take from container 1 to container 2. Therefore the above example should output 2.

## Test cases

15 16
9  2

We move into this state:

15 16
- 5  6
======
10 10

Therefore 4 is the expected result.

## Test case #2

16 12
13  1

We move into this state:

16 12
- 9  5
======
7  7

Therefore 4 is the expected output.

## Test case #3

20 12
10 2

Moved:

20 12
-10 2
======
10 10

Therefore 0 is the expected output.

## More test cases

(They don't have explanations as this should be quite clear)

12 5
9  0 -> 1

9 11
7  3 -> 3

## Rules

• The test cases will be made so that the result is always possible as a positive integer. The result will never be a decimal.
• Input can be taken in any convenient and reasonable format.
• Output can be given in any convenient and reasonable format.
• The input string will be made so that a moving is always possible without involving decimals or negative numbers. Also there will not be more liquid than how much a container can hold.
• Of course, this is , so the answer in each language consisting of the fewest number of bytes wins. Happy golfing!
• An extra restriction for the test cases: you can assume that there is always going to be space available in the second container. So a test case like this:
10 2
2 2

is not going to be a valid testcase.

# JavaScript (ES6) / C#, 21 bytes

(v,V,q,Q)=>V-Q-v+q>>1

Try it online! (JS)

Try it online! (C#)

# 05AB1E, 3 bytes

®β;

Try it online!

Input is an array of volumes in the order [first liquid, first container, second container, second liquid].

Alternative 3-byter:

ÆÆ;

Try it online!

Input is a nested array: [[first container, first liquid], [second container, second liquid]].

# Jelly, 3 bytes

ISH

Try it online!

A monadic link taking as its argument [[l1, v1], [v2, l2]] and returning the volume needed to move.

A relatively rare ASCII-only Jelly answer!

## Explanation

I   | Increments
S  | Sum
H | Half
• Well, it's ASCII-ISH... – Neil Jan 17 at 10:32

# Keg, 14 13 bytes

-±¿-¿+½:0<[±]

Try it online!

A port of Arnauld's original javascript answer. Takes input in form of:

volume1
container1
volume2
container2

-1 byte by utilising implicit input correctly.

# Perl 6 Raku, 12 11 bytes

(*-*-*+*)/2

Try it online!

Edit: I realized that I could take the parameters in any order. Four parameters are: liquid in container 1, liquid in container 2, volume of container 1, volume of container 2.

# Whitespace, 79 bytes

[S S S N
_Push_0][S N
S _Dupe_0][T    N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_vol1][S N
S _Dupe_input_vol1][S N
S _Dupe_input_vol1][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_liq1][T S S T   _Subtract_vol1-liq1][S N
S _Dupe_vol1-liq1][S N
S _Dupe_vol1-liq1][T    N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_vol2][S N
S _Dupe_input_vol2][S N
S _Dupe_input_vol2][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_liq2][T S S T   _Subtract_vol2-liq2][T  S S T   _Subtract_(vol1-liq1)-(vol2-liq2)][S S S T  S N
_Push_2][T  S T S _Integer_divide_((vol1-liq1)-(vol2-liq2))/2][T    N
S T _Print_as_integer]

Letters S (space), T (tab), and N (new-line) added as highlighting only.

Try it online (with raw spaces, tabs and new-lines only).

Explanation in pseudo-code:

Integer vol1 = STDIN as integer
Integer liq1 = STDIN as integer
Integer vol2 = STDIN as integer
Integer liq2 = STDIN as integer
Integer diff1 = vol1 - liq1
Integer diff2 = vol2 - liq2
Integer diffOfDiff = diff1 - diff2
Integer result = diffOfDiff / 2
Print result as integer to STDOUT

Since the integers are guaranteed to be positive, it uses them as heap-address for the next input in line with some duplicates, saving bytes.

# Excel, 16 bytes

=(B1-D1-A1+C1)/2

Same approach as others

# Java 8 / CoffeeScript 2, 22 21 bytes

(V,v,L,l)->v-l-V+L>>1

Port of @Grimmy's second 05AB1E answer.
-1 byte by porting @Arnauld's approach, golfing the (...)/2 I had to ...>>1.

Explanation:

(V,v,L,l)->    // Method with four integer parameters and integer return-type
v-l         //  Calculate the difference between the volume and liquid of container 1
-V+L     //  Subtract the difference between the volume and liquid of container 2
>>1  //  Divide that result by 2 (by bit-shifting once towards the right)
• Coffescript polyglot. (Not a JavaScript polyglot because of -> vs =>). – Grimmy Jan 17 at 12:29
• @Grimmy Added. :) – Kevin Cruijssen Jan 17 at 12:57

# Python 3, 27 26 25 bytes

Saved a bytes thanks to meetaig!!!

lambda a,b,c,d:b-d-a+c>>1

Try it online!

• lambda a,b,c,d:b-d-a+c>>1 would save 1 character=1byte – meetaig Jan 17 at 13:52
• @meetaig Thanks! Forgot right-shift had lower precedence than addition/subtraction. :-) – Noodle9 Jan 17 at 15:03

# C (clang), 29 28 bytes

Saved a bytes thanks to meetaig!!!

#define f(a,b,c,d)b-d-a+c>>1

Try it online!

# C (gcc), 39 25 bytes

f(a,b,c,d){a=b-d-a+c>>1;}

Wrote a testing framework for possible future testcases.

Takes as input 4 integers:

• a: capacity of first container
• b: capacity of second container
• c: amount of liquid in first container
• d: amount of liquid in second container

Explanation:

f(a,b,c,d)

Function implicitly returning int with four arguments of type int.

a=

A sort of return that exploits GCC's code generation when compiling without optimization.

b-d-a+c>>1

The main algorithm. There's several steps involved here:

• b-d: Subtract the amount of liquid in the second container from the capacity in the second container to get the free space in the second container.

• -a+c: Subtract the capacity of the first container and add back the amount of liquid in the second container to get the total free space in both.

• >>1 After that, divide the amount of free space in both by 2 to get the amount of liquid to move from container one to container two. This is a binary right shift and binary is base 2, so it is equivalent to division by 2 but removes the need for parentheses as the operator precedence of >> is lower than that of + and -. The operator precedence for / is higher than that of + and - so it would require extra parentheses.

Try it online!

1,$0=($2-$4-$1+$3)/2 Try it online! # Retina 0.8.2, 19 bytes \d+;?$*
+1,1
,
11

Try it online! Link includes test cases. Explanation:

\d+;?
\$*

Convert to unary and sum the middle two numbers.

+1,1
,

Subtract the outer numbers from that sum.

11

Divide by 2 and convert to decimal.

# TI-BASIC, 26 21 bytes

Prompt A,B,C,D:.5abs(A-C-B+D

Input is a series of prompts for each value. A = volume1 B = volume2 C = liquid1 D = liquid2

Output is the amount of liquid that needs to be moved from the fuller container to the emptier one.

Explantion:

Prompt A,B,C,D     ;Prompt the user to input the values
.5abs(A-C-B+D      ;Compute the amount of liquid needed, convert it
;  to a positive number if need be and leave the
;  result in Ans
;Implicit print of Ans

Examples:

prgmCDGF21

A=?15
B=?16
C=?9
D=?2
4
prgmCDGF21

A=?11
B=?10
C=?6
D=?7
1

Note: TI-BASIC is a tokenized language. Character count does not equal byte count.

• I'm pretty sure just taking the 4 numbers as 4 different variables would be shorter. – randomdude999 Jan 17 at 21:38
• @randomdude999 looks like that's the case, but only by 7 bytes. this is code golf after all, i guess – Tau Jan 17 at 21:48

# Brainf*ck, 39 bytes

Try it online!

,>,>,<[->+<]<[->>-<<]>,[->-<]>[-->+<]>.

The input/output are the ASCII values of characters. Using "aaz0" gives "%" as output, as it corresponds to the case

97 122

97  48

which is solved by moving 37 units of liquid, the % symbol.

Could shave a couple of bytes if the input order could be rearranged, but not sure if that would be acceptable.