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
"Case #N:"
(whereN
is 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.
The TestEngine
type
To execute test cases, you need to create a TestEngine
and call TestEngine::run()
.
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 as aDisplay
type.
This two-step process to writing solutions is useful for two reasons:
- It seperates parsing from the solution itself, making your code easier to read;
- It enables test case parallelisation if the
parallel
feature is enabled, improving run-time performance at the cost of increased build times.
The InputReader
type
gcj-helper
provides parsers with access to an InputReader
, a simple wrapper around a
std::fs::File
. InputReader
implements io::BufRead
and also provides a convenience
method, read_next_line()
, which reads a line of text from an input file and truncates the
end-of-line marker if one is present.
Formatting test results
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.
Structs
InputReader |
Supports reading from an input file. |
TestEngine |
Facilitates the execution of problem solving code. |