16
\$\begingroup\$

You're with your best bud, Jim, at the amusement park and as your favorite ride comes into view, you and Jim exchange glances. Naturally you race to get in line. Unfortunately he wins because you're a golfer and he plays a real sport (sorry friends). In fact, you're so far behind that you and Jim are separated by x people. Assuming the line is length n and you're at the back and the line zigs and zags every j people, at what positions in the line will you and Jim be in the same column allowing you to chat (only one row apart)?

Input

3 Integers

  • n - The length of the line. This number will always be greater than or equal to j and will be in the form y * j where y is a positive integer (the number of rows in the queue).
  • j - The number of people in one row of the line (the number of columns in one row). This number will always be greater than 0.
  • x - The number of people between you and Jim such that 0 <= x < 2j - 1. Hint: If this number is odd, then your output should be empty.

Output

A list of integer positions in the line at which Jim is in the same column as you.
1 These integers can be 0 or 1-indexed as long as you specify in your answer.
2 These integers can assume you start at position 0 or position n-1 as long as you specify in your answer.

Example

Queueing Example Small
In this example, the input would be n = 9, j = 3, x = 0. The output should be 2, 5 because your position is 2 or 5 when you're in the same column as Jim

Test Cases

[9, 3, 0] -> [2, 5]
[12, 3, 0] -> [2, 5, 8]
[9, 3, 1] -> []
[9, 3, 2] -> [1, 4]
[14, 7, 10] -> [1]
[24, 4, 6] -> [0, 4, 8, 12, 16]

Scoring

This is , so the shortest answer (in bytes) wins.

\$\endgroup\$
  • 4
    \$\begingroup\$ I'm queuerious to see some interesting solutions to this! (sorry bad pun and also improper use of the word but whatever, don't judge me :P) \$\endgroup\$ – HyperNeutrino Mar 27 '17 at 13:16
  • 1
    \$\begingroup\$ Can we return a falsy value instead of an empty array? \$\endgroup\$ – Rɪᴋᴇʀ Mar 27 '17 at 13:41
  • \$\begingroup\$ @Riker I see no reason to disallow that. Go for it \$\endgroup\$ – Poke Mar 27 '17 at 13:57
9
\$\begingroup\$

Python 2, 45 41 40 37 bytes

lambda n,j,x:range(j-x/2,x%2or n-x,j)

Pretty much the trivial solution. I just quickly scanned for patterns and found a pattern. 1-indexed, 1 is at the back of the queue.

-4 bytes by avoiding ternaries and using an array for the values instead
-1 byte thanks to some inspiration from @DeadPossum, by using and instead of ternaries or array selectors
-3 bytes by switching to or in the opposite order. Only works because of 1-indexing

Also, crossed out 4 is still 4 on all of the 4s :(

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Lol, you posted just 14 minutes, before me :) My version is shorter by 3 bytes: lambda n,j,x:x%2-1and range(j-x/2,n-j+1,j) \$\endgroup\$ – Dead Possum Mar 27 '17 at 13:47
  • \$\begingroup\$ @DeadPossum Nice. I have another solution that's one byte shorter than that, lol \$\endgroup\$ – HyperNeutrino Mar 27 '17 at 13:48
  • \$\begingroup\$ I had a mistake in second argument of range. It shouldn't be n-j+1, so lambda n,j,x:x%2-1and range(j-x/2,n-x,j) is shorter by one more in total of 40 bytes \$\endgroup\$ – Dead Possum Mar 27 '17 at 13:53
  • \$\begingroup\$ @DeadPossum Wait, were we allowed to return a falsy value instead of an empty array? \$\endgroup\$ – HyperNeutrino Mar 27 '17 at 13:55
  • 3
    \$\begingroup\$ @FelipeNardiBatista that's what "1-indexed" means, btw. \$\endgroup\$ – Rɪᴋᴇʀ Mar 27 '17 at 14:14
2
\$\begingroup\$

Pip, 22 21 bytes

20 bytes of code, +1 for -p flag.

c%2?lv-c/2+b*\,a/b-1

Takes n, j, and x as command-line arguments. 0-indexed, starting at position 0. Try it online!

Explanation

This is my original 22-byte version because it's a bit more understandable.

                       a, b, c are cmdline args; l is [] (implicit)
c%2?                   Test c mod 2
    l                  If it's 1 (truthy), return empty list; else:
                a/b-1  Number of rows in the queue minus 1
               ,       Range(^)
             b*        Multiply each element by b
     b-1-c/2+          Add (b-1)-c/2 to each element
                       Output in [1;2;3] format (implicit, -p flag)

The formula was obtained by observing the pattern for n=9, j=3:

x Output
0 [2;5]
2 [1;4]
4 [0;3]

If we take x/2 (0,1,2), subtract it from j-1 (2,1,0), and add that to [0;3], we get the correct result in all cases.

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

Java 8 lambda, 101 bytes

(n,j,x)->{int[]i=new int[n/j-1];int c=0,k=j-x/2;for(;k<n-x;k+=j)i[c++]=k;return x/2==0?i:new int[0];}

Almost-direct port of my Python answer. Range doesn't exist in Java though.

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

Haskell, 43 bytes

(n#j)x|odd$round x=[]|m<-j-x/2=[m,m+j..n-x]

Pretty much directly ported from the Python answer from HyperNeutrino

More well-formatted code:

f n j x |odd$round x = []
        |otherwise   = let m=j-x/2 in [m,m+j..n-x]

EDIT: Forgot to mention it was one-indexed

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

C# - 91 bytes

int[]r=new int[n/j-1];for(int i=1;i<n/j;i++){r[i-1]=i*j-x/2-1;}return(x%2==0)?r:new int[0];
\$\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.