9
\$\begingroup\$

Your task is to take an string containing an isotope of an element as input, encoded like the following example with the atomic number followed by a space and the IUPAC chemical symbol for the element:

162 Dy

and return the number of neutrons in an atom of that isotope.

In the above example, dysprosium-162 has 96 neutrons (162 total nucleons, minus 66 protons because it's dysprosium), so the output should be 96.

You may assume that the element given will be one of the 114 elements currently given permanent names by the IUPAC (including flerovium and livermorium) and not a generic name such as Uus for "ununseptium". You may also assume that the atomic number of the isotope will not exceed 1000, or be less than the number of protons in the element.

You may not use built-ins to retrieve data about the proton or neutron number of elements, or use any function within your code that evaluates a string or number token as code on its own.

The program to use the fewest tokens to do this in any language wins. However, for the purpose of this challenge, every character in a string, or a variable name converted into a string, counts as a token.

List of elements and their atomic number for reference:

{
    "H": 1,
    "He": 2,
    "Li": 3,
    "Be": 4,
    "B": 5,
    "C": 6,
    "N": 7,
    "O": 8,
    "F": 9,
    "Ne": 10,
    "Na": 11,
    "Mg": 12,
    "Al": 13,
    "Si": 14,
    "P": 15,
    "S": 16,
    "Cl": 17,
    "Ar": 18,
    "K": 19,
    "Ca": 20,
    "Sc": 21,
    "Ti": 22,
    "V": 23,
    "Cr": 24,
    "Mn": 25,
    "Fe": 26,
    "Co": 27,
    "Ni": 28,
    "Cu": 29,
    "Zn": 30,
    "Ga": 31,
    "Ge": 32,
    "As": 33,
    "Se": 34,
    "Br": 35,
    "Kr": 36,
    "Rb": 37,
    "Sr": 38,
    "Y": 39,
    "Zr": 40,
    "Nb": 41,
    "Mo": 42,
    "Tc": 43,
    "Ru": 44,
    "Rh": 45,
    "Pd": 46,
    "Ag": 47,
    "Cd": 48,
    "In": 49,
    "Sn": 50,
    "Sb": 51,
    "Te": 52,
    "I": 53,
    "Xe": 54,
    "Cs": 55,
    "Ba": 56,
    "La": 57,
    "Ce": 58,
    "Pr": 59,
    "Nd": 60,
    "Pm": 61,
    "Sm": 62,
    "Eu": 63,
    "Gd": 64,
    "Tb": 65,
    "Dy": 66,
    "Ho": 67,
    "Er": 68,
    "Tm": 69,
    "Yb": 70,
    "Lu": 71,
    "Hf": 72,
    "Ta": 73,
    "W": 74,
    "Re": 75,
    "Os": 76,
    "Ir": 77,
    "Pt": 78,
    "Au": 79,
    "Hg": 80,
    "Tl": 81,
    "Pb": 82,
    "Bi": 83,
    "Po": 84,
    "At": 85,
    "Rn": 86,
    "Fr": 87,
    "Ra": 88,
    "Ac": 89,
    "Th": 90,
    "Pa": 91,
    "U": 92,
    "Np": 93,
    "Pu": 94,
    "Am": 95,
    "Cm": 96,
    "Bk": 97,
    "Cf": 98,
    "Es": 99,
    "Fm": 100,
    "Md": 101,
    "No": 102,
    "Lr": 103,
    "Rf": 104,
    "Db": 105,
    "Sg": 106,
    "Bh": 107,
    "Hs": 108,
    "Mt": 109,
    "Ds": 110,
    "Rg": 111,
    "Cn": 112,
    "Fl": 114,
    "Lv": 116
}

\$\endgroup\$
  • \$\begingroup\$ Ahaha right, just read the tag wiki. Seems like I misunderstood what the tag encompassed. \$\endgroup\$ – Sp3000 Dec 10 '14 at 4:53
  • 1
    \$\begingroup\$ In general, atomic code golf is better than code golf in terms of fairness between languages because it doesn't favour golfing-specific languages like CJam or Golfscript. \$\endgroup\$ – Joe Z. Dec 10 '14 at 5:56
  • 2
    \$\begingroup\$ Are numbers considered as single token ? Like in my answer, is 65 a single token or 2 tokens ? \$\endgroup\$ – Optimizer Dec 10 '14 at 8:09
  • 4
    \$\begingroup\$ "In general, atomic code golf is better than code golf in terms of fairness between languages because it doesn't favour golfing-specific languages like CJam or Golfscript." Or does it? ;) \$\endgroup\$ – Martin Ender Dec 10 '14 at 9:25
  • 1
    \$\begingroup\$ If built-ins were allowed, the following function, written in Mathematica, might be used: f[i_] := {n = ElementData[#[[2]], ToString@"StandardName"] <> ToString[#[[1]]], IsotopeData[n, "NeutronNumber"]} &[i] \$\endgroup\$ – DavidC Dec 10 '14 at 15:29
5
\$\begingroup\$

CJam, 120 6 or 116 tokens

If numbers are single token, then we have the whole code as 6 tokens:

212065132249371573644124577121541653292994215804249878509033804566332949723850494006885998432589817663973272353884582668784347806472090795216938149345570424681103578762377771127645936569136562173556524971260383556793671988140498193154122873972644190003488894117491373991987567199279175132071186860926217964652052981868515510731685503564515183920386542420190587555479397630718313762477968862282786518176572529906744217608955037610524188966159703528812103601771438848515748286560373093942224945233978303729665751831532717 128b:c~

where the tokens are

212065132249371573644124577121541653292994215804249878509033804566332949723850494006885998432589817663973272353884582668784347806472090795216938149345570424681103578762377771127645936569136562173556524971260383556793671988140498193154122873972644190003488894117491373991987567199279175132071186860926217964652052981868515510731685503564515183920386542420190587555479397630718313762477968862282786518176572529906744217608955037610524188966159703528812103601771438848515748286560373093942224945233978303729665751831532717
128
b
:
c
~

This code is equivalent to the below code containing the string version of the big number in the above code. But due to a string, the below code has 116 tokens:

ri"ᘭ᛭绊ڏ晍嬨塐弶⛡ᠸ庐ᖩે槑湘ࡊ㚋䊌栕ᄂỗ∘抁埵ໂČ槩唹ᘇ穗≧ṷ㴛勤烓≿Ⲳ㇭Ȋ嬅͙獚簜䱡数㍉㉦䩛爈拴矍㚴燌㾄䱮⃜⢴ⶏ㯗႒ݘ੡䅄瞟⮘㢧⳻⮵∼䚽珯ほֹ㳰櫣ݰ牜᫦殙ᆌ穟䖻ᄭⓚ獙஧༧撒咛啺"2F#b57b65f+:cr2*2<#2/)-

Tokens (with explanation) are

r                           "Read the first input";
i                           "and convert it to integer";
"
91 character string         "Then this base converted string";
"
2
F
#                           "'s ASCII representation of each character gets converted";
b                           "to base 2**15";
57                          "which gets converted to";
b                           "base 57";
65                          "and we add 65 to each element in the base 57 array";
f
+
:
c                           "and convert each array element to character.";
r                           "Then read the next input string, which is atom's IUPAC";
2                           "double it";
*
2                           "and take only first 2 characters";
<
#                           "Find the occurrence of these 2 characters in the big string";
2                           "and divide the index by 2";
/
)                           "increment the index to counter 0 offset";
-                           "and subtract this number, which is the atomic number from"
                            "the input number of nucleons";

To run the above string, copy the code from this link as SE removes some characters while uploading the answer.

This can be reduced to 109 tokens too, but then SE won't let me upload my answer, throwing malformed URI exception.

Input goes like

162 Dy

output is like

96

The weird string is just a base encoded string containing all IUPAC names at their Atomic number * 2 - 1 index.

Try it online here

\$\endgroup\$
  • \$\begingroup\$ Why would numbers not be a single token? \$\endgroup\$ – Martin Ender Dec 10 '14 at 9:23
  • \$\begingroup\$ @MartinBüttner Well, if for the purpose of this question, strings are not single token, then numbers might not be too :) \$\endgroup\$ – Optimizer Dec 10 '14 at 9:24
  • 1
    \$\begingroup\$ This gives the wrong the results for Boron and Indium, because two-element letters starting with B and I appear before them. \$\endgroup\$ – Martin Ender Dec 10 '14 at 10:30
  • \$\begingroup\$ ^ You mean boron and iodine. Indium is In. \$\endgroup\$ – Joe Z. Dec 10 '14 at 19:06
  • \$\begingroup\$ Also, guys. That bug for B and I is fixed. \$\endgroup\$ – Optimizer Dec 10 '14 at 19:08
1
\$\begingroup\$

Python 3 with exec, 17 tokens

exec(int.to_bytes(42580960806925240587487231677747050990110980939298529158008049507419456038066480774222358994792932281429500848123044123619998194774734911333011516763318834841258668032468977581617546825403043048781904307873076644287421190283925612029151422009703963147720234582458918676020358978146687598642493196719470433413287097024943497230356536978257362073205770196031226838532057690859535911353521203287284228407660035870497366713816359382867026152168356178620422021081074864815228071041303891869741111572003521808946355179139580269537828514345177247630946236685801543450404664783011350766913659964138280312012942354586269107632396118108534925651704031851802293836135007879834261627022944650861299698061444211422731907625,295,'big'))

Python 3 without exec, 35 tokens

m,n=input().split()
print(int(m)-(0x50000003c00000000000000000000000000000000000000000000000000000000000000000000000000000000008c00000000000000000000000000000000000000000000000000000000d80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000116880005ad0000d002b832400000000000000000000000000000004c0000064f8000003806a0088015660000000000000000000000000000b00000000000ac0000000016efd12c0004b60000000000000000000000000000000000000000000000000000000000000000000000000000000000000000017a700760005400f400000000000001700a56c000000000000000000000000000004c0000000000000000000000000000000000000000000000000000000000000000002ee6000000000700000029e00522c0000000000000000000000000003680000002a3200000000000c00032800000000000000000000000000000e91c000ce000000000000000c000000000000e400000000000000000000000000000004800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009a000006200000000000000000000000000000000000000000000000000006c0000043000000000000509008000000000000000000000000000000000000000000000000000000000000082000007c0000000000000000000000000000000ae00000019390000000000068000000000000000000000000000000000fc06388000000000000000000000000000000000000000000000000108000000006e0000000000000000000000000000d2000000000000000000000000000074037300001be1808800000000c4e98000050000000000000000000000000000000046000000000061014f580001000000e0000000000000000000000000013eaa12400000017c680000002f0000059000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009c04a2f7001000007880e0001300d408012000060a0000000000000000000>>7*int(n,36)&127))

Ruby with eval, 17 tokens

eval [22146635005300445083784033446026580324048447941091204274213253110537099437818224958820496527991920943430421799402248351995854377736142191462062582991150146209770141259010870255095388946677505144127700666745571877848513432112199556183753919673308110291261587736766001750331575554182707626697924148465335917814388410868486650419909619279250674754531982074694183257024218097391705830277480110741636037821082572926228904583257826932150641336017429157246896262085081972165351023141358378905645164343005169041637147077645200273099823888392038731180787177889720531999846231330677441270444855911286469030157575699579420898896870179419644019549285098577609138033580761786048462145007410.to_s(16)].pack('H*')

Javascript, 49 tokens

m=prompt().split(/ /);
alert(m[0]-(function(){
_H_He_Li_Be_B_C_N_O_F_Ne_Na_Mg_Al_Si_P_S_Cl_Ar_K_Ca_Sc_Ti_V_Cr_Mn_Fe_Co_Ni_Cu_Zn_Ga_Ge_As_Se_Br_Kr_Rb_Sr_Y_Zr_Nb_Mo_Tc_Ru_Rh_Pd_Ag_Cd_In_Sn_Sb_Te_I_Xe_Cs_Ba_La_Ce_Pr_Nd_Pm_Sm_Eu_Gd_Tb_Dy_Ho_Er_Tm_Yb_Lu_Hf_Ta_W_Re_Os_Ir_Pt_Au_Hg_Tl_Pb_Bi_Po_At_Rn_Fr_Ra_Ac_Th_Pa_U_Np_Pu_Am_Cm_Bk_Cf_Es_Fm_Md_No_Lr_Rf_Db_Sg_Bh_Hs_Mt_Ds_Rg_Cn_Uut_Fl_Uup_Lv_
}).toString().split(/_/).indexOf(m[1]))

Just to illustrate Count big-ints/symbols/variables as N tokens in atomic-code-golf :)

\$\endgroup\$
  • \$\begingroup\$ Does disallowing eval and exec work against that? \$\endgroup\$ – Joe Z. Dec 10 '14 at 16:48
  • \$\begingroup\$ @JoeZ. Still vulnerable to "big integer lookup table" attack, see update. \$\endgroup\$ – kennytm Dec 10 '14 at 18:57
  • \$\begingroup\$ It seems like this problem was ill-formed from the start, then. \$\endgroup\$ – Joe Z. Dec 10 '14 at 19:05
0
\$\begingroup\$

Javascript, 42 tokens (?)

alert(parseInt(x=prompt().split(" "))-Object.getOwnPropertyNames({__H_HeLiBeB_C_N_O_F_NeNaMgAlSiP_S_ClArK_CaScTiV_CrMnFeCoNiCuZnGaGeAsSeBrKrRbSrY_ZrNbMoTcRuRhPdAgCdInSnSbTeI_XeCsBaLaCePrNdPmSmEuGdTbDyHoErTmYbLuHfTaW_ReOsIrPtAuHgTlPbBiPoAtRnFrRaAcThPaU_NpPuAmCmBkCfEsFmMdNoLrRfDbSgBhHsMtDsRgCn__Fl__Lv:0})[0].indexOf(x[1])/2)

PS: Where I can find some script to count tokens?

\$\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.