def two_sum(nums, target) nums_map = Hash.new nums.each_with_index do |num, idx| nums_map.store(num, idx) end nums.each_with_index do |num, idx| diff = target - num if nums_map.has_key?(diff) and nums_map[diff] != idx return [idx, nums_map[diff]] end end [] end sum_file = File.readlines("input.txt") nums = [] target = -1 sum_file.each_with_index do |line, index| if index == 0 line.split(" ").each do |num| nums.push(num.to_i) end else target = line.to_i end end puts two_sum(nums, target)