#[solution]Expand description
A solution of an advent of code problem.
You need to pass in day = X for the macro to work. If the function is named part1 or
part2, the part gets set accordingly; otherwise you need to specify it as part = Y.
You can also specify the expected result of the example given in the puzzle using example = Z
or example = [A, B, C, ...] if there are multiple. The examples need to be defined somewhere
using elvish::example!() for them to work.
At the end of the day, this macro is mostly to reduce boilerplate but it’s easily expandable by hand.
§Example usage
Solution for day 1 of 2023:
#[elvish::solution(day = 1, example = 142)]
fn part1(input: &str) -> u32 {
input
.lines()
.filter(|line| !line.is_empty())
.map(|line| {
let mut iter = line.chars().filter_map(|c| c.to_digit(10));
let a = iter.next().unwrap();
let b = iter.last().unwrap_or(a);
a * 10 + b
})
.sum()
}which generates:
impl elvish::solution::Part<1, 1> for crate::Solutions {
fn solve(input: &str) -> impl std::fmt::Display {
part1(input)
}
}
#[test]
fn part1_example() {
assert_eq!(part1(EXAMPLE_PART1), 142)
}
fn part1(input: &str) -> u32 {
// --snip--
}