| Bytes | Lang | Time | Link |
|---|---|---|---|
| 260 | Raku Perl 6 rakudo | 240803T181842Z | bb94 |
| 124 | Jelly | 210608T224102Z | Nick Ken |
| 265 | JavaScript ES6 | 210603T173942Z | Arnauld |
| 117 | 05AB1E | 210604T083545Z | ovs |
| 194 | Jelly | 210603T140810Z | hyper-ne |
Raku (Perl 6) (rakudo), 260 bytes
{2 min+(.match(/^(\d|a<[cglmrstu]>|b<[aehikr]>?|c<[adeflmnorsu]>?|d<[bsy]>|e<[rsu]>|f<[elmr]>?|g<[ade]>|h<[efgos]>?|in|ir?|kr?|l<[airuv]>|m<[cdgnot]>|n<[abdehiop]>?|os?|og|p<[abdmortu]>?|r<[abefghnu]>?|s<[bcegimnr]>?|t<[abcehilms]>|u|v|w|yb?|xe|zn|zr)+$/):ex)}
Explanation
{2 min+(.match(/^(...)+$/):ex)}
/^(...)+$/ # Regex that matches a digit or chemical symbol one or more times
.match( ):ex # Get a list of all possible matches of this regex against the input
+( ) # Get the number of elements in that list
2 min # Takes the min of 2 and that number
💎
Created with the help of Luminespire.
Jelly, 124 bytes
“€ṡƥvẏÄhʠ!ịẈ?iḥẋỌ¹'ṅɦçç⁹.Sð$ṚṠȥṢK}/>œ;ỵZƤẓ}:;tÆṾḶj⁶ḄỵḋİʠUḊẈ½^ɲṆßṇµ⁶æḟɦ1¥©ḟþjHṀṪỤƑgạȥḂṾḞƓƑ@Ð⁾æƭVẈṣ’ḃ90Äḃ26ịØaØDW€¤;
ŒṖfƑƇ¢L«2
A set of links that takes a string argument and returns 0 for impossible, 1 for a single valid interpretation and 2 for ambiguous.
Loosely based on @hyper-neutrino’s answer, but mostly rewritten.
JavaScript (ES6), 265 bytes
Saved 6 bytes thanks to @tsh
Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or -1 for impossible.
s=>s.reduce((p,c,i)=>p|=1/c||b?b=0:/[bcfhiknopsuvwy]/.test(c)+(b=/([ace][rsu]|[cgn][ade]|[iz][nr]|a[cglmt]|b[aehikr]|c[fl-o]|d[bsy]|f[elmr]|h[efgos]|kr|l[airuv]|m[cdgnot]|n[bhiop]|o[gs]|p[abdmortu]|r[abe-hnu]|s[bcegimnr]|t[abcehilms]|xe|yb)$/.test(c+s[i+1]))-1,b=0)
How?
The variable \$p\$ holds the final result. The flag \$b\$ is set whenever a 2-character chemical symbol is matched.
For each character \$c\$ at position \$i\$ in the input array:
If \$c\$ is a digit or the flag \$b\$ is set, we clear \$b\$ and leave \$p\$ unchanged.
Otherwise, we compute:
p |= /E1/.test(c) + (b = /E2/.test(c + s[i + 1])) - 1where
/E1/and/E2/are regular expressions matching 1-character and 2-character chemical symbols respectively.Which leads to:
/E1/ matching | /E2/ matching | sum - 1 | meaning ---------------+---------------+---------+------------ no | no | -1 | impossible no | yes | 0 | unchanged yes | no | 0 | unchanged yes | yes | 1 | ambiguous
05AB1E, 117 bytes
Prints -1 for impossible, 0 for unambiguous and 1 for ambiguous.
.œε.•B‚Δ—₅H^ƶƵXJ∍擺l¦¯í-āмIõVÑK:Λ‘(·÷ΓKå<70'V∍∞±ζښ˛¿ĆÅRÙΔu”ípā<À¿ýÌQмÛì‰Óǝ“§KhúγIÑËM`Êιš•#€SASõšøε`ì}˜žhS«såP}O<.±
.œ # push all partitions of the input
ε } # map over the partitions:
... # push the table of valid parts (see below)
s # swap to the current partition
åP # test if every part is in the table
O # take the sum over all results
< # decrease by 1
.± # print the sign of this
Compressing the table
.•B...š• # alphabet compressed string (contains 26 spaces)
# # split on spaces
€S # split each string into a list of chars
ASõš # the characters of the alphabet with the empty string prepended
øε`ì} # zip alphabet with previous list and prepend matching characters
˜ # flatten the list
žhS« # append the digits
Jelly, 194 bytes
“×ĿcṚ¦mßṛy|3¹,GƊ§ḋCƤṂỴƇƈÇ¡ɱɓkȷ¶ʂạ⁺ịọ9ėżḣ÷8ÆC⁼³°0|KṪƈŀ@:ḤvƥÄƭsɲẸpI0⁷tÞO§ƒ;ḷ8ḟhıA€ḊṪḷ`ɦḊa¥IƭịƤñ¡÷?²ạ©œ5EẏṢɲ⁺ȷɗL4Q¤F⁹~Ṿ⁸ṪṄ|eėgṖ€ṂɠƲ2ʂ⁺ɱḌḳWƘɠṛ;⁵2nȦƝ]ṇUṆ84¥ẸṪḲdþ¹Ẋd©’ṃØẠØD;µe€ØD;ØA¤œṗ
¢Œl⁸ŒṖ¤e€€ȦƇL«2
-2 bytes thanks to caird coinheringaahing via better usage of chaining
Output 2 for ambiguous, 1 for unambiguous, 0 for impossible.
The logic behind this is pretty trivial, I just need to figure out a better way of encoding this periodic table...
“...’ṃØẠØD;µe€ØD;ØA¤œṗ Helper Link; produce the list of components
“...’ Compressed Integer
ṃØẠ Base-decoded into the upper and lower-case alphabet
ØD; Prepend the digits (as strings) as well
µ With the list of components (joined on nothing) as the left argument
e€ Check if each element is in
ØD;ØA¤ The digits + uppercase alphabet
œṗ And partition the components before those indices
¢Œl⁸ŒṖ¤e€€ȦƇL«2 Main Link
¢Œl The components above, lowercased
⁸ŒṖ¤ All ways of partitioning the input
€ For each partition
€ For each component
e Is it in the list of valid components?
ȦƇ Filter to keep partitions where every component was valid
L Length; 0 if impossible, 1 if unambiguous, 2+ if ambiguous
«2 Minimum of the above and 2 (sets ambiguous case to 2 no matter how many occurrences existed)