module UVaTools

0: Problem ID
1: Problem Number
2: Problem Title
3: Number of Distinct Accepted User (DACU)
4: Best Runtime of an Accepted Submission
5: Best Memory used of an Accepted Submission
6: Number of No Verdict Given (can be ignored)
7: Number of Submission Error
8: Number of Can't be Judged
9: Number of In Queue

10: Number of Compilation Error 11: Number of Restricted Function 12: Number of Runtime Error 13: Number of Output Limit Exceeded 14: Number of Time Limit Exceeded 15: Number of Memory Limit Exceeded 16: Number of Wrong Answer 17: Number of Presentation Error 18: Number of Accepted 19: Problem Run-Time Limit (milliseconds) 20: Problem Status (0 = unavailable, 1 = normal, 2 = special judge)

Constants

ROOT_DIR
SAVE_LOCATION
USER_SAVE_LOCATION
VERSION

Public Class Methods

download_multiple(prob_nums, worker_count = 4) click to toggle source
# File lib/uva-tools/uva-tools.rb, line 36
def download_multiple(prob_nums, worker_count = 4)
  to_download = Array(prob_nums).map do |prob_num|
    problems_by_number[prob_num]
  end

  length = to_download.length

  if length > 0
    worker_count = [worker_count, length].min
    workers = []

    worker_count.times do
      workers << worker(to_download)
    end

    reads = workers.map{|worker| worker[:read]}
    writes = workers.map{|worker| worker[:write]}

    index = 0
    finished = 0

    loop do
      break if finished >= length

      ready = IO.select(reads, writes)

      ready[0].each do |readable|
        number = Marshal.load(readable)
        finished += 1
        puts "(#{finished}/#{length}) Finished: #{number}"
      end

      ready[1].each do |write|
        break if index >= length

        Marshal.dump(index, write)
        index += 1
      end
    end

    workers.each do |worker|
      worker[:read].close
      worker[:write].close
    end

    workers.each do |worker|
      Process.wait worker[:pid]
    end
  end

  nil
end
load() click to toggle source
# File lib/uva-tools/uva-tools.rb, line 27
def load
  if File.directory?(UVaTools::SAVE_LOCATION) && File.exists?("#{UVaTools::SAVE_LOCATION}/problems")
    @@problems = Marshal.load(File.binread("#{UVaTools::SAVE_LOCATION}/problems"))
    true
  else
    false
  end
end
problems() click to toggle source
# File lib/uva-tools/uva-tools.rb, line 7
def problems
  problem_hash.values
end
problems_by_id() click to toggle source
# File lib/uva-tools/uva-tools.rb, line 15
def problems_by_id
  h = {}
  problem_hash.values.each{ |p| h[p.id] = p }
  h
end
problems_by_number() click to toggle source
# File lib/uva-tools/uva-tools.rb, line 11
def problems_by_number
  problem_hash
end
save() click to toggle source
# File lib/uva-tools/uva-tools.rb, line 21
def save
  FileUtils::mkdir_p UVaTools::SAVE_LOCATION
  File.open("#{UVaTools::SAVE_LOCATION}/problems", 'w') {|f| f.write(Marshal.dump(problem_hash))}
  true
end

Private Class Methods

problem_hash() click to toggle source
# File lib/uva-tools/uva-tools.rb, line 116
def problem_hash
  @@problems ||= begin
    uri = URI("http://uhunt.felix-halim.net/api/p")
    res = JSON.parse(Net::HTTP.get(uri))

    h = {}
    res.each{ |row| h[row[1]] = UVaTools::Problem.new(row) }
    h
  end
end
worker(problems) click to toggle source
# File lib/uva-tools/uva-tools.rb, line 90
def worker(problems)
  child_read, parent_write = IO.pipe
  parent_read, child_write = IO.pipe

  pid = fork do
    begin
      parent_write.close
      parent_read.close

      while !child_read.eof?
        problem = problems[Marshal.load(child_read)]
        problem.download
        Marshal.dump(problem.number, child_write)
      end
    ensure
      child_read.close
      child_write.close
    end
  end

  child_read.close
  child_write.close

  {:read => parent_read, :write => parent_write, :pid => pid}
end