[Question]
  [
Let an 8x8 chessboard be represented by any two distinct values, with one value being an empty square and the other being a queen. In the following examples, I use 0s as the empty squares and 1s as queens. For example:
[![Queens on a chessboard](https://i.stack.imgur.com/oa2Ze.png)](https://i.stack.imgur.com/oa2Ze.png)
is given by
```
1 0 1 1 1 0 0 0
1 0 1 0 1 0 1 1
1 0 1 0 1 1 0 1
0 1 0 1 0 1 0 0
0 1 1 0 0 1 0 1
1 0 0 0 1 0 0 0
0 1 0 0 0 1 1 1
0 1 1 1 0 1 0 1
```
Consider the number of pairs of queens that are attacking each that are at least one square away (as a reminder, queens attack orthogonally and diagonally). In the above example, the following incredible ugly diagram shows all these pairs as arrows.
[![Attacking queens](https://i.stack.imgur.com/IRxGm.png)](https://i.stack.imgur.com/IRxGm.png)
There are 43 pairs found above giving the following test case:
```
Input:
1 0 1 1 1 0 0 0
1 0 1 0 1 0 1 1
1 0 1 0 1 1 0 1
0 1 0 1 0 1 0 0
0 1 1 0 0 1 0 1
1 0 0 0 1 0 0 0
0 1 0 0 0 1 1 1
0 1 1 1 0 1 0 1
Output: 43
```
## Challenge
Write a program that, given a board state represented by two distinct values, outputs the number of pairs of queens that attack each other with at least one square in between them.
* You may input in whatever format is most convenient that uses two values to represent the empty squares and queens, e.g., a string of 64 "."s for empty squares and "Q"s for queens by rows from bottom to top, an 8x8 matrix of booleans, a list of list of integers 0 and 1 etc, as long as it is explained in your solution
* Output is an integer
* Standard I/O methods apply and standard loopholes forbidden
* This is code golf so shortest answer in bytes wins
## Test cases:
Using the 0 and 1 format, with 0 being empty squares and 1 being queens:
```
Input:
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 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
Output: 0
Input:
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
Output: 0
Input:
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 1 0 0 0 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
Output: 1
Input:
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0
0 1 0 0 0 0 1 0
0 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0
0 0 0 1 0 0 1 0
0 0 0 0 0 0 0 0
Output: 10
Input:
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 0 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
Output: 4
Input:
1 1 0 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 0 1 1 1
1 1 1 1 0 1 1 1
1 1 1 1 0 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
Output: 11
```
  
[Answer]
# [Python 2](https://docs.python.org/2/), 105 bytes
```
lambda b:sum(b[i+d::d][:(8,7-i%8,i%8)[d%8%5]].find('1')*int(c)>0for i,c in enumerate(b)for d in[1,7,8,9])
```
[Try it online!](https://tio.run/##PYvBDsIgEER/hQspKJpdE1Mk0R9BDqWUSNLSptKDX4/UqtnMJvNmZnqlxxhP2V/vuW8G6xpi1XMZmNVh75RyRismRX0IVIoirh2V9GzM0YfoWIUV34WYWMtv4MeZBNGSEEkXl6Gbm9Qxy1fsCtQoaiHFxfA8zWVDfJkDIgIAbofb/9k1QNgQfOyX/asVz28 "Python 2 – Try It Online")
# Explanation
We take the input as a string of 64 characters `'0'` or `'1'`. Using step slices, we cast four "lines of sight" from every queen we encounter. For example, when **i = 10** and **d = 7**, marking the queen as ♥ and the tiles selected by `b[i+d::d]` as █:
```
1 0 1 1 1 0 0 0
1 0 ♥ 0 1 0 1 1
1 █ 1 0 1 1 0 1
█ 1 0 1 0 1 0 █
0 1 1 0 0 1 █ 1
1 0 0 0 1 █ 0 0
0 1 0 0 █ 1 1 1
0 1 1 █ 0 1 0 1
```
Clearly, we don't actually want vision to wrap around the board like this. So we compute how far away the edge of the board is in each direction, and view the tiles at `b[i+d::d][:…]`.
For each tile-direction pair we count:
```
ray.find('1')*int(c)>0
```
This will fail whenever
* `c` is not a queen; or
* the queen this ray sees is too close (`find` returns 0); or
* this ray sees no queen (`find` returns −1).
Each pair of queens is only checked once, since rays are always casted *forward* in reading order, from an "earlier" queen to a "later" one.
[Answer]
# JavaScript (ES7), 86 bytes
Takes input as an array of 64 integers with **254** for a queen and **0** for an empty square.
```
a=>[s=0,6,7,8].map(d=>a.map(g=(n,p)=>(p%8-(p+=~d)%8)**2<n%4?a[p]?s+=n&1:g(n/2,p):0))|s
```
[Try it online!](https://tio.run/##fZBNDoIwEIX3nIINpoWClRglxIGVXoKwaPiLBtvGGsPCePVKiEbRwublzeSbl5k5sRtTxeUorz4XZaVr0AySTAElG7IlUR6cmUQlJGwwDSBOJIYESSfykfTgUWInwq4b7rizTlkm81R5wBeruEF8GfZwTDG@K10IrkRbBa1oUI0yy7Zpd9gT2lFKXu4jQ@8tJtTkeplHR@WAzgeOFjDF/A/9LGCiJlJN/OisyT99OyvHWD8B "JavaScript (Node.js) – Try It Online")
This version abuses arithmetic underflow to get a stop condition in the recursive part.
---
# JavaScript (ES7), 89 bytes
Takes input as an array of 64 bits.
```
a=>[s=0,6,7,8].map(d=>a.map(g=(n,p,x)=>(p%8-(p+=~d)%8)**2>1|p<0?0:a[p]?s+=!x&n:g(n,p)))|s
```
[Try it online!](https://tio.run/##VU7LDoIwELzzFXjAdKGQ1oMS4sKHEA4NINFg21hjOBB/vWqVh5nMZncyM9mLeAhT3876HkvVtPaEVmBeGmR0Tw80rZKr0KTBXLilQyKppgNgTnSQxkRH@GwgSCEMdzkf9ZEVLBOlrgoT4WbYyqz7JABgNLZW0qi@TXrVkRMpPd/nlFHuwBxmaSL/l77zLa09vyCbe2bXVLuqXw4@1S8vOHoVgH0B "JavaScript (Node.js) – Try It Online")
### How?
We recursively call a named callback function of `map()` to walk through the squares in a given direction. Although we don't really need the content of the third parameter of the callback (the array `map()` was called upon), we indirectly use it nonetheless to know whether it is the first iteration or not.
> 
> *arr.map(function callback(currentValue[, index[, array]])*
> 
> 
> 
This is the **x** variable in the code.
```
a =>                        // given the input array a[]
  [ s = 0,                  // initialize the sum s to 0
    6, 7, 8 ].map(d =>      // for each direction d in [0, 6, 7, 8]:
    a.map(g = (n, p, x) =>  //   for each square n at position p in a[]:
      (                     //     we are out of the board if:
        p % 8 -             //       - abs(p % 8 - p' % 8) is greater than 1
        (p += ~d) % 8       //         where p' = p - (d + 1)
      ) ** 2 > 1 |          //         (squaring is shorter than using Math.abs)
      p < 0 ?               //       - or p' is less than 0
        0                   //       if so, stop recursion
      :                     //     else:
        a[p] ?              //       if there's a queen on the target square:
          s +=              //         increment s if:
            !x &            //           x is undefined (this is not the 1st iteration)
            n               //           and n = 1 (there's a queen on the source square)
        :                   //       else:
          g(n, p)           //         do a recursive call to g(), with x undefined
    )                       //   end of inner map()
  ) | s                     // end of outer map(); return s
```
[Answer]
# [Snails](https://github.com/feresum/PMA), 14 bytes
```
A
rdaa7\1\0+\1
```
[Try it online!](https://tio.run/##K85LzMwp/v/fkasoJTHRPMYwxkA7xvD/f0MDQ0NDAwMDLiADBA0hDCDigggApUBcEJMLpBCsGEwCNXKBdQMhAA "Snails – Try It Online")
Input is the 0/1 format, without spaces within lines.
Snails was [created for a 2D pattern matching language design PPCG challenge](https://codegolf.stackexchange.com/a/47905/6972). Most importantly, it by default outputs the number of matches found, which is perfect for this challenge.
---
`A` sets the "all paths" option, so that if a queen is in multiple pairs, each of those pairs would generate a match.
`rdaa7` sets the match direction to S, SE, E, and NE. Setting to all directions (`z`) would cause double counting.
`\1\0+\1` matches a `1`, then one or more `0`s, then another `1`.
[Answer]
# [APL (Dyalog Classic)](https://www.dyalog.com/), ~~41~~ ~~39~~ 32 bytes
```
(+/+⌿-⌈⌿)2<⌿0⍪⊢,⍉,8 31⍴⊢,≠⍨,⌽,≠⍨
```
[Try it online!](https://tio.run/##pY/PDsFAEMbvnqK3JdqYD4dGvExDKqIJwUXETUTpikfASZy5SETiUeZFamiJ@B92k9n5ZnZ/O5/T8Kxyx/HqFavkOa1WtRSGLg@myXQmzcHe4mAoRypblEisVzxamKx90zZyYL0@S3/GemlysIvTMGwLoct6w/68KanLeltQ9ZricV@5TtVTUpB2s5fI59rygvXksLQNW4gKBICIEG1E8SJPDVBUorOMa9erKmHQPZP@XM@Y@HMJE1/PeTX6aU68845HHGKNu99w6z3/i3d66x14ZNJLBn3DPAI "APL (Dyalog Classic) – Try It Online")
`≠⍨` is "not equal to itself" - an 8x8 all-zero matrix
`⊢,≠⍨,⌽,≠⍨` - if the original matrix is `ABC...`, this expression returns:
```
A B C D E F G H 0 0 0 0 0 0 0 0 H G F E D C B A 0 0 0 0 0 0 0 0
I J K L M N O P 0 0 0 0 0 0 0 0 P O N M L K J I 0 0 0 0 0 0 0 0
Q R S T U V W X 0 0 0 0 0 0 0 0 X W V U T S R Q 0 0 0 0 0 0 0 0
Y Z A B C D E F 0 0 0 0 0 0 0 0 F E D C B A Z Y 0 0 0 0 0 0 0 0
G H I J K L M N 0 0 0 0 0 0 0 0 N M L K J I H G 0 0 0 0 0 0 0 0
O P Q R S T U V 0 0 0 0 0 0 0 0 V U T S R Q P O 0 0 0 0 0 0 0 0
W X Y Z A B C D 0 0 0 0 0 0 0 0 D C B A Z Y X W 0 0 0 0 0 0 0 0
E F G H I J K L 0 0 0 0 0 0 0 0 L K J I H G F E 0 0 0 0 0 0 0 0
```
`8 31⍴` reshapes it from 8x32 to 8x31, reusing the elements in row-major order:
```
A B C D E F G H 0 0 0 0 0 0 0 0 H G F E D C B A 0 0 0 0 0 0 0
0 I J K L M N O P 0 0 0 0 0 0 0 0 P O N M L K J I 0 0 0 0 0 0
0 0 Q R S T U V W X 0 0 0 0 0 0 0 0 X W V U T S R Q 0 0 0 0 0
0 0 0 Y Z A B C D E F 0 0 0 0 0 0 0 0 F E D C B A Z Y 0 0 0 0
0 0 0 0 G H I J K L M N 0 0 0 0 0 0 0 0 N M L K J I H G 0 0 0
0 0 0 0 0 O P Q R S T U V 0 0 0 0 0 0 0 0 V U T S R Q P O 0 0
0 0 0 0 0 0 W X Y Z A B C D 0 0 0 0 0 0 0 0 D C B A Z Y X W 0
0 0 0 0 0 0 0 E F G H I J K L 0 0 0 0 0 0 0 0 L K J I H G F E
```
`⊢,⍉,` prepends the original matrix and its transpose (extra spaces for clarity):
```
A B C D E F G H  A I Q Y G O W E  A B C D E F G H 0 0 0 0 0 0 0 0 H G F E D C B A 0 0 0 0 0 0 0
I J K L M N O P  B J R Z H P X F  0 I J K L M N O P 0 0 0 0 0 0 0 0 P O N M L K J I 0 0 0 0 0 0
Q R S T U V W X  C K S A I Q Y G  0 0 Q R S T U V W X 0 0 0 0 0 0 0 0 X W V U T S R Q 0 0 0 0 0
Y Z A B C D E F  D L T B J R Z H  0 0 0 Y Z A B C D E F 0 0 0 0 0 0 0 0 F E D C B A Z Y 0 0 0 0
G H I J K L M N  E M U C K S A I  0 0 0 0 G H I J K L M N 0 0 0 0 0 0 0 0 N M L K J I H G 0 0 0
O P Q R S T U V  F N V D L T B J  0 0 0 0 0 O P Q R S T U V 0 0 0 0 0 0 0 0 V U T S R Q P O 0 0
W X Y Z A B C D  G O W E M U C K  0 0 0 0 0 0 W X Y Z A B C D 0 0 0 0 0 0 0 0 D C B A Z Y X W 0
E F G H I J K L  H P X F N V D L  0 0 0 0 0 0 0 E F G H I J K L 0 0 0 0 0 0 0 0 L K J I H G F E
```
`2<⌿0⍪` adds 0s on top and compares using `<` every element against the element below it, so we get a 1 for the leading 1 in every vertical group of 1s, and we get 0s everywhere else
`+⌿-⌈⌿` the sums by column minus the maxima by column - we compute the number of gaps between the 1-groups in every column, 0 if there are none
`+/` sum
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~22~~ 20 bytes
```
t0ŒgL:2
Z,U;ŒD$€ẎÇ€S
```
[Try it online!](https://tio.run/##y0rNyan8/7/E4OikdB8rI64onVDro5NcVB41rXm4q@9wO5AO/v//fzRXtKGOgY4hGBqAYKwOTAiGDVGFwCRICFkNVKMB3By4KqixyMbDORB7ERrh5sVyxQIA "Jelly – Try It Online")
[Answer]
# [K (ngn/k)](https://codeberg.org/ngn/k), ~~45~~ 43 bytes
-2 bytes thanks to @coltim
```
{+/0|-1+/0|-':x,'+x,+8 31#,/+x,a,(|x),a:^x}
```
[Try it online!](https://ngn.codeberg.page/k#eJylj8sKgzAQRff5ioAFFVOciy4k+RRJURdCaaEgLiI+vr2tLxRrS3ECk3lxZm4ua8+n5oze29II2zPCi3gAS/ivMBVOY1yRyotpGStlfYqrrpA5Nyp53JST5On1royqVOHqlpVxxCMLBICIMDwMfkrfDdBQoj4da/NopsNgRNFByzSn6aqDtkDtrpvV/LwKWxRWwQKFNRLLFZnGPwLpu8BwRm0Haef/jALYExxlfoE=)
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), ~~60~~ 58 bytes
```
1
¶1$';___¶
_
$n$%`7$*_
(.)(?=.*;(_)*)(?<-2>.)*
$1
m`^10+1
```
[Try it online!](https://tio.run/##K0otycxL/B@ScGibQoJO/H9DrkPbDFXUrePj4w9t44rnUslTUU0wV9GK59LQ09Swt9XTstaI19QCMm10jez0NLW4VAy5chPiDA20Df//N1BAgVwQ2hCNDxfhQpIDsVDkkfnYzMMiDwA "Retina 0.8.2 – Try It Online") Takes input as 8 comma-separated 8-character binary strings, but the header converts the provided format for you. Explanation:
```
1
¶1$';___¶
```
Create all of the substrings of the board starting at a queen. Suffix a marker value to each substring. Edit: Saved 2 bytes by leaving some garbage strings behind; these are effectively ignored.
```
_
$n$%`7$*_
```
Split each marker up into an inclusive range, and add 7 to the non-zero elements.
```
(.)(?=.*;(_)*)(?<-2>.)*
$1
```
Delete every run of characters that equals the length of the marker. This is equivalent to finding each east, southwest, south or southeast ray from each queen.
```
m`^10+1
```
Count all the rays that pass through at least one empty square before meeting another queen.
[Answer]
# JavaScript (ES6) + [SnakeEx](http://www.brianmacintosh.com/snakeex/), 38 bytes
```
s=>snakeEx.run('m:<*>10+1',s).length/2
```
Takes input in the form `'10111000\n10101011\n10101101\n01010100\n01100101\n10001000\n01000111\n01110101'`. Turns out, SnakeEx can still be used outside of its original challenge!
]