Skip to content

Instantly share code, notes, and snippets.

@naoyat
Created August 1, 2018 06:28
Show Gist options
  • Save naoyat/88536412d2ddc2a54c54a46f14f0f8fe to your computer and use it in GitHub Desktop.
Save naoyat/88536412d2ddc2a54c54a46f14f0f8fe to your computer and use it in GitHub Desktop.
atcoder checker - サンプルケースはディレクトリ [abcdef] に、ソースコードは [abcdef][0-9].cc に
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys
import os
import re
import time
import tempfile
import click
def _ansi(num):
return '\x1b[%dm' % num
def _ansi_tag(num, stderr=False):
if stderr:
sys.stderr.write(_ansi(num))
else:
sys.stdout.write(_ansi(num))
def _ansi_reset(stderr=False):
_ansi_tag(0, stderr=stderr)
def run_make(name, debug=False):
src_path = '%s.cc' % name
assert os.path.exists(src_path)
src_mtime = os.path.getmtime(src_path)
if debug:
# os.system('make %sd' % name)
bin_path = name + 'd'
os.system('c++11 %s -O2 -o %s -DDEBUG' % (src_path, bin_path))
else:
# os.system('make %s' % name)
bin_path = name
if os.path.exists(bin_path):
bin_mtime = os.path.getmtime(bin_path)
# print src_mtime, bin_mtime
if src_mtime < bin_mtime:
print '%s is up to date.' % bin_path
return bin_path
src_c_path = '%s_c.cc' % name
os.system('awk -f ../cleanup.awk %s > %s' % (src_path, src_c_path))
os.system('c++11 %s -O2 -o %s' % (src_c_path, bin_path))
return bin_path
@click.command()
@click.argument('name', type=str)
@click.argument('test_id', type=int, default=-1)
@click.option('--sample', type=str, default=None)
@click.option('--debug', is_flag=True, default=False)
def main(name, test_id, sample, debug):
mo = re.match(r'^([abcdef])([0-9])(d?)', name)
assert mo is not None
problem = mo.group(1)
if mo.group(3) == 'd':
debug = True
name = name[:-1]
bin_path = run_make(name, debug)
if test_id >= 0:
test_range = [test_id,]
else:
test_range = range(1, 10)
for i in test_range:
sample_file = '%s/sample%d.txt' % (problem, i)
if not os.path.exists(sample_file):
break
answer_file = '%s/answer%d.txt' % (problem, i)
print 'Case #%d: ' % (i,),
tmp_fd, tmp_fname = tempfile.mkstemp()
t0 = time.time()
_ansi_tag(33, stderr=True)
os.system('./%s < %s > %s' % (bin_path, sample_file, tmp_fname))
_ansi_reset(stderr=True)
t1 = time.time()
os.system('diff --strip-trailing-cr -u %s %s' % (answer_file, tmp_fname))
os.remove(tmp_fname)
_ansi_tag(36)
print '%.2f[msec]' % (1000*(t1 - t0), )
_ansi_reset()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment