use std::{ fs::{create_dir_all, read_to_string, write}, path::PathBuf, }; use {color_eyre::Result, dialoguer::Password, rand::seq::IteratorRandom}; use crate::{solution::Solution, year_2020, year_2021}; pub fn get_solutions() -> Vec> { vec![year_2020::get_solutions(), year_2021::get_solutions()] } pub fn random_emoji() -> String { emojis::iter() .filter(|emoji| emoji.group() == emojis::Group::AnimalsAndNature) .choose(&mut rand::thread_rng()) .unwrap() .to_string() } pub fn session_cookie() -> Result { let session_cookie_path = PathBuf::from("aoc/session.txt"); if session_cookie_path.exists() { read_to_string(session_cookie_path).map_err(Into::into) } else { let session_cookie = Password::new() .with_prompt("Advent of Code Session Cookie") .interact()?; write_file(session_cookie_path, session_cookie) } } pub fn write_file(path: PathBuf, contents: String) -> Result { create_dir_all(path.parent().unwrap())?; write(path, &contents)?; Ok(contents) }