|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::path::PathBuf; |
| 3 | + |
| 4 | +use anyhow::{bail, Context, Error}; |
| 5 | +use clap::Parser; |
| 6 | + |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | +use task_maker_format::ioi::IOITask; |
| 9 | +use task_maker_format::{ |
| 10 | + EvaluationConfig, EvaluationData, Solution, SolutionCheckResult, TaskFormat, |
| 11 | +}; |
| 12 | + |
| 13 | +use crate::{FilterOpt, FindTaskOpt}; |
| 14 | + |
| 15 | +#[derive(Parser, Debug, Clone)] |
| 16 | +pub struct ExportSolutionChecksOpt { |
| 17 | + #[clap(flatten, next_help_heading = Some("TASK SEARCH"))] |
| 18 | + pub find_task: FindTaskOpt, |
| 19 | + |
| 20 | + #[clap(flatten, next_help_heading = Some("FILTER"))] |
| 21 | + pub filter: FilterOpt, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Serialize, Deserialize)] |
| 25 | +struct SolutionWithChecks { |
| 26 | + path: PathBuf, |
| 27 | + checks: Vec<Option<SolutionCheckResult>>, |
| 28 | + min_score: f64, |
| 29 | + max_score: f64, |
| 30 | +} |
| 31 | + |
| 32 | +pub fn main_export_solution_checks(opt: ExportSolutionChecksOpt) -> Result<(), Error> { |
| 33 | + let eval_config = EvaluationConfig { |
| 34 | + solution_filter: opt.filter.filter, |
| 35 | + booklet_solutions: false, |
| 36 | + no_statement: true, |
| 37 | + solution_paths: opt.filter.solution, |
| 38 | + disabled_sanity_checks: Default::default(), |
| 39 | + seed: Default::default(), |
| 40 | + dry_run: true, |
| 41 | + }; |
| 42 | + let task = opt |
| 43 | + .find_task |
| 44 | + .find_task(&eval_config) |
| 45 | + .context("Failed to locate the task")?; |
| 46 | + |
| 47 | + let TaskFormat::IOI(task) = task else { |
| 48 | + bail!("Exporting solution checks is only supported for IOI tasks") |
| 49 | + }; |
| 50 | + |
| 51 | + let (mut eval, _) = EvaluationData::new(task.path()); |
| 52 | + let solutions = eval_config.find_solutions( |
| 53 | + task.path(), |
| 54 | + vec!["sol/*"], |
| 55 | + Some(task.grader_map.clone()), |
| 56 | + &mut eval, |
| 57 | + ); |
| 58 | + |
| 59 | + let subtasks = task |
| 60 | + .subtasks |
| 61 | + .iter() |
| 62 | + .filter_map(|(_, info)| info.name.clone().map(|name| (name, info.id))) |
| 63 | + .collect::<HashMap<_, _>>(); |
| 64 | + |
| 65 | + let checks = solutions |
| 66 | + .iter() |
| 67 | + .map(|solution| extract_solution_checks(&task, &subtasks, solution)) |
| 68 | + .collect::<Result<Vec<_>, _>>()?; |
| 69 | + |
| 70 | + println!("{}", serde_json::to_string_pretty(&checks)?); |
| 71 | + |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +fn extract_solution_checks( |
| 76 | + task: &IOITask, |
| 77 | + subtasks: &HashMap<String, u32>, |
| 78 | + solution: &Solution, |
| 79 | +) -> anyhow::Result<SolutionWithChecks> { |
| 80 | + let mut checks = vec![None; task.subtasks.len()]; |
| 81 | + for check in &solution.checks { |
| 82 | + if let Some(&idx) = subtasks.get(&check.subtask_name_pattern) { |
| 83 | + let idx: usize = idx.try_into()?; |
| 84 | + if checks[idx].is_some() { |
| 85 | + bail!( |
| 86 | + "Found multiple checks for subtask {} in solution {}", |
| 87 | + check.subtask_name_pattern, |
| 88 | + solution.source_file.path.display() |
| 89 | + ); |
| 90 | + } |
| 91 | + checks[idx] = Some(check.result); |
| 92 | + } else if check.subtask_name_pattern == "*" { |
| 93 | + for subtask_check in checks.iter_mut() { |
| 94 | + if subtask_check.is_some() { |
| 95 | + bail!( |
| 96 | + "Found multiple checks for subtask {} in solution {}", |
| 97 | + check.subtask_name_pattern, |
| 98 | + solution.source_file.path.display() |
| 99 | + ); |
| 100 | + } |
| 101 | + *subtask_check = Some(check.result); |
| 102 | + } |
| 103 | + } else { |
| 104 | + bail!( |
| 105 | + "Found invalid subtask check {} in solution {}", |
| 106 | + check.subtask_name_pattern, |
| 107 | + solution.source_file.path.display() |
| 108 | + ); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + let mut min_score = 0.; |
| 113 | + let mut max_score = 0.; |
| 114 | + |
| 115 | + for (i, check) in checks.iter().enumerate() { |
| 116 | + if *check == Some(SolutionCheckResult::Accepted) { |
| 117 | + min_score += task.subtasks[&i.try_into()?].max_score; |
| 118 | + max_score += task.subtasks[&i.try_into()?].max_score; |
| 119 | + } else if *check == Some(SolutionCheckResult::PartialScore) || check.is_none() { |
| 120 | + max_score += task.subtasks[&i.try_into()?].max_score; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + Ok(SolutionWithChecks { |
| 125 | + path: solution.source_file.path.clone(), |
| 126 | + checks, |
| 127 | + min_score, |
| 128 | + max_score, |
| 129 | + }) |
| 130 | +} |
0 commit comments