use std::fs::read_to_string;
use ahc_evaluation::{arguments::Arguments, build, config::Config, evaluation};
use anyhow::{ensure, Context};
use clap::Parser;
use rayon::ThreadPoolBuilder;
fn main() -> anyhow::Result<()> {
let args = Arguments::parse();
let config = Config::read_from_file(args.config)?;
if let Some(thread_num) = config.thread.thread_num {
ThreadPoolBuilder::new()
.num_threads(thread_num)
.build_global()
.with_context(|| "Failed to set the number of threads.")?;
}
let seeds = read_seed_from_file(&config)?;
ensure!(!seeds.is_empty(), "Seed list is empty.");
build::build_tester(&config)?;
build::build_submission(&config)?;
let evaluation_table = evaluation::evaluate(&config, &seeds)?;
evaluation::show_statistics(&evaluation_table)?;
evaluation::write_to_csv(&config.path.evaluation_record, &evaluation_table)?;
Ok(())
}
fn read_seed_from_file(config: &Config) -> anyhow::Result<Vec<usize>> {
let seeds = read_to_string(&config.path.seed_file)
.with_context(|| format!("Failed to read seed file `{:?}`.", config.path.seed_file))?
.lines()
.filter_map(|line| line.split('#').next()?.split_whitespace().next())
.map(|seed| {
seed.parse::<usize>()
.with_context(|| format!("Failed to parse `{}` as seed.", seed))
})
.collect::<Result<Vec<usize>, _>>()?;
Ok(seeds)
}