Advent of Code Challenges
$number_words = {
  "one": 1,
  "two": 2,
  "three": 3,
  "four": 4,
  "five": 5,
  "six": 6,
  "seven": 7,
  "eight": 8,
  "nine": 9,
}

class String
  def get_first_number_word
    res = nil
    i = self.length

    $number_words.each do |key, value|
      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

  def get_last_number_word
    res = nil
    i = 0

    $number_words.each do |key, value|
      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

  def numeric?
    Float(self) != nil rescue false
  end
end

def part_one(data)
  res = 0
  data.each do |line|
    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

def part_two(data)
  res = 0
  data.each do |line|
    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_one}"
  result_two = part_two(file_data)
  puts "Part Two: #{result_two}"
end