9
\$\begingroup\$

Here is a relatively simple two dimensional array challenge.

Imagine a battlefield of 625 foot soldiers. You command the odd troops, but unfortunately the strength of the even troops overwhelms you. Thankfully, your soldiers have a secret power: If the power of each odd troop and the fellow odd allies surrounding them is divisible by a secret power number, they unleash their ultimate attack and win! You must honor each victorious soldier.

Rules

Given a 25 x 25 integer array where each element contains the product of its x and y position plus 1, return the coordinates of every "victorious" odd element that meets the following criteria:

The sum of the element's value and its adjacent odd elements (up, down, left, and right) is divisible by the input (secret power number). It must have elements adjacent to it on all four sides and not be on an edge.

Submissions can be either a function or full program that requires a single input. The output can be in any order.

Our 25 x 25 array, the battlefield, looks like this:

1, 1, 1, 1,...
1, 2, 3, 4,...
1, 3, 5, 7,...
1, 4, 7, 10,...
etc.

Example

Here is a 3 x 3 example:

43, 57, 71
46, 61, 76
49, 65, 81

To determine if an element (61, in the center) wins, we sum the values of it and the adjacent odd elements.

61 + 57 + 65 = 183

If the total is divisible by the input, the element's x and y position is printed. If our input is 3, because 183 is divisible by 3, "1, 1" is printed.

Output

If the input (secret power number) is 37, the elements returned (victorious soldiers to be commended) must be:

2, 18
3, 12
4, 9
5, 22
6, 6
8, 23
9, 4
10, 11
11, 10
12, 3
18, 2
22, 5
23, 8

If the input is 191, the elements returned must be:

10, 19
19, 10

An input of 3:

1, 2
1, 4
1, 6
1, 8
1, 10
1, 12
1, 14
1, 16
1, 18
1, 20
1, 22
2, 1
2, 3
2, 4
2, 5
2, 7
2, 9
2, 10
2, 11
2, 13
2, 15
2, 16
2, 17
2, 19
2, 21
2, 22
2, 23
3, 2
3, 4
3, 6
3, 8
3, 10
3, 12
3, 14
3, 16
3, 18
3, 20
3, 22
4, 1
4, 2
4, 3
4, 5
4, 7
4, 8
4, 9
4, 11
4, 13
4, 14
4, 15
4, 17
4, 19
4, 20
4, 21
4, 23
5, 2
5, 4
5, 6
5, 8
5, 10
5, 12
5, 14
5, 16
5, 18
5, 20
5, 22
6, 1
6, 3
6, 5
6, 7
6, 9
6, 11
6, 13
6, 15
6, 17
6, 19
6, 21
6, 23
7, 2
7, 4
7, 6
7, 8
7, 10
7, 12
7, 14
7, 16
7, 18
7, 20
7, 22
8, 1
8, 3
8, 4
8, 5
8, 7
8, 9
8, 10
8, 11
8, 13
8, 15
8, 16
8, 17
8, 19
8, 21
8, 22
8, 23
9, 2
9, 4
9, 6
9, 8
9, 10
9, 12
9, 14
9, 16
9, 18
9, 20
9, 22
10, 1
10, 2
10, 3
10, 5
10, 7
10, 8
10, 9
10, 11
10, 13
10, 14
10, 15
10, 17
10, 19
10, 20
10, 21
10, 23
11, 2
11, 4
11, 6
11, 8
11, 10
11, 12
11, 14
11, 16
11, 18
11, 20
11, 22
12, 1
12, 3
12, 5
12, 7
12, 9
12, 11
12, 13
12, 15
12, 17
12, 19
12, 21
12, 23
13, 2
13, 4
13, 6
13, 8
13, 10
13, 12
13, 14
13, 16
13, 18
13, 20
13, 22
14, 1
14, 3
14, 4
14, 5
14, 7
14, 9
14, 10
14, 11
14, 13
14, 15
14, 16
14, 17
14, 19
14, 21
14, 22
14, 23
15, 2
15, 4
15, 6
15, 8
15, 10
15, 12
15, 14
15, 16
15, 18
15, 20
15, 22
16, 1
16, 2
16, 3
16, 5
16, 7
16, 8
16, 9
16, 11
16, 13
16, 14
16, 15
16, 17
16, 19
16, 20
16, 21
16, 23
17, 2
17, 4
17, 6
17, 8
17, 10
17, 12
17, 14
17, 16
17, 18
17, 20
17, 22
18, 1
18, 3
18, 5
18, 7
18, 9
18, 11
18, 13
18, 15
18, 17
18, 19
18, 21
18, 23
19, 2
19, 4
19, 6
19, 8
19, 10
19, 12
19, 14
19, 16
19, 18
19, 20
19, 22
20, 1
20, 3
20, 4
20, 5
20, 7
20, 9
20, 10
20, 11
20, 13
20, 15
20, 16
20, 17
20, 19
20, 21
20, 22
20, 23
21, 2
21, 4
21, 6
21, 8
21, 10
21, 12
21, 14
21, 16
21, 18
21, 20
21, 22
22, 1
22, 2
22, 3
22, 5
22, 7
22, 8
22, 9
22, 11
22, 13
22, 14
22, 15
22, 17
22, 19
22, 20
22, 21
22, 23
23, 2
23, 4
23, 6
23, 8
23, 10
23, 12
23, 14
23, 16
23, 18
23, 20
23, 22

An input of 5:

1, 4
1, 14
2, 2
2, 4
2, 6
2, 7
2, 8
2, 10
2, 12
2, 14
2, 16
2, 17
2, 18
2, 20
2, 22
3, 8
3, 18
4, 1
4, 2
4, 4
4, 6
4, 8
4, 10
4, 11
4, 12
4, 14
4, 16
4, 18
4, 20
4, 21
4, 22
6, 2
6, 4
6, 6
6, 8
6, 9
6, 10
6, 12
6, 14
6, 16
6, 18
6, 19
6, 20
6, 22
7, 2
7, 12
7, 22
8, 2
8, 3
8, 4
8, 6
8, 8
8, 10
8, 12
8, 13
8, 14
8, 16
8, 18
8, 20
8, 22
8, 23
9, 6
9, 16
10, 2
10, 4
10, 6
10, 8
10, 10
10, 12
10, 14
10, 16
10, 18
10, 20
10, 22
11, 4
11, 14
12, 2
12, 4
12, 6
12, 7
12, 8
12, 10
12, 12
12, 14
12, 16
12, 17
12, 18
12, 20
12, 22
13, 8
13, 18
14, 1
14, 2
14, 4
14, 6
14, 8
14, 10
14, 11
14, 12
14, 14
14, 16
14, 18
14, 20
14, 21
14, 22
16, 2
16, 4
16, 6
16, 8
16, 9
16, 10
16, 12
16, 14
16, 16
16, 18
16, 19
16, 20
16, 22
17, 2
17, 12
17, 22
18, 2
18, 3
18, 4
18, 6
18, 8
18, 10
18, 12
18, 13
18, 14
18, 16
18, 18
18, 20
18, 22
18, 23
19, 6
19, 16
20, 2
20, 4
20, 6
20, 8
20, 10
20, 12
20, 14
20, 16
20, 18
20, 20
20, 22
21, 4
21, 14
22, 2
22, 4
22, 6
22, 7
22, 8
22, 10
22, 12
22, 14
22, 16
22, 17
22, 18
22, 20
22, 22
23, 8
23, 18

This is , the lowest byte-count code without using standard loopholes is the winner.

As this is my first submission, any advice is greatly appreciated. Thanks!

\$\endgroup\$
  • \$\begingroup\$ I'm a tad confused about Given a 25 x 25 integer array. Should our submissions generate this list or is it actually given on input? \$\endgroup\$ – Mr. Xcoder Aug 14 at 8:14
  • \$\begingroup\$ Only the output matters. Generating the actual array is optional. \$\endgroup\$ – Pandazoic Aug 14 at 8:15
  • \$\begingroup\$ "Imagine a battlefield of 625 foot soldiers" Right. That's tall. O_o \$\endgroup\$ – Chronocidal Aug 14 at 17:05
3
\$\begingroup\$

JavaScript (ES6),  83 81 80  76 bytes

The output is a space-delimited string of coordinates \$x,y\$.

f=(n,x=y=23,k=5,v=x*y)=>y?(v&1|~v*k%n?[]:[x,y]+' ')+f(n,--x||23|!y--,k^6):[]

Try it online!

How?

Let \$c_{x,y}=xy+1\$ be the value of the cell located at \$(x,y)\$.

Since the cells on the edge are not accounted, we assume \$0<x<24\$ and \$0<y<24\$.

\$c_{x,y}\$ is odd if either \$x\$ or \$y\$ is even (or both of them).

If \$x\$ is odd, then \$c_{x-1,y}\$ and \$c_{x+1,y}\$ are odd. But in that case, \$y\$ must be even, and so are \$c_{x,y-1}\$ and \$c_{x,y+1}\$. The sum of the cell and its odd surrounding neighbors is:

$$s_{x,y}=c_{x,y}+c_{x-1,y}+c_{x+1,y}=3c_{x,y}$$

Similarly, if \$y\$ is odd:

$$s_{x,y}=c_{x,y}+c_{x,y-1}+c_{x,y+1}=3c_{x,y}$$

If both \$x\$ and \$y\$ are even, all surrounding neighbors are odd:

$$s_{x,y}=c_{x,y}+c_{x-1,y}+c_{x+1,y}+c_{x,y-1}+c_{x,y+1}=5c_{x,y}$$

This multiplier (\$3\$ or \$5\$) is named \$k\$ in the JS code.

Commented

f = (                // f is a recursive function taking:
  n,                 //   n      = input
  x = y = 23,        //   (x, y) = current coordinates, starting at (23, 23)
  k = 5,             //   k      = multiplier (3 or 5)
  v = x * y          //   v      = x * y (value of the current cell - 1)
) =>                 //
  y ?                // if y is greater than 0:
    ( v & 1 |        //   if v is odd (meaning that v + 1 is not)
      ~v * k % n ?   //   or n is not a divisor of -(v + 1) * k:
        []           //     append nothing
      :              //   else:
        [x, y] + ' ' //     append the coordinates followed by a space
    ) +              //
    f(               //   append the result of a recursive call:
      n,             //     pass n unchanged
      --x ||         //     decrement x; if the result is 0:
        23 | !y--,   //       pass 23 instead and decrement y
      k ^ 6          //     update k (5 -> 3 -> 5 -> ...)
    )                //   end of recursive call
  :                  // else:
    []               //   stop recursion
\$\endgroup\$
  • \$\begingroup\$ Thanks for the insightful explanation. Clever using a bitwise XOR to flip between the multipliers. \$\endgroup\$ – Pandazoic Aug 15 at 4:26
3
\$\begingroup\$

C# (Visual C# Interactive Compiler), 97 93 91 90 bytes

x=>{for(int i=0,d=0,j;++i<24;d=5)for(j=0;++j<24;d^=6)if(i*j%2+(i*j+1)*d%x<1)Print((i,j));}

Saved 6 bytes thanks to @Kevin Cruijssen!

Try it online!

\$\endgroup\$
  • \$\begingroup\$ int i=0; ... int j=0; can be int i=0,j; ... j=0; and <1& can be + for -4 bytes. \$\endgroup\$ – Kevin Cruijssen Aug 14 at 12:43
  • \$\begingroup\$ @KevinCruijssen For the int declarations, originally I had it that way until I changed my function to a different method, then reverted it. The + one is nice though, thanks \$\endgroup\$ – Embodiment of Ignorance Aug 14 at 12:45
  • \$\begingroup\$ Another -2 by replacing ((i+j)%2>0?3:5) with (5-(i+j)%2*2) \$\endgroup\$ – Kevin Cruijssen Aug 14 at 12:50
2
\$\begingroup\$

Stax, 25 bytes

âÖÅ{┼îÄï$εS╢,σδXú(Γ°#↑√nG

Run and debug it

This is mostly just brute force, with one marginally clever observation. All odd troops have either 2 or 4 odd neighbors. And the total sum of these plus the original solder is either 3p or 5p where p is the soldier's power. The coefficient (3 or 5) can be determined by gcd(2, x, y) * 2 + 1) where x and y are the soldier's coordinates.

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

Python 2, 83 bytes

lambda n:[(x,y)for x in R for y in R if~(x*y)*[5,3][x+y&1]%n<1>x*y%2]
R=range(1,24)

Try it online!

Thanks to Arnauld for saving a byte.

\$\endgroup\$
  • \$\begingroup\$ Ah, basically the same program to my prepared js answer... \$\endgroup\$ – tsh Aug 14 at 7:53
1
\$\begingroup\$

JavaScript (SpiderMonkey), 69 bytes

x=>{for(i=24;--i;)for(j=24,d=5;--j;d^=6)i*j%2+~(i*j)*d%x||print(j,i)}

Try it online!

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

Jelly, 22 bytes

23×þ`‘µḤḤÐeÐe+×Ḃ³ḍaƊŒṪ

Try it online!

A full program that takes a single argument, the secret power number, and implicitly prints a list of [x, y] pairs. Uses the observation others have made about multiples of 3 and 5.

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

Wolfram Language (Mathematica), 60 58 bytes

Position[If[2∣+##,5,3](1+1##)&~Array~{23,23},a_/;#∣a]&

Try it online!

Looked around after posting and realized I really needed to turn my brain on...

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