-7
\$\begingroup\$

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

18 Answers 18

7
\$\begingroup\$

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.
\$\endgroup\$
7
\$\begingroup\$

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

\$\endgroup\$
5
\$\begingroup\$

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!

\$\endgroup\$
4
\$\begingroup\$

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.
\$\endgroup\$
4
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ 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 + \$\endgroup\$ – Luis Mendo May 4 '16 at 23:41
  • \$\begingroup\$ @LuisMendo Well that just made my day! =D I'm getting -0 with 3, 5, 1 as input. \$\endgroup\$ – DJMcMayhem May 4 '16 at 23:47
  • \$\begingroup\$ 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 \$\endgroup\$ – Luis Mendo May 4 '16 at 23:49
  • \$\begingroup\$ 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. \$\endgroup\$ – DJMcMayhem May 4 '16 at 23:50
  • \$\begingroup\$ Anytime :-) h by default takes two inputs. v would take all the stack and try to concatenate vertically. That would also work here \$\endgroup\$ – Luis Mendo May 4 '16 at 23:52
3
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Martin Ender May 4 '16 at 21:38
  • \$\begingroup\$ @MartinBüttner thanks for the hint, I'll have a look at it and the w does shift to the right, will fix. \$\endgroup\$ – MickyT May 4 '16 at 22:08
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

Python, 25 bytes

lambda a,b,c:max(0,a-b+c)
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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 (~).

\$\endgroup\$
1
\$\begingroup\$

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.

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

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.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ you could use print(X if Y else Z) to save some bytes \$\endgroup\$ – Leaky Nun May 5 '16 at 10:39
  • 1
    \$\begingroup\$ @KennyLau does such a thing exist? WOW! \$\endgroup\$ – Erik the Outgolfer May 5 '16 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.