15
\$\begingroup\$

Task

Given a representation of a line, output the number of quadrants that that line passes through.

Valid Representations of a Line

You can represent a line as

  • Three signed integers A, B, and C which share no common factor and where A and B are not both zero, representing the line Ax + By = C,
  • Four signed integers X1, Y1, X2, and Y2, representing the line passing through the points (X1, Y1) and (X2, Y2), or
  • A data type that describes a line, if your language has one (it must support vertical lines).

You may not take input in any format that does not allow for a vertical line (e.g. slope-intercept form). If you choose to take integers as input, you can assume that they lie in the inclusive range [-127, 128].

Specifications

  • The output will always be 0, 2, or 3 (a line can never pass through all four quadrants, nor can it pass through only a single one).
  • A line on an axis is considered not to pass through any quadrants. A line through the origin is considered to only pass through 2 quadrants.
  • You do not have to return which quadrants are being passed through (though the test cases include them for clarity).
  • This is , so the shortest valid answer (measured in bytes) wins.

Test Cases

You will have to convert these to a suitable format before using them.

1x + 1y = 1   ->  3  (quadrants I, II, and IV)
-2x + 3y = 1  ->  3  (quadrants I, II, and III)
2x + -3y = 0  ->  2  (quadrants III and I)
1x + 1y = 0   ->  2  (quadrants II and IV)
3x + 0y = 6   ->  2  (quadrants I and IV)
-3x + 0y = 5  ->  2  (quadrants II and III)
0x + -8y = 4  ->  2  (quadrants III and IV)
0x + 1y = 0   ->  0  (lies on the x-axis)
1x + 0y = 0   ->  0  (lies on the y-axis)
\$\endgroup\$
1
  • 1
    \$\begingroup\$ They should teach the tactic we all borrowed from Leaky Nun in school, if there was a need for it. \$\endgroup\$
    – mbomb007
    Commented Nov 20, 2017 at 22:16

14 Answers 14

22
\$\begingroup\$

Python 3, 24 bytes

lambda a:3<<a.count(0)&3

Try it online!

\$\endgroup\$
5
  • 3
    \$\begingroup\$ ... wow. This is more trivial than I thought. \$\endgroup\$ Commented Nov 19, 2017 at 21:23
  • \$\begingroup\$ You could maybe use a string instead of a list if the I/O permits it. \$\endgroup\$ Commented Nov 19, 2017 at 21:24
  • \$\begingroup\$ Would using '320'[a.count(0)] and returning the value in string form be acceptable? \$\endgroup\$
    – FlipTack
    Commented Nov 19, 2017 at 21:47
  • 2
    \$\begingroup\$ And wow, looks like all answers will now be "based off Leaky's" \$\endgroup\$
    – FlipTack
    Commented Nov 19, 2017 at 21:49
  • 3
    \$\begingroup\$ @FlipTack bithacks win :P \$\endgroup\$
    – Leaky Nun
    Commented Nov 19, 2017 at 21:51
3
\$\begingroup\$

Jelly, 5 bytes

TL’ȧ$

Try it online!

  • -1 byte thanks to Challenger5
  • -1 byte thanks to Leaky Nun
  • -2 bytes thanks to H.PWiz

No longer based off Leaky's answer!

\$\endgroup\$
8
  • \$\begingroup\$ ċ0ị2,0,3 saves a byte \$\endgroup\$ Commented Nov 19, 2017 at 21:29
  • \$\begingroup\$ @Challenger5 Huh, so it does. Thanks! \$\endgroup\$ Commented Nov 19, 2017 at 21:30
  • 1
    \$\begingroup\$ 7 bytes \$\endgroup\$
    – Leaky Nun
    Commented Nov 19, 2017 at 21:53
  • 1
    \$\begingroup\$ How about TL’ȧ$. Don't know Jelly, so this might be golfable \$\endgroup\$
    – H.PWiz
    Commented Nov 19, 2017 at 21:58
  • \$\begingroup\$ @H.PWiz Very nice! I don't think that can be golfed, but I may be wrong. \$\endgroup\$ Commented Nov 19, 2017 at 22:03
3
\$\begingroup\$

Javascript (ES6), 30 24 22 bytes

This is my first time trying to golf in Javascript. There's gotta be a better way to count zeros...

(a,b,c)=>3<<!a+!b+!c&3

-6 bytes thanks to Herman Lauenstein, -2 bytes to remembering operator precedences.

Alternate 24-bytes solution to return a string instead:

(a,b,c)=>"320"[!a+!b+!c]
\$\endgroup\$
3
  • 1
    \$\begingroup\$ That's actually fairly clever... \$\endgroup\$ Commented Nov 19, 2017 at 22:08
  • 1
    \$\begingroup\$ 24 bytes by not using an array (a,b,c)=>3<<(!a+!b+!c)&3 \$\endgroup\$
    – Endenite
    Commented Nov 20, 2017 at 16:42
  • \$\begingroup\$ Looks like I can't golf mine to not use an array anymore... \$\endgroup\$
    – ericw31415
    Commented Nov 20, 2017 at 22:04
2
\$\begingroup\$

05AB1E, 6 bytes

Ƶܹ0¢è

Try it online!

Based on Leaky Nun's answer.

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

SOGL V0.12, 8 bytes

0233{.‽X

Try it Here!

Based off Leaky Nun's answer.

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

GolfScript, 16 14 bytes

~{!!}%{+}*.1>*

Try it online!

  • @Challenger5 -2 bytes

This program takes an array of 3 integers representing the coefficients in the equation Ax + By = C

Example Input/Output

[1 1 1]   -> 3
[-2 3 1]  -> 3

How it Works

~                       - Eval string (input)
  {  }%                 - Map to array
   !!                   - Double not (equivalent to != 0)
        {+}*            - total array (fold addition)
            .           - Duplicate top of stack
             1>         - Greater than 1?
               *        - Multiply     

This was a little tricky at first for me to figure out a mathematical way to calculate this. However there are only 8 possible configurations such that a != 0 & b != 0 & c != 0

0 0 0 = 0
a 0 0 = 0
0 b 0 = 0
0 0 c = 0
a 0 c = 2
0 b c = 2
a b 0 = 2
a b c = 3

I eventually came to the following function.

F(a,b,c) {
    var r = sign(a)+sign(b)+sign(c);
    if(r > 1)
        r;
    else
        return 0;
}

and the whole thing can be condensed to a single math problem

F(a,b,c) {
    return (sign(a)+sign(b)+sign(c)) * (sign(a)+sign(b)+sign(c) > 1);
}
\$\endgroup\$
4
  • \$\begingroup\$ I think you can use {!!}% instead of [{!!}/]. \$\endgroup\$ Commented Nov 20, 2017 at 2:00
  • \$\begingroup\$ CJam translation of this submission is {:!:!:+_1>*}. \$\endgroup\$ Commented Nov 20, 2017 at 2:02
  • \$\begingroup\$ @Challenger5 lol, How did I not realize that. Also nice port, I just have to learn how to read it now. \$\endgroup\$
    – Marcos
    Commented Nov 20, 2017 at 2:40
  • \$\begingroup\$ Significant differences in this case are 1) shorthand for mapping (:! is equivalent to {!}%), 2) shorthand for reducing (:+ is equivalent to {+}*), 3) that . is changed to _ (because CJam has floats), and 4) that CJam does not have input on the stack by default, meaning that you wrap the code in {} to make it a function. \$\endgroup\$ Commented Nov 20, 2017 at 4:05
2
\$\begingroup\$

Retina, 13 bytes

M`\b0
T`d`320

Try it online

Also based on Leaky Nun's answer.

\$\endgroup\$
1
  • \$\begingroup\$ This doesn't work if the input contains 10 for example. The first regex would need to be \b0. \$\endgroup\$ Commented Nov 22, 2017 at 12:43
1
\$\begingroup\$

JavaScript, 25 bytes

_=>3<<!_[0]+!_[1]+!_[2]&3

Based off Leaky Nun's answer.

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

Haskell, 22 bytes

f l="320"!!sum[1|0<-l]

Try it online!

Point-free solution, 27 bytes

("320"!!).(\l->sum[1|0<-l])

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I was about to suggest that change... beat me to it \$\endgroup\$ Commented Nov 19, 2017 at 23:50
1
\$\begingroup\$

Perl 6, 18 bytes

{3+<@_.grep(0)+&3}
\$\endgroup\$
1
\$\begingroup\$

ABCR, 30 bytes

Input is in the form A,B,C where the commas can be replaced by any non-numeric, non-- character.

BBi7baxci7baxci7bax@7)A7(xxo

No online interpreter yet, but here's an explanation:

BB                                Add two values to the B queue. (Values are unimportant)
  i7 ax                           Read in a number.  If it's non-zero...
    b                             Dequeue one item from the B queue.
       c                          Read in the delimiter...
        i                         ... And promptly overwrite it with the next number.
         7baxci7bax               Repeat the whole "if 0, dequeue from B" for the
                                     other two input numbers.
                   @              Get the current length of the B queue. [2, 1, or 0]
                    7             If the length isn't 0...
                     )            ... Increment it to our required [3,2,0]
                      A           ... And enqueue it to A.
                                  (We don't need to add to A otherwise, because it defaults
                                    to 0 already if there's no value in it.
                                    I used that to exit the queue with 7_ax earlier.)
                       7(xx       Set the register to 0 to exit from loop.
                           o      Peek A and print as a number.
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 14 11 bytes

0⌈3-×⍨+/0=⎕

Try it online!

⎕IO is 0. Thanks to @Adám for -3 bytes!

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

Deorst, 12 bytes

l0EN))A:k?Z+

Try it online!

Somewhat based off Leaky's answer; uses the same premise, but a different mapping method.

How it works

Deorst has a count occurrences builtin, but doesn't (for some reason) have an indexing command, so I had to create the following mapping, where the left is a.count(0) and the right is the wanted result

0 -> 3
1 -> 2
2 -> 0

The program itself works like this (example input of [1,1,1])

l0           - Push 0;     STACK = [[1 1 1] 0]
  EN         - Count;      STACK = [0]
    ))       - Subtract 2; STACK = [-2]
      A      - Absolute;   STACK = [2]
       :     - Duplicate;  STACK = [2 2]
        k?Z  - Positive?;  STACK = [2 1]
           + - Sum;        STACK = [3]
\$\endgroup\$
0
\$\begingroup\$

Add++, 23 bytes

D,f,@@@,!$!@!s2$_|d0$>+

Try it online!

Based off both my Deorst answer and Leaky's Python answer

How it works

D,f,@@@,  - Create a triadic function. 
            Example arguments;   [1 1 1]
        ! - Logical NOT; STACK = [1 1 0]
        $ - Swap;        STACK = [1 0 1]
        ! - Logical NOT; STACK = [1 0 0]
        @ - Reverse;     STACK = [0 0 1]
        ! - Logical NOT; STACK = [0 0 0]
        s - Sum;         STACK = [0]
        2 - Push 2;      STACK = [0 2]
        $ - Swap;        STACK = [2 0]
        _ - Subtract;    STACK = [-2]
        | - Absolute;    STACK = [2]
        d - Duplicate;   STACK = [2 2]
        0 - Push 0;      STACK = [2 2 0]
        $ - Swap;        STACK = [2 0 2]
        > - Greater to;  STACK = [2 1]
        + - Sum;         STACK = [3]

However, I think I've been using functions too much in Add++, rather than the main code body. So I attempted to do this using both functions, and the code body, and resulted in a much nicer 50 byte piece (yes, that is the longest answer here):

# Example input: 1 1 1;
# x and y are the accumulators

D,f,@@@,!$!@!s # Count the 0s
$f>?>?>?       # Call f with the input.
-2   # Subtract 2;    x: -2;  y: 0
^2   # Square;        x: 4;   y: 0
S    # Square root;   x: 2.0; y: 0
\1   # To integer;    x: 2;   y: 0
y:x  # Assign x to y; x: 2;   y: 2
}    # Switch to y;   x: 2;   y: 2
>0   # Is positive?;  x: 2;   y: 1
}    # Switch to x;   x: 2;   y: 1
+y   # Add y to x;    x: 3;   y: 1
O    # Print x

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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