Skip to content

Instantly share code, notes, and snippets.

@nojima
Last active January 1, 2016 22:09
Show Gist options
  • Save nojima/8207756 to your computer and use it in GitHub Desktop.
Save nojima/8207756 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'colored'
require 'optparse'
require 'open3'
def compile(problem_id)
puts "======== Compile ========".bold.green
cmd = "g++ -Wall -Wextra -Wno-unused-result -O2 -g -std=c++0x #{problem_id}.cpp"
output, status = Open3.capture2e(cmd)
output.each_line do |line|
if /error:/ =~ line
puts line.bold.red
elsif /warning:/ =~ line
puts line.cyan
else
puts line
end
end
status
end
def execute(problem_id, testcase_id)
if testcase_id
testcases = ["#{problem_id}.in#{testcase_id}"]
else
testcases = Dir.glob("#{problem_id}.in*").sort
end
testcases.each do |testcase|
puts "======== #{testcase} ========".bold.green
system "./a.out < #{testcase}"
end
end
def main
opt = OptionParser.new
opt.banner = "Usage: #{File.basename(__FILE__)} PROBLEM_ID [TESTCASE_ID]"
opt.parse!(ARGV)
unless (1..2).include?(ARGV.size)
$stderr.puts opt.help
exit 1
end
unless /\A[0-9]+\Z/ =~ ARGV[0]
$stderr.puts "Error: PROBLEM_ID must be a positive integer."
exit 1
end
problem_id = ARGV[0]
if ARGV.size >= 2
unless /\A[0-9]+\Z/ =~ ARGV[1]
$stderr.puts "Error: TESTCASE_ID must be a positive integer."
exit 1
end
testcase_id = ARGV[1]
else
testcase_id = nil
end
status = compile(problem_id)
exit status.to_i unless status.success?
execute(problem_id, testcase_id)
end
if __FILE__ == $0
main
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment