Skip to content

Instantly share code, notes, and snippets.

@danman01
Last active January 30, 2017 15:32
Show Gist options
  • Save danman01/14e669f05fbe615d9974b45ace52e934 to your computer and use it in GitHub Desktop.
Save danman01/14e669f05fbe615d9974b45ace52e934 to your computer and use it in GitHub Desktop.
class Euler
ALPHABET = ("A".."Z").to_a
VALUES = {}
def initialize
get_values
get_names
find_score
end
def find_score
@score = calculate_scores
end
def score
puts @score
end
def get_values
ALPHABET.each{|letter| VALUES.merge!({"#{letter}": (ALPHABET.find_index(letter) + 1) })}
end
def get_names
names = File.readlines('./euler_name_scores.txt');
@names = names.first.split(",").sort
end
def calculate_scores
total = 0
@names.each do |name|
name.gsub!("\"","")
val = 0
name.each_char do |letter|
val += VALUES[letter.to_sym]
end
if name == "COLIN"
puts "TEST: \nval is #{val}"
puts "name index is #{@names.find_index(name)}"
puts "total is #{val * @names.find_index(name)}"
end
total += val * (@names.find_index(name) + 1)
end
total
end
end
Euler.new.score
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment