Initial 2021 runner

This commit is contained in:
2021-11-20 11:57:32 +01:00
parent 5f98b62f21
commit cece8439a7
5 changed files with 102 additions and 0 deletions

3
.gitignore vendored
View File

@@ -56,3 +56,6 @@ docs/_build/
# PyBuilder
target/
*.swp
# Rust lock
*.lock

8
2021/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "aoc-2021"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = "3.0.0-beta.5"

10
2021/src/day01.rs Normal file
View File

@@ -0,0 +1,10 @@
use crate::Solution;
#[derive(Default)]
pub struct Day01;
impl Solution for Day01 {
fn part1(&mut self, _input: &mut dyn std::io::Read) -> String {
todo!()
}
}

29
2021/src/lib.rs Normal file
View File

@@ -0,0 +1,29 @@
use std::io::Read;
mod day01;
pub trait Solution {
fn part1(&mut self, input: &mut dyn Read) -> String;
fn part2(&mut self, _input: &mut dyn Read) -> String {
unimplemented!("Still working on part 1");
}
}
pub fn get_implementation(day: usize) -> Box<dyn Solution> {
match day {
1 => Box::new(day01::Day01::default()),
_ => panic!("Unsupported day {}", day),
}
}
#[cfg(test)]
fn test_implementation(mut day: impl Solution, part: u8, mut input: &[u8], answer: impl ToString) {
let result = match part {
1 => day.part1(&mut input),
2 => day.part2(&mut input),
_ => panic!("Invalid part: {}", part),
};
assert_eq!(answer.to_string(), result);
}

52
2021/src/main.rs Normal file
View File

@@ -0,0 +1,52 @@
use std::fs::File;
use std::io::Read;
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::time::Instant;
use clap::Parser;
use aoc_2021::get_implementation;
/// Advent of Code 2021 runner
#[derive(Parser)]
struct Opts {
/// Which day to run
day: NonZeroUsize,
/// Print time taken
#[clap(short, long)]
time: bool,
/// Run part 2 instead of part 1
#[clap(short = '2', long)]
part2: bool,
/// Read input from the given file instead of stdin
#[clap(short, long)]
input: Option<PathBuf>,
}
fn main() {
let opts: Opts = Opts::parse();
let mut implementation = get_implementation(opts.day.get());
let mut input: Box<dyn Read> = if let Some(input) = opts.input {
Box::new(File::open(&input).expect("Failed to open input"))
} else {
Box::new(std::io::stdin())
};
let begin = Instant::now();
let result = if opts.part2 {
implementation.part2(&mut input)
} else {
implementation.part1(&mut input)
};
if opts.time {
eprintln!("Execution time: {:?}", Instant::now().duration_since(begin));
}
println!("{}", result);
}