Skip to main content
added 376 characters in body
Source Link
G B
  • 22.8k
  • 1
  • 20
  • 53

Ruby, 9393 81 bytes

->a,n{w=*1..8;w.product(*[w]*~-a.sizen).select{|r|r.zip(a).mapsum{|x,y|(x-y).absabs*8/3}.sort-[0]==[1,2]==7}}

Try it online!Try it online!

Quickly explained - old version

Generate all possible positions as vetorsvectors of N numbers between 1 and 8, then check the differences between each vector and the starting position, the sorted array of absolute values of the components must be [<bunch of 0s ...>, 1, 2]

And then:

(Thanks @dingledooper): If we multiply the differences by 8 and divide them by 3, the vector becomes [<bunch of 0s>, 2, 5] and its sum is unequivocally 7, no other combination of numbers can produce the same sum, so we can simplify the check a lot and shave off 12 bytes.

Ruby, 93 bytes

->a{w=*1..8;w.product(*[w]*~-a.size).select{|r|r.zip(a).map{|x,y|(x-y).abs}.sort-[0]==[1,2]}}

Try it online!

Quickly explained

Generate all possible positions as vetors of N numbers between 1 and 8, then check the differences between each vector and the starting position, the sorted array of absolute values of the components must be [<bunch of 0s ...>, 1, 2]

Ruby, 93 81 bytes

->a,n{w=*1..8;w.product(*[w]*~-n).select{|r|r.zip(a).sum{|x,y|(x-y).abs*8/3}==7}}

Try it online!

Quickly explained - old version

Generate all possible positions as vectors of N numbers between 1 and 8, then check the differences between each vector and the starting position, the sorted array of absolute values of the components must be [<bunch of 0s ...>, 1, 2]

And then:

(Thanks @dingledooper): If we multiply the differences by 8 and divide them by 3, the vector becomes [<bunch of 0s>, 2, 5] and its sum is unequivocally 7, no other combination of numbers can produce the same sum, so we can simplify the check a lot and shave off 12 bytes.

Source Link
G B
  • 22.8k
  • 1
  • 20
  • 53

Ruby, 93 bytes

->a{w=*1..8;w.product(*[w]*~-a.size).select{|r|r.zip(a).map{|x,y|(x-y).abs}.sort-[0]==[1,2]}}

Try it online!

Quickly explained

Generate all possible positions as vetors of N numbers between 1 and 8, then check the differences between each vector and the starting position, the sorted array of absolute values of the components must be [<bunch of 0s ...>, 1, 2]