Find the remaining side of the tangential quadrilateral

-7

(Taken from https://en.wikipedia.org/wiki/File:Tangential_quadrilateral.svg) 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

Leaky Nun

Posted 2016-05-04T14:26:42.683

Reputation: 45 011

Is the orientation of the sides always as in the picture? – Dennis – 2016-05-04T14:37:37.993

@Dennis Yes, it is. – Leaky Nun – 2016-05-04T14:38:09.260

7According to Wikipedia's mention of the Pitot theorem, a+c-b should be the only valid solution, provided it's positive. – Martin Ender – 2016-05-04T14:43:12.673

Is the order for inputting a, b and c strict, or can I also provide input like a, c, b? – Adnan – 2016-05-04T15:06:39.763

2Input are not strict by default. – Leaky Nun – 2016-05-04T15:08:38.857

8

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. – 2016-05-04T16:56:11.200

6

@AlexA. Strangely, it seems he is aware of the sandbox as he keeps recommending that other people use it.

– Suever – 2016-05-05T01:46:50.493

Answers

7

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.

Dennis

Posted 2016-05-04T14:26:42.683

Reputation: 196 637

7

Piet, 32 Codels enter image description here

Codelsize 20:

enter image description here

Notes

Npiet trace images

Valid input [1, 2, 3]

enter image description here

Invalid input [1, 5, 3]

enter image description here

Marv

Posted 2016-05-04T14:26:42.683

Reputation: 839

5

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!

Adnan

Posted 2016-05-04T14:26:42.683

Reputation: 41 965

4

Jelly, 5 bytes

_@/»0

Try it online! or verify all test cases.

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.

Dennis

Posted 2016-05-04T14:26:42.683

Reputation: 196 637

4

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

Try it online!.

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

James

Posted 2016-05-04T14:26:42.683

Reputation: 54 537

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 – 2016-05-04T23:41:28.017

@LuisMendo Well that just made my day! =D I'm getting -0 with 3, 5, 1 as input. – James – 2016-05-04T23:47:40.647

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 – 2016-05-04T23:49:29.443

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. – James – 2016-05-04T23:50:12.053

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 – 2016-05-04T23:52:53.933

3

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

MickyT

Posted 2016-05-04T14:26:42.683

Reputation: 11 735

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 – 2016-05-04T21:38:58.247

@MartinBüttner thanks for the hint, I'll have a look at it and the w does shift to the right, will fix. – MickyT – 2016-05-04T22:08:10.770

1

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.

AdmBorkBork

Posted 2016-05-04T14:26:42.683

Reputation: 41 581

1

J, 12 9 8 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) +/@}:-{:), +/@(}:,-@{:)

Conor O'Brien

Posted 2016-05-04T14:26:42.683

Reputation: 36 228

1

Python, 25 bytes

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

user53386

Posted 2016-05-04T14:26:42.683

Reputation:

1

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

Conor O'Brien

Posted 2016-05-04T14:26:42.683

Reputation: 36 228

1

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

part 2: square root loop

:x²eq!vx1+#x

: duplicates our maximum value a from the top of the stack (for the equality check). squares our counter x and e pushes a Boolean representing the equality of 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 (~).

Conor O'Brien

Posted 2016-05-04T14:26:42.683

Reputation: 36 228

1

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.

Neil

Posted 2016-05-04T14:26:42.683

Reputation: 95 035

1I sometimes wish all the properties of Math were in window. – Conor O'Brien – 2016-05-04T18:54:49.447

1

><>, 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!

SE - stop firing the good guys

Posted 2016-05-04T14:26:42.683

Reputation: 529

1

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!

Blue

Posted 2016-05-04T14:26:42.683

Reputation: 26 661

1

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.

LegionMammal978

Posted 2016-05-04T14:26:42.683

Reputation: 15 731

0

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

Suever

Posted 2016-05-04T14:26:42.683

Reputation: 10 257

0

Python 2, 59 53 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

Erik the Outgolfer

Posted 2016-05-04T14:26:42.683

Reputation: 38 134

1you could use print(X if Y else Z) to save some bytes – Leaky Nun – 2016-05-05T10:39:30.490

1@KennyLau does such a thing exist? WOW! – Erik the Outgolfer – 2016-05-05T10:41:26.790

0

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.

FryAmTheEggman

Posted 2016-05-04T14:26:42.683

Reputation: 16 206