22
\$\begingroup\$

This challenge requires a small amount of knowledge about . A description of the details required can be found at the bottom if you are not familiar with chess or want a refresher.

For a certain board configuration consisting only of queens we can say that each queens threat number is the number of other queens it threatens.

Here is an example board with each queen labeled by their threat number:

. . . . . . . .
. . . . . . . .
. . . 2 . . . .
. . . . . . . .
. . . . . . . .
. 2 . 3 . . 2 .
. 1 . . . . . .
. . . . . . . 0

A board is at a peaceful standoff if every queen can only attack other queens with the same threat number as themselves.

For example:

. . . . . . . .
. . . . . . . .
. 2 . 2 . . . .
. . . . . . . .
. . . . . . . .
. 2 . 2 . . . .
. . . . . . . .
. . . . . . . .

Each queen can attack 2 others so it's a peaceful standoff.

As another example:

3 . . . . . . 3
. . . . . . . .
. 1 . . . . 1 .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
3 . . . . . . 3

Here not all queens have the same threat number. Some threaten 3 others and some only threaten 1 other. However none of the 3s threaten a 1 (or vice versa) so it's peaceful.

Task

You will take as input a chessboard and output whether it is a peaceful standoff. You may assume the input board is always the standard chess size of 8 units by 8 units and you may take it as a list of list of booleans, a list of piece locations or any other reasonable format.

You may not require the threat numbers to be pre-calculated in the input. They are shown in the above examples but they are not a part of the input.

You should output one of two consistent distinct values. One when the input is a peaceful standoff and the other when it is not.

This is so the goal is to minimize the size of your source code as measured in bytes.

Test cases

False

. . . . . . . .
. . . . . . . .
. . . Q . . . .
. . . . . . . .
. . . . . . . .
. Q . Q . . Q .
. Q . . . . . .
. . . . . . . Q

. . . . . . . .
. Q . Q . Q . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .

True

. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .

. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . Q . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .

. . . . . . . .
. . . Q . . . .
. . . . . . . .
. . . . . . . .
. Q . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .


. . . . . . . .
. . . . . . . .
. . Q Q . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .

. . . . . . . .
. . . . . . . .
. Q . Q . . . .
. . . . . . . .
. . . . . . . .
. Q . Q . . . .
. . . . . . . .
. . . . . . . .

Q . . . . . . Q
. . . . . . . .
. Q . . . . Q .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
Q . . . . . . Q

Q . . . . . . Q
. . Q . . Q . .
. Q . . Q . Q .
. . Q . . . . .
. . . . . Q . .
. Q . Q . . Q .
. . Q . . Q . .
Q . . . . . . Q

Chess

Chess is a game played on an 8 by 8 square grid. Each location on the grid can have up to 1 piece. One such piece is the "queen". Queens can attack any piece that's located in the same row, column, or diagonal as they are, so long as there's not another piece between them an their target. If a piece can attack a piece it is considered to "threaten" that piece.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Can I take input as a 1d list of 64 values? Or a string of length 64? \$\endgroup\$
    – Aiden Chow
    Oct 19, 2022 at 3:43
  • \$\begingroup\$ @AidenChow - "... you may take it as a list of list of booleans, a list of piece locations or any other reasonable format." \$\endgroup\$
    – ysap
    Oct 22, 2022 at 16:12

9 Answers 9

8
\$\begingroup\$

JavaScript (ES6), 157 bytes

Expects a binary matrix. Returns an inverted Boolean value.

m=>(F=p=>m.some((r,y)=>r.some((v,x)=>!p*([s=0,..."1235678"].map(d=>(g=X=>((X+=d%3-1)|(Y+=~-(d/3)))&8?0:v*(V=m[Y][X])?s+=p|V!=v:g(X))(x,Y=y)),r[x]=s))))(F(1))

Try it online!

How?

This code solves the puzzle in two passes:

  • When called with \$p=1\$, \$F\$ computes the number of queens that interact with each queen, updates the matrix accordingly and returns \$\text{false}\$.

  • When called with \$p=\text{false}\$, \$F\$ tests whether at least one queen interacts with a queen that interacts with a different number of queens.

Commented

Main wrapper

m => F(F(1))         // call F(1), then call F(false)

Helper function F

F = p =>             // p = pass
m.some((r, y) =>     // for each row r[] at position y in m[]:
  r.some((v, x) =>   //   for each value v at position x in r[]:
    !p * (           //     do not trigger some() during the 1st pass
      [ s = 0,       //     initialize s to 0 and build the list
        ..."1235678" //     [ 0..3, 5..8 ]
      ].map(d =>     //     for each direction d in this list:
        g(x, Y = y)  //       invoke g with (X, Y) = (x, y)
      ),             //     end of map()
      r[x] = s       //     update m[y][x] to s
    )                //
  )                  //   end of inner some()
)                    // end of outer some()

Helper function g

g = X =>             // X is passed explicitly,
                     // Y is passed implicitly
(                    //
  (X += d % 3 - 1) | // add dx = (d mod 3) - 1 to X
  (Y += ~-(d / 3))   // add dy = floor(d / 3) - 1 to Y
)                    //
& 8 ?                // if the resulting position is out of bounds:
  0                  //   stop the recursion
:                    // else:
  v *                //   force the test to fail if there's no queen
                     //   on the source square
  (V = m[Y][X])      //   let V be the value stored at (X, Y)
  ?                  //   if there's a queen there:
    s +=             //     increment s if:
      p |            //       this is the first pass
      V != v         //       or V is not equal to v
  :                  //   else:
    g(X)             //     keep testing this ray
\$\endgroup\$
3
\$\begingroup\$

Python3, 301 bytes:

lambda b:all(all(len([*t(b,x,y)])==len([*t(b,X,Y)])for X,Y in t(b,x,y))for x in R(8)for y in R(8)if b[x][y])
R=range
def t(b,x,y):
 q=[(x,y,X,Y)for X in[-1,0,1]for Y in[-1,0,1]if X or Y]
 while q:
  x,y,X,Y=q.pop(0)
  A,B=x+X,y+Y
  if 0<=A<8 and 0<=B<8:
   if b[A][B]:yield(A,B)
   else:q+=[(A,B,X,Y)]

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ you can remove the parentheses around A,B in the yield statement for -1 byte \$\endgroup\$
    – c--
    Oct 21, 2022 at 18:26
  • \$\begingroup\$ you can probably get rid of the if X or Y counting an extra threat for every queen, otherwise if X|Y does the trick \$\endgroup\$
    – c--
    Oct 22, 2022 at 16:09
3
\$\begingroup\$

J, 82 71 bytes

[:(-:|:)@(*+/)@(=[:+/@(*]=&|[:<./"1|+_*0&=)]*"2(%|)=/~(%|)@1j1^i.@8)-/~

Try it online!

Takes input as a list of complex numbers.

We'll consider this example:

3 . . . . . . 3
. . . . . . . .
. 1 . . . . 1 .
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . . . . . . .
3 . . . . . . 3

which in complex numbers is 0j0 0j7 7j0 7j7 2j1 2j6.

  • -/~ Table of differences:

      0 0j_7   _7 _7j_7 _2j_1 _2j_6
    0j7    0 _7j7    _7  _2j6  _2j1
      7 7j_7    0  0j_7  5j_1  5j_6
    7j7    7  0j7     0   5j6   5j1
    2j1 2j_6 _5j1 _5j_6     0  0j_5
    2j6 2j_1 _5j6 _5j_1   0j5     0
    
  • (%|) Each of these differences divided by their magnitude, ie, as unit lengths. Pieces can attack each other only if these normalized vectors are equal to one of the 8 evenly spaced unit vectors:

    unit vectors

  • =/~(%|)@1j1^i.@8 So, create a 3d table showing where each of the normalized vectors of the difference table is equal to each of the 8 "compass" unit vectors:

    0 0 0 0 0 0
    0 0 0 0 0 0
    1 0 0 0 0 0   Differences that point E
    0 1 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0  NE
    1 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    
    0 0 0 0 0 0
    1 0 0 0 0 0
    0 0 0 0 0 0  N
    0 0 1 0 0 0
    0 0 0 0 0 0
    0 0 0 0 1 0
    
    0 0 0 0 0 0
    0 0 1 0 0 0
    0 0 0 0 0 0  NW
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    
    0 0 1 0 0 0
    0 0 0 1 0 0
    0 0 0 0 0 0  W
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    
    0 0 0 1 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0  SW
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    
    0 1 0 0 0 0
    0 0 0 0 0 0
    0 0 0 1 0 0  S
    0 0 0 0 0 0
    0 0 0 0 0 1
    0 0 0 0 0 0
    
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 1 0 0 0 0  SE
    0 0 0 0 0 0
    0 0 0 0 0 0
    0 0 0 0 0 0
    
  • ]*"2 Use these as as filters for the original input:

    0    0    0     0   0    0
    0    0    0     0   0    0
    7    0    0     0   0    0
    0    7    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    
    0    0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    7j7  0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    
    0    0    0     0   0    0
    0j7  0    0     0   0    0
    0    0    0     0   0    0
    0    0  0j7     0   0    0
    0    0    0     0   0    0
    0    0    0     0 0j5    0
    
    0    0    0     0   0    0
    0    0 _7j7     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    
    0    0   _7     0   0    0
    0    0    0    _7   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    
    0    0    0 _7j_7   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    
    0 0j_7    0     0   0    0
    0    0    0     0   0    0
    0    0    0  0j_7   0    0
    0    0    0     0   0    0
    0    0    0     0   0 0j_5
    0    0    0     0   0    0
    
    0    0    0     0   0    0
    0    0    0     0   0    0
    0 7j_7    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    0    0    0     0   0    0
    
  • The problem now is that only the shortest positive distance in each direction counts, because the others are blocked by the first piece. The next series of steps filters these blocked pieces.

  • (=[:+/@(*]=&|[:<./"1|+_*0&=) Ignoring zeros, take the min of each row (using norm for comparison), and keep only entries equal to that min. These are attackable queens. Then sum the planes, which are guaranteed not to overlap, and check where those equal the normalized differences. This is adjanceny matrix of connected queens:

    1 1 0 0 1 1
    1 1 0 0 1 1
    0 0 1 1 0 0
    0 0 1 1 0 0
    1 1 0 0 1 1
    1 1 0 0 1 1
    
  • (-:|:)@(*+/)@ Multiply the rows by the column sums, and check if the matrix is symmetric around the main diagonal:

    4 4 0 0 4 4
    4 4 0 0 4 4
    0 0 2 2 0 0
    0 0 2 2 0 0
    4 4 0 0 4 4
    4 4 0 0 4 4
    
\$\endgroup\$
3
\$\begingroup\$

Jelly, 30 bytes

-4 thanks to Kevin Cruijssen (remove an effectively redundant filter of the "direction" [0,0], which also means there's no need to get the threatening queen separately.)

+8RĖ×Ɱ2Ż’p`¤¤f€ZḢ
ŒṪç€iƇẈEɗⱮ$Ạ

A monadic Link that accepts a list of lists of 1s (queens) and 0s (not queens) that yields 1 if there is a peaceful standoff or 0 if not.

Try it online! Or see the test-suite.

How?

+8RĖ×Ɱ2Ż’p`¤¤f€ZḢ - Helper Link = get queen & threatened queens(
                      queen coordinate, Q;
                      all queen coordinates, A
                    ):
            ¤     - nilad followed by links as a nilad:
 8                -   eight
  R               -   range -> [1,...,7,8]
   Ė              -   enumerate -> [[1,1],...,[7,7],[8,8]]
           ¤      -   nilad followed by links as a nilad:
      2           -     two
       Ż          -     zero-range -> [0,1,2]
        ’         -     decrement -> [-1,0,1]
          `       -     use as both arguments of:
         p        -       Cartesian product -> [[-1,-1],[-1,0],[-1,1],[0,-1],[0,0],[0,1],[1,-1],[1,0],[1,1]]
     Ɱ            -   map with:
    ×             -     multiply -> [[[-1,-1],...,[-7,-7],[-8,-8]],[[-1,0],...,[-7,0],[-8,0]],[[-1,1],...,[-7,7],[-8,8]], ... ... ... ,[[1,1],...,[7,7],[8,8]]]
+                 - (Q) add (vectorises) -> nine lists of potential locations in
                                            each of the eight lines of sight
                                            in proximity order plus the direction
                                            [0,0] which will all be Q
                                            (includes off-board locations)
             f€   - for each: filter keep (A) -> nine lists (eight are possibly empty)
               Z  - transpose
                Ḣ - head -> list of threatened queens and Q herself.

ŒṪç€iƇẈEɗⱮ$Ạ - Link = is peaceful?(board):
ŒṪ           - truthy multidimensional indices -> all queen coordinates
          $  - last two links as a monad - f(all queens):
  ç€         -   call the Helper link for each queen with all queens on the right
         Ɱ   -   map (across Q in all queens) with:
        ɗ    -     last three links as a dyad - f(Helper results, Q):
     Ƈ       -       keep those for which:
    i        -         first 1-indexed index of Q or 0 if not found
      Ẉ      -       length of each
       E     -       all equal?
           Ạ - all?
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think you can get rid of the ẸƇ for the same reason I've stated at the bottom of my Java answer. \$\endgroup\$ Oct 20, 2022 at 8:03
  • 1
    \$\begingroup\$ @KevinCruijssen Good point, this is like an extra direction of "self" and she'll always be found. This means I can remove ⁸ṭ too. Thanks! \$\endgroup\$ Oct 20, 2022 at 16:32
2
\$\begingroup\$

C (gcc), 256 bytes

#define A(c)for(i=0;i<64;++i)for(m=j=0;j<64;++j)if(j-i&&b[i]>0&b[j]>0){x=i/8-j/8,y=i%8-j%8;t=0;x||(t=(y>0)+1);y||(t=(x>0)*4+4);abs(x)-abs(y)||(t=16<<((x>0)+(y>0)*2));if(t&&!(m&t)){c;}}
f(b,i,j,m,t,y,x)int*b;{A(b[i]++;m|=t)A(if(b[i]-b[j])return 0)return 1;}

Try it online!

\$\endgroup\$
7
  • \$\begingroup\$ 216 :-) \$\endgroup\$
    – jdt
    Oct 19, 2022 at 16:20
  • 1
    \$\begingroup\$ 203 \$\endgroup\$
    – c--
    Oct 19, 2022 at 17:48
  • 2
    \$\begingroup\$ I knew I left a couple of bits here and there, but I wouldn't have thought so much \$\endgroup\$
    – matteo_c
    Oct 19, 2022 at 20:36
  • 1
    \$\begingroup\$ 191 \$\endgroup\$
    – jdt
    Oct 21, 2022 at 17:10
  • 1
    \$\begingroup\$ @jdt I think you can reset m=0 at the end of the first loop for -1 byte, nice golfing \$\endgroup\$
    – c--
    Oct 21, 2022 at 17:46
1
\$\begingroup\$

Charcoal, 70 bytes

E⁸S≔E⁸E⁸⟦⟧ηF⁸F⁸F⁸«Jικ≔⌕AKD⁸✳λQθ¿∧№θ⁰⌈θ«M§θ¹✳λ⊞§§ηⅈⅉ§§ηικ»»⎚⬤η⬤ι⬤λ⁼LνLλ

Try it online! Link is to verbose version of code. Takes input as a list of 8 strings of 8 Qs and .s. Explanation:

E⁸S

Input the board and print it to the canvas.

≔E⁸E⁸⟦⟧η

Start with no Queens being attacked.

F⁸F⁸F⁸«

Loop over each possible direction for each possible Queen.

Jικ≔⌕AKD⁸✳λQθ

Look at the pieces at the current position in the current direction.

¿∧№θ⁰⌈θ«

If there is a Queen at the current position and at least one more Queen in the current direction, then...

M§θ¹✳λ⊞§§ηⅈⅉ§§ηικ

... move to the next Queen in the current direction, and add the current Queen to the list of Queens that are attacking it.

»»⎚⬤η⬤ι⬤λ⁼LνLλ

Check that all lists of Queens have elements of the same length. Example: If there are Queens at a1, a8, h1 and h8, then let the lists at those positions be denoted by a1, a8, h1 and h8, then we have a1 = [a8, h1, h8], a8 = [a1, h1, h8], h1 = [a1, a8, h8] and h8 = [a1, a8, h1]; each list a1, h8, h1 and h8 contains three elements and those three elements are also all lists of three elements. (Conveniently this is also vacuously true for the squares that don't contain Queens and are therefore empty lists.)

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

Japt, 91 bytes

Jõ ïJõ
@VmA{8õ@Am*XîíX'+Ãfe_>J©Z<8ãg$...$X
U=Ë£X©[EY]gW md x
Ëe@!Xª[EY]gW me_¥XªZ¥0Ãe}Ãe

Try it

Note that there should be an additional empty line in front of the code, but it's not displaying here. Takes input as a 2D array with Queens as 1 and empty spaces as 0.

This is probably the worst Japt code I've ever written and I am certain there are a lot of improvements to make.

Explanation

The first line is blank, because Japt automatically stores the result of each line in a variable. If the first line is not blank then its result would overwrite the input

            # Implicitly store input as U

The second line generates the 8 direction vectors a queen can move in, as well as [0,0] i.e. "not moving" which is unnecessary but ends up not mattering

Jõ ïJõ
Jõ     # The range [-1...1]
   ï   # Get every possible pair with:
    Jõ #  The range [-1...1] again
       # Store as V

The third line does the bulk of the work here. It declares a function which takes a pair of coordinates as input, and returns an array indicating the cells in U which are in each of the 9 "directions" relative to those coordinates.

@VmA{8õ@Am*XîíX'+Ãfe_>J©Z<8ãg$...$X
@                                     # Declare a function:
 VmA{             Ã                   #  For each direction:
     8õ@Am*XîíX'+                    #  (Get the coordinates for 8 cells in that direction) 
     8õ@    Ã                         #   For each value X in the range [1...8]
        Am*X                          #    Multiply the direction vector by X
             ®                        #   For each resulting pair:
              íX'+                    #    Add the input coordinates
                   fe_>J©Z<8Ã         #  (Remove out-of-bounds coordinates)
                   f                  #   Keep only coordinates which where:
                    e_      Ã         #    Both parts:
                      >J              #     Are greater than -1
                        ©             #     And
                         Z<8          #     Are less than 8
                             £g$...$X #  (Get the values from U at those coordinates)
                             £        #   For each remaining coordinates X:
                              g       #    Get the value from U at:
                               $...$X #     Multi-dimensional coordinates X

The fourth line calls V on every pair of coordinates that has a queen, counts the number of directions which see another queen, then saves that count back in the same cell of U. Technically the number saved is 1 higher than the threat number since one of the directions is "don't move" but it doesn't affect anything.

U=Ë£X©[EY]gW md x
  ˣ              # For every cell X in U:
    X©            #  Return 0 if X is 0
      [EY]gW      #  Otherwise call W on the coordinates of X
             m    #  For each direction:
              d   #   True if there are any queens
                x #  Count the number of true directions
U=                # Save the results back into U

The fifth line uses W again to get the sightlines, then checks that every cell's sightlines only contain empty spaces and other queens with the same threat number.

Ëe@!Xª[EY]gW me_¥XªZ¥0Ãe}Ãe
Ë                        Ã  # For every row D of U:
 e@                     }   #  True if every cell X in that row returns true:
   !Xª                      #   Return true if X is 0
      [EY]gW                #   Otherwise call W on the coordinates of X
             m              #   For each direction:
              e_      Ã     #    True if every cell Z in that direction returns true: 
                ¥X          #     Z == X
                  ª         #     Or
                   Z¥0      #     Z == 0
                       e    #   Return true if every direction returned true
                          e # Output true if every row returned true
\$\endgroup\$
0
\$\begingroup\$

Raku, 122 bytes

{my%x=@^q.map:{$^q=>map {first(@q.any,($q,*+$^d...*)[1..7])//|()},((-1,0,1)X+(-i,0,i))};[&&] %x{*}.map:{[==] @^v,|%x{@v}}}

Try it online!

Input is a list of complex numbers, the queen coordinates.

  • my %x = @^q.map: { $^q => ... } generates a hash from each queen to a list of the other queens that it threatens, storing it in the variable %x.
  • (-1, 0, 1) X+ (-i, 0, i) crosses the real numbers -1, 0, and 1 with the complex numbers -i, 0, and i with addition, producing the nine complex numbers -1-i, -1, -1+i, -i, 0, i, 1-i, 1, and 1+i. These are the steps that will be followed from each queen, looking for another queen that it threatens. For brevity, we don't omit the step 0, so each queen will count as attacking itself. That doesn't change the final boolean answer.
  • first(@q.any, ($q, * + $^d ... *)[1..7]) // |() takes up to seven steps from each queen, stopping when it encounters one of the other queens. first returns Nil if it doesn't find anything, so the // |() converts that into a Slip object, which doesn't leave anything in the output list.
  • [&&] %x{*}.map: { [==] @^v, |%x{@v} } checks that each hash value @^v (that is, each list of threatened queens) satisfies the condition that its size is equal to the sizes of every other list of queens threatened by those queens.
\$\endgroup\$
2
  • \$\begingroup\$ I don't know a thing about Raku but it looks like you can remove the space in my %x \$\endgroup\$
    – naffetS
    Oct 20, 2022 at 19:18
  • \$\begingroup\$ @Sʨɠɠan Ah yes, it's declarations of sigil-less variables like my \x = ... that require the space, which for "regular" declarations is optional. I sometimes forget which is which. Fixed. \$\endgroup\$
    – Sean
    Oct 23, 2022 at 19:14
0
\$\begingroup\$

Java 10, 248 246 241 239 237 219 217 212 bytes

b->{boolean r=1>1,B;for(int m[]=new int[64],f=128,i,d,X,Y;f-->0;)for(d=9;b[i=f%64]&d-->0;m[i]+=B&&b[Y+=X*8]?f/64:0,r|=f<64&B&&m[i]!=m[Y])for(X=i/8,Y=f%8;(B=~(X+=d/3-1)%9<0&~(Y+=d%3-1)%9<0)&&!b[X*8+Y];);return!r;}

Inspired by @Arnauld's JavaScript answer.
-25 bytes thanks to @ceilingcat.

Input as a flat boolean-array.

Try it online.

Explanation:

b->{                       // Method with boolean-array parameter & boolean return
  boolean r=0>1,           //  Result-boolean, starting at false
          B;               //  Flag whether we're still in boundary
  for(int m[]=new int[64], //  Array to keep track of queen's thread levels
      f=128,               //  Flag-integer
      i,                   //  Index-integer
      d,                   //  Direction-integer
      X,Y;                 //  X,Y-coordinate integers of a taken direction of a queen
      f-->0;)              //  Loop `f` in the range (128,0]:
    for(d=9;b[i=f%64       //   Set `i` to `f` modulo-64
                    ]&     //   If the `i`'th cell contains a queen:
             d-->0         //    Inner loop `d` in the range (9,0]:
        ;                  //      After every iteration of loop `d`:
         m[i]+=            //       Increase the current `i`'th threat value by:
          B                //        If we're still in bounds,
          &&b[Y+=X*8]?     //        and `X,Y` contains a threatening† queen:
           f/64            //         Increase it by `f` integer-divided by 64
                           //         (1 in the first 64 iterations, 0 in the second 64)
          :                //        Else:
           0,              //         Leave it unchanged by increasing with 0
         r|=               //        Also update the result to:
            f<64           //         If this is within the first 64 iterations
            &B             //         And we're still in bounds:
            &&m[i]!=m[Y])  //          Check whether the `i` and `X,Y` threat-levels
                           //          are NOT the same
      for(X=i/8,Y=f%8;     //     (Re)set `X,Y` based on `i` and `f`
          (B=~(X+=d/3-1)   //     Go to the next `X`-coordinate based on `d`
               %9<0        //     And check if it's still in bounds
          &~(Y+=d%3-1)%9<0)//     Do the same for the `Y`-coordinate
          &&               //     And if we're still in bound:
            !b[X*8+Y];);   //      Stop the inner-most loop if we've encountered a
                           //      threatening† queen
  return!r;}               //  Finally: return the inverse of result `r`

: Because we don't skip direction d=4 (which means x,y==X,Y), every queen will have a tread-level one higher than in the challenge description, because it'll increase for its own position (a.k.a. every queen 'threatens' itself). But since this is done for each individual queen, it won't matter for the final output.

\$\endgroup\$
4
  • \$\begingroup\$ -1 byte: int m[][] -> int[][]m \$\endgroup\$
    – corvus_192
    Oct 19, 2022 at 14:22
  • \$\begingroup\$ @corvus_192 I initialize more than one int on that line.. \$\endgroup\$ Oct 19, 2022 at 14:36
  • \$\begingroup\$ Right, seems like I need a refresher on rules of that strange array syntax. \$\endgroup\$
    – corvus_192
    Oct 19, 2022 at 14:38
  • \$\begingroup\$ @corvus_192 Well, if it was int[][]m=new int[8][8];int f=2,...; your suggestion would have worked. Although in that case, var m=new int[8][8];int f=2,...; would have been shorter in Java 10+. :) \$\endgroup\$ Oct 19, 2022 at 14:44

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.