Expand description

impl Solution<'_, Day25, Part2> for AdventOfCode2021<Day25>

What is this?

This is advent_of_code_traits, a set of traits to implement solutions to Advent of Code in Rust.

It takes a trait-based approach using const-generics and autoderef specialization.

It’s basically an excuse to play with rust’s type system.

Usage

Please see also the examples.

Implement traits with your solutions to each day of Advent of Code.

Import the machinery:

use advent_of_code_traits::{days::*, MissingPartTwo, Part1, Part2, ParseInput, run, Solution, SolutionRunner};

Implement Solution for your struct.

pub struct AdventOfCode2021<const DAY: u32>;

impl Solution<'_, Day25, Part1> for AdventOfCode2021<Day25> {
    type Input = Vec<u32>;
    type Output = u32;

    fn solve(&self, input: &Self::Input) -> Self::Output {
        // your solution to Part1 here...
    }
}

That’s how we solve the solution given a nicely typed Vec<u32>, but Advent of Code gives us plaintext input.

So first we need to parse the input…

Implement ParseInput for your struct

// ..continued from above

impl ParseInput<'_, Day25, Part1> for AdventOfCode2021<Day25> {
    type Parsed = Vec<u32>; // <-- the input type fed to Solution::solve

    fn parse_input(&self, input: &'_ str) -> Self::Parsed {
        input
            .lines()
            .map(|s| s.parse().expect("invalid integer"))
            .collect()
    }
}

Mark Part2 as missing

To run only Part1 of a day of Advent of Code, you currently need to impl MissingPartTwo to help disambiguate the specialization:

impl MissingPartTwo<Day25> for AdventOfCode2021<Day25> {}

If you don’t do this (and haven’t implemented Solution for Part2) you’ll see an error like:

the method `run` exists for reference `&&&AdventOfCode2021<25_u32>`, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
`AdventOfCode2021<25_u32>: MissingPartTwo<25_u32>`
which is required by `AdventOfCode2021<25_u32>: SolutionRunner<25_u32, 1_u16>`rustcE0599

Please refer to the examples for more demonstrations.

Run from main.rs

Here comes the part where we actually run our solution!

let input = std::fs::read_to_string("./input/2021/day25.txt").expect("failed to read input");
run!(AdventOfCode2021::<Day25>, &input);

This reads input from a file and passes it to your struct to parse and then solve. It will print the output of your solution (which must impl Debug).

run is currently a humble macro_rules! declarative macro and is very simple. It’s main purpose is to veil the use of autoderef specialization.

Modules

Constants for all 25 days of Advent

This crate uses autoderef specialization to choose an implementation of SolutionRunner that is most appropriate to the user’s implementation of Solution.

Macros

The run macro expands

Constants

Traits

MissingPartTwo is a marker trait to tell the compiler that your struct doesn’t impl Solution for Part2.

Implement this trait to parse the raw input into a more useful type for your Solution to use as input.

Implement this trait with your solution to the Advent of Code problem for a particular day and part.

Import this trait to run your advent of code solutions once they implement Solution.