Helpers for Advent of Code
This crate contains some helper methods that I regularly use in my Advent of Code solutions.
Processing of command line arguments
args::get_args
Reads the command line arguments and checks whether the correct number of arguments are present.
Example
use get_args;
Error handling
error::WithOrExit
This trait adds a or_exit_with
method.
The purpose of this method, is to allow you to easily let your program terminate with a specific exit code.
It has been implemented for Result and Option.
The implementation for Result requires that the associated error type implements fmt::Debug.
Example
use WithOrExit;
error::ParseError
A generic error containing just a message. It implements fmt::Display and fmt::Debug, and it can be converted from io::Error and num::ParseIntError.
Example
use ParseError;
I/O operations
input::MultilineFromStr
This trait is inspired by the str::FromStr trait, and allows you to parse input where data might span several lines.
Example
use ParseError;
use MultilineFromStr;
input::WithReadLines
This trait adds a read_lines
method.
The purpose of this method is to read the lines from some source (e.g. a file), and then convert each line to a specific type.
As an argument, this method takes an exit code that should be used if processing the source fails, and it returns an iterator.
This trait has been implemented for fs::File.
Example
use WithReadLines;
input::WithReadMultiLines
This trait adds a read_multi_lines
method.
It's the equivalent of input::WithReadLines
, but rather than depending on str::FromStr, it depends on input::MultilineFromStr
.
Example
use WithReadMultiLines;
input::WithAsRecords
& input::WithAsMultilineRecords
These traits allow you to easily convert an object to a vec of items of the required type.
Example
Assembly machine
This module provides a machine that can process assembly-like instructions. A machine consist of three parts:
- Instructions (must implement
MachineInstruction
) - A register (must implement
MachineRegister
) - An output receiver (must implement
OutputReceiver
)
When the machine is run, it will start executing the instructions one by one.
Each instruction specifies what the next instruction is (usually the next line, but you can also jump to some other spot).
As soon as the program counter reaches a non-existent instruction, the machine will halt.
Instructions may use the register to keep track of values, and may use the output receiver to send output to some (imaginary) output device, such as an antenna.
When running the machine, you also get to specify a pre-execute hook (PreExecuteHook
).
For each instructions, the machine will call this hook before actually executing it.
This allows you to manipulate the normal flow of the program.
For example:
To more easily parse instructions from your input file, this library provides:
machine::instruction::Value
- Represents either a raw value or a reference to a register.machine::instruction::ParsedMachineInstruction
- Represents a single parsed line. Allows you to easily access to command (.get_command()
) or specific arguments, which will be parsed to the correct type (.get_argument::<T>(idx)
).
Example
use ParseError;
use NoopHook;
use ;
use OutputReceiver;
use MachineRegister;
use Machine;
Math
math::gcd
Computes the greatest common divisor of two numbers.
Example
use gcd;
math::lcm
Computes the least common multiple of two numbers.
Example
use lcm;
math::solve_crt
Solve the chinese remainder theorem for (n1, a1) and (n2, a2). We assume that:
- n1 and n2 are coprime
- n1 and n2 are no more than 63 bits (as they are converted to i64)
Example
use solve_crt;
math::bezout_coefficients
Find t and s, such that ta + sb = gcd(p, q).
Example
use bezout_coefficients;
Parts
part::Part
This enum is useful if you need to explicitly refer to a part. It implemts str::FromStr and fmt::Display, so you can easily convert to and from a string.
Example
use Part;
Search
search::Navigable
Implement this trait to be able to use A* to find the shortest path between two points.