Crate aocsol

Source
Expand description

A crate that holds solutions to Advent of Code(AoC) puzzles. The solutions are implemented in async rust, thus need an async runtime such as tokio.

§Feature-flags

By default, Puzzl and Solve traits are provided.

To generate solvers for a puzzle using solver, add solvers feature flag in Cargo.toml

If you wish to implement your own solver, and only require some useful utilities, add toolcase feature flag in Cargo.toml

§Examples

Two traits Puzzl and Solve are provided to interface with the solvers.

use std::{error, sync::Arc};

use aocsol::{puzzle::Puzzl, solver};

#[tokio::main]
async fn main() -> Result<(), Box<dyn error::Error + Send + Sync>> {
    let data = "1000\n2000\n\n4000\n\n5000\n6000\n\n7000\n9000";
    let puzzle = AocPuzzle(Arc::from(data));
    let solver = solver::solver(puzzle)?;   // solver generated

    let answer_part_one: u32 = solver.part_one().await?.to_string().parse()?;
    let answer_part_two: u32 = solver.part_two().await?.to_string().parse()?;

    assert_eq!(16000, answer_part_one);
    assert_eq!(31000, answer_part_two);

    Ok(())
}

// Sturct to hold data.
struct AocPuzzle(Arc<str>);

// Implement Puzzl<InputType = Arc<str>> to generate solver using aocsol::solver::solver().
impl<'input> Puzzl for AocPuzzle {
    type InputType = Arc<str>;

    fn year(&self) -> u32 {
        2022
    }

    fn day(&self) -> u32 {
        1
    }

    fn data(&self) -> Self::InputType {
        Arc::clone(&self.0)
    }
}

Modules§

error
Module to hold AOC specific errors
puzzle
Holds the traits for puzzle and solver.
solver
Module to get puzzle solver.
toolcase
Module for utilities to help implement solutions for the puzzles.