Jelly, 25 27 bytes
+2 bytes to fix a bug with my golfing :( hopefully I'll find a shorter way though.
ṫi¥³ḣi
L2ṗŒ!瀵ạ2\S€ỊẠ×LµÞṪ
A full program taking the bit-strings using 1
and 2
* as lists. The arguments are from
and to
. The program prints a list of lists of the same.
* 0
and 1
may be used instead at the cost of a byte (add ’
between L2ṗ
and Œ!ç€...
to decrement).
How?
updating...
ṫi¥³ḣi - Link 1, getSlice: list of lists, bitstrings; list, toBitstring
³ - get 3rd command line argument (fromBitstring)
¥ - last two links as a dyad:
i - index (of fromBitstring in bitstrings)
ṫ - tail (bitstrings) from (that) index
i - index (of toBitstring in that result)
ḣ - head to (that) index
L2ṗŒ!瀵ạ2\S€ỊẠ×LµÞṪ - Main link: list, fromBitstring; list, toBitstring
L - length (of fromBitstring)
2 - literal two
ṗ - Cartesian power (of implicit range(2)=[1,2] with L(fromBitstring))
- ...i.e. all unique bitstrings of the required length (using [1,2])
Œ! - all permutations (of that list)
ç€ - call the last link (1) as a dyad (i.e. f(that, toBitstring))
µ µÞ - sort by the monadic function:
2\ - 2-wise reduce with:
ạ - absolute difference
S€ - sum €ach
Ị - insignificant (vectorises) (abs(z)<=1 - for our purposes it's really just used for z==1 since only positive integers are possible)
Ạ - all truthy? (1 if so 0 otherwise)
L - length
× - multiply
Ṫ - tail (the last one is one of the maximal results)
- implicit print