Revision ee5f529a-4ca1-476d-b38a-c128e3746a95 - Code Golf Stack Exchange
Perl 5, 243 (242 + 1 for -E)
====================================
<!-- language: lang-pl -->
Golfed:
$-=<>;@a=unpack'C*',' ‚#¢c$£d%ã¤e&ä¥f';@b=unpack'C*',')*,-./9:@NOYZ[\]`g';if($-~~@b){$y=1+($-~~[46,90]);$p=2+$-/33;$->87|$-~~[57..64]&&($y*=-1);$o[$p]+=$y,$o[$p+1]-=$y}$e=($e=$a[$q]>>4)>$-?$-:$e,$o[$a[$q++]&7]+=$e while($--=$e);,ドル=$";say@o
Note: a hex dump is provided at the bottom of this post, as some of the string literals contain control characters (which was entered through a hex editor).
Ungolfed with comments:
<!-- language: lang-pl -->
$_=<>;
# For each byte, the first nibble is the number of spaces to fill at a time, the second nibble is the shell number, minus 1.
# Values: 20 81 82 23 a2 63 24 a3 64 25 e3 a4 65 26 e4 a5 66
# The 1st shell takes 2 electrons
# Then the 2nd shell take 8, then the third takes 8...
@a=unpack'C*',' ‚#¢c$£d%ã¤e&ä¥f';
# 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
$e = $a[$q] >> 4,
# do not overfill
$e = $e > $_ ? $_ : $e,
# add electrons to shell
$o[ $a[$q++] & 7 ] += $e
# reduce number of electrons left to fill shells with
while($_ -= $e);
# set list separator to space
,ドル = $";
# print list representing shells
say @o
Hex Dump:
0000000 2d24 3c3d 3b3e 6140 753d 706e 6361 276b
0000010 2a43 2c27 2027 8281 a223 2463 64a3 e325
0000020 65a4 e426 66a5 3b27 6240 753d 706e 6361
0000030 276b 2a43 2c27 1827 291d 2c2a 2e2d 392f
0000040 403a 4f4e 5a59 5c5b 605d 2767 693b 2866
0000050 2d24 7e7e 6240 7b29 7924 313d 282b 2d24
0000060 7e7e 345b 2c36 3039 295d 243b 3d70 2b32
0000070 2d24 332f 3b33 2d24 383e 7c37 2d24 7e7e
0000080 355b 2e37 362e 5d34 2626 2428 2a79 2d3d
0000090 2931 243b 5b6f 7024 2b5d 243d 2c79 6f24
00000a0 245b 2b70 5d31 3d2d 7924 247d 3d65 2428
00000b0 3d65 6124 245b 5d71 3e3e 2934 243e 3f2d
00000c0 2d24 243a 2c65 6f24 245b 5b61 7124 2b2b
00000d0 265d 5d37 3d2b 6524 7720 6968 656c 2428
00000e0 2d2d 243d 2965 243b 3d2c 2224 733b 7961
00000f0 6f40
00000f2