# \[QUIZ\] Chess960 (#106)

**URL:** <https://rubytalk.org/t/quiz-chess960-106/33891>\
**Category:** ruby-talk\
**Created:** [20 December 2006 04:43 UTC](https://rubytalk.org/t/quiz-chess960-106/33891 "2006-12-20T04:43:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Tran1](https://avatars.discourse-cdn.com/v4/letter/d/51bf81/32.png) [@David\_Tran1](https://rubytalk.org/u/David_Tran1)\
**Post date:** [20 December 2006 04:43 UTC](https://rubytalk.org/t/quiz-chess960-106/33891/1 "2006-12-20T04:43:56Z")

</div>

My solution:

=begin

> import Data.List  
> import Data.Maybe
> 
> pieces = "RNBKQBNR"
> 
> permutation = []  
> permutation xs = [x:y | x \<- nub xs, y \<- permutation $ delete x xs]
> 
> restriction position =  
> &nbsp;&nbsp;r1 \< k && k \< r2 &&  
> &nbsp;&nbsp;sum (elemIndices 'B' position) `mod` 2 /= 0  
> &nbsp;&nbsp;where  
> &nbsp;&nbsp;r1:r2:\_ = elemIndices 'R' position  
> &nbsp;&nbsp;k = fromJust $ elemIndex 'K' position
> 
> results = filter restriction (permutation pieces)

=end

def permutation(pieces)  
&nbsp;&nbsp;return [pieces] if pieces.length \<= 1  
&nbsp;&nbsp;result =   
&nbsp;&nbsp;pieces.uniq.each do |p|  
&nbsp;&nbsp;&nbsp;&nbsp;\_pieces = pieces.dup  
&nbsp;&nbsp;&nbsp;&nbsp;\_pieces.delete\_at(pieces.index(p))  
&nbsp;&nbsp;&nbsp;&nbsp;permutation(\_pieces).each do |perm|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result \<\< (perm \<\< p)  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
&nbsp;&nbsp;result  
end

results = permutation("RNBKQBNR".split(//)).select do |position|  
&nbsp;&nbsp;r1 = position.index('R')  
&nbsp;&nbsp;r2 = position.rindex('R')  
&nbsp;&nbsp;b1 = position.index('B')  
&nbsp;&nbsp;b2 = position.rindex('B')  
&nbsp;&nbsp;k = position.index('K')  
&nbsp;&nbsp;r1 \< k && k \< r2 && ((b1+b2) % 2 != 0)  
end

puts "Total positions = #{results.length}"  
puts results[rand(results.length)].join(' ')

---

<div class="post-metadata">

**Author:** ![Morton\_Goldberg](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@Morton\_Goldberg](https://rubytalk.org/u/Morton_Goldberg)\
**Post date:** [20 December 2006 11:55 UTC](https://rubytalk.org/t/quiz-chess960-106/33891/2 "2006-12-20T11:55:29Z")

</div>

Very nice, compact solution. But what programing language is that in the block comment? I don't recognize it.

Regards, Morton

> **···**
>
> On Dec 19, 2006, at 11:43 PM, David Tran wrote:
> 
> > My solution:
> > 
> > =begin
> > 
> > > import Data.List  
> > > import Data.Maybe
> > > 
> > > pieces = "RNBKQBNR"
> > > 
> > > permutation = []  
> > > permutation xs = [x:y | x \<- nub xs, y \<- permutation $ delete x xs]
> > > 
> > > restriction position =  
> > > &nbsp;&nbsp;r1 \< k && k \< r2 &&  
> > > &nbsp;&nbsp;sum (elemIndices 'B' position) `mod` 2 /= 0  
> > > &nbsp;&nbsp;where  
> > > &nbsp;&nbsp;r1:r2:\_ = elemIndices 'R' position  
> > > &nbsp;&nbsp;k = fromJust $ elemIndex 'K' position
> > > 
> > > results = filter restriction (permutation pieces)
> > 
> > =end
> > 
> > def permutation(pieces)  
> > return [pieces] if pieces.length \<= 1  
> > result =   
> > pieces.uniq.each do |p|  
> > &nbsp;&nbsp;&nbsp;\_pieces = pieces.dup  
> > &nbsp;&nbsp;&nbsp;\_pieces.delete\_at(pieces.index(p))  
> > &nbsp;&nbsp;&nbsp;permutation(\_pieces).each do |perm|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result \<\< (perm \<\< p)  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > result  
> > end
> > 
> > results = permutation("RNBKQBNR".split(//)).select do |position|  
> > r1 = position.index('R')  
> > r2 = position.rindex('R')  
> > b1 = position.index('B')  
> > b2 = position.rindex('B')  
> > k = position.index('K')  
> > r1 \< k && k \< r2 && ((b1+b2) % 2 != 0)  
> > end
> > 
> > puts "Total positions = #{results.length}"  
> > puts results[rand(results.length)].join(' ')
