Code for the Advent of Code event
aoc advent-of-code
at rust 106 lines 2.9 kB view raw
1use once_cell::sync::Lazy; 2use std::collections::HashMap; 3 4/// Function type for AoC solutions 5pub type SolutionFn = fn(&str) -> String; 6 7/// Metadata for a solution 8#[derive(Debug, Clone)] 9pub struct Solution { 10 pub year: u32, 11 pub day: u32, 12 pub part: u32, 13 pub name: &'static str, 14 pub function: SolutionFn, 15} 16 17/// Global registry for all AoC solutions 18pub struct Registry { 19 solutions: std::sync::Mutex<HashMap<(u32, u32, u32), Solution>>, 20} 21 22impl Registry { 23 fn new() -> Self { 24 Self { 25 solutions: std::sync::Mutex::new(HashMap::new()), 26 } 27 } 28 29 /// Register a solution with the registry 30 pub fn register_solution( 31 &self, 32 year: u32, 33 day: u32, 34 part: u32, 35 name: &'static str, 36 function: SolutionFn, 37 ) { 38 let solution = Solution { 39 year, 40 day, 41 part, 42 name, 43 function, 44 }; 45 46 let mut solutions = self.solutions.lock().unwrap(); 47 solutions.insert((year, day, part), solution); 48 } 49 50 /// Get a specific solution 51 pub fn get_solution(&self, year: u32, day: u32, part: u32) -> Option<Solution> { 52 let solutions = self.solutions.lock().unwrap(); 53 solutions.get(&(year, day, part)).cloned() 54 } 55 56 /// Get all solutions for a specific year and day 57 pub fn get_day_solutions(&self, year: u32, day: u32) -> Vec<Solution> { 58 let solutions = self.solutions.lock().unwrap(); 59 solutions 60 .values() 61 .filter(|s| s.year == year && s.day == day) 62 .cloned() 63 .collect() 64 } 65 66 /// Get all solutions for a specific year 67 pub fn get_year_solutions(&self, year: u32) -> Vec<Solution> { 68 let solutions = self.solutions.lock().unwrap(); 69 solutions 70 .values() 71 .filter(|s| s.year == year) 72 .cloned() 73 .collect() 74 } 75 76 /// Get all available solutions 77 pub fn get_all_solutions(&self) -> Vec<Solution> { 78 let solutions = self.solutions.lock().unwrap(); 79 solutions.values().cloned().collect() 80 } 81 82 /// Get all available years 83 pub fn get_years(&self) -> Vec<u32> { 84 let solutions = self.solutions.lock().unwrap(); 85 let mut years: Vec<u32> = solutions.values().map(|s| s.year).collect(); 86 years.sort_unstable(); 87 years.dedup(); 88 years 89 } 90 91 /// Get all available days for a year 92 pub fn get_days(&self, year: u32) -> Vec<u32> { 93 let solutions = self.solutions.lock().unwrap(); 94 let mut days: Vec<u32> = solutions 95 .values() 96 .filter(|s| s.year == year) 97 .map(|s| s.day) 98 .collect(); 99 days.sort_unstable(); 100 days.dedup(); 101 days 102 } 103} 104 105/// Global registry instance 106pub static REGISTRY: Lazy<Registry> = Lazy::new(Registry::new);