Crate gcj_helper [−] [src]
A helper library for Google Code Jam solutions.
In the Google Code Jam, solving a problem typically requires the following steps:
- Open an input file containing a series of test cases.
- Open an output file where solutions will be written.
- Read the first line from the input file, which consists solely of an unsigned integer specifying the number of test cases in the input file.
- For each test case, perform the following steps:
- Obtain the corresponding test data by reading one or more lines from the input file (it may be a fixed number, or specified within the test data itself).
- Perform some logic using the test data, in order to obtain a set of results.
- Write the string
Nis the number of completed test cases) followed by the results obtained in the previous step, formatted as the problem requires.
Writing code to handle all of the above is tedious and time-consuming, in a situation where
every microsecond counts.
gcj-helper is designed to handle the boilerplate, so you can focus
on writing solutions instead.
To execute test cases, you need to create a
TestEngine and call
TestEngine::run() accepts two closures:
- A parser that reads from an input file and returns the data for one test case.
- A solver that performs logic on the data for one test case and returns a result, encoded
This two-step process to writing solutions is useful for two reasons:
- It separates parsing from the solution itself, making your code easier to read;
- It enables test case parallelisation if the
parallelfeature is enabled, improving run-time performance at the cost of increased build times.
gcj-helper provides parsers with access to an
InputReader, which obtains data from the
input file in a
io::Read-like fashion. The
InputReader::read_next_line() method reads a
line of text from the input file, consuming the end-of-line marker, and returns a
containing the result.
Before each test case, the
TestEngine writes the string
"Case #N:", where
N is the
current test case. This does not prepend or append any whitespace. This means that if the
colon must be followed by a space, your result should begin with one, and that the result must
end with a newline.
Supports reading from an input file.
Facilitates the execution of problem solving code.