Created
January 10, 2016 20:17
-
-
Save lukasvermeer/eee7d4723bdde984a329 to your computer and use it in GitHub Desktop.
Check if input can be constructed from chemical element symbols. (For a puzzle).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use Data::Dumper; | |
die "Malfunction. Need input." unless $ARGV[0]; | |
my @e = ( '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' ); | |
my %eh = map { lc($_) => $_ } @e; | |
sub solve { | |
my ($string) = @_; | |
if (length($string) == 0) { return ("."); } | |
if ($eh{lc(substr($string, 0, 1))} and my @s = solve(substr($string, 1))) { | |
return ( $eh{lc(substr($string, 0, 1))}, @s) ; | |
} | |
if ($eh{lc(substr($string, 0, 2))} and my @s = solve(substr($string, 2))) { | |
return ( $eh{lc(substr($string, 0, 2))}, @s) ; | |
} | |
return (); | |
} | |
if (open(W, $ARGV[0])) { | |
while (my $line = <W>) { | |
chomp($line); | |
#print Dumper solve(join '', split ' ', $line); | |
if (solve(join '', split ' ', $line)) { print $line . "\n"; } | |
} | |
close(W); | |
} else { | |
print( (join "", (solve(join '', split ' ', $ARGV[0]))) . "\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment