# Socket - Plug compatibility

Traveling with electronics is always fun, especially when you need an adapter to charge them. Your challenge is to make planning a trip a little easier by checking if a given plug will be compatible with a given socket.

### Challenge

Given a plug type and a socket type, return a value that indicates whether they will work together or not.

### Compatibility table

Socket  Accepted plugs  |  Plug  Accepting Sockets
A       A               |  A     A, B
B       A, B            |  B     B
C       C               |  C     C, D, E, F, H, J, K, L, N, O
D       C, D, E, F      |  D     D
E       C, E, F         |  E     D, E, F, H, K, O
F       C, E, F         |  F     D, E, F, H, K, O
G       G               |  G     G
H       C, E, F, H      |  H     H
I       I               |  I     I
J       C, J            |  J     J
K       C, E, F, K      |  K     K
L       C, L            |  L     L
M       M               |  M     M
N       C, N            |  N     N
O       C, E, F, O      |  O     O


The tables show the same information, only transposed.

### Input

• The input will be given as two uppercase or two lowercase letters (you choose).

• Inputs will always be /[A-O]/ (or /[a-o]/), there's no need to handle invalid inputs.

• You may accept the two inputs in any order (please specify which).

• Input can be taken in any reasonable format (string, stdin, array, ...).

• If you take both inputs in a single string, they can be separated by no more than one character and there must be nothing surrounding them

• Good inputs: "G,K", "EF", "a b", ['l', 'o']

• Bad inputs: "K l", "f(O)(I)", [1,5]

### Output

• Output can be returned in any reasonable format.

• Output must be either truthy/falsy or one of 2 constant values

• Good outputs: false/any positive number, 1/2, 'T'/'F'

• Bad outputs: an even number/an odd number, 1/more than 1

### Examples

Using the format socket, plug => true / false.

A, A => true
I, K => false
O, C => true
C, O => false
E, F => true
F, E => true


Standard loopholes are disallowed.

This is so the answer with the fewest bytes in each language wins.

• My country uses only C and F, I have also seen J... but why are there so many??? – AlexRacer Jul 16 '18 at 15:00
• @AlexRacer Here, have some reading, there's also the weird shaver plug and the Italian extra wide plug and some others. Europlug is masterplug tho. – Asone Tuhid Jul 17 '18 at 8:16

# Retina 0.8.2, 30 29 bytes

(.)\1|[DEFHKO][CEF]|[JLN]C|BA


Try it online! Link includes test cases.

# Python 3, 76 bytes

lambda s,p:any([s==p,p in"CEF"and s in"DEFHKO",s=="B"<p,s in"JLN"and"C"==p])


Try it online!

# Credits:

• lambda s,p:any([s==p,p in"CEF"and s in"DEFHKO",s=="B"<p,s in"JLN"and"C"==p]) for 76 bytes? – Neil Jul 15 '18 at 18:23
• @Neil Thanks I updated that. – Neil Jul 15 '18 at 18:24
• So funny haha Better Niel – Luis felipe De jesus Munoz Jul 15 '18 at 18:26

# Python 3, 72 Bytes73 bytes 70 bytes

lambda p,s:s in{"A":"AB","C":y+"CJLN","E":y,"F":y}.get(p,p)
y="DEFHKO"


Try it online!

Edit: Thanks to Chas Brown for cutting some fat!

• Found a typo that caused C, C to return false. – machina.widmo Jul 15 '18 at 19:04
• Nice! q= can be omitted since the function is anonymous. Also, there's an extra space. 70 bytes. Try it online. – Chas Brown Jul 15 '18 at 19:51
• Thanks! For some reason I thought that the lambda would capture y, which is why I had written it that way initially. – machina.widmo Jul 15 '18 at 20:02
• Welcome to PPCG, and nice first post! – user202729 Jul 16 '18 at 7:44
• @user202729: Hadn't seen that before; nice tip to know. – Chas Brown Jul 16 '18 at 8:37

# C (gcc) (x86 architecture), 76 60 bytes

Many thanks to Arnauld for the changes!

Arguments are given in (plug, socket) order.

c[15]={6,0,56696,0,35184,35184};f(a,b){a=a==b|c[a-65]>>b&1;}


Try it online!

• Using the reference you provided here about x86 architectures: you can save 3 bytes by doing 1<<~-b. – Arnauld Jul 16 '18 at 10:13
• 66 bytes by getting rid of d and the ternary operator. – Arnauld Jul 16 '18 at 10:21
• 62 bytes by doubling the bitmasks. Now beating all languages but Jelly and Retina! \o/ – Arnauld Jul 16 '18 at 10:29
• 60 bytes by just comparing a with b. – Arnauld Jul 16 '18 at 10:43
• Suggest L"\6\0\xdd78\0襰襰" instead of {6,0,56696,0,35184,35184} – ceilingcat Jul 31 '18 at 17:01

p#s=p==s||or[elem s b|a:b<-words"AB CDEFHJKLNO EDFHKO FDEHKO",a==p]


Arguments to function # are two characters, plug first, socket second.

Try it online!

# JavaScript (Node.js), 79 bytes

S=>P=>P==S|P<'B'&S<'C'|P=='C'&'JLN'[K='includes'](S)|'CEF'[K](P)&'DEFHKO'[K](S)


Invoked as a curried function, f(socket)(plug).

Try it online! (includes testcases, showing a matrix of results.)

# Jelly, 31 bytes

Oḅ⁴_ȷe“j⁼⁽⁾ƇƑƓƘƝƤḄẸỊṂṢỴẒĊḞĿ‘o⁼/


A monadic link accepting a list of characters [plug,socket] which yields 1 if compatible or 0 if not.

Try it online! Or see a test-suite (which separates out the two classes).

# R, 132 129 113 bytes

function(S,P,!=utf8ToInt)"[<-"(diag(15),cbind(rep(!"",!"	"),!"
"),1)[-64+!P,-64+!S]


Try it online!

Builds the following matrix and extracts m[S,P] => 1 if TRUE, else 0.

       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
[1,]    1    1    0    0    0    0    0    0    0     0     0     0     0     0     0
[2,]    0    1    0    0    0    0    0    0    0     0     0     0     0    0     0
[3,]    0    0    1    1    1    1    0    1    0     1     1     1     0    1     1
[4,]    0    0    0    1    0    0    0    0    0     0     0     0     0    0     0
[5,]    0    0    0    1    1    1    0    1    0     0     1     0     0    0     1
[6,]    0    0    0    1    1    1    0    1    0     0     1     0     0    0     1
[7,]    0    0    0    0    0    0    1    0    0     0     0     0     0    0     0
[8,]    0    0    0    0    0    0    0    1    0     0     0     0     0    0     0
[9,]    0    0    0    0    0    0    0    0    1     0     0     0     0    0     0
[10,]    0    0    0    0    0    0    0    0    0     1     0     0     0    0     0
[11,]    0    0    0    0    0    0    0    0    0     0     1     0     0    0     0
[12,]    0    0    0    0    0    0    0    0    0     0     0     1     0    0     0
[13,]    0    0    0    0    0    0    0    0    0     0     0     0     1    0     0
[14,]    0    0    0    0    0    0    0    0    0     0     0     0     0    1     0
[15,]    0    0    0    0    0    0    0    0    0     0     0     0     0    0     1


Saved 3 bytes by compressing indexes using intToUtf8 and replacing this function with !. See History for more legible version.

Saved 16 bytes thanks to @Giuseppe!

• do you need t= in rep? – Giuseppe Aug 7 '18 at 14:49
• @Giuseppe looks like I do not! – JayCe Aug 7 '18 at 14:52
• IDK why this saved so many bytes – Giuseppe Aug 7 '18 at 14:58
• @Giuseppe It totally looks like an esoteric language submission now. – JayCe Aug 7 '18 at 15:05

# PHP, 81 bytes

<?list(,$s,$p)=$argv;$A=AB;$C=$E=$F=DEFHKO;$C.=JLN;echo+($p==$s||strpos(p,\$s));


To run it:

php -n -d error_reporting=0 <filename> <socket> <plug>


Example:

php -n -d error_reporting=0 socket_plug_compatibility.php F E


Notes:

• To save some bytes, I have used strings without single/double quotations as the string wrapper. Thus, the error_reporting=0 option is used to not output warnings.
• Input only works with uppercase letters.
• Outputs 1 for compatibility and 0 for non-compatibility.

# How?

Every plug is compatible with same socket as itself. There are four special plugs (A, C, E, F) which are compatible with a few more sockets as well. Four string variables with the name of the special plugs are defined to hold list of their extra compatible sockets.

It is checked if input plug and socket are same or if the socket is in the list of compatible sockets for that plug. The latter check is done with the help of PHP's variable variables.

# Javascript ES6, 6665 64 chars

p=>s=>p==s|{A:1,C:14172,E:8788,F:8780}[p]&(1<<parseInt(s,36)-11)


Takes capital letters into p for plug and s for socket, returns falsy (0) or truthy (1, 2, 4, ..., 8192) value.

### Test:

f=p=>s=>p==s|{B:1,C:14172,E:8788,F:8780}[p]&(1<<parseInt(s,36)-11)

console.log(A, A => true
I, K => false
O, C => true
C, O => false
E, F => true
F, E => true
.split
.map(x=>x.match(/(.), (.) => (\w+)/))
.every(([m,p,s,res])=>!!f(s)(p)==eval(res)))

• -1 byte with currying – Asone Tuhid Jul 19 '18 at 19:22
• @AsoneTuhid, thank you, updated. But your link is smth strange and doesn't open. – Qwertiy Jul 19 '18 at 21:25
• It's TIO. Opens for me. – Asone Tuhid Jul 20 '18 at 5:39
• @AsoneTuhid, ERR_CONNECTION_RESET – Qwertiy Jul 20 '18 at 7:04
• Did you try removing https? Do other TIO links work? I tried on mac (chrome and safari) and android and it works fine... – Asone Tuhid Jul 20 '18 at 12:12

# Pascal (FPC), 113 bytes

var p,s:char;begin read(p,s);write((p=s)or(pos(p,'CEF')>0)and(pos(s,'DEFHKO')>0)or(pos(p+s,'AB CJ CL CN')>0))end.


Try it online!

Test for all values

Following the DEFHKO train...

pos(string1,string2) checks for first occurence of string1 in string2 and returns its position in it or 0 if it doesn't exist.