20
\$\begingroup\$

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.

For reference.

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.

\$\endgroup\$
2
  • \$\begingroup\$ My country uses only C and F, I have also seen J... but why are there so many??? \$\endgroup\$
    – AlexRacer
    Jul 16, 2018 at 15:00
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Jul 17, 2018 at 8:16

11 Answers 11

7
\$\begingroup\$

Retina 0.8.2, 30 29 bytes

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

Try it online! Link includes test cases.

\$\endgroup\$
7
\$\begingroup\$

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:

\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – Neil
    Jul 15, 2018 at 18:23
  • \$\begingroup\$ @Neil Thanks I updated that. \$\endgroup\$
    – Neil
    Jul 15, 2018 at 18:24
  • 4
    \$\begingroup\$ So funny haha Better Niel \$\endgroup\$ Jul 15, 2018 at 18:26
6
\$\begingroup\$

Python 3, 72 Bytes 73 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!

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

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!

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

Haskell, 67 bytes

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!

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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

\$\endgroup\$
3
\$\begingroup\$

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!

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

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

Or Try it online!

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.

\$\endgroup\$
2
\$\begingroup\$

Javascript ES6, 66 65 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)))

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

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.