# Find the Best Set of Adapters

I'm trying to plug this really old phone into my computer but the phone seems to use a very obscure plug. Luckily I have some adapters. Unfortunately, I can't figure out which of them to use to connect my phone to my computer. Can you find the smallest number of adapters that can link my phone and computer?

## Input

A pair of strings representing the phone and the computer's port type and a list of pairs of strings where the pair A, B transforms an input of type A to type B. Example:

("A", "D")
[
("A", "B"),
("C", "D"),
("B", "C"),
]

The first pair ("A", "D") is the desired connection. The rest are adapters.

## Challenge

Output the number of adapters required to connect the phone to the computer. In the above example, the answer would be 3 as all 3 adapters would be required.

Phone, Computer
TypeA -> TypeB
TypeC -> TypeD
Output
A, D
A -> B
B -> C
C -> D
3
A, Z
A -> B
B -> C
C -> D
D -> Z
X -> Y
Y -> Z
4
A, B
A -> B
X -> Y
Y -> Z
1
A, C
A -> B
B -> C
C -> A
2
A, C
A -> A
B -> B
C -> C
A -> B
B -> C
2

## Rules

• Standard loopholes disallowed
• This is , shortest code wins
• You can assume it is possible to make the connection
• The port on the phone and computer will not be of the same type
• There may be adapters of type A -> A
• It is not guaranteed that all of the adapters will be possible to use
• You may take input in any form that is convenient
• So we take a (directed?) graph and find the shortest path?
– user
Mar 19 '21 at 16:49
• Pretty much yup Mar 19 '21 at 16:50
• Looks similar to a hackerrank question I tried solving earlier. If your question is take from a similar website, do credit it. Mar 19 '21 at 17:20
• @Razetime I did not take this question from hakerrank. I came up with it myself. Mar 19 '21 at 17:39
• "Pretty much yup" + "You may take input in any form that is convenient" = Can I take the connections as an adjacency matrix? Mar 20 '21 at 3:39

# Jelly, 13 bytes

ṗJ$Ẏy@ƒ⁼⁵ɗƇẈṂ Try it online! Full program taking a list of pairs L (e.g. ['AB', 'BC', 'CD', 'DZ', 'XY', 'YZ']) as the first argument, the source as the second and the target as the third. ## How it works Brute-force approach. ṗJ$Ẏy@ƒ⁼⁵ɗƇẈṂ - Main link. Takes L on the left and S on the right
J            -   Yield the indices of L; [1, 2, 3, ..., len(L)]
ṗ             -   Get the n'th Cartesian power of L for each n in this range
Ẏ          - Flatten into a list of lists
ɗƇ   - Keep those for which the following is true:
ƒ       -   Reduce the list by the following, starting with S:
y@        -     Translate, according to the pair mapping
⁼⁵     - Does the final result equal T?
ẈṂ - Get the length of the shortest element
• Doesn't work if the adapters are in the wrong order. Try it online! Mar 19 '21 at 17:32
• @xigoi Fixed, thanks! Mar 19 '21 at 17:35

# J, 28 bytes

1 :'[:#+./ .*^:(1-u{])^:a:~'

Try it online!

Perhaps stretching the definition of convenient input, I take the adapter list as an adjacency matrix, and the target connection as a boxed pair of indexes (the noun being modified).

The test cases include an (ungolfed) conversion step, so if this is not allowed it can be easily corrected, though the byte count will about double.

## main idea

Simply multiply the adjacency matrix by itself until the cell representing the target adapter becomes 1, keeping track of the intermediate matrices.

The length of that list is the minimal number of adapters.

## J, 30 byte using a verb rather than adverb

[:#(,{.+/ .*{:)@]^:(-.@{{:)^:_

Try it online!

# Wolfram Language (Mathematica), 13 bytes

GraphDistance

Try it online!

...and here's the built-in.

# JavaScript (ES6), 78 bytes

Expects (target, list)(source), where list is an array of string pairs.

(b,c,m)=>g=(a,n)=>a==b?m<n?m:m=n:c.map(p=>p[0]==a?p[g(p[1],p[0]=-~n),0]=a:0)|m

Try it online!

### Commented

(                      // outer function:
b,                   //   b   = target plug
c,                   //   c[] = list of connectors
m                    //   m   = minimum number of connections
) =>                   //
g = (                  // inner recursive function:
a,                   //   a = current plug
n                    //   n = number of connections
) =>                   //
a == b ?             // if a = b:
m < n ? m          //   update m to min(m, n)
: m = n      //   (the update is forced if m is still undefined)
:                    // else:
c.map(p =>         //   for each pair p[] in c[]:
p[0] == a ?      //     if the input plug of p is a:
p[             //
g(           //       recursive call:
p[1],      //         set a to the output plug p[1]
p[0] = -~n //         increment the number of connections and
//         invalidate p[0] so that it's not used again
),           //       end of recursive call
0            //
] = a          //       restore p[0] to a afterwards
:                //     else:
0              //       do nothing
) | m              //   end of map(); yield m
• (b,c,m)=>g=(a,n=0)=>a==b?m<n?m:m=n:c[n]&&c.map(p=>p[0]==a&&g(p[1],-~n))|m
– tsh
Mar 21 '21 at 22:44

# Charcoal, 40 bytes

⊞υθＦυＦζＦ›⁼§ι±¹§κ⁰№ι§κ¹⊞υ⁺ι§κ¹Ｉ⊖⌊ＥΦυ№ιηＬι

Try it online! Link is to verbose version of code. Explanation:

⊞υθ

Start a breadth-first search using the computer's port.

Ｆυ

Loop over all chains of adapters tried so far.

Ｆζ

Loop over all of the adapters.

Ｆ›⁼§ι±¹§κ⁰№ι§κ¹

Can this adapter be plugged in to provide a port not yet seen in this chain?

⊞υ⁺ι§κ¹

If so then save this new chain to the search list.

Ｉ⊖⌊ＥΦυ№ιηＬι

Print the shortest number of adapters that result in the desired output port.

# Java, 474 bytes

This runs a single breadth-first search (BFS) while keeping track of the distances to each node.

Try it online!

• 462 bytes Mar 22 '21 at 3:54

# JavaScript (Node.js), 73 bytes

(b,c,m)=>g=(a,n=0)=>a==b?m<n?m:m=n:c[n]&&c.map(p=>p[0]==a&&g(p[1],n+1))|m

Try it online!