# Find the remaining side of the tangential quadrilateral

A tangential quadrilateral (see example above) is a quadrilateral in which a circle can be inscribed.

Your task is to find the possible value of d given a, b, and c.

# Specs

• a, b, c will all be positive integers.
• If no possible value of d exists, output 0.

# Scoring

This is . Shortest solution in bytes wins.

# Testcases

a b c d
3 4 4 3
1 2 3 2
1 5 3 0

• Is the orientation of the sides always as in the picture? – Dennis May 4 '16 at 14:37
• According to Wikipedia's mention of the Pitot theorem, a+c-b should be the only valid solution, provided it's positive. – Martin Ender May 4 '16 at 14:43
• Input are not strict by default. – Leaky Nun May 4 '16 at 15:08
• In case no one has mentioned it before, I recommend posting challenges to the Sandbox where they can get feedback before going live. – Alex A. May 4 '16 at 16:56
• @AlexA. Strangely, it seems he is aware of the sandbox as he keeps recommending that other people use it. – Suever May 5 '16 at 1:46

# Dyalog APL, 4 bytes

0⌈-/


Try it online!

### How it works

0⌈-/  Monadic function train. Argument: (a b c)

-/  Reduce by subtraction.
APL evaluates everything from right to left, so this computes
a - b - c = a - (b - c) = a - b + c.
0⌈    Take the maximum of 0 and a - b + c.


# Piet, 32 Codels

Codelsize 20:

### Npiet trace images

Valid input [1, 2, 3]

Invalid input [1, 5, 3]

# 05AB1E, 5 4 bytes

Input is taken as:

b
a
c


Code:

-+0M


Explanation:

      # Inputs: b, a, c
-     # Substract, (a - b).
+    # Add with implicit input, (a - b + c).
0   # Push zero on top of the stack.
M  # Get the largest number that exists in the stack and implicitly print that value.


Try it online!

# Jelly, 5 bytes

_@/»0


### How it works

_@/»0    Main link. Argument: [a, b, c]

_@/      Reduce by swapped subtraction.
This computes (a _@ b) _@ c = c - (b - a) = c - b + a.
»0    Take the maximum of 0 and c - b + a.


# MATL, 6 bytes

-+OvX>


My very first MATL answer! It can probably be shorter, I'd love tips! Input is backwards, e.g.:

c
b
a


Explanation:

-        #Subtract the top two numbers (c and b)
+       #Add the top two numbers (c-b and a)
O      #Push a 0
v     #Concatenate this into a array
X>   #Print the smallest value of the array

• My attempt was 6 bytes too, so I cannot offer many tips here :-) For which values do you get -0? BTW, that -0 is an Octave thing; I may need to look into it to produce just 0 in that case. Anyway, -+OhX> (also 6 bytes) probably avoids the minus in the output. You need to use O, not 0, to avoid the 0 sticking to the + – Luis Mendo May 4 '16 at 23:41
• @LuisMendo Well that just made my day! =D I'm getting -0 with 3, 5, 1 as input. – DJMcMayhem May 4 '16 at 23:47
• Oh I see. Running on Matlab gives 0, not -0. It's an Octave thing (TIO uses Octave). I'll take note to maybe solve that in the future – Luis Mendo May 4 '16 at 23:49
• Also, thanks for the tip with h! I was trying to do X> but I couldn't figure out how to turn [0, 3] into an array. – DJMcMayhem May 4 '16 at 23:50
• Anytime :-) h by default takes two inputs. v would take all the stack and try to concatenate vertically. That would also work here – Luis Mendo May 4 '16 at 23:52

# Cubix, 12 9 bytes

This turned out longer than I wanted and then I messed up the spec. I might be able to knock out a byte or 2.

@w+?II-.\0O@


Wraps onto a cube with side length 2

    @ w
+ ?
I I - . \ 0 O @
. . . . . . . .
. .
. .


Gets II integer input twice, -. subtract followed by noop, \ redirect down (in this case around), hits the second I input again, + adds, ? conditional that turns left on negative, right on positive and straight through for zero.
Turning left (invalid negative) w change lane to the right onto the literal 0 heading right through the O@ output and terminate.
Straight through (invalid zero) hits \ reflector and head right onto the literal 0 and the O@ output terminate.
Turning right (valid positive) travels around the cube, hitting the O output on the bottom face, onto the w change lane right, that switches the lane to the @ terminate.

Try it here

• Have you considered putting a U where the - is? I'm on mobile so I can't really play around with it but it seems like it might allow you to use the third line a bit more efficiently. – Martin Ender May 4 '16 at 21:38
• @MartinBüttner thanks for the hint, I'll have a look at it and the w does shift to the right, will fix. – MickyT May 4 '16 at 22:08

# J, 1298 5 bytes

Input is given as a, b, c. Saved a byte inspired by using Dennis's subtraction method.

0>.-/


## Explanation

0>.-/
-/   reduce argument over subtraction
0>.     greater of the reduction and 0


## Test cases

   rem =: 0>.-/
rem 3 3 4
4
rem 1 2 3
2
rem 1 5 3
0
rem"1 > (3 3 4 ; 1 2 3 ; 1 5 3)
4 2 0


Previously: (a,c,b input) +/@}:-{:), +/@(}:,-@{:)

# Python, 25 bytes

lambda a,b,c:max(0,a-b+c)


## PowerShell, 41 38 bytes

param($a,$b,$c)[math]::Max(0,$a+$c-$b)


Pretty straightforward implementation of the Pitot theorem (thanks to Martin for remembering the name of it).

Saved 3 bytes by using the .NET [math]::Max() function.

# Jolf, 4 bytes

\r0m4


Replace \r with a literal return, or try it here! Explanation:

\r0m4
m4  anti-sum of implicit input
\r0    max of 0 and the anti-sum


# Reng v.3.3, 33 bytes

ii+i-:²1#x1ø
:x²eq!vx1+#x
~n%2+<


Takes input like a c b. Try it here!

The calculation is simple. i gets input, so ii+ is a + c, and i- is then (a + c) - b. Very simple stuff.

The hard part is taking the max with 0. "What!? How could it be that hard?" Well, I didn't implement inequality in Reng. So we'll have to use math!

Observation 1: max(a,b) = (a+b+|a-b|)/2

Observation 2: max(a,0) = (a+|a|)/2

Observation 3: |a| = Sqrt[a^2]

Observation 4: Reng doesn't have square roots, either. Nor does it have power. But knowing that we'll only be taking the square roots of perfect squares, we can use trial and error, starting at 1. That is, our algorithm can look like this:

x = 1 to a, iterate:
if x * x == a break, return X


Since we always have k being a perfect square, this algorithm always terminates.

So, into the rest of the code explanation!

ii+i-:²1#x1ø
:x²eq!vx1+#x
~n%2+<


## part 1: initialization

I've already explained how ii+i- works, so let's look at the rest of the line.

     :²1#x1ø


: duplicates this number. The bottom number will be a in (a + |a|)/2. Now, we need to take the absolute value of the top value. We can do this using observations 3 and 4. ² squares the top value, and 1#x initializes our counter x with 1. Then we go to the next line with 1ø

## part 2: square root loop

:x²eq!vx1+#x


: duplicates our maximum value a from the top of the stack (for the equality check). x² squares our counter x and e pushes a Boolean representing the equality of x² and a. If they are equal, q! breaks out of the loop by going down (v), leaving |a| on the stack. Otherwise, we increment x (x1+) and set x to that value (#x).

## finalization

 ~n%2+<


< redirects the program to look left. shhh it saves bytes. It's equivalent to this series of steps:

+2%n~


+ adds the top two of the stack, thus implementing the first part of (a + |a|)/2. 2% divides that sum by two (% is division here because / is a mirror). Lastly, we output this as a number (n) and terminates the program (~).

## JavaScript (ES6), 22 bytes

(a,b,c)=>a+c>b?a+c-b:0


Math.max is too long and there isn't a long enough common subexpression to deduplicate, so that's basically it. If a falsy value had been acceptable, then 21 bytes: (a,b,c)=>a+c>b&&a+c-b.

• I sometimes wish all the properties of Math were in window. – Conor O'Brien May 4 '16 at 18:54

# Mathematica, 13 bytes

Max[##2-#,0]&


Anonymous function. Takes input in the order b, a, c. The ##2-# is just some crazy Sequencecraft that computes #2+#3-#. The Max[..,0] just takes the maximum of the result and 0.

## Pyke, 6 bytes

-+0]Se


Explanation:

+-     - do the mathsy bit (a+c)-b
0]  - create a list with [0, ^]
Se - Get the maximum value


Try it here!

## ><>, 9 bytes

-+:0(?0n;


Input assumed to be on the stack, in the order a, c, b.

It does c-b+a, and compares that with zero. Then it pushes either 0 or the answer before it terminates.

Try it online!

# MATLAB / Octave, 20 bytes

@(a,b,c)max(c-b+a,0)


Anonymous function that accepts the three inputs and returns the length of the fourth side. Can be called using ans(a,b,c).

Demo with all test cases

# Hexagony, 16

?{?>+'<'-{?/@.!<


Try it online!

In expanded form:

  ? { ?
> + ' <
' - { ? /
@ . ! <
. . .


This is actually a fairly simple program. First, we always execute the instructions in this order: ?{?'-{?/<'+ which just reads some values and computes a-b+c. Now if the value is positive, the > sends us north west and we bounce to the lower right < and then print the number (!) and exit (@).

The only really fancy part of the program is how it handles if the result was negative. If this happens, we move south west and execute: '?{ which actually attempts to read another integer after moving the memory pointer around a bit and we land back on the place where we read another integer. Since the read fails to find a value, it returns zero and because zero isn't positive we wrap to the upper edge of the hexagon in this configuration, so we hit /<!.@ to print out the zero and exit.

# Python 2, 5953 47 bytes

-6 bytes thanks to @KennyLau!

i=input;a,b,c=i(),i(),i();print(0,a+c-b)[b<a+c]


Takes 3 inputs, a, b and c. Then applies the Pitot Theorem (d=a+c-b).

Ungolfed

a = input()
b = input()
c = input()
if b < a + c:
print a + c - b
else:
print 0

• you could use print(X if Y else Z)` to save some bytes – Leaky Nun May 5 '16 at 10:39
• @KennyLau does such a thing exist? WOW! – Erik the Outgolfer May 5 '16 at 10:41