-6 bytes thanks to @Kevin Cruijssen
-43 bytes thanks to @Jitse
import numpy
z=len
d=lambda a,b,c,d:abs(a-c)+abs(b-d)
def g(l,m):
if z(l)==1:yield d(*l[0],*m[0])
for i in range(z(l)):
for s in g(l[1:],m[:i]+m[i+1:]):yield d(*l[0],*m[i])+s
def c(a,b):
e=a-b;k=z(a);f,*h=[],
for x in range(k*z(a[0])):w=[(x%k,x//k)];v=e[x%k][x//k];f+=w*(v>0);h+=w*(v<0)
return-1 if numpy.sum(e)else min(g(f,h))
Try it online!
Takes input as two numpy arrays.
Explanation:
First, e=a-b
determines which positions change from one grid to the other.
The next line finds all of the differences and sorts them into the lists f
and h
. If the sum of differences is not zero, meaning there are more 1s in
one grid than the other, this returns -1
, or the minimum of all possible
paths found by g
.
This is the fun part. Essentially, in order to make this work, a 1 must move from one position to another by swapping. Therefore, the minimum number of swaps for each pair is the manhattan distance between them, found in d
. g
finds every possible pairing between start and end points and returns a list
of the total distances between them. If there is only one start point, it returns the distance between that and the end point. Beyond that, it pairs the first start point with each end point iteratively and adds their distance to the total distance of the rest of the points, calculated recursively.
I have a strong feeling that this is a duplicate but can't find one... – Giuseppe – 2019-08-19T16:57:25.980
1
Also, I highly recommend using The Sandbox to get feedback on a challenge. I do think this is an interesting challenge, but you only stand to gain by posting there first, and at worst you lose a day or two.
– Giuseppe – 2019-08-19T16:59:39.3802What is a swap? Please define it more rigorously. – HyperNeutrino – 2019-08-19T17:02:16.483
1Is it adjacent? Any two items? Cycling any n items? – wizzwizz4 – 2019-08-19T17:03:01.717
2@HyperNeutrino, by swap I mean exchanging the values of two adjacent cells. – Kiara Dan – 2019-08-19T17:07:46.203
@wizzwizz4, swap between adjacent elements, no cycles. – Kiara Dan – 2019-08-19T17:08:29.570
3Guys, cmon, this challenge is clear enough. Don't make it a clown fiesta. I don't have any problem with understanding the challenge. – Krzysztof Szewczyk – 2019-08-19T17:27:00.083
1When you say "adjacent" do you include diagonally adjacent? – Luis Mendo – 2019-08-19T17:30:27.297
@LuisMendo, only horizontally and vertically adjacent. – Kiara Dan – 2019-08-19T18:19:24.863
2Is this a question from another site? – xnor – 2019-08-19T19:35:01.840
1@KrzysztofSzewczyk Even after you commented that, there was still a clarification to be made. So, stop berating others for requesting clarification; just because (you think) you get it doesn't mean others have to. – HyperNeutrino – 2019-08-20T13:55:45.340
@HyperNeutrino I feel like these guys never played three-in-a-row game - you can request clarification - but sometimes the challenge is clear enough, and if you don't get it just abadon it because you are making people angry and not helping – Krzysztof Szewczyk – 2019-08-20T14:51:33.723
1@KrzysztofSzewczyk If a challenge isn't clear and we can't request clarification, the alternative method is to VTC it, which is much worse of a solution for an almost perfectly fine challenge otherwise. I don't see OP getting angry; if you are, you have every right to ignore our comments. – HyperNeutrino – 2019-08-20T16:11:12.410