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 String
s as input.
Implementations§
Source§impl AocDay<String>
impl AocDay<String>
Sourcepub fn new(year: i32, day: u8) -> Self
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?
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>
impl<T> AocDay<T>
Sourcepub fn new_with_serializer(
year: i32,
day: u8,
serializer: fn(String) -> T,
) -> Self
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<_>>());
Sourcepub fn input(&mut self, input_path: &str)
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");
Sourcepub fn with_input(self, input_path: &str) -> Self
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");
Sourcepub fn session_id(&mut self, session_id: &str)
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");
Sourcepub fn with_session_id(self, session_id: &str) -> Self
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");
Sourcepub fn test(&self, puzzle: &Puzzle<T, impl Display>)
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?
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}
Sourcepub fn run(
&mut self,
puzzle: &Puzzle<T, impl Display>,
) -> Result<(), Box<dyn Error>>
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?
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}