Skip to main content
saved 1 byte and added an explanation
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 266265 bytes

Saved 6 bytes thanks to @tsh

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or 3-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]))^2-1,b=0) 

Try it online!Try it online!

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])) - 1 

    where /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 

JavaScript (ES6), 266 bytes

Saved 6 bytes thanks to @tsh

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or 3 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]))^2,b=0) 

Try it online!

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) 

Try it online!

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])) - 1 

    where /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 
saved 6 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 272266 bytes

Saved 6 bytes thanks to @tsh

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or 3 for impossible.

s=>s.reduce((p,c,i)=>p|=1/c||b?b=0:/[bcfhiknopsuvwy]/.test(c)-~(b=/(a[cglmr-u]|b[aehikr]|c[adefl[ace][rsu]|[cgn][ade]|[iz][nr]|a[cglmt]|b[aehikr]|c[fl-orsu]|d[bsy]|e[rsu]|f[elmr]|g[ade]|h[efgos]|i[nr]|kr|l[airuv]|m[cdgnot]|n[abdehiop]|o[gs]|p[abdmortu]|r[abeo]|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|z[nr]hnu]|s[bcegimnr]|t[abcehilms]|xe|yb)$/.test(c+s[i+1]))^2,b=0) 

Try it online!Try it online!

JavaScript (ES6), 272 bytes

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or 3 for impossible.

s=>s.reduce((p,c,i)=>p|=1/c||b?b=0:/[bcfhiknopsuvwy]/.test(c)-~(b=/(a[cglmr-u]|b[aehikr]|c[adefl-orsu]|d[bsy]|e[rsu]|f[elmr]|g[ade]|h[efgos]|i[nr]|kr|l[airuv]|m[cdgnot]|n[abdehiop]|o[gs]|p[abdmortu]|r[abe-hnu]|s[bcegimnr]|t[abcehilms]|xe|yb|z[nr])$/.test(c+s[i+1]))^2,b=0) 

Try it online!

JavaScript (ES6), 266 bytes

Saved 6 bytes thanks to @tsh

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or 3 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]))^2,b=0) 

Try it online!

saved 4 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), 276272 bytes

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or 3 for impossible.

s=>s.reduce((p,c,i)=>p|=1/c||+bc||b?b=0:e/[bcfhiknopsuvwy]/.test(c)-~(b=e.test(c+s[i+1]))^2,b=e=b=/^(a[cglmr-u]|b[aehikr]?|c[adeflu]|b[aehikr]|c[adefl-orsu]?|d[bsy]|e[rsu]|f[elmr]?|g[ade]|h[efgos]?|i[nr]?|kr?|l[airuv]|m[cdgnot]|n[abdehiop]?|o[gs]?|p[abdmortu]?|r[abeorsu]|d[bsy]|e[rsu]|f[elmr]|g[ade]|h[efgos]|i[nr]|kr|l[airuv]|m[cdgnot]|n[abdehiop]|o[gs]|p[abdmortu]|r[abe-hnu]|s[bcegimnr]?|t[abcehilms]|u|v|w|xe|yb?|z[nr]hnu]|s[bcegimnr]|t[abcehilms]|xe|yb|z[nr])$/.test(c+s[i+1]))^2,b=0) 

Try it online!Try it online!

JavaScript (ES6), 276 bytes

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or 3 for impossible.

s=>s.reduce((p,c,i)=>p|=1/c||+b?b=0:e.test(c)-~(b=e.test(c+s[i+1]))^2,b=e=/^(a[cglmr-u]|b[aehikr]?|c[adefl-orsu]?|d[bsy]|e[rsu]|f[elmr]?|g[ade]|h[efgos]?|i[nr]?|kr?|l[airuv]|m[cdgnot]|n[abdehiop]?|o[gs]?|p[abdmortu]?|r[abe-hnu]|s[bcegimnr]?|t[abcehilms]|u|v|w|xe|yb?|z[nr])$/) 

Try it online!

JavaScript (ES6), 272 bytes

Expects an array of characters. Returns 0 for unambiguous, 1 for ambiguous, or 3 for impossible.

s=>s.reduce((p,c,i)=>p|=1/c||b?b=0:/[bcfhiknopsuvwy]/.test(c)-~(b=/(a[cglmr-u]|b[aehikr]|c[adefl-orsu]|d[bsy]|e[rsu]|f[elmr]|g[ade]|h[efgos]|i[nr]|kr|l[airuv]|m[cdgnot]|n[abdehiop]|o[gs]|p[abdmortu]|r[abe-hnu]|s[bcegimnr]|t[abcehilms]|xe|yb|z[nr])$/.test(c+s[i+1]))^2,b=0) 

Try it online!

saved 4 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading