3
\$\begingroup\$

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

\$\endgroup\$
12
  • \$\begingroup\$ 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? :) \$\endgroup\$
    – Andrew
    Commented Mar 6, 2018 at 20:58
  • \$\begingroup\$ @AndrewPiliser ASCII. Sorry if i was misunderstood \$\endgroup\$ Commented Mar 6, 2018 at 20:59
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 6, 2018 at 21:01
  • 6
    \$\begingroup\$ The 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). \$\endgroup\$ Commented Mar 6, 2018 at 21:13
  • 1
    \$\begingroup\$ Step 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?) \$\endgroup\$
    – Fox
    Commented Mar 7, 2018 at 14:00

2 Answers 2

2
\$\begingroup\$

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

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

Perl 5, 44 bytes

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

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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