Socket - Plug compatibility

19

0

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.

Asone Tuhid

Posted 2018-07-15T16:54:21.273

Reputation: 1 944

My country uses only C and F, I have also seen J... but why are there so many??? – AlexRacer – 2018-07-16T15:00:11.740

1

@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 – 2018-07-17T08:16:53.797

Answers

7

Retina 0.8.2, 30 29 bytes

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

Try it online! Link includes test cases.

Neil

Posted 2018-07-15T16:54:21.273

Reputation: 95 035

7

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:

Neil

Posted 2018-07-15T16:54:21.273

Reputation: 2 417

1lambda 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 – 2018-07-15T18:23:07.210

@Neil Thanks I updated that. – Neil – 2018-07-15T18:24:26.503

3So funny haha Better Niel – Luis felipe De jesus Munoz – 2018-07-15T18:26:43.523

6

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!

machina.widmo

Posted 2018-07-15T16:54:21.273

Reputation: 101

Found a typo that caused C, C to return false. – machina.widmo – 2018-07-15T19:04:48.907

Nice! q= can be omitted since the function is anonymous. Also, there's an extra space. 70 bytes. Try it online.

– Chas Brown – 2018-07-15T19:51:45.283

Thanks! For some reason I thought that the lambda would capture y, which is why I had written it that way initially. – machina.widmo – 2018-07-15T20:02:10.493

Welcome to PPCG, and nice first post! – user202729 – 2018-07-16T07:44:29.140

@user202729: Hadn't seen that before; nice tip to know. – Chas Brown – 2018-07-16T08:37:35.783

@ChasBrown That really confuses me. >.< I thought that cokment was of OP so I deleted the comment. – user202729 – 2018-07-16T12:16:15.413

4

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!

ErikF

Posted 2018-07-15T16:54:21.273

Reputation: 2 149

Using the reference you provided here about x86 architectures: you can save 3 bytes by doing 1<<~-b.

– Arnauld – 2018-07-16T10:13:08.937

66 bytes by getting rid of d and the ternary operator. – Arnauld – 2018-07-16T10:21:28.383

62 bytes by doubling the bitmasks. Now beating all languages but Jelly and Retina! \o/ – Arnauld – 2018-07-16T10:29:39.563

160 bytes by just comparing a with b. – Arnauld – 2018-07-16T10:43:39.040

Suggest L"\6\0\xdd78\0襰襰" instead of {6,0,56696,0,35184,35184} – ceilingcat – 2018-07-31T17:01:08.770

3

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!

nimi

Posted 2018-07-15T16:54:21.273

Reputation: 34 639

3

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

FireFly

Posted 2018-07-15T16:54:21.273

Reputation: 7 107

3

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

Jonathan Allan

Posted 2018-07-15T16:54:21.273

Reputation: 67 804

2

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.

Night2

Posted 2018-07-15T16:54:21.273

Reputation: 5 484

2

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!

JayCe

Posted 2018-07-15T16:54:21.273

Reputation: 2 655

do you need t= in rep? – Giuseppe – 2018-08-07T14:49:10.337

@Giuseppe looks like I do not! – JayCe – 2018-08-07T14:52:32.953

IDK why this saved so many bytes – Giuseppe – 2018-08-07T14:58:20.610

@Giuseppe It totally looks like an esoteric language submission now. – JayCe – 2018-08-07T15:05:55.013

2

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

Qwertiy

Posted 2018-07-15T16:54:21.273

Reputation: 2 697

-1 byte with currying – Asone Tuhid – 2018-07-19T19:22:52.393

@AsoneTuhid, thank you, updated. But your link is smth strange and doesn't open. – Qwertiy – 2018-07-19T21:25:37.020

It's TIO. Opens for me. – Asone Tuhid – 2018-07-20T05:39:53.710

@AsoneTuhid, ERR_CONNECTION_RESET – Qwertiy – 2018-07-20T07:04:19.327

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 – 2018-07-20T12:12:40.770

@AsoneTuhid, no tio does NOT work with ERR_CONNECTION_RESET, niether https, nor http. – Qwertiy – 2018-07-20T13:15:40.693

That's gotta be a problem with your system then – Asone Tuhid – 2018-07-20T15:29:08.327

@AsoneTuhid, can't open on 2 different computers in different networks. – Qwertiy – 2018-07-20T16:30:20.960

How about this?

– Asone Tuhid – 2018-07-20T19:14:38.507

Let us continue this discussion in chat.

– Qwertiy – 2018-07-20T19:47:07.017

0

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.

AlexRacer

Posted 2018-07-15T16:54:21.273

Reputation: 979