mirror of
https://github.com/Noratrieb/advent-of-code.git
synced 2026-01-14 09:35:01 +01:00
helper
This commit is contained in:
parent
c59b21ebc7
commit
b093225df0
3 changed files with 102 additions and 22 deletions
|
|
@ -1,3 +1,4 @@
|
|||
use helper::{Day, Variants};
|
||||
use nom::{
|
||||
branch::alt,
|
||||
bytes::complete::tag,
|
||||
|
|
@ -24,6 +25,18 @@ fn main() {
|
|||
println!("result: {result}");
|
||||
}
|
||||
|
||||
struct Day4;
|
||||
|
||||
impl Day for Day4 {
|
||||
fn part1() -> helper::Variants {
|
||||
Variants::basic(part1)
|
||||
}
|
||||
|
||||
fn part2() -> helper::Variants {
|
||||
Variants::basic(part2)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
enum Color {
|
||||
Red = 0,
|
||||
|
|
@ -90,22 +103,21 @@ fn part2(input: &str) -> u64 {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn part1_small() {
|
||||
assert_eq!(super::part1(include_str!("../input_small.txt")), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
assert_eq!(super::part1(include_str!("../input.txt")), 1931);
|
||||
helper::test_part1::<super::Day4>(&[
|
||||
(include_str!("../input_small.txt"), 8),
|
||||
(include_str!("../input.txt"), 1931),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_small() {
|
||||
assert_eq!(super::part2(include_str!("../input_small.txt")), 2286);
|
||||
}
|
||||
#[test]
|
||||
fn part2() {
|
||||
assert_eq!(super::part2(include_str!("../input.txt")), 83105);
|
||||
helper::test_part2::<super::Day4>(&[
|
||||
(include_str!("../input_small.txt"), 2286),
|
||||
(include_str!("../input.txt"), 83105),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
use std::collections::HashMap;
|
||||
|
||||
use helper::{Day, Variants};
|
||||
|
||||
fn main() {
|
||||
let kind = std::env::args().nth(1).unwrap_or("naive".into());
|
||||
|
||||
|
|
@ -17,6 +19,18 @@ fn main() {
|
|||
println!("result: {result}");
|
||||
}
|
||||
|
||||
struct Day3;
|
||||
|
||||
impl Day for Day3 {
|
||||
fn part1() -> Variants {
|
||||
Variants::basic(part1)
|
||||
}
|
||||
|
||||
fn part2() -> Variants {
|
||||
Variants::basic(part2)
|
||||
}
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> u64 {
|
||||
fn contains_symbol(s: &str) -> bool {
|
||||
s.chars().any(|c| !c.is_ascii_digit() && c != '.')
|
||||
|
|
@ -118,22 +132,21 @@ fn part2(input: &str) -> u64 {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn part1_small() {
|
||||
assert_eq!(super::part1(include_str!("../input_small.txt")), 4361);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part1() {
|
||||
assert_eq!(super::part1(include_str!("../input.txt")), 537832);
|
||||
helper::test_part1::<super::Day3>(&[
|
||||
(include_str!("../input_small.txt"), 4361),
|
||||
(include_str!("../input.txt"), 537832),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn part2_small() {
|
||||
assert_eq!(super::part2(include_str!("../input_small.txt")), 467835);
|
||||
}
|
||||
#[test]
|
||||
fn part2() {
|
||||
assert_eq!(super::part2(include_str!("../input.txt")), 81939900);
|
||||
helper::test_part2::<super::Day3>(&[
|
||||
(include_str!("../input_small.txt"), 467835),
|
||||
(include_str!("../input.txt"), 81939900),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,60 @@
|
|||
use std::borrow::Cow;
|
||||
|
||||
use nom::{character::complete::digit1, combinator::map, IResult};
|
||||
|
||||
pub type Solution = fn(&str) -> u64;
|
||||
|
||||
pub trait Day {
|
||||
fn part1() -> Variants;
|
||||
|
||||
fn part2() -> Variants;
|
||||
|
||||
/// Pad or manipulate the input in ways that don't necessarily
|
||||
/// change it but do things that may be sound or unsound.
|
||||
fn pad_input(input: &str) -> Cow<str> {
|
||||
Cow::Borrowed(input)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Variants {
|
||||
pub variants: Vec<Variant>,
|
||||
}
|
||||
|
||||
pub struct Variant {
|
||||
pub name: &'static str,
|
||||
pub f: Solution,
|
||||
}
|
||||
|
||||
impl Variants {
|
||||
pub fn basic(f: Solution) -> Self {
|
||||
Variants {
|
||||
variants: vec![Variant { name: "basic", f }],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_part1<D: Day>(inputs: &[(&str, u64)]) {
|
||||
for variant in D::part1().variants {
|
||||
for input in inputs {
|
||||
let actual = (variant.f)(input.0);
|
||||
if actual != input.1 {
|
||||
panic!("failed: {}: {actual} != {}", variant.name, input.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_part2<D: Day>(inputs: &[(&str, u64)]) {
|
||||
for variant in D::part2().variants {
|
||||
for input in inputs {
|
||||
let actual = (variant.f)(input.0);
|
||||
if actual != input.1 {
|
||||
panic!("failed: {}: {actual} != {}", variant.name, input.1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn integer(input: &str) -> IResult<&str, u64> {
|
||||
map(digit1, |d: &str| d.parse::<u64>().unwrap())(input)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue