Struct AocDay

Source
pub struct AocDay<T> { /* private fields */ }
Expand description

The AocDay struct stores information for an aoc day.

You can provide an optional serializer function to serialize the input data into a custom type. The serializer, and the solver functions if you’re not using a custom serializer function, take Strings as input.

Implementations§

Source§

impl AocDay<String>

Source

pub fn new(year: i32, day: u8) -> Self

Create a new AocDay instance for the provided year and day.

§Example
use aoc_helper::AocDay;

// Create a new AocDay instance for day 12 of aoc 2015
let day_12 = AocDay::new(2015, 12);
Examples found in repository?
examples/2015_day1.rs (line 22)
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§

impl<T> AocDay<T>

Source

pub fn new_with_serializer( year: i32, day: u8, serializer: fn(String) -> T, ) -> Self

Create a new AocDay instance for the provided year and day, with a custom serializer function.

§Example
use aoc_helper::AocDay;

let day_2 = AocDay::new_with_serializer(2017, 2, |input| input.split_whitespace().collect::<Vec<_>>());
Source

pub fn input(&mut self, input_path: &str)

Provide a custom input file

§Example
use aoc_helper::AocDay;

let mut day_2 = AocDay::new(2017, 2);
day_2.input("path/to/my/input/file.txt");
Source

pub fn with_input(self, input_path: &str) -> Self

Chainable version of AocDay::input()

§Example
use aoc_helper::AocDay;

let day_2 = AocDay::new(2017, 2)
    .with_input("path/to/my/input/file.txt");
Source

pub fn session_id(&mut self, session_id: &str)

Provide the session ID

§Example
use aoc_helper::AocDay;

let mut day_8 = AocDay::new(2015, 8);
day_8.session_id("82942d3671962a9f17f8f81723d45b0fcdf8b3b5bf6w3954f02101fa5de1420b6ecd30ed550133f32d6a5c00233076af");
Source

pub fn with_session_id(self, session_id: &str) -> Self

Chainable version of AocDay::session_id()

§Example
use aoc_helper::AocDay;

let date_8 = AocDay::new(2015, 8)
    .with_session_id("82942d3671962a9f17f8f81723d45b0fcdf8b3b5bf6w3954f02101fa5de1420b6ecd30ed550133f32d6a5c00233076af");
Source

pub fn test(&self, puzzle: &Puzzle<T, impl Display>)

Run a solver function on some example inputs. The function and the inputs should be provided using a Puzzle instance.

§Example
use aoc_helper::{AocDay, Puzzle};

let day_5 = AocDay::new(2019, 5);
day_5.test(
    &Puzzle::new(1, |x: String| x.chars().filter(|&y| y == 'z').count())
        .with_examples(&["test", "cases"])
);
Examples found in repository?
examples/2015_day1.rs (line 37)
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 run( &mut self, puzzle: &Puzzle<T, impl Display>, ) -> Result<(), Box<dyn Error>>

Run a solver function on the day’s input. The function should be provided using a Puzzle instance.

§Example
use aoc_helper::{AocDay, Puzzle};

let mut day_5 = AocDay::new(2019, 5);
let part_1 = Puzzle::new(
        1,
        |x: String| x.chars().filter(|&y| y == 'z').count()
    )
    .with_examples(&["foo", "bar", "baz"]);
let part_2 = Puzzle::new(
        2,
        |x: String| x.chars().filter(|&y| y != 'z').count()
    )
    .with_examples(&["fubar", "bazz", "fubaz"]);
day_5.test(&part_1);
day_5.test(&part_2);
day_5.run(&part_1);
day_5.run(&part_2);
Examples found in repository?
examples/2015_day1.rs (line 40)
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> Freeze for AocDay<T>

§

impl<T> RefUnwindSafe for AocDay<T>

§

impl<T> Send for AocDay<T>

§

impl<T> Sync for AocDay<T>

§

impl<T> Unpin for AocDay<T>

§

impl<T> UnwindSafe for AocDay<T>

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.