g | x | w | all
Bytes Lang Time Link
182Python3240717T011303ZAjax1234
123Haskell171001T220213ZZgarb
114Mathematica171003T154731ZDavidC
207Haskell170930T204511Zflawr
024Husk170930T213242ZZgarb
108Mathematica171001T042355ZBen
027Jelly170930T230803ZJonathan
175Mathematica170930T184733ZZaMoC

Python3, 182 bytes

def f(a,b):
 q,s=[(a,[a])],[]
 for a,c in q:
  if a==b:s+=[c];continue
  q+=[(u,c+[u])for i,j in enumerate(a)if(u:=a[:i]+str(int(not int(j)))+a[i+1:])not in c]
 return max(s,key=len)

Try it online!

Haskell, 141 123 bytes

c(a:b)=(1-a:b):map(a:)(c b)
c _=[]
q#z=[z]:[z:s|w<-c z,notElem w q,s<-(w:q)#w]
x!y=snd$maximum[(p*>x,p)|p<-[x]#x,last p==y]

Uses lists of integers. Try it online!

Explanation

The main function is !, and the auxiliary functions are # and c. Given a list of bits, c gives all possible ways of flipping one of them, e.g. [0,1,1] -> [[1,1,1],[0,0,1],[0,1,0]].

c(a:b)=        -- c on nonempty list with head a and tail b is
 (1-a:b):      -- the list with negated a tacked to b, then
 map(a:)(c b)  -- c applied recursively to b, with a tacked to each of the results.
c _=[]         -- c on empty list gives an empty list.

The function # takes a list of lists (the "memory") and a list (the "initial bitstring"). It constructs all hypercube paths that begin with the initial element, contain only distinct bitstrings, and do not step on the strings in the memory.

q#z=            -- # on memory q and initial string z is
 [z]:           -- the singleton path [z], and
 [z:s|          -- z tacked to each path s, where
  w<-c z,       -- w is obtained by flipping a bit of z,
  notElem w q,  -- w is not in the memory, and
  s<-(w:q)#w]   -- s is a path starting from w that avoids w and all elements of q.

The main function ! ties it all together. A trick I use here is p*>x (x repeated length p times) instead of length p. Since longer repetitions of x come later in the natural ordering of lists, maximum chooses the longest path in either case, since the first coordinates of pairs are compared before the second ones.

x!y=          -- ! on inputs x and y is
 snd$maximum  -- the second element of the maximal pair in
 [(p*>x,p)|   -- the list of pairs (p*>x,p), where
  p<-[x]#x,   -- p is a path starting from x that avoids stepping on x, and
  last p==y]  -- p ends in y.

Mathematica 116 114 bytes

With several bytes saved thanks to Misha Lavrov.

Last@FindPath[Graph[Rule@@@Cases[Tuples[Tuples[{0,1},{l=Length@#}],{2}],x_/;Count[Plus@@x,1]==1]],##,{1,2^l},Alll]&

Input (8 dimensions)

[{1,0,0,1,0,0,0,1},{1,1,0,0,0,0,1,1}]//AbsoluteTiming

Output (length = 254, after 1.82 seconds)

{1.82393, {{1, 0, 0, 1, 0, 0, 0, 1}, {0, 0, 0, 1, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 1, 0}, {0, 0,0, 0, 0, 0, 1, 1}, {0, 0, 0, 0, 0, 1, 1, 1}, {0, 0, 0, 0, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 1, 1, 0}, {0, 0, 0, 0,1, 1, 1,0}, {0, 0, 0, 0, 1, 0, 1, 0}, {0, 0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0, 1}, {0, 0, 0, 0, 1, 0, 1, 1}, {0, 0, 0, 0,1, 1, 1, 1}, {0, 0, 0, 0, 1, 1, 0, 1}, {0, 0, 0, 0, 1, 1, 0, 0}, {0, 0, 0, 1, 1, 1, 0, 0}, {0, 0, 0, 1, 0, 1, 0, 0}, {0, 0, 0, 1,0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 1, 0, 0, 1, 1}, {0, 0, 0, 1, 0, 1, 1, 1}, {0, 0, 0, 1, 0, 1, 0, 1}, {0, 0, 0, 1, 1, 1, 0, 1}, {0, 0, 0, 1, 1, 0, 0, 1}, {0, 0, 0, 1, 1, 0, 0, 0}, {0, 0, 0, 1, 1, 0, 1, 0}, {0, 0, 0, 1, 1, 0, 1, 1}, {0, 0, 0, 1,1, 1, 1, 1}, {0, 0, 0, 1, 1, 1, 1, 0}, {0, 0, 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 0}, {1, 1, 1, 1, 0, 0, 0, 0}, {1, 1, 1, 1,0, 0, 0, 1}, {1, 1, 1, 1, 1, 0, 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, 0}, {1, 1, 1, 1,1, 0, 1, 0}, {1, 1, 1, 1, 1, 0, 0, 0}, {1, 0, 1, 1, 1, 0, 0, 0}, {1, 0, 1, 1, 1, 0, 0, 1}, {1, 0, 1, 1, 1, 0, 1, 1}, {1, 1, 1, 1,1, 0, 1, 1}, {1, 1, 1, 1, 0, 0, 1, 1}, {1, 1, 1, 0, 0, 0, 1, 1}, {1, 1, 0, 0, 0, 0, 1, 1}}}

Tuples[{0,1},{l=Length@#}],{2}]& generates the numbers 0...8 as binary lists.

The outer Tuples...{2} produces all ordered pairs of those binary numbers.

Plus@@x sums each of the pairs, generating triplets of 0, 1.

Cases....Count[Plus@@x, 1]==1 returns all of the sums that contain a single 1. These arise when the two original binary numbers are connected by an edge.

Rules connects the vertices of the graph, each vertex being a binary number.

Graph creates a graph corresponding to said vertices and edges.

FindPath finds up to 2^n paths connecting vertex a to vertex b, the given numbers.

Last takes the longest of these paths.


For three dimensions, the graph can be represented in a plane as shown here:

graph flat

For the input, {0,0,0}, {1,1,1}, the following is output:

{{{0, 0, 0}, {0, 0, 1}, {0, 1, 1}, {0, 1, 0}, {1, 1, 0}, {1, 0, 0}, {1, 0, 1}, {1, 1, 1}}}

This path can be found in the above graph.

It can also be conceived as the following path in 3-space, where each vertex corresponds to a point {x,y,z}. {0,0,0} represents the origin and {1,1,1} represents the "opposite" point in a unit cube.

So the solution path corresponds to a traversal of edges along the unit cube. In this case, the path is Hamiltonian: it visits each vertex one time (i.e. with no crossings and no vertices omitted).

g4

Haskell, 212 207 bytes

This is probably way too long, but it finally works now. (Thanks to @Lynn for the cartesian product trick!) Thansk @nimi for -5 bytes!

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id$[0>1..]<$b]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!

Explanation:

b%l -- helper function:
    -- given a path l (that should end in b) this generates all possible extensions
    -- of l (if not possible also l itself) 
            x<-mapM id$[0>1..]<$b -- generate all possible vertices of the hypercube
             -- and check the criteria
           b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v] 
             -- extend if possible
    [l++[x|  ...                                                   ]| ... ]
b!a| -- actual function: 
     -- first define a helper function:
    f<-nub.concat.((b%)<$>)
     -- begin with the vertex a and apply the function from above repeatedly
     -- until you cannot make the path any longer without violating the
     -- criteria 
                                                                             until(f>>=(==))f[[a]]
     -- only take the paths that actually end in b          
                                                          filter((==b).last)$
     -- and find the one with the maximum length    
                           =snd$maximum$map(length>>=(,))$    

Husk, 27 26 24 bytes

→foΛεẊδṁ≠ÖLm↓≠⁰←ġ→PΠmṠe¬

Brute force, so very slow. Try it online!

Explanation

Husk reads naturally from right to left.

←ġ→PΠmṠe¬  Hypercube sequences ending in second input, say y=[1,1,0]
     mṠe¬  Pair each element with its negation: [[0,1],[0,1],[1,0]]
    Π      Cartesian product: [[0,0,1],[1,0,1],..,[1,1,0]]
   P       Permutations.
 ġ→        Group by last element
←          and take first group.
           The permutations are ordered so that those with last element y come first,
           so they are grouped together and returned here.

ÖLm↓≠⁰  Find first input.
  m     For each permutation,
   ↓≠⁰  drop all elements before the first input.
ÖL      Sort by length.

foΛεẊδṁ≠  Check path condition.
fo        Keep those lists that satisfy:
    Ẋ      For each adjacent pair (e.g. [0,1,0] and [1,1,0]),
      ṁ    take sum of
       ≠   absolute differences
     δ     of corresponding elements: 1+0+0 gives 1.
  Λε       Each value is at most 1.

→  Finally, return last element (which has greatest length).

Mathematica, 108 bytes

a=#~FromDigits~2+1&;Last@PadLeft[IntegerDigits[#-1,2]&/@FindPath[HypercubeGraph@Length@#,a@#,a@#2,∞,All]]&

Input:

[{0, 0, 0, 0}, {1, 1, 1, 1}]

Output:

{{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 1, 1}, {0, 0, 1, 0}, {0, 1, 1, 0},
 {0, 1, 0, 0}, {0, 1, 0, 1}, {1, 1, 0, 1}, {1, 0, 0, 1}, {1, 0, 0, 0},
 {1, 1, 0, 0}, {1, 1, 1, 0}, {1, 0, 1, 0}, {1, 0, 1, 1}, {1, 1, 1, 1}}

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).

Try it online!

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

Mathematica, 175 bytes

Nice first question!

(m=#;n=#2;Last@SortBy[(S=Select)[S[Rest@Flatten[Permutations/@Subsets[Tuples[{0,1},(L=Length)@m]],1],First@#==m&&Last@#==n&],Union[EditDistance@@@Partition[#,2,1]]=={1}&],L])&   


Input

[{0, 0, 0}, {1, 1, 1}]