challenges

my solutions to various "programming challenge" problems
git clone https://wehaveforgeathome.hates.computer/challenges.git
Log | Files | Refs | LICENSE

022.rb (354B)


      1 #!/usr/bin/ruby
      2 # run from same dir as names.txt
      3 
      4 names = File.open("names.txt").first
      5 names.chomp
      6 names = names.gsub(/["\s]/, '').split(",")
      7 
      8 def score (name, index)
      9   s = 0
     10   for i in (0...name.size)
     11     c = name[i].ord - 64
     12     s += c
     13   end
     14   s * (index + 1)
     15 end
     16 
     17 total = 0
     18 names.sort.each_with_index {|name, i|
     19   total += score name, i
     20 }
     21 
     22 puts total