#!/usr/bin/python # -*- coding: utf-8 -*- # NOI '13 - Day 1, Problem 3 # Little Q's Training (小Q的修炼) # # Solution Checker for Experimentation # By Alex Li import collections import os import sys max_steps = 1000000 max_vars = 20 def main(): print_steps = False try: if len(sys.argv) < 2: raise if sys.argv[1] == "-w": print_steps = True del sys.argv[1] if len(sys.argv) == 2: case = int(sys.argv[1]) fi_name = 'train' + str(case) + '.in' fo_name = 'train' + str(case) + '.out' elif len(sys.argv) == 3: fi_name = sys.argv[1] fo_name = sys.argv[2] else: raise except: sys.exit("Usage:\n" "\ttrain_check.py [-w] \n" "\ttrain_check.py [-w] \n") if (not os.path.isfile(fi_name)) or (not os.path.isfile(fo_name)): sys.exit('Input/Output File Does Not Exist!\n') E = [0] # events: padded with a value to make the list 1-based varval = [0L for i in xrange(max_vars + 1)] # 1 more for 1-based fi = open(fi_name, "r") fi.readline() # discard test case number n, m = map(int, fi.readline().strip().split()) for i in range(n): E.append(fi.readline().strip().split()) fi.close() fo = open(fo_name, "r") try: q = collections.deque() for line in fo: line = line.strip() if line != '1' and line != '2': raise Exception('Each line must be either "1" or "2".') q.append(int(line)) # simulate pos = 1 for steps in xrange(max_steps): if not (1 <= pos <= n): break if print_steps: print 'Position: %-6d %-30s EXP: %d' % \ (pos, ' '.join(E[pos]), varval[1]) if E[pos][0] == 'v': vid = int(E[pos][1]) num = int(E[pos][4]) val = varval[num] if E[pos][3] == 'v' else num if E[pos][2] == '+': varval[vid] += val else: varval[vid] -= val pos += 1 elif E[pos][0] == 's': if q: pos = int(E[pos][1]) if q.popleft() == 1 else int(E[pos][2]) else: raise Exception('Not enough decisions.') elif E[pos][0] == 'i': num1 = int(E[pos][2]) num2 = int(E[pos][4]) val1 = varval[num1] if E[pos][1] == 'v' else num1 val2 = varval[num2] if E[pos][3] == 'v' else num2 pos = int(E[pos][5]) if val1 < val2 else int(E[pos][6]) if q: raise Exception('Too many decisions.') if 1 <= pos <= n: raise Exception('Game took took many steps to terminate.') except ValueError: sys.exit('Input invalid.\n') except Exception as e: sys.exit('Output invalid. ' + e.args[0] + '\n') except: sys.exit('Output invalid.\n') if print_steps: print 'Final position is %d.' % pos print 'Correct! Your answer is %d.' % varval[1] if __name__ == '__main__': main()