Crate advent_of_code_traits[][src]

impl Solution<Day25> for AdventOfCode2021

What is this?

This is advent_of_code_traits, a minimal, flexible framework for in implementing solutions to Advent of Code in Rust.

It takes a trait-based approach using const-generics.

Usage

Please see also the examples.

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

Import the traits:

use advent_of_code_traits::{days::Day1, ParseInput, Solution};

Implement Solution for your struct.

pub struct AdventOfCode2020;

impl Solution<Day1> for AdventOfCode2020 {
    type Part1Output = u32;
    type Part2Output = u32;

    fn part1(input: &Vec<u32>) -> u32 {
        // your solution to part1 here...
    }

    fn part2(input: &Vec<u32>) -> u32 {
        // your solution to part2 here...
    }
}

This is completely valid rust code, don’t you like the way it reads?

“But where does input: Vec<u32> come from?”, you ask.

Well spotted, eagle-eyed reader!

That comes from an implementation of ParseInput.

Implement ParseInput for your struct

// ..continued from above

impl ParseInput<Day1> for AdventOfCode2020 {
    type Parsed = Vec<u32>; // <-- the input to both part1 and part2 for Solution<Day1>

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

Please refer to the examples for more possibilities, including parsing a different type for each Part and opting out of parsing entirely to work directly with the &str.

Run from main.rs

Here comes the ugly part.

let input = std::fs::read_to_string("./input/2020/day1.txt").expect("failed to read input");
<AdventOfCode2020 as Solution<Day1>>::run(&input);

This reads input from a file and passes it to your struct. Fully Qualified Syntax is required in order to disambiguate which day’s Solution we are running.

Modules

days

Constants for all 25 days of Advent

Constants

Part1

Constant for part1 of each day. See also ParseEachInput.

Part2

Constant for part2 of each day. See also ParseEachInput.

Traits

ParseEachInput

Implement this trait if you need a different input for each part of a day.

ParseInput

Implement this trait to parse the the day’s input into a type.

Solution

Implement the Solution trait for each day of Advent of Code for your struct(s).