Wanna be an Alchemist

3

1

Wanna be an Alchemist

I've been watching Full Metal Alchemist these past days and I've been wondering, It is really possible to trans-mutate matter? In the real world, I don't know if it is possible, but in the machine world, we think we "can".


Problem

Your task is, Given an input of N chemical elements (K, Mg, C, O, etc...), you are going to:

  1. Get the first letter of the element
  2. Convert that letter into binary
  3. Rotate 2 bits to the right
  4. Apply Binary Addition
  5. If the result is more that 8 bits then get the one's complement of the result and removes the highest bit
  6. Output the ASCII (ISO 8859-1) representation of the result

Example

Given Chemical Elements: (K, Ca, Mg)

  1. Get the first letter of the element

K, C, M

  1. Convert that letter into binary

K => 01001011, C => 01000011, M => 01001101

  1. Rotate 2 bits to the right

K => 11010010, C => 11010000, M => 01010011

  1. Sum bits

    K => 11010010 + C => 11010000 + M => 01010011 = 111110101

  2. If the result is more that 8 bits then get the one's complement and removes the highest bit

00001010

  1. Output the ASCII representation of the result. Result:

Test Cases

(H, Mn, C) =>  Ê

(I, H, Se) =>  Ç

(K, Ca, Mg) => 

This is code golf so the shortest answer wins

Luis felipe De jesus Munoz

Posted 2018-03-06T20:55:03.553

Reputation: 9 639

When you say "Convert that letter into binary" am I correct in thinking you mean using ASCII? Or is it ok to use EBDIC or UTF-32? :) – Andrew – 2018-03-06T20:58:16.163

@AndrewPiliser ASCII. Sorry if i was misunderstood – Luis felipe De jesus Munoz – 2018-03-06T20:59:50.953

1

This is a great idea! In the future though, if you have an idea but it isn't a complete challenge, or if you just need help, you can use the Sandbox.

– NoOneIsHere – 2018-03-06T21:01:01.960

6The binary sum question needs addressing, the test cases don't work and the given link is about adding numbers that are represented in binary (which does not work out with the examples either). I am voting to close this as unclear in the interim (i.e. will vote to reopen when that is addressed). – Jonathan Allan – 2018-03-06T21:13:00.307

@totallyhuman yuup, I really don't know why I thought I read that. – Magic Octopus Urn – 2018-03-06T21:58:35.240

1Step 3 of the problem and example don't match (shift discards bits, rotate keeps them). Also, your test cases are wrong because the problem requires "ASCII" and the result characters are not in the seven-bit character set known as ASCII (perhaps you want ISO 8859-1?) – Fox – 2018-03-07T14:00:32.190

I'd recommend changing "ASCII" to "Unicode" and remove the requirement to check whether the result is printable or not (allow the output to be the character, no matter what it is) – caird coinheringaahing – 2018-03-07T14:06:00.807

@Fox Sorry, I meant the Extended ASCII (ISO 8859) that includes 8 bits . About the shit, I meant rotate. I'll update my question – Luis felipe De jesus Munoz – 2018-03-07T14:16:16.380

@cairdcoinheringaahing from 00000000 to 00011111 are non printable characters. – Luis felipe De jesus Munoz – 2018-03-07T14:19:10.163

@LuisfelipeDejesusMunoz 1) That doesn't include all the non-printable characters and 2) it adds unnecessary overhead to the challenge, which isn't recommended – caird coinheringaahing – 2018-03-07T14:20:33.120

@cairdcoinheringaahing ok, so just removing that part will be ok? – Luis felipe De jesus Munoz – 2018-03-07T14:22:16.410

Personally, I'd allow results with more than 8 bits to stay as they were (but that's up to you). If you remove the printable part, I'd definitely vote to reopen – caird coinheringaahing – 2018-03-07T14:23:46.160

Answers

1

Perl 5, 44 bytes

$n+=257*ord>>2&255}{$_=chr($n>>8?~$n%256:$n)

Try it online!

Ton Hospel

Posted 2018-03-06T20:55:03.553

Reputation: 14 114

2

Python 2, 78 bytes

e=sum(ord(e[0])/4+ord(e[0])%4*64for e in input());print chr([e,~e][e>255]%256)

Try it online!

-8 bytes thanks to Jonathan Frech

HyperNeutrino

Posted 2018-03-06T20:55:03.553

Reputation: 26 575

An imperative function is shorter. – Jonathan Frech – 2018-03-08T05:10:32.633

Python 2, 78 bytes.

– Jonathan Frech – 2018-03-08T05:12:28.110

@JonathanFrech oh thanks :D – HyperNeutrino – 2018-03-08T11:28:45.903