Attempt at solving some Advent of Code 2022 puzzles using Rust which I have never used before.
Thanks to MetroWind/advent2019 and fspoettel/advent-of-code-rust for inspiration on how to structure the project.
Run cargo start-day n
where n
is the day number
Let us say you are adding day 4
- Add files
inputs/day4.txt
andexample_inputs/day4.txt
and add AOC input - Add
pub mod day4;
into themod days {...}
block inmain.rs
- Add
"4" => Box::new(days::day4::Day4 {}),
into thematch n {...}
block inmain.rs
- Create file
src/days/day4.rs
and implement theDay
trait like so:
use crate::days::day;
use crate::days::day::Input;
pub struct Day4 {}
impl day::Day for Day4 {
fn part_one(&self, input: &Input) -> String {
return "p1".to_string();
}
fn part_two(&self, input: &Input) -> String {
return "p2".to_string();
}
}
- The type
Input
passed intopart_one
andpart_two
is defined inday.rs
, and has two useful methods on it -as_string
andas_list
. These will get you the relevant day's input parsed as either aString
or aVec<String>
. See below for how to use real or example input.
cargo run-examples 1
cargo run 1
cargo run