18
\$\begingroup\$

Electron Configurations

Your mission is to accept an element's atomic number as input, and output its electron configuration (e.g. 2,8,8,2 for calcium).

Input

An atomic number from 1 to 118. You may assume valid input. The atom is not charged (it has as many electrons as protons). You may not expect the input to be stored in a variable, and you must write a complete program.

Output

The number of electrons in each non-empty electron shell. I will be quite lenient with the output format; all of the following are acceptable, i.e. you may use any punctuation or whitespace to separate the numbers, and brackets of any sort are permitted. Please specify which is used.

  • 2,8,8,2
  • 2.8.8.2
  • 2, 8, 8, 2,
  • [2,8,8,2]
  • 2 8 8 2
  • ([2 [8]] [8] 2)

How Electrons Work

In atoms, electrons are ordered into "shells", which are energy levels. Each shell has a certain capacity, the maximum number of electrons that it is capable of holding. The shells are filled from the inside out, but not evenly. Your task is to determine, given an atomic number, how many electrons there are in each shell, according to this source.

Up to and including calcium (atomic number 20), the shells fill evenly and in order; the inner shell is first filled to its capacity of 2, the second to 8, then the third to 8 and the last to 2. The electron configuration of calcium is 2,8,8,2.

After calcium, things get complicated; further electrons go into the third shell, not the last one. To make things worse, vanadium (23) is 2,8,11,2, while chromium (24) is 2,8,13,1 and manganese (25) is 2,8,13,2.

There are some consistent patterns, however: a noble gas and the seven elements before it will always have the number of electrons in the outer shell increase from 1 to 8. For example:

  • gold (79): 2,8,18,32,18,1
  • mercury (80): 2,8,18,32,18,2
  • ...
  • astatine (85): 2,8,18,32,18,7
  • radon (86): 2,8,18,32,18,8

Rules

  • Standard loopholes are forbidden.
  • Libraries which existed prior to this challenge are permitted.
  • Any built-in or library features which specifically deal with atoms, molecules or chemistry are banned.
  • Lowest code length in bytes wins.
  • In the linked source, configurations of elements 103-118 are marked with (?), as they are predicted, and the elements are too unstable for this to be checked. For this challenge, assume them to be correct.
  • You may hardcode part or all of your data.
  • [NEW RULE] Please provide a base64 or an xxd dump of your files if you use control characters in them (as many answers seem to be doing)

WINNER: Dennis's CJam answer at 80 bytes!

\$\endgroup\$
14
\$\begingroup\$

CJam, 87 83 82 80 bytes

0"hTHøìð¼Ä¿håêÛ¾ªÔ¼"256b6b5a/4a8**li<{[33:B_B*65_B*1]=\_B%8=B\#*+}/Bb0-`

The above code contains unprintable characters.

Try it online in the CJam interpreter. If the link doesn't work for you, copy from this paste.

Background

To obtain the electron configuration of the Nth atom, we start with an atom without electrons and apply N transformations to it.

To reduce the byte count of the implementation, we represent the electron configuration of an atom as an integer. Each digit of that integer in base 33 corresponds to the number of electrons in a certain shells; the least significant digit represents the outer shell.

For example, the electron configuration of Molybdenum (42) is [2 8 18 13 1]. This corresponds to the integer 2 × 334 + 8 × 333 + 18 × 332 + 13 × 33 + 1 = 26,79,370.

Palladium (48) is a special case, which we treat as [2 8 18 18 0] instead of [2 8 18 18].

This convenient representation reduces the aforementioned transformations to simple arithmetic:

  • R += 1 (add an electron to the outer shell)
  • R += 33 (add an electron to the second outer shell)
  • R += 65 (add two electrons to the second outer shell; remove one from the first)
  • R += 1089 (add an electron to the third outer shell)
  • R += 2145 (add two electrons to the third outer shell; remove one from the second)
  • R *= 33, R += 1 (add a new shell containing a single electron)

All that's left is to somehow encode which transformation has to be applied to pass from a particular atom to the next. The differences of the integer representations of two consecutive atoms are as follows:

[1 1 65 1 1 1 1 1 1 1 2369 1 1 1 1 1 1 1 78401 1 33 33 33 65 1 33 33 33 65 1 1 1 1 1 1 1 2598017 1 33 33 65 33 1 65 33 65 1 1 1 1 1 1 1 1 85745345 1 33 1089 2145 1089 1089 1089 1089 33 2145 1089 1089 1089 1089 1089 33 33 33 33 33 33 33 65 33 1 1 1 1 1 1 1 2830095041 1 33 33 2145 1089 1089 2145 1089 33 2145 1089 1089 1089 1089 1089 65 1 33 33 33 33 33 33 65 1 1 1 1 1 1 1]

The unique differences in this array are the following:

[1 33 65 1089 2145 2369 78401 2598017 85745345 2830095041]

All but the first 5 correspond to instances when a new shell is added; these can be omitted since multiplying by 33 and adding 1 yields the same result as adding the difference.

Since we have to add a new shell if and only if the current atom has exactly eight electrons in its outer shell (with the exception of He (2) ↦ Li (3), which can be encoded as add 65), we can encode those transformations as add 1 and determine the need for multiplication on the fly.

Thus, if we define

X := 0
I := 0
L := [33 1089 65 2145 1]
T := [1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 3 1 0 0 0 3 1 1 1 1 1 1 1 1 1 0 0 3 0 1 3 0 3 1 1 1 1 1 1 1 1 1 1 0 2 4 2 2 2 2 0 4 2 2 2 2 2 0 0 0 0 0 0 0 3 0 1 1 1 1 1 1 1 1 1 0 0 4 2 2 4 2 0 4 2 2 2 2 2 3 1 0 0 0 0 0 0 3 1 1 1 1 1 1 1]

the electron configuration of the Nth atom can be computed as follows:

while(X < N):
    R *= (33 ** (R % 33 == 8))
    R += L[T[X]]
    X += 1

How it works

" Push 0, the initial value of R; convert the following array into an integer by
  considering it a base 256 number, then back to the array of its digits in base 6.       ";

0"hTHøìð¼Ä¿håêÛ¾ªÔ¼"256b6b

" Replace each 5 in the resulting array by [4 4 4 4 4 4 4 4]. This yields the array T
  from the above section.                                                                 ";

5a/4a8**

" Read an integer N from STDIN and discard all but the first N elements of T.             ";

li<

" For each element Y of the remainder of T, do the following:                             ";

{
    [33:B_B*65_B*1]=\   " Define B := 33, push L, retrieve L[Y] and swap it with R.       ";

    _B%8=B\#*           " Execute R *= 33 ** (R % 33 == 8).                               ";

    +                   " Execute R += L[Y].                                              ";
}/

" Convert R into the array of its digits in base 33, remove eventual zeros (Palladium)
  and replace the resulting array with its string representation.                         ";

Bb0-`

Example run

$ base64 -d > electrons.cjam <<< MCJoVEgM+OzwErzEGL9o5erbvqrUB4YZhrwRIjI1NmI2YjVhLzRhOCoqbGk8e1szMzpCX0IqNjVfQioxXT1cX0IlOD1CXCMqK30vQmIwLWA=
$ cksum electrons.cjam
3709391992 80 electrons.cjam
$ for i in {1..118}; do LANG=en_US cjam electrons.cjam <<< $i; echo; done
[1]
[2]
[2 1]
[2 2]
[2 3]
[2 4]
[2 5]
[2 6]
[2 7]
[2 8]
[2 8 1]
[2 8 2]
[2 8 3]
[2 8 4]
[2 8 5]
[2 8 6]
[2 8 7]
[2 8 8]
[2 8 8 1]
[2 8 8 2]
[2 8 9 2]
[2 8 10 2]
[2 8 11 2]
[2 8 13 1]
[2 8 13 2]
[2 8 14 2]
[2 8 15 2]
[2 8 16 2]
[2 8 18 1]
[2 8 18 2]
[2 8 18 3]
[2 8 18 4]
[2 8 18 5]
[2 8 18 6]
[2 8 18 7]
[2 8 18 8]
[2 8 18 8 1]
[2 8 18 8 2]
[2 8 18 9 2]
[2 8 18 10 2]
[2 8 18 12 1]
[2 8 18 13 1]
[2 8 18 13 2]
[2 8 18 15 1]
[2 8 18 16 1]
[2 8 18 18]
[2 8 18 18 1]
[2 8 18 18 2]
[2 8 18 18 3]
[2 8 18 18 4]
[2 8 18 18 5]
[2 8 18 18 6]
[2 8 18 18 7]
[2 8 18 18 8]
[2 8 18 18 8 1]
[2 8 18 18 8 2]
[2 8 18 18 9 2]
[2 8 18 19 9 2]
[2 8 18 21 8 2]
[2 8 18 22 8 2]
[2 8 18 23 8 2]
[2 8 18 24 8 2]
[2 8 18 25 8 2]
[2 8 18 25 9 2]
[2 8 18 27 8 2]
[2 8 18 28 8 2]
[2 8 18 29 8 2]
[2 8 18 30 8 2]
[2 8 18 31 8 2]
[2 8 18 32 8 2]
[2 8 18 32 9 2]
[2 8 18 32 10 2]
[2 8 18 32 11 2]
[2 8 18 32 12 2]
[2 8 18 32 13 2]
[2 8 18 32 14 2]
[2 8 18 32 15 2]
[2 8 18 32 17 1]
[2 8 18 32 18 1]
[2 8 18 32 18 2]
[2 8 18 32 18 3]
[2 8 18 32 18 4]
[2 8 18 32 18 5]
[2 8 18 32 18 6]
[2 8 18 32 18 7]
[2 8 18 32 18 8]
[2 8 18 32 18 8 1]
[2 8 18 32 18 8 2]
[2 8 18 32 18 9 2]
[2 8 18 32 18 10 2]
[2 8 18 32 20 9 2]
[2 8 18 32 21 9 2]
[2 8 18 32 22 9 2]
[2 8 18 32 24 8 2]
[2 8 18 32 25 8 2]
[2 8 18 32 25 9 2]
[2 8 18 32 27 8 2]
[2 8 18 32 28 8 2]
[2 8 18 32 29 8 2]
[2 8 18 32 30 8 2]
[2 8 18 32 31 8 2]
[2 8 18 32 32 8 2]
[2 8 18 32 32 10 1]
[2 8 18 32 32 10 2]
[2 8 18 32 32 11 2]
[2 8 18 32 32 12 2]
[2 8 18 32 32 13 2]
[2 8 18 32 32 14 2]
[2 8 18 32 32 15 2]
[2 8 18 32 32 16 2]
[2 8 18 32 32 18 1]
[2 8 18 32 32 18 2]
[2 8 18 32 32 18 3]
[2 8 18 32 32 18 4]
[2 8 18 32 32 18 5]
[2 8 18 32 32 18 6]
[2 8 18 32 32 18 7]
[2 8 18 32 32 18 8]
\$\endgroup\$
  • 1
    \$\begingroup\$ An upvote for the sheer amount of effort, sir. ;) \$\endgroup\$ – COTO Sep 18 '14 at 13:45
6
\$\begingroup\$

GolfScript (96 bytes)

Output is in the form

[2 8 18 18]

This uses a magic string which contains non-printable characters, so I'm giving the script in xxd format:

0000000: 7e30 5c27 0193 ca05 528e 6b25 e461 4d12  ~0\'....R.k%.aM.
0000010: 3195 9abf c9a4 bfad 588b d876 5e72 c82a  1.......X..v^r.*
0000020: 2dd3 6e92 4940 e00b 80dc 71f6 fc97 2732  -.n.I@....q...'2
0000030: 3536 6261 7365 2037 6261 7365 3c7b 2731  56base 7base<{'1
0000040: 0a29 0a5c 295c 0a2b 310a 2b29 0a40 2940  .).\)\.+1.+).@)@
0000050: 400a 402b 5c28 3227 6e2f 3d7e 7d2f 5d60  @.@+\(2'n/=~}/]`

For online testing I'm writing the magic string with escapes:

"\x01\x93\xCA\x05R\x8Ek%\xE4aM\x121\x95\x9A\xBF\xC9\xA4\xBF\xADX\x8B\xD8v^r\xC8*-\xD3n\x92I@\xE0\v\x80\xDCq\xF6\xFC\x97"

but it's equivalent when you're not running into problems of pasting characters into browser textareas.

The approach is to build a virtual machine with 7 instructions, each of which manipulates a list of electron counts. Then for element n we start with an electron count list of 0 and run the first n instructions from the list encoded by the magic string.

The instructions are:

  1. Add a new shell with 1 electron: 1
  2. Add an electron to the outer shell: )
  3. Add an electron to the next-to-outer shell: \)\
  4. Combine the two outer shells and add one electron: +). (This is used only for palladium).
  5. Combine the two outer shells and create a new shell with 1 electron: +1
  6. Add an electron to the third shell in: @)@@
  7. Add an electron to the third shell and move one from the second shell to the third shell. This only occurs when the outer shell has 2 electrons, so it's implemented as @+\(2 rather than the longer @2+@(@
\$\endgroup\$
  • \$\begingroup\$ What are your 7 instructions? \$\endgroup\$ – COTO Sep 12 '14 at 11:59
  • \$\begingroup\$ Ah. Thanks. I guess I'm going to need to pick up one of these esoteric golfing languages in order to win a golf challenge one of these days. I had a heck of a time writing compact code that could interpret two instructions, let alone seven. :P \$\endgroup\$ – COTO Sep 12 '14 at 12:12
  • \$\begingroup\$ Do I provide input on STDIN? (and then it appears on the stack)? \$\endgroup\$ – user16402 Sep 12 '14 at 19:35
  • \$\begingroup\$ @professorfish: Yes. GolfScript automatically places the input from STDIN on the stack. \$\endgroup\$ – Dennis Sep 12 '14 at 19:37
5
\$\begingroup\$

Python 2 (46+271=327)

Code:

print open('f').read().decode('zip').split(';')[input()]

File f, containing the following binary garbage (these are the char codes)

120, 156, 101, 146, 219, 21, 195, 32, 12, 67, 87, 233, 8, 193, 188, 204, 201, 254, 123, 21, 40, 46, 146, 253, 65, 163, 171, 10, 98, 199, 188, 233, 149, 87, 62, 243, 247, 179, 158, 121, 174, 50, 87, 157, 171, 205, 213, 231, 210, 181, 118, 66, 119, 70, 119, 74, 119, 78, 119, 82, 119, 86, 127, 233, 147, 183, 29, 182, 103, 156, 103, 122, 76, 36, 19, 249, 68, 167, 56, 78, 49, 81, 77, 52, 19, 118, 110, 210, 235, 100, 19, 197, 68, 53, 209, 76, 116, 19, 250, 23, 247, 36, 56, 107, 192, 139, 30, 208, 114, 211, 183, 96, 172, 121, 87, 123, 253, 6, 90, 175, 66, 23, 118, 66, 15, 216, 6, 118, 130, 205, 96, 63, 216, 18, 119, 197, 141, 185, 222, 6, 146, 36, 76, 138, 16, 101, 162, 66, 84, 29, 225, 153, 157, 254, 163, 90, 100, 32, 229, 135, 136, 106, 201, 226, 104, 16, 225, 136, 22, 38, 70, 97, 204, 140, 133, 177, 50, 246, 251, 33, 23, 170, 71, 97, 204, 140, 133, 177, 50, 54, 198, 206, 168, 14, 253, 155, 195, 187, 135, 55, 220, 103, 145, 199, 69, 230, 188, 157, 225, 63, 44, 207, 121, 25, 53, 26, 110, 75, 247, 9, 95, 170, 27, 187, 248, 201, 75, 28, 126, 152, 255, 111, 232, 41, 56, 62, 147, 130, 35, 193, 201, 193, 41, 193, 169, 193, 105, 193, 209, 80, 79, 172, 153, 111, 72, 188, 36, 241, 158, 196, 171, 18, 111, 203, 185, 16, 95, 151, 67, 8, 97

Base64:

eJxlktsVwyAMQ1fpCMG8zMn+exUoLpL9QaOrCmLHvOmVVz7z97Oeea4yV52rzdXn0rV2QndGd0p3
TndSd1Z/6ZO3HbZnnGd6TCQT+USnOE4xUU00E3Zu0utkE8VENdFMdBP6F/ckOGvAix7QctO3YKx5
V3v9BlqvQhd2Qg/YBnaCzWA/2BJ3xY253gaSJEyKEGWiQlQd4Zmd/qNaZCDlh4hqyeJoEOGIFiZG
YcyMhbEy9vshF6pHYcyMhbEyNsbOqA79m8O7hzfcZ5HHRea8neE/LM95GTUabkv3CV+qG7v4yUsc
fpj/b+gpOD6TgiPBycEpwanBacHRUE+smW9IvCTxnsSrEm/LuRBfl0MIYQ==

On request, this is now a full program rather than a function.

Old answer: Python (Naive baseline, 422):

f=lambda n:'eJxlktsVwyAMQ1fpCMG8zMn+exUoLpL9QaOrCmLHvOmVVz7z97Oeea4yV52rzdXn0rV2QndGd0p3TndSd1Z/6ZO3HbZnnGd6TCQT+USnOE4xUU00E3Zu0utkE8VENdFMdBP6F/ckOGvAix7QctO3YKx5V3v9BlqvQhd2Qg/YBnaCzWA/2BJ3xY253gaSJEyKEGWiQlQd4Zmd/qNaZCDlh4hqyeJoEOGIFiZGYcyMhbEy9vshF6pHYcyMhbEyNsbOqA79m8O7hzfcZ5HHRea8neE/LM95GTUabkv3CV+qG7v4yUscfpj/b+gpOD6TgiPBycEpwanBacHRUE+smW9IvCTxnsSrEm/LuRBfl0MIYQ=='.decode('base64').decode('zip').split(';')[n]

Contents of the zip:

>>>'eJxlktsVwyAMQ1fpCMG8zMn+exUoLpL9QaOrCmLHvOmVVz7z97Oeea4yV52rzdXn0rV2QndGd0p3TndSd1Z/6ZO3HbZnnGd6TCQT+USnOE4xUU00E3Zu0utkE8VENdFMdBP6F/ckOGvAix7QctO3YKx5V3v9BlqvQhd2Qg/YBnaCzWA/2BJ3xY253gaSJEyKEGWiQlQd4Zmd/qNaZCDlh4hqyeJoEOGIFiZGYcyMhbEy9vshF6pHYcyMhbEyNsbOqA79m8O7hzfcZ5HHRea8neE/LM95GTUabkv3CV+qG7v4yUscfpj/b+gpOD6TgiPBycEpwanBacHRUE+smW9IvCTxnsSrEm/LuRBfl0MIYQ=='.decode('base64').decode('zip')
';1;2;2 1;2 2;2 3;2 4;2 5;2 6;2 7;2 8;2 8 1;2 8 2;2 8 3;2 8 4;2 8 5;2 8 6;2 8 7;2 8 8;2 8 8 1;2 8 8 2;2 8 9 2;2 8 10 2;2 8 11 2;2 8 13 1;2 8 13 2;2 8 14 2;2 8 15 2;2 8 16 2;2 8 18 1;2 8 18 2;2 8 18 3;2 8 18 4;2 8 18 5;2 8 18 6;2 8 18 7;2 8 18 8;2 8 18 8 1;2 8 18 8 2;2 8 18 9 2;2 8 18 10 2;2 8 18 12 1;2 8 18 13 1;2 8 18 13 2;2 8 18 15 1;2 8 18 16 1;2 8 18 18;2 8 18 18 1;2 8 18 18 2;2 8 18 18 3;2 8 18 18 4;2 8 18 18 5;2 8 18 18 6;2 8 18 18 7;2 8 18 18 8;2 8 18 18 8 1;2 8 18 18 8 2;2 8 18 18 9 2;2 8 18 19 9 2;2 8 18 21 8 2;2 8 18 22 8 2;2 8 18 23 8 2;2 8 18 24 8 2;2 8 18 25 8 2;2 8 18 25 9 2;2 8 18 27 8 2;2 8 18 28 8 2;2 8 18 29 8 2;2 8 18 30 8 2;2 8 18 31 8 2;2 8 18 32 8 2;2 8 18 32 9 2;2 8 18 32 10 2;2 8 18 32 11 2;2 8 18 32 12 2;2 8 18 32 13 2;2 8 18 32 14 2;2 8 18 32 15 2;2 8 18 32 17 1;2 8 18 32 18 1;2 8 18 32 18 2;2 8 18 32 18 3;2 8 18 32 18 4;2 8 18 32 18 5;2 8 18 32 18 6;2 8 18 32 18 7;2 8 18 32 18 8;2 8 18 32 18 8 1;2 8 18 32 18 8 2;2 8 18 32 18 9 2;2 8 18 32 18 10 2;2 8 18 32 20 9 2;2 8 18 32 21 9 2;2 8 18 32 22 9 2;2 8 18 32 24 8 2;2 8 18 32 25 8 2;2 8 18 32 25 9 2;2 8 18 32 27 8 2;2 8 18 32 28 8 2;2 8 18 32 29 8 2;2 8 18 32 30 8 2;2 8 18 32 31 8 2;2 8 18 32 32 8 2;2 8 18 32 32 10 1;2 8 18 32 32 10 2;2 8 18 32 32 11 2;2 8 18 32 32 12 2;2 8 18 32 32 13 2;2 8 18 32 32 14 2;2 8 18 32 32 15 2;2 8 18 32 32 16 2;2 8 18 32 32 18 1;2 8 18 32 32 18 2;2 8 18 32 32 18 3;2 8 18 32 32 18 4;2 8 18 32 32 18 5;2 8 18 32 32 18 6;2 8 18 32 32 18 7;2 8 18 32 32 18 8'
>>>len(_)
1478

And a quick test:

map(f, range(119))
Out[48]: 
['',
 '1',
 '2',
 '2 1',
 '2 2',
 '2 3',
 '2 4',
 '2 5',
 '2 6',
 '2 7',
 '2 8',
 '2 8 1',
 '2 8 2',
 '2 8 3',
 '2 8 4',
 '2 8 5',
 '2 8 6',
 '2 8 7',
 '2 8 8',
 '2 8 8 1',
 '2 8 8 2',
 '2 8 9 2',
 '2 8 10 2',
 '2 8 11 2',
 '2 8 13 1',
 '2 8 13 2',
 '2 8 14 2',
 '2 8 15 2',
 '2 8 16 2',
 '2 8 18 1',
 '2 8 18 2',
 '2 8 18 3',
 '2 8 18 4',
 '2 8 18 5',
 '2 8 18 6',
 '2 8 18 7',
 '2 8 18 8',
 '2 8 18 8 1',
 '2 8 18 8 2',
 '2 8 18 9 2',
 '2 8 18 10 2',
 '2 8 18 12 1',
 '2 8 18 13 1',
 '2 8 18 13 2',
 '2 8 18 15 1',
 '2 8 18 16 1',
 '2 8 18 18',
 '2 8 18 18 1',
 '2 8 18 18 2',
 '2 8 18 18 3',
 '2 8 18 18 4',
 '2 8 18 18 5',
 '2 8 18 18 6',
 '2 8 18 18 7',
 '2 8 18 18 8',
 '2 8 18 18 8 1',
 '2 8 18 18 8 2',
 '2 8 18 18 9 2',
 '2 8 18 19 9 2',
 '2 8 18 21 8 2',
 '2 8 18 22 8 2',
 '2 8 18 23 8 2',
 '2 8 18 24 8 2',
 '2 8 18 25 8 2',
 '2 8 18 25 9 2',
 '2 8 18 27 8 2',
 '2 8 18 28 8 2',
 '2 8 18 29 8 2',
 '2 8 18 30 8 2',
 '2 8 18 31 8 2',
 '2 8 18 32 8 2',
 '2 8 18 32 9 2',
 '2 8 18 32 10 2',
 '2 8 18 32 11 2',
 '2 8 18 32 12 2',
 '2 8 18 32 13 2',
 '2 8 18 32 14 2',
 '2 8 18 32 15 2',
 '2 8 18 32 17 1',
 '2 8 18 32 18 1',
 '2 8 18 32 18 2',
 '2 8 18 32 18 3',
 '2 8 18 32 18 4',
 '2 8 18 32 18 5',
 '2 8 18 32 18 6',
 '2 8 18 32 18 7',
 '2 8 18 32 18 8',
 '2 8 18 32 18 8 1',
 '2 8 18 32 18 8 2',
 '2 8 18 32 18 9 2',
 '2 8 18 32 18 10 2',
 '2 8 18 32 20 9 2',
 '2 8 18 32 21 9 2',
 '2 8 18 32 22 9 2',
 '2 8 18 32 24 8 2',
 '2 8 18 32 25 8 2',
 '2 8 18 32 25 9 2',
 '2 8 18 32 27 8 2',
 '2 8 18 32 28 8 2',
 '2 8 18 32 29 8 2',
 '2 8 18 32 30 8 2',
 '2 8 18 32 31 8 2',
 '2 8 18 32 32 8 2',
 '2 8 18 32 32 10 1',
 '2 8 18 32 32 10 2',
 '2 8 18 32 32 11 2',
 '2 8 18 32 32 12 2',
 '2 8 18 32 32 13 2',
 '2 8 18 32 32 14 2',
 '2 8 18 32 32 15 2',
 '2 8 18 32 32 16 2',
 '2 8 18 32 32 18 1',
 '2 8 18 32 32 18 2',
 '2 8 18 32 32 18 3',
 '2 8 18 32 32 18 4',
 '2 8 18 32 32 18 5',
 '2 8 18 32 32 18 6',
 '2 8 18 32 32 18 7',
 '2 8 18 32 32 18 8']
\$\endgroup\$
  • \$\begingroup\$ This is good, but please make it a complete program (that reads in input from stdin or arguments); also, you can use an external file to avoid the base64 (but the file and its name count towards code length) \$\endgroup\$ – user16402 Sep 11 '14 at 21:39
  • \$\begingroup\$ The answer you link to begins with Unless the question is an obvious exception (the primary exception being those tagged kolmogorov-complexity), which is the case here. The whole idea of kolmogorov-complexity questions is to hardcode the output in the most efficient manner. \$\endgroup\$ – Dennis Sep 11 '14 at 23:32
  • \$\begingroup\$ I missed the kolmogorov-complexity tag. And here I am trying to decipher the energy levels of the various valence subshells, and I'm not even a physicist. :I \$\endgroup\$ – Sammitch Sep 12 '14 at 0:06
  • \$\begingroup\$ The "old answer" only works in Python2. I haven't checked the new one - have you got a base64 of the file f so I can reproduce it? \$\endgroup\$ – user16402 Sep 12 '14 at 19:32
  • \$\begingroup\$ @professorfish Most certainly. The new one is also python 2 only (print as a keyword rather than a function). \$\endgroup\$ – ɐɔıʇǝɥʇuʎs Sep 12 '14 at 19:37
3
\$\begingroup\$

MATLAB - 248 244 241 178 + 44 = 222 bytes

Minified:

i=1;a=fread(fopen('a'));b=fix(a/7);a=a-7*b+1;d=0*a;for n=1:input(''),A=a(i);if b(i),m=1;i=i+(d(A)+2>b(i));else A=A-[1;0];m=[2;-1];i=i+1;end;d(A)=d(A)+m;end;fprintf('%d ',d(~~d));

Expanded:

i = 1;
a = fread( fopen( 'a' ) );
b = fix( a/7 );
a = a-7*b+1;
d = 0*a;
for n = 1:input('')
    A = a(i);
    if b(i)
        m = 1;
        i = i + (d(A)+2 > b(i));
    else
        A = A - [1; 0];
        m = [2; -1];
        i = i + 1;
    end
    d(A) = d(A) + m;
end
fprintf( '%d ', d(~~d) );

Binary File Dependency (Filename 'a'):

0e 39 3a 11  4f 03 72 03  3b 12 49 04  5e 12 04 73
04 3c 13 43  88 04 b2 43  04 e3 6d 05  82 3d 14 4b
05 9e 05 b3  44 05 e4 06  14 75 06 3e

I trust this is a "complete program" in that it can be invoked from the commandline, it reads from stdin and outputs to stdout.

It uses a kind of two-instruction bytecode to build the electron configurations. The two instructions are

inc D until N      (i.e. increment valence D by 1; advance to next instruction when D = N)

and

pulldown D         (i.e. pull down one electron from valence D, thereby decrementing it by 1
                         and incrementing valence D-1 by 2)

The instructions are encoded in two arrays. The first stores the D argument in all cases. The second stores the N argument or 0 to indicate a pulldown instruction, since N = 0 is never used as an argument.

The complete sequence of instructions is:

inc 1 until 2
inc 2 until 8
inc 3 until 8
inc 4 until 2
inc 3 until 11
pulldown 4

inc 3 until 16
pulldown 4

inc 4 until 8
inc 5 until 2
inc 4 until 10
pulldown 5

inc 4 until 13
inc 5 until 2
pulldown 5

inc 4 until 16
pulldown 5

inc 5 until 8
inc 6 until 2
inc 5 until 9
inc 4 until 19
pulldown 5

inc 4 until 25
inc 5 until 9
pulldown 5

inc 4 until 32
inc 5 until 15
pulldown 6

inc 5 until 18
inc 6 until 8
inc 7 until 2
inc 6 until 10
pulldown 6

inc 5 until 22
pulldown 6

inc 5 until 25
inc 6 until 9
pulldown 6

inc 5 until 32
pulldown 7

inc 7 until 2
inc 6 until 16
pulldown 7

inc 7 until 8

It's worth noting that 28 characters can be dropped if we use the MATLAB-specific character set, but I wanted my solution to be representable as plaintext on Stack Exchange, without any external file references.

External file references it is.

Sample Outputs

39: 2 8 18 9 2

78: 2 8 18 32 17 1

117: 2 8 18 32 32 18 7

5: 2 3

\$\endgroup\$
  • \$\begingroup\$ How do I reconstruct the "binary file dependency"? \$\endgroup\$ – user16402 Sep 12 '14 at 19:45
  • \$\begingroup\$ The file is the 44 byte codes I have listed, left-to-right, top-to-bottom in the usual order. The filename is simply "a", without extension. It needs to be in the working directory when the script is run. I've uploaded a downloadable copy to syonidv.hodginsmedia.com/misc/a \$\endgroup\$ – COTO Sep 13 '14 at 0:04
2
\$\begingroup\$

Perl 5, 235 (234 + 1 for -E)

Golfed:

@a=unpack'C*','ABR3S4sT5tU6';if(($-=<>)~~[unpack'C*',')*,-./9:@NOYZ[\]`g']){$.+=($-~~[46,90]);$p=2+$-/33;$->87|$-~~[57..64]&&($.*=-1);$o[$p]+=$.,$o[$p+1]-=$.}$%=($%=$a[$q]/8)>$-?$-:$%,$o[$a[$q++]&7]+=$%while($--=$%);$,=$";say@o

Note: a hex dump is provided at the bottom of this post, as some of the string literals contain control characters (which were entered through a hex editor).

Ungolfed with comments:

$_=<>;
# For each byte, the first 5 bits are the number of spaces to fill at a time, the next 3 bits represent the shell number, minus 1.
# Values: 10 41 42 13 52 33 14 53 34 15 73 54 35 16 74 55 36
# The 1st shell takes 2 electrons
# Then the 2nd shell take 8, then the third takes 8...
@a=unpack'C*','ABR3S4sT5tU6';
# Contains the atomic numbers of abnormal elements
# Values: 18 1d 29 2a 2c 2d 2e 2f 39 3a 40 4e 4f 59 5a 5b 5c 5d 60 67
@b=unpack'C*',')*,-./9:@NOYZ[\]`g';
# if abnormal
if($_~~@b){
    # All abnormals, except element 46 and 90, only displace 1 electron
    $y=1+($_~~[46,90]);
    # Abnormals with atomic number less than 33 involve switches between shells 3 and 4
    # 33-65: 4 and 5
    # 66-98: 5 and 6
    # 99+ : 6 and 7
    $p = (3 + ~~($_/33)) - 1;
    # abnormals in these ranges move electrons from lower to higher
    # abnormals elsewhere do higher to lower
    if($_ >= 88 || $_ ~~ [57..64]){
        $y *= -1;
    }
    # move electrons
    $o[$p] += $y;
    $o[$p+1] -= $y;
}

    # extract max number of electrons to fill shell with
    # >> 3 is equivalent to /8 for integers; $% is always an integer.
    $% = $a[$q] / 8,
    # do not overfill
    $% = $% > $_ ? $_ : $%,
    # add electrons to shell
    $o[ $a[$q++] & 7 ] += $%
# reduce number of electrons left to fill shells with
while($_ -= $%);
# set list separator to space
$, = $";
# print list representing shells
say @o 

Hex Dump:

0000000: 4061 3d75 6e70 6163 6b27 432a 272c 2710  @a=unpack'C*','.
0000010: 4142 1352 3314 5334 1573 5435 1674 5536  AB.R3.S4.sT5.tU6
0000020: 273b 6966 2828 242d 3d3c 3e29 7e7e 5b75  ';if(($-=<>)~~[u
0000030: 6e70 6163 6b27 432a 272c 2718 1d29 2a2c  npack'C*','..)*,
0000040: 2d2e 2f39 3a40 4e4f 595a 5b5c 5d60 6727  -./9:@NOYZ[\]`g'
0000050: 5d29 7b24 2e2b 3d28 242d 7e7e 5b34 362c  ]){$.+=($-~~[46,
0000060: 3930 5d29 3b24 703d 322b 242d 2f33 333b  90]);$p=2+$-/33;
0000070: 242d 3e38 377c 242d 7e7e 5b35 372e 2e36  $->87|$-~~[57..6
0000080: 345d 2626 2824 2e2a 3d2d 3129 3b24 6f5b  4]&&($.*=-1);$o[
0000090: 2470 5d2b 3d24 2e2c 246f 5b24 702b 315d  $p]+=$.,$o[$p+1]
00000a0: 2d3d 242e 7d24 253d 2824 253d 2461 5b24  -=$.}$%=($%=$a[$
00000b0: 715d 2f38 293e 242d 3f24 2d3a 2425 2c24  q]/8)>$-?$-:$%,$
00000c0: 6f5b 2461 5b24 712b 2b5d 2637 5d2b 3d24  o[$a[$q++]&7]+=$
00000d0: 2577 6869 6c65 2824 2d2d 3d24 2529 3b24  %while($--=$%);$
00000e0: 2c3d 2422 3b73 6179 406f                 ,=$";say@o
\$\endgroup\$
  • \$\begingroup\$ How do I reverse the hexdump? (It's probably better to either do a reversible xxd dump or a base64) \$\endgroup\$ – user16402 Sep 12 '14 at 19:44
  • \$\begingroup\$ @professorfish reversible xxd dump added \$\endgroup\$ – es1024 Sep 13 '14 at 0:45
2
\$\begingroup\$

CJam, 309 289 bytes

0000000: 22 cc b5 a3 1a f7 bd 07 1b 26 ce 73 16 55 87 08  "........&.s.U..
0000010: 27 d2 65 54 66 ac c1 38 ff de 95 d8 8a 77 6d 4e  '.eTf..8.....wmN
0000020: 0d 13 df bb b7 c6 8c ae 6b 32 4d b9 f1 7c b9 f1  ........k2M..|..
0000030: bc 68 2d 8a 5c 22 e6 5c 22 e1 d7 c9 80 ba a5 5d  .h-.\".\"......]
0000040: 64 24 47 0b aa 78 c9 13 a5 0a 65 41 08 f3 ee e3  d$G..x....eA....
0000050: 2e 58 92 19 5f 1a 80 fc d9 30 3b 51 99 c7 1b 51  .X.._....0;Q...Q
0000060: ba 0c 8a 3c 7d f0 60 1e d5 1c e7 2f 33 16 c8 1f  ...<}.`..../3...
0000070: e6 df 24 75 d1 51 e6 af 38 b4 f7 b1 63 77 14 8d  ..$u.Q..8...cw..
0000080: d3 69 bc 99 9e a5 98 56 53 e7 71 f7 48 76 7a 24  .i.....VS.q.Hvz$
0000090: a7 dc 5c 22 fc a6 55 05 30 e2 03 d6 a8 ef 1a 9f  ..\"..U.0.......
00000a0: e4 03 c6 a0 5e 60 be 01 2b ca 12 83 d4 64 69 3d  ....^`..+....di=
00000b0: a7 2e cc 59 5e 0c bb 69 b0 19 1d e1 f2 53 e4 1b  ...Y^..i.....S..
00000c0: 6e 6d cc 45 d3 1f cc 3c b7 1b 5f ca c8 d0 94 fe  nm.E...<.._.....
00000d0: 05 ea ae dc 98 9e 9a 47 a6 fa 3a 0e c3 45 ef 31  .......G..:..E.1
00000e0: 61 a0 7c 80 55 9a 5d 7a af 8e 51 e8 5c 79 c4 22  a.|.U.]z..Q.\y."
00000f0: 32 35 36 62 33 38 62 22 24 12 23 20 5c 22 12 21  256b38b"$.# \".!
0000100: 08 00 02 22 3a 69 32 2f 7b 5f 30 3d 5f 29 33 33  ...":i2/{_0=_)33
0000110: 3f 61 40 5c 2f 5c 2a 7d 2f 30 61 2f 6c 69 28 3d  ?a@\/\*}/0a/li(=
0000120: 60                                               <

Works by replacing common runs (e.g., 2 8 18 32) with integers greater than 32 and considering the array of all configurations a base 38 number, which is encoded in binary.

Example run

$ base64 -d > electrons.cjam <<< Isy1oxr3vQcbJs5zFlWHCCfSZVRmrME4/96V2Ip3bU4NE9+7t8aMrmsyTbnxfLnxvGgtilwi5lwi4dfJgLqlXWQkRwuqeMkTpQplQQjz7uMuWJIZXxqA/NkwO1GZxxtRugyKPH3wYB7VHOcvMxbIH+bfJHXRUeavOLT3sWN3FI3TabyZnqWYVlPncfdIdnokp9xcIvymVQUw4gPWqO8an+QDxqBeYL4BK8oSg9RkaT2nLsxZXgy7abAZHeHyU+Qbbm3MRdMfzDy3G1/KyNCU/gXqrtyYnppHpvo6DsNF7zFhoHyAVZpdeq+OUehcecQiMjU2YjM4YiIkEiMgXCISIQgAAiI6aTIve18wPV8pMzM/YUBcL1wqfS8wYS9saSg9YA==
$ cksum electrons.cjam 
3109698089 289 electrons.cjam
$ LANG=en_US cjam electrons.cjam <<< 42; echo
[2 8 18 13 1]
$ for i in {1..118}; do LANG=en_US cjam electrons.cjam <<< $i; echo; done | md5sum
d09cb34c282ee52c2466a6b80aa30d22  -
\$\endgroup\$

Your Answer

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