読者です 読者をやめる 読者になる 読者になる

LOWEST UNIQUE NUMBER(CodeEval)

codeeval ruby

CHALLENGE DESCRIPTION:

here is a game where each player picks a number from 1 to 9, writes it on a paper and gives to a guide. A player wins if his number is the lowest unique. We may have 10-20 players in our game.

INPUT SAMPLE:

Your program should accept as its first argument a path to a filename.

You're a guide and you're given a set of numbers from players for the round of game. E.g. 2 rounds of the game look this way:

3 3 9 1 6 5 8 1 5 3
9 2 9 9 1 8 8 8 2 1 1

OUTPUT SAMPLE:

Print a winner's position or 0 in case there is no winner. In the first line of input sample the lowest unique number is 6. So player 5 wins.

5
0

My Code

#!/usr/bin/env ruby -w

def lowest_uniq_number(numbers)
  hash = {}
  numbers.each do |n|
    hash[n] ||= 0
    hash[n] += 1
  end
  hash.select { |_k, v| v == 1 }.keys.min
end

ARGF.each_line do |line|
  numbers = line.chomp.split.map(&:to_i)
  min_uniq_number =  lowest_uniq_number(numbers)
  if min_uniq_number
    puts(numbers.find_index(lowest_uniq_number(numbers)) + 1)
  else
    puts 0
  end
end