Rust solutions for Advent of Code problems
This repo is structured as a cargo workspace. Each day's problem is solved in a
separate binary crate, named dayNN. There are also library crates. The following
steps set me up with a baseline to work on a new day's problem. When I learn
how, I'll write an app that plugs in to cargo so I can just say something like
cargo aoc NN
.
cargo new dayNN
- Add
dayNN
to the workspace members list in./Cargo.toml
- If the problem has a large puzzle input (most days do), create file
dayNN/src/dayNN.txt
and copy in the puzzle input - Add
const INPUT: &str = include_str!("dayNN.txt");
to the top ofdayNN/main.rs
. If the puzzle input is short (e.g., a single line), then it can be assigned directly toINPUT
instead of reading it out ofdayNN.txt
withinclude_str!
. - Replace the rest of
main.rs
with:
// const INPUT: &str = include_str!("dayNN.txt");
// const INPUT: &str = "";
fn main() {
println!("Day NN Part 1: {:?}", part1(INPUT));
println!("Day NN Part 2: {:?}", part2(INPUT));
}
// replace return type as required by the problem
fn part1(input: &str) -> i32 {
0
}
// replace return type as required by the problem
fn part2(input: &str) -> i32 {
0
}
#[cfg(test)]
mod tests {
// const TEST_INPUT: &str = include_str!("dayNN_test.txt");
use super::*;
use test_support::test_support::TestCase;
#[test]
fn test_part1() {
let test_cases = [
TestCase { input: TEST_INPUT, expected: 123 },
TestCase { input: "abc", expected: 345 },
];
for TestCase{input, expected} in test_cases.iter() {
assert_eq!(part1(*input), *expected);
}
}
#[test]
fn test_part2() {
let test_cases = [
TestCase { input: TEST_INPUT, expected: 123 },
TestCase { input: "abc", expected: 345 },
];
for TestCase{input, expected} in test_cases.iter() {
assert_eq!(part2(*input), *expected);
}
}
}
- Add
test_support = {path="../test_support"}
to thedependencies
indayNN/Cargo.toml
(depending on the problem, this might not be used) - Add
dayNN/notes.md
to capture thoughts and reflections on the problem and Rust features - Run
cargo build -p dayNN
andcargo test -p dayNN
to trigger an update toCargo.lock
git add dayNN Cargo.toml Cargo.lock
andgit commit -m "Added Day NN skeleton
andgit push