Struct Puzzle

Source
pub struct Puzzle<T, D> { /* private fields */ }
Expand description

The Puzzle struct stores information for an aoc puzzle. Two puzzles release each aoc day.

Implementations§

Source§

impl<T, D: Display> Puzzle<T, D>

Source

pub fn new(part: u8, solver: fn(T) -> D) -> Self

Create a new Puzzle instance for the provided part number and solver function.

§Example
use aoc_helper::Puzzle;

let part_1 = Puzzle::new(1, |x| x.lines().count());
Examples found in repository?
examples/2015_day1.rs (lines 25-29)
18fn main() {
19    // NOTE: You need to specify a session ID for this to work
20
21    // Create a new `AocDay` instance for day 1 of aoc 2015
22    let mut day_1 = AocDay::new(2015, 1);
23
24    // Create a new `Puzzle` instance for part 1
25    let part_1 = Puzzle::new(1, |instructions: String| {
26            let mut floor = 0;
27            instructions.chars().for_each(|x| if x == '(' { floor += 1 } else { floor -= 1 });
28            floor
29        })
30        .with_examples(&["(())", "()()", ")))", ")())())"]);
31
32    // Create a new `Puzzle` instance for part 2
33    let part_2 = Puzzle::new(2, first_instruction_that_sends_to_basement)
34        .with_examples(&[")", "()())"]);
35
36    // Test the example cases
37    day_1.test(&part_1);
38    day_1.test(&part_2);
39    // Run the day's input
40    day_1.run(&part_1).unwrap();
41    day_1.run(&part_2).unwrap();
42}
Source

pub fn examples<S: ToString>(&mut self, examples: &[S])

Provide some example inputs for a Puzzle instance that the solver function will be given when AocDay::test() is called.

§Example
use aoc_helper::Puzzle;

let mut part_2 = Puzzle::new(2, |x| x.lines().nth(0).unwrap());
part_2.examples(&["example\ninput", "foo\nbar"]);
Source

pub fn with_examples<S: ToString>(self, examples: &[S]) -> Self

Chainable version of Puzzle::examples()

§Example
use aoc_helper::Puzzle;

let part_2 = Puzzle::new(2, |x| lines().nth(0).unwrap())
    .with_examples(&["example\ninput", "foo\nbar"]);
Examples found in repository?
examples/2015_day1.rs (line 30)
18fn main() {
19    // NOTE: You need to specify a session ID for this to work
20
21    // Create a new `AocDay` instance for day 1 of aoc 2015
22    let mut day_1 = AocDay::new(2015, 1);
23
24    // Create a new `Puzzle` instance for part 1
25    let part_1 = Puzzle::new(1, |instructions: String| {
26            let mut floor = 0;
27            instructions.chars().for_each(|x| if x == '(' { floor += 1 } else { floor -= 1 });
28            floor
29        })
30        .with_examples(&["(())", "()()", ")))", ")())())"]);
31
32    // Create a new `Puzzle` instance for part 2
33    let part_2 = Puzzle::new(2, first_instruction_that_sends_to_basement)
34        .with_examples(&[")", "()())"]);
35
36    // Test the example cases
37    day_1.test(&part_1);
38    day_1.test(&part_2);
39    // Run the day's input
40    day_1.run(&part_1).unwrap();
41    day_1.run(&part_2).unwrap();
42}

Auto Trait Implementations§

§

impl<T, D> Freeze for Puzzle<T, D>

§

impl<T, D> RefUnwindSafe for Puzzle<T, D>

§

impl<T, D> Send for Puzzle<T, D>

§

impl<T, D> Sync for Puzzle<T, D>

§

impl<T, D> Unpin for Puzzle<T, D>

§

impl<T, D> UnwindSafe for Puzzle<T, D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.