Created
January 23, 2011 22:42
-
-
Save foca/792525 to your computer and use it in GitHub Desktop.
Simple script to generate combinations of the elements from the periodic table to create strings provided by the user. Very inefficient, and probably buggy, but it works well enough to play with my http://www.thinkgeek.com/interests/giftsunder10/8fc5/ :)
This file contains hidden or 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
#!/usr/bin/env ruby | |
abort "You need to provide a string as the last argument" if ARGV.empty? | |
$debug = true if (ARGV & %w(-d -v --debug --verbose)).any? | |
def ruler_for(used) | |
size = used.map(&:size).inject(:+) + used.size - 1 | |
"-" * size | |
end | |
def debug(msg) | |
$stderr.puts msg if $debug | |
end | |
ELEMENTS = %W( Ac Ag Al Am Ar As At Au B Ba Be Bh Bi Bk Br C Ca Cd Ce Cf Cl Cm | |
Co Cr Cs Cu Db Dy Er Es Eu F Fe Fm Fr Ga Gd Ge H He Hf Hg Ho Hs I In Ir K Kr | |
La Li Lr Lu Md Mg Mn Mo Mt N Na Nb Nd Ne Ni No Np O Os P Pa Pb Pd Pm Po Pr Pt | |
Pu Ra Rb Re Rf Rh Rn Ru S Sb Sc Se Sg Si Sm Sn Sr Ta Tb Tc Te Th Ti Tl Tm U V | |
W Xe Y Yb Zn Zr ) | |
input = ARGV.last.downcase.gsub(/[^a-z]/, '') | |
used, skip, tried_combinations = [], [], [] | |
while input.length.nonzero? | |
element = (ELEMENTS - used - skip).detect {|el| input =~ /^#{el}/i } | |
debug "-- Attempting with #{element}" if element | |
if element.nil? | |
debug "-- skip: #{skip.inspect}" | |
debug "-- used: #{used.inspect}" | |
if used.empty? || tried_combinations.include?(used) | |
abort "can't write that phrase, sorry :(" | |
end | |
tried_combinations << used | |
last = used.pop | |
skip << last | |
input = last.downcase + input | |
else | |
used << element | |
skip.clear | |
input = input.gsub(/^#{element}/i, "") | |
end | |
end | |
puts ruler_for(used) | |
puts used.join(" ") | |
puts ruler_for(used) | |
puts " Using: #{used.join(", ")}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment