[rs] Detect day based on input file name
This commit is contained in:
parent
930b3196fe
commit
214eb5b258
1 changed files with 56 additions and 10 deletions
|
|
@ -1,27 +1,73 @@
|
|||
mod d01;
|
||||
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str::FromStr;
|
||||
use std::{fmt, fs};
|
||||
|
||||
use clap::{Parser, ValueEnum};
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, ValueEnum)]
|
||||
enum Day {
|
||||
D01,
|
||||
Y2022D01,
|
||||
}
|
||||
|
||||
impl Day {
|
||||
fn from_path(path: &Path) -> Option<Self> {
|
||||
Self::from_str(path.file_stem()?.to_str()?).ok()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Day {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Day::Y2022D01 => "2022_01",
|
||||
}
|
||||
.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for Day {
|
||||
type Err = ();
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
Ok(match s {
|
||||
"2022_01" => Self::Y2022D01,
|
||||
_ => return Err(()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Args {
|
||||
day: Day,
|
||||
file: PathBuf,
|
||||
files: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let args = Args::parse();
|
||||
let input = fs::read_to_string(&args.file)?;
|
||||
|
||||
match args.day {
|
||||
Day::D01 => d01::solve(input)?,
|
||||
if args.files.is_empty() {
|
||||
eprintln!("No days specified");
|
||||
}
|
||||
|
||||
let mut first_day = true;
|
||||
for file in args.files {
|
||||
if !first_day {
|
||||
println!();
|
||||
}
|
||||
first_day = false;
|
||||
|
||||
let day = match Day::from_path(&file) {
|
||||
Some(day) => day,
|
||||
None => {
|
||||
eprintln!("### Could not determine day: {file:?}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
println!("### Solving day {day}");
|
||||
let input = fs::read_to_string(file)?;
|
||||
match day {
|
||||
Day::Y2022D01 => d01::solve(input)?,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue