47
\$\begingroup\$

We live in a wonderful age of technology where we can have beautifully detailed 8K screens on our TVs, and even 2K displays on our phones for our mobile browsing pleasure. We've come a long way in recent years in terms of screen technology.

One of the products of this is a term that was made popular by Apple, Retina. This is referring to the pixel density of the display in question being so high, that at a viewing distance of 10-12 inches away, individual pixels cannot be easily picked out.

Steve Jobs said that the pixel density where this occurs is right around 300 pixels per inch, and they started employing pixel densities in this range on their devices with the Retina buzzword used for advertising.

Pixel density can be calculated by the following formula:

D=sqrt(w^2+h^2)/d

Where d is the diagonal of the screen in inches, w is the number of pixels on the horizontal axis, and h is the number of pixels on the vertical axis.

Your Task

For this task, you'll be using the Retina standard to decide what products are worth buying. Being the modern consumer that you are, when you shop for devices you want to make sure that you're getting a good product, not some device from the 90s! As such, you want to build a program or function that takes the screen width, height and diagonal length as input or function parameters, and tells you whether the particular screen qualifies as a retina screen (D > 300) by printing to the screen or returning.

Due to your contempt for non-Retina devices, your program or function will output Retina! when the device qualifies, and Trash! when it does not.

You may assume that all of the numbers will be greater than 0. Pixel values for width and height will always be whole numbers. Screen size may be interpreted in any way, as long as it supports decimals. The input may be in any order you choose, and may also be on up to 3 separate lines.

Example I/O

1920 1080 4.95   -> Retina!
2560 1440 5.96   -> Retina!
1920 1080 10.5   -> Trash!
10 10 0.04       -> Retina!
4096 2160 19(.0) -> Trash!
8192 4320 100.00 -> Trash!
3000 1500 11.18  -> Retina!
180 240 1(.0)    -> Trash!

This is , so the fewest number of bytes wins.


Here's a Stuck solution, a stack based programming language I'm making:

r;`;/300>"Retina!""Trash!"?
\$\endgroup\$
  • 34
    \$\begingroup\$ Someone, please, do a Retina answer \$\endgroup\$ – Digital Trauma Jul 21 '15 at 20:48
  • 2
    \$\begingroup\$ @DigitalTrauma But floats. \$\endgroup\$ – Sp3000 Jul 21 '15 at 20:49
  • 7
    \$\begingroup\$ @Sp3000 pfft, excuses!. Build your own float parsing regex in Retina! \$\endgroup\$ – Optimizer Jul 21 '15 at 20:50
  • 46
    \$\begingroup\$ @Sp3000 Okay, lets raise the stakes. I hereby promise to slap a big juicy 500 pt bounty on the shortest legal (community consensus) Retina answer to this question one week from this comment posting timestamp. \$\endgroup\$ – Digital Trauma Jul 21 '15 at 21:09
  • 2
    \$\begingroup\$ Hmm, the threshold chosen doesn't quite match Apple's marketing, e.g. for the Retina iMac: 5120 2880 27 \$\endgroup\$ – Ed Avis Jul 30 '15 at 9:16

38 Answers 38

5
\$\begingroup\$

Pyth - 27 bytes

Uses ternary operator and abs to calculate pythagorean.

?>c.avzQ300"Retina!""Trash!

Takes input in two lines, first line width, height, second line diag.

The rules were relaxed so rolling back.

Try it online here.

\$\endgroup\$
115
+500
\$\begingroup\$

Retina, 530 220 210 202 201 193 191 187 185 (184) bytes

Credits to randomra for saving 3 bytes! (And paving the way for a couple more.)

+`\.(\d)(.+)( .+)
$1.$2_$3_
\b
#
+`(\d*)#((((((((((9)|8)|7)|6)|5)|4)|3)|2)|1)|\w)
$1$1$1$1$1$1$1$1$1$1$3$4$5$6$7$8$9$10$11#
\d
11
(?=(1*)\1)[^.]
$1
^(1+)\.\1{90000}1+
Retina!
1.+
Trash!

For byte-counting purposes, each line goes in a separate file, but you can run the above code as is from a single file by invoking Retina with the -s flag.

This expects the density first (which must contain a decimal point, even if it's a trailing one), followed by width and height, i.e. d w h.

This is a bit slow. I wouldn't try most of the given test cases, because it will run for ages. However, you can check that it works correctly with the test cases

19. 4096 2160     -> Trash!
1. 180 240        -> Trash!
1. 181 240        -> Retina!
1. 180 241        -> Retina!
0.04 10 10        -> Retina!

Basically, after multiplying all numbers through to make the density an integer, you don't want the width and height to have more than 4 digits.

While this is slow, it is completely exact... there are no floating point issues or anything like that. All arithmetic is using (unary) integers.

In principle, I could shave off one more byte: the ^ can be omitted, but it will make Trash! test cases horribly slow due to excessive amounts of backtracking.

Explanation

First, let's rearrange the inequality to avoid floating point operations:

√(w2 + h2) / d > 300
√(w2 + h2) > 300 d
w2 + h2 > 90000 d2

We can also notice that this is invariant under multiplying w, h and d by the same number x:

w2 + h2 > 90000 d2
(x w)2 + (x h)2 > 90000 (x d)2
x2 (w2 + h2) > 90000 x2 d2
w2 + h2 > 90000 d2

There are several ways to square a unary number, but we'll be making use of the identity

n2 = Σi=1..2n ⌊i/2⌋

This gives us a way to solve the problem using only integer arithmetic (representing integers in unary).

Let's go through the code. Each pair of lines is a regex substitution.

+`\.(\d)(.+)( .+)
$1.$2_$3_

This repeatedly moves the decimal point in the density to the right while multiplying width and height by 10 (the x above). This is to ensure that all numbers are integers. Instead of appending zeroes, I'm appending _, which I'll be treating as zero later on. (This is a golfing trick, because otherwise I'd need to write ...${3}0 to avoid ambiguity with $30.) The + in front of the regex tells Retina to repeat this substitution until the result stops changing (which is the case when the pattern no longer matches).

\b
#

We're preparing the three numbers for conversion to unary now. In principle, we need a marker (the #) in front of each number, but it's shorter to add one to the end of each number as well, which won't affect the conversion step.

+`(\d*)#((((((((((9)|8)|7)|6)|5)|4)|3)|2)|1)|\w)
$1$1$1$1$1$1$1$1$1$1$3$4$5$6$7$8$9$10$11#

This is the conversion to unary, using a trick that has been developed by dan1111. Essentially I'm translating each digit to a rep-digit of itself, while multiplying the existing digits by 10 (moving the # marker to the right in the process). This binary representation will be quite a jumble of different digits, but the total number will be equal to the value of the original integer. Note the \w at the end - normally this is just 0, but we want to treat _ as zero as well (which is considered a word character in regex).

\d
11

We turn each digit into two 1s, thereby a) ensuring all digits are the same (which will be necessary later) and b) doubling each of the numbers.

(?=(1*)\1)[^.]
$1

This does two things: it squares all numbers (or rather half of each number, by computing a sum over 2n), and adds the resulting squares of the width and the height. Notice that [^.] matches 1s, # markers and spaces. If it's a # or a space, the lookahead won't capture anything, which means all of those are simply removed, i.e. the results for the width and height are concatenated/added. The decimal point . remains to separate the result for d from those. If [^.] matches a 1 instead, then the lookahead ensures that we capture half of the 1s after it (rounded down) in group 1. This computes the sum I mentioned above, which will then yield the square of the original number.

^(1+)\.\1{90000}1+
Retina!

The string is now d2 (in unary), then ., then w2 + h2 (in unary). We want to know if the first unary number times 90000 is shorter than the second. We can easily do this multiplication using a capturing group and {n} repetition syntax. We use 1+ (instead of 1*) afterwards to ensure that the second number is actually greater than that and not just equal. If so, we replace all of that by Retina!.

1.+
Trash!

If the second number wasn't big enough, then the previous step won't have changed anything and the string will still start with a 1. If that's the case, we just replace the entire string by Trash! and are done.

\$\endgroup\$
  • 24
    \$\begingroup\$ Well, it happened. Now we can all die happy. \$\endgroup\$ – Alex A. Jul 21 '15 at 22:00
  • 94
    \$\begingroup\$ As a side note, since this is Retina, does that make all the other answers trash? \$\endgroup\$ – Alex A. Jul 21 '15 at 22:02
  • 5
    \$\begingroup\$ Our god has arrived! \$\endgroup\$ – Kade Jul 21 '15 at 22:16
  • 7
    \$\begingroup\$ OK, so my bounty promise still stands, but I would like to see a thorough explanation (conceptually I think I know how its done, but would like to see the details). Also I'll award the bounty to the shortest legal Retina answer, so, folks, the bounty is still open to shorter Retina answers! \$\endgroup\$ – Digital Trauma Jul 21 '15 at 22:21
  • 2
    \$\begingroup\$ @AlexA. No, because there's another Retina answer. \$\endgroup\$ – Ismael Miguel Jul 23 '15 at 12:33
38
\$\begingroup\$

Python, 49

lambda w,h,d:"RTertaisnha!!"[w*w+h*h<=9e4*d*d::2]

Uses string interleaving.

It turned out shorter to square both sides than to use the complex norm.

w*w+h*h<=9e4*d*d
abs(w+1j*h)<=300*d
\$\endgroup\$
  • 18
    \$\begingroup\$ We were supposed to print either Retina! or Trash!! Not retweet @ertaisnha!! \$\endgroup\$ – Optimizer Jul 21 '15 at 20:49
  • 20
    \$\begingroup\$ Relevant. \$\endgroup\$ – Kaz Wolfe Jul 21 '15 at 21:44
  • 2
    \$\begingroup\$ @Mew but that is the opposite ;) \$\endgroup\$ – Optimizer Jul 22 '15 at 12:46
  • \$\begingroup\$ Nice, I haven't yet seen that (ab)use to print one or another string \$\endgroup\$ – Nick T Jul 23 '15 at 22:45
15
\$\begingroup\$

Retina, 312 bytes

(\d+) (\d+) (\d+)(?:\.(\d+))?
a$1bc$2dj300ke$3fg$4h9iiiiiiiii8iiiiiiii7iiiiiii6iiiiii5iiiii4iiii3iii2ii1i0
+`(b.*)(d.*)fg(\d)
0$10$2$4fg
+`(a|c|e|j)(\d)(\d*)(i*)((?:b|d|f|k).*h.*\2(i*))
$1$3$4$4$4$4$4$4$4$4$4$4$6$5
g`(i+)
Q$1R$1
+`Q(i+)Ri
$1Q$1R
+`(j(i*).*e)i(.*f)
$1$3$2
a(i*).*c(i*).*f\1\2.*
Trash!
.*0
Retina!

This does take quite a while to run, but it seems to work.

Probably could be golfed a lot more...

Explanation:

(\d+) (\d+) (\d+)(?:\.(\d+))?
a$1bc$2dj300ke$3fg$4h9iiiiiiiii8iiiiiiii7iiiiiii6iiiiii5iiiii4iiii3iii2ii1i0

Add tags to make the string more convenient to parse, and add some junk to make it easier to convert to base 1, and add a 300 to multiply by later

+`(b.*)(d.*)fg(\d)
0$10$2$4fg

Append 0s to the width and height, while appending the decimal part of the diagonal to the integer part. When this is done, the diagonal will be an integer, and the width and height will be multiplied by however many 10s were necessary.

+`(a|c|e|j)(\d)(\d*)(i*)((?:b|d|f|k).*h.*\2(i*))
$1$3$4$4$4$4$4$4$4$4$4$4$6$5

Convert all numbers to base 1, using the lookup-table I appended in the first step

g`(i+)
Q$1R$1

Prepare to square all of the numbers

+`Q(i+)Ri
$1Q$1R

Square each number

+`(j(i*).*e)i(.*f)
$1$3$2

Multiply the square of the diagonal by the square of the 300 we inserted in the first step

a(i*).*c(i*).*f\1\2.*
Trash!

If the width appended to the height fits in the product we just computed, the pixel density is too low, and it's Trash!

.*0
Retina!

Otherwise, it's Retina!

\$\endgroup\$
14
\$\begingroup\$

CJam, 30 29 27 bytes

q~mh300/<"Retina""Trash"?'!

Requires input to be in form of diagonal width height

UPDATE: 1 byte saved thanks to Dennis!

Try it online here

\$\endgroup\$
9
\$\begingroup\$

APL, 40 36 bytes

Saved 4 bytes thanks to Dennis!

{(9E4×⍵*2)<+/⍺*2:'Retina!'⋄'Trash!'}

This creates an unnamed dyadic function that takes the first two arguments on the left and the third on the right. It checks whether the sum of the squares of the left values is greater than 300^2 times the square of the right one. Output is printed accordingly.

You can try it online!

\$\endgroup\$
  • \$\begingroup\$ Should have refreshed before posting... You can save a few bytes byte taking the first two arguments on the left. \$\endgroup\$ – Dennis Jul 21 '15 at 21:55
  • \$\begingroup\$ @Dennis Thanks for your help! I considered deleting my answer when I saw yours because yours was better. :P \$\endgroup\$ – Alex A. Jul 21 '15 at 22:00
9
\$\begingroup\$

TI-BASIC, 43

Takes width and height through the homescreen as a two-element list, and diagonal through Input.

Input D
If 300D>√(sum(Ans²
Then
Disp "Retina!
Else
"Trash!

TI-BASIC's two-byte lowercase letters add 7 bytes (i, being the imaginary unit, is one byte) cause it to be quite uncompetitive. Thankfully, ! is also one byte because it represents the factorial function.

\$\endgroup\$
9
\$\begingroup\$

JavaScript ES6, 49 bytes

(w,h,d)=>Math.hypot(w,h)/d>300?'Retina!':'Trash!'

I hate that JavaScript has such long math operators. But even if there was a Math.pythagorean this would be shorter.

\$\endgroup\$
  • \$\begingroup\$ Someone mind explaining downvote? \$\endgroup\$ – Downgoat Jul 21 '15 at 20:46
  • \$\begingroup\$ Probably for being too long. \$\endgroup\$ – lirtosiast Jul 21 '15 at 20:47
  • 1
    \$\begingroup\$ @ThomasKwa Python is just 1 byte shorter \$\endgroup\$ – Optimizer Jul 21 '15 at 20:47
  • 5
    \$\begingroup\$ Instead of calculating the square root, it might be shorter if you compare the squares of the values. \$\endgroup\$ – Reto Koradi Jul 21 '15 at 20:54
  • 4
    \$\begingroup\$ @RetoKoradi Yes, it is shorter: (w,h,d)=>w*w+h*h>9e4*d*d?'Retina!':'Trash!' \$\endgroup\$ – pepkin88 Jul 23 '15 at 14:13
8
\$\begingroup\$

Excel, 44 bytes

Type your inputs in these cells.

  • A1 = Width in pixels
  • B1 = Height in pixels
  • C1 = Diagonal in inches

And this formula gives your result:

=IF((300*C1)^2<A1^2+B1^2,"Retina!","Trash!")
\$\endgroup\$
  • 4
    \$\begingroup\$ I attempted to golf 2 bytes out of this by typing 9E4*C1^2 instead of (300*C1)^2 ... this yields a length of 42. However, typing 9E4 in a formula in Excel, will be changed to 90000 as soon as you hit enter. :( \$\endgroup\$ – Ross Presser Jul 22 '15 at 6:33
7
\$\begingroup\$

Prolog, 51 bytes

a(W,H,D,R):-9e4*D*D<W*W+H*H,R="Retina!";R="Trash!".

Running a(8192,4320,100.0,R). outputs: R = "Trash!" .

Edit: Thanks to @PaulButcher for correcting an edge case and golfing one byte.

\$\endgroup\$
  • \$\begingroup\$ This returns "Retina!", when density is 300 (e.g. 180 240 1 -> Trash!) from the example. Fortunately, since you can shave off two characters with e notation, changing > to >= leaves it at 52: b(W,H,D,R):-9e4*D*D>=W*W+H*H,R="Trash!";R="Retina!". \$\endgroup\$ – Paul Butcher Jul 22 '15 at 15:42
  • \$\begingroup\$ @PaulButcher Using 9e4 instead of 90000 actually shaves off 2 characters, not one. So you effectively shortened my answer by one byte as well as correcting this edge case, thanks. \$\endgroup\$ – Fatalize Jul 22 '15 at 15:43
  • 1
    \$\begingroup\$ Well done! It's good to see a reasonably short Prolog example. \$\endgroup\$ – Paul Butcher Jul 22 '15 at 15:44
  • \$\begingroup\$ @PaulButcher Shaved one more byte by actually using < instead of >= and swapping the two string results... \$\endgroup\$ – Fatalize Jul 22 '15 at 15:50
5
\$\begingroup\$

JavaScript (ES6), 45 bytes

f=(w,h,d)=>w*w+h*h>d*d*9e4?'Retina!':'Trash!'

CoffeeScript, 47 bytes

No ternary operator, but there is exponentiation (which doesn't help in the latest attempt).

f=(w,h,d)->w*w+h*h>d*d*9e4&&'Retina!'||'Trash!'

# Previous attempt
f=(w,h,d)->(w*w+h*h)**.5/d>300&&'Retina!'||'Trash!'
\$\endgroup\$
  • 1
    \$\begingroup\$ Per the spec, it isn't necessary to have the function named (f=). You can cut off 2 bytes in all versions. \$\endgroup\$ – Kroltan Jul 28 '15 at 21:26
5
\$\begingroup\$

O, 40 37 bytes

jjjrmd\/'īu>{"Retina!"p}{"Trash!"p}?

A lot of bytes for the input formatting :\

Try it online

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

Pure Bash (no bc/other external commands), 138 136 135 82 83 bytes

a=${3#*.}
d=${a//?/0}
r=(Trash Retina)
echo ${r[$1$d**2+$2$d**2>90000*${3/./}**2]}!

I decided to try doing it in pure bash. I've probably made a few obvious inefficiencies as this is my first time code golfing, but I am VERY familiar with bash and have had fun in the past trying to write things that don't use any external commands (ie pure bash).

The printf statement is the most annoying. Anyone got any better ideas for padding numbers with zeroes?

EDIT: Saved two bytes, turns out printf will take an empty argument for zero. Saved another byte, turns out I'd previously miscounted and just assigning the output of printf to a variable is smaller than using -v.

EDIT2: Thanks to Digital Trauma in the comments, this is now down much more significantly. Tricks: using bash's regex support to replace the string of digits with zeroes instead of counting it then printing that number of zeroes (seems obvious when I put it like that...), storing the strings in a bash array to save an echo, and taking into account a slight change in rules that means you can end all input numbers in .0.

EDIT3: Added a byte to fix bug introduced by Digital Trauma's suggested modification.

\$\endgroup\$
  • \$\begingroup\$ Something like this for a score of 102: a=${3#*.};d=${a/./0};((${#a}-${#3}))||d=;r=(Trash Retina);echo ${r[$1$d**2+$2$d**2>90000*${3/./}**2]}! (you can replace the ; with newlines - I was just trying to get it in a comment). \$\endgroup\$ – Digital Trauma Jul 23 '15 at 1:09
  • \$\begingroup\$ And if you assume density whole-numbers will end in .0 (ok, I think) then you can score 82: a=${3#*.};d=${a/./0};r=(Trash Retina);echo ${r[$1$d**2+$2$d**2>90000*${3/./}**2]}! \$\endgroup\$ – Digital Trauma Jul 23 '15 at 1:14
  • \$\begingroup\$ The original example had some that didn't end with .0, but I notice the question has now been modified, so that'll save quite a bit. Thanks for the ideas! I especially like your idea for cutting down the two echo statements; I was trying to work out how to do that, for some reason it didn't occur to me to use an array! For some reason, it ALSO never occurred to me that you could use **... I'll try and verify your answer, I'm a little confused about how $d is working here but I'll figure it out. \$\endgroup\$ – Muzer Jul 23 '15 at 13:43
  • \$\begingroup\$ The $d is not too tricky. a contains the digits of the density after the decimal point. d=${a/./0} just replaces all those digits with zeros. Thus we can concat those zeros to the end of w and h to multiply by the same power of ten that is achieved by removing the decimal point from d. \$\endgroup\$ – Digital Trauma Jul 23 '15 at 17:37
  • 1
    \$\begingroup\$ Cheers, I thought I was going mad for a minute! \$\endgroup\$ – Muzer Jul 30 '15 at 9:50
4
\$\begingroup\$

dc, 41 bytes

[[Retina!]pq]sr?d*rd*+vr/300<r[Trash!]p

Requires args to be input in d, w, h order - I hope this is OK.

Test output:

$ for t in \
> "4.95 1920 1080" \
> "5.96 2560 1440" \
> "10.5 1920 1080" \
> "0.04 10 10" \
> "19 4096 2160" \
> "100.00 8192 4320" \
> "11.18 3000 1500" ; do \
> echo $t | dc -e'9k[[Retina!]pq]sr?d*rd*+vr/300<r[Trash!]p'
> done
Retina!
Retina!
Trash!
Retina!
Trash!
Trash!
Retina!
$ 
\$\endgroup\$
3
\$\begingroup\$

Julia, 46 45 42 bytes

f(w,h,d)=w^2+h^2>9e4d^2?"Retina!":"Trash!"

This creates a function that accepts three numeric values and returns a string.

It's a direct implementation of the formula, just rearranged a bit. Both sides of the inequality were multiplied by d then squared.

\$\endgroup\$
3
\$\begingroup\$

R, 59 55 Bytes

As an unnamed function now

function(h,w,d)if(h^2+w^2>9e4*d^2)'Retina!'else'Trash!'

Very simple implementation, that eliminates the need for the index references.

Previous

cat(if((n=scan()^2)[1]+n[2]>9e4*n[3])'Retina!'else'Trash!')

Fairly simple, get the input from scan into a vector (single line, space separated or multi-line). Square the vector. Do the calculation and cat the result.

\$\endgroup\$
3
\$\begingroup\$

MATLAB - 49 45 bytes

c={'Trash!','Retina!'};c{(w*w+h*h>9e4*d*d)+1}

I first had to declare a cell array which contains Trash! and Retina! which are stored in locations 1 and 2 in the cell array. Next, I use the observation observed by many to rearrange the equation so that you are checking for the condition only using integer arithmetic. I represented 90000 as 9e4 to save some bytes. If this condition is true, we output a 1, else we output a 0. I use this output to index directly into the cell array. Because MATLAB starts indexing at 1, I also had to add 1 to complete the indexing. What's nice is that adding true with 1 gives 2, while adding false with 1 gives 1. This will output either Trash! or Retina! in the MATLAB command prompt.

Example

>> w=1920;h=1080;d=4.95;
>> c={'Trash!','Retina!'};c{(w*w+h*h>9e4*d*d)+1}

ans =

Retina!
\$\endgroup\$
  • \$\begingroup\$ you do not need int8, true+1 is a double(2). \$\endgroup\$ – Jonas Jul 24 '15 at 11:47
  • \$\begingroup\$ @Jonas I tried that. It wouldn't change to 2 on MATLAB R2013a... Weird. \$\endgroup\$ – rayryeng Jul 24 '15 at 12:00
  • \$\begingroup\$ @Jonas - I had to encapsulate the logical expression in parentheses for it to work. Order of operations... d'oh. Thanks for the tip! \$\endgroup\$ – rayryeng Jul 24 '15 at 14:38
  • \$\begingroup\$ Nice approach! (I tried with hypot but your w*w+h*h>9e4*d*d is shorter). However, does this meet the question requirements? It's a program, not a function. So it should take w, h and d as inputs. I assume that means stdin, as usual in code challenge \$\endgroup\$ – Luis Mendo Jul 26 '15 at 3:04
  • \$\begingroup\$ @LuisMendo ah I'll have to change that! Thanks \$\endgroup\$ – rayryeng Jul 26 '15 at 3:13
3
\$\begingroup\$

XSLT, 400 bytes

This is the debut of a never-before seen language on PPCG, and I hope to use it more in the future as I get to know it more.

Code:

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="input"><xsl:variable name="n" select="for $i in tokenize(.,'[^\d\.]+')return number($i)" /><xsl:choose><xsl:when test="$n[1]*$n[1]+$n[2]*$n[2]>90000*$n[3]*$n[3]">Retina!</xsl:when><xsl:otherwise>Trash!</xsl:otherwise></xsl:choose></xsl:template></xsl:stylesheet>

Pretty Printed

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="input">
        <xsl:variable name="n" select="for $i in tokenize(.,'[^\d\.]+')return number($i)" />
        <xsl:choose>
            <xsl:when test="$n[1]*$n[1]+$n[2]*$n[2]>90000*$n[3]*$n[3]">
                Retina!
            </xsl:when>
            <xsl:otherwise>
                Trash!
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
</xsl:stylesheet>

Notes:

As XSLT has no way of taking input via STDIN, we have to use an XML file, with the input between two <input> tags. Of course, this method has its limitations but it will work perfectly well for most challenges.

Example I/O

Input file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<input>3000 1500 11.18</input> 

Output file:

<?xml version="1.0" encoding="UTF-8"?>Retina!

Input file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<input>1920 1080 10.5</input>

Output file:

<?xml version="1.0" encoding="UTF-8"?>Trash!
\$\endgroup\$
2
\$\begingroup\$

C# (81)

string D(int w,int h,double d){return Math.Sqrt(w*w+h*h)/d>300?"Retina":"Trash";}

Ungolfed:

string Density(int width, int height, double diagonal)
{
    return Math.Sqrt(width * width + height * height) / diagonal > 300 ? "Retina" : "Trash";
}
\$\endgroup\$
  • \$\begingroup\$ You can shorten this up to 73 with the trick several others have used to re-write the formula and remove the sqrt operation: string D(int w,int h,double d){return w*w+h*h>9e4*d*d?"Retina":"Trash";}. But you also need to add in the ! to each string so that would be back to 75 i think. \$\endgroup\$ – goric Jul 28 '15 at 15:51
2
\$\begingroup\$

Swift, 77 bytes

Function parameter declerations mean this takes up way more characters than it should:

func r(w:Float,h:Float,d:Float){print((w*w+h*h)>9e4*d*d ?"Retina!":"Trash!")}

\$\endgroup\$
2
\$\begingroup\$

Swift, 56 bytes

let r={print($0*$0+$1*$1>9e4*$2*$2 ?"Retina!":"Trash!")}

Basically the same as GoatInTheMachine's but with implicit closure parameters

When Code Golfing with Swift, always declare methods like this, it's much shorter

\$\endgroup\$
2
\$\begingroup\$

Haskell, 46

f w h d|w^2+h^2>d^2*9e4="Retina!"|0<1="Trash!"
\$\endgroup\$
  • \$\begingroup\$ What Haskell system should be used? It doesn't work with the version of ghci I tried, giving <interactive>:2:8: parse error on input '|'. \$\endgroup\$ – Ed Avis Jul 29 '15 at 13:31
  • \$\begingroup\$ @EdAvis: GHCi doesn’t run programs; to try it there, you need let f w h d|…. \$\endgroup\$ – Ry- Jul 29 '15 at 18:03
2
\$\begingroup\$

C++ 72 70 Byte

void F(int w,int h,float d){cout<<w*w+h*h>9e4*d*d?"Retina!":"Trash!";}

Similiar to other solutions, figured out myself to warm up with code golf.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can shave a couple by writing 90000 as 9e4 \$\endgroup\$ – Toby Speight Jul 23 '15 at 8:39
2
\$\begingroup\$

Here is my contribution for this problem

Ruby, 67 bytes reading from stdin

w,h,d=ARGV.map{|v|Float(v)}
puts w*w+h*h>d*d*9e4?"Retina!":"Trash!"

Ruby, 56 bytes in a function

A bit shorter

def r(w,h,d)
puts w*w+h*h>d*d*9e4?"Retina!":"Trash!"
end

Thanks to the previous contributors for the 9e4!

\$\endgroup\$
2
\$\begingroup\$

Bash, 85 bytes

if [ $(echo "sqrt($1^2+$2^2)/$3"|bc) -gt 300 ];then
echo Retina!
else
echo Trash!
fi
\$\endgroup\$
  • 1
    \$\begingroup\$ This doesn't work for "3000 1500 11.18". By default, bc's precision is 0 decimal places. You will need to set scale, or you can probably get away with bc -l which implicitly sets scale to 20 \$\endgroup\$ – Digital Trauma Jul 22 '15 at 16:21
  • \$\begingroup\$ Oh ok, I updated my answer. Thanks! \$\endgroup\$ – addison Jul 22 '15 at 16:25
  • 1
    \$\begingroup\$ Oh, and check out codegolf.stackexchange.com/questions/15279/…. For example ((`bc<<<"sqrt($1^2+$2^2)/$3"`>300))&&echo Retina!||echo Trash! \$\endgroup\$ – Digital Trauma Jul 22 '15 at 16:26
2
\$\begingroup\$

PHP, 47,43,40 38 bytes

<?=sqrt($w*$w+$h*$h)/$d>300?'Retina':'Trash'?>!
<?=sqrt($w*$w+$h*$h)/$d>300?Retina:Trash?>!
<?=$w*$w+$h*$h>9e4*$d*$d?Retina:Trash?>!

<?=hypot($w,$h)/$d>300?Retina:Trash?>!

Requires register_globals==true (which it should never be!), with GET values w,h,d
- Saved 4 bytes by removing quotes around string. Bad coding, but it works.
- Moved d and square root to the other side of the equation, saving the sqrt() function
- Saved 2 bytes by switching to hypot() (thank you Lucas Costa)

\$\endgroup\$
  • \$\begingroup\$ You should just say that this is a PHP4.1 answer and you don't need the register_globals directive to be set. \$\endgroup\$ – Ismael Miguel Jul 23 '15 at 12:06
  • 2
    \$\begingroup\$ Either option is just as bad haha \$\endgroup\$ – Martijn Jul 23 '15 at 12:22
2
\$\begingroup\$

C# 6, 67 Bytes

string D(int w,int h,double d)=>w*w+h*h>9e4*d*d?"Retina!":"Trash!";

This answer is based on Wolfsheads answer. I made it 8 bytes shorter using a new feature of C# 6.

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6) 58 54 43 Bytes

43 Bytes

Removed function assignment (as per PPCG rules) (-2), as well as remove square root and comparing to 900 (300^2) (-12)

(w,h,d)=>w*w+h*h/d*d>300?"Retina!":"Trash!"

54 Bytes

Got rid of unnessesary parentheses (-4 bytes)

a=(w,h,d)=>Math.sqrt(w*w+h*h)/d>300?"Retina!":"Trash!"

58 Bytes

a=(w,h,d)=>Math.sqrt((w*w)+(h*h))/d>300?"Retina!":"Trash!"

Explanation here:

a =                           // The function is a
 (w,h,d) =>                   // Accepts the three arguments
   Math.sqrt((w*w)+(h*h))/d   // Calculate pixel density
   > 300                      // Above the threshold
   ? "Retina!"                // If so, return "Retina!"
   : "Trash!"                 // Otherwise return "Trash!"

This uses ternary operators to test the density and kills a couple of bytes by using arrow functions

\$\endgroup\$
  • 1
    \$\begingroup\$ You can make this shorter by avoiding the square root, and comparing the squares of the values instead. \$\endgroup\$ – Reto Koradi Jul 25 '15 at 18:07
1
\$\begingroup\$

Java, 82 74 bytes

String g(int w,int h,double d){return 9e4*d*d>w*w+h*h?"Trash!":"Retina!";}

Call it with g(width,height,diagonal)

\$\endgroup\$
  • 1
    \$\begingroup\$ You can cut this down by getting rid of the costly double array like: String g(int w,int h,double x){return 9e4*x*x>w*w+h*h?"Trash!":"Retina!";} Sometimes simplest is best :) \$\endgroup\$ – Geobits Jul 22 '15 at 18:13
  • \$\begingroup\$ @Geobits Thanks, I hadn't took the time to budget out the bytes on the two approach, I'm glad you caught it though! \$\endgroup\$ – DeadChex Jul 22 '15 at 18:56
1
\$\begingroup\$

Clojure, 58 bytes

#(if(>(+(* %1%1)(* %2%2))(* %3%3 90000))"Retina!""Trash!")

Used @Kroltan's fancy math to shorten this. Uses implicit arguments passed in the order of (w, h, d).

First Clojure golf...I was surprised how much whitespace I am allowed to leave out

\$\endgroup\$

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.