$number_words = {
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
}
res = nil
i = self.length
$number_words.each do
key = key.to_s
if (self.include?(key) && self.index(key) < i)
i = self.index(key)
res = $number_words[key.to_sym]
end
end
str_only_digits = self[0...i].tr('^0-9', '')
if not str_only_digits[0].nil?
res = str_only_digits[0]
end
res
end
res = nil
i = 0
$number_words.each do
key = key.to_s
if (self.include?(key) && self.rindex(key) >= i)
i = self.rindex(key)
res = $number_words[key.to_sym]
end
end
str_only_digits = self[i..-1].tr('^0-9', '')
if not str_only_digits[0].nil?
res = str_only_digits[-1]
end
res
end
Float(self) != nil rescue false
end
end
res = 0
data.each do
str_only_digits = line.tr('^0-9', '')
if not str_only_digits[0].nil?
res += (str_only_digits[0] + str_only_digits[-1]).to_i
end
end
res
end
res = 0
data.each do
res += (line.get_first_number_word.to_s + line.get_last_number_word.to_s).to_i
end
res
end
if __FILE__ == $0
file_path = File.expand_path("./../puzzle.txt", __FILE__)
input = File.read(file_path)
file_data = input.split("\n")
result_one = part_one(file_data)
puts "Part One: "
result_two = part_two(file_data)
puts "Part Two: "
end