Skip to content

Commit

Permalink
Compress code
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Aug 27, 2024
1 parent 725391d commit 122741a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 111 deletions.
77 changes: 31 additions & 46 deletions rust/src/include/problems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use seq_macro::seq;

use crate::include::utils::Answer;
seq!(N in 0001..=0024 {
use crate::p~N::p~N;
use crate::p~N::p~N;
});
use crate::p0027::p0027;
use crate::p0034::p0034;
Expand All @@ -24,51 +24,36 @@ use crate::p0836::p0836;
type ProblemType = fn() -> Answer;
type ProblemRef<'a> = (&'a usize, ProblemType, bool);

pub fn get_problem<'b>(n: usize) -> Option<ProblemRef<'b>> {
return match n {
1 => Some(( &1, p0001, false)),
2 => Some(( &2, p0002, false)),
3 => Some(( &3, p0003, false)),
4 => Some(( &4, p0004, false)),
5 => Some(( &5, p0005, false)),
6 => Some(( &6, p0006, false)),
7 => Some(( &7, p0007, false)),
8 => Some(( &8, p0008, false)),
9 => Some(( &9, p0009, false)),
10 => Some(( &10, p0010, false)),
11 => Some(( &11, p0011, false)),
12 => Some(( &12, p0012, false)),
13 => Some(( &13, p0013, false)),
14 => Some(( &14, p0014, false)),
15 => Some(( &15, p0015, false)),
16 => Some(( &16, p0016, false)),
17 => Some(( &17, p0017, false)),
18 => Some(( &18, p0018, false)),
19 => Some(( &19, p0019, false)),
20 => Some(( &20, p0020, false)),
21 => Some(( &21, p0021, true)),
22 => Some(( &22, p0022, false)),
23 => Some(( &23, p0023, false)),
24 => Some(( &24, p0024, false)),
27 => Some(( &27, p0027, true)),
34 => Some(( &34, p0034, false)),
36 => Some(( &36, p0036, false)),
37 => Some(( &37, p0037, true)),
44 => Some(( &44, p0044, false)),
45 => Some(( &45, p0045, false)),
53 => Some(( &53, p0053, false)),
59 => Some(( &59, p0059, false)),
67 => Some(( &67, p0067, false)),
69 => Some(( &69, p0069, false)),
76 => Some(( &76, p0076, false)),
77 => Some(( &77, p0077, false)),
87 => Some(( &87, p0087, false)),
187 => Some((&187, p0187, false)),
357 => Some((&357, p0357, true)),
836 => Some((&836, p0836, false)),
_ => None,
};
}
seq!(N in 0001..=0020 {
pub fn get_problem<'b>(n: usize) -> Option<ProblemRef<'b>> {
return match n {
#(
N => Some(( &N, p~N, false)),
)*
21 => Some(( &21, p0021, true)),
22 => Some(( &22, p0022, false)),
23 => Some(( &23, p0023, false)),
24 => Some(( &24, p0024, false)),
27 => Some(( &27, p0027, true)),
34 => Some(( &34, p0034, false)),
36 => Some(( &36, p0036, false)),
37 => Some(( &37, p0037, true)),
44 => Some(( &44, p0044, false)),
45 => Some(( &45, p0045, false)),
53 => Some(( &53, p0053, false)),
59 => Some(( &59, p0059, false)),
67 => Some(( &67, p0067, false)),
69 => Some(( &69, p0069, false)),
76 => Some(( &76, p0076, false)),
77 => Some(( &77, p0077, false)),
87 => Some(( &87, p0087, false)),
187 => Some((&187, p0187, false)),
357 => Some((&357, p0357, true)),
836 => Some((&836, p0836, false)),
_ => None,
};
}
});


pub fn generate_supported_problems(include_slow: bool) -> Vec<usize> {
Expand Down
122 changes: 60 additions & 62 deletions rust/src/include/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,74 +12,72 @@ pub enum Answer {
}

#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const ANSWERS_TSV: &str = include_str!("../../../_data/answers.tsv");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0022_NAMES_TXT: &str = include_str!("../../../_data/p0022_names.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0042_WORDS_TXT: &str = include_str!("../../../_data/p0042_words.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0054_POKER_TXT: &str = include_str!("../../../_data/p0054_poker.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0059_CIPHER_TXT: &str = include_str!("../../../_data/p0059_cipher.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0067_TRIANGLE_TXT: &str = include_str!("../../../_data/p0067_triangle.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0079_KEYLOG_TXT: &str = include_str!("../../../_data/p0079_keylog.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0081_MATRIX_TXT: &str = include_str!("../../../_data/p0081_matrix.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0082_MATRIX_TXT: &str = include_str!("../../../_data/p0082_matrix.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0083_MATRIX_TXT: &str = include_str!("../../../_data/p0083_matrix.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0089_ROMAN_TXT: &str = include_str!("../../../_data/p0089_roman.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0096_SUDOKU_TXT: &str = include_str!("../../../_data/p0096_sudoku.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0098_WORDS_TXT: &str = include_str!("../../../_data/p0098_words.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0099_BASE_EXP_TXT: &str = include_str!("../../../_data/p0099_base_exp.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0102_TRIANGLES_TXT: &str = include_str!("../../../_data/p0102_triangles.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0105_SETS_TXT: &str = include_str!("../../../_data/p0105_sets.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0107_NETWORK_TXT: &str = include_str!("../../../_data/p0107_network.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0424_KAKURO200_TXT: &str = include_str!("../../../_data/p0424_kakuro200.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0673_BEDS_TXT: &str = include_str!("../../../_data/p0673_beds.txt");
#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0673_DESKS_TXT: &str = include_str!("../../../_data/p0673_desks.txt");
macro_rules! file_name_to_constant {
($file_name:expr) => {
concat!("FILE_", $file_name.to_uppercase().replace(".", "_"))
};
}

#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0674_I_EXPRESSIONS_TXT: &str = include_str!("../../../_data/p0674_i_expressions.txt");
macro_rules! define_files {
($($file_name:expr),*) => {
$(
// Create a constant name by transforming the file name to upper case with underscores
const file_name_to_constant!($file_name): &str = include_str!(concat!("../../../_data/", $file_name));
)*
}
}

#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
const P0828_NUMBER_CHALLENGES_TXT: &str = include_str!("../../../_data/p0828_number_challenges.txt");
define_files!(
"answers.tsv",
"p0022_names.txt",
"p0042_words.txt",
"p0054_poker.txt",
"p0059_cipher.txt",
"p0067_triangle.txt",
"p0079_keylog.txt",
"p0081_matrix.txt",
"p0082_matrix.txt",
"p0083_matrix.txt",
"p0089_roman.txt",
"p0096_sudoku.txt",
"p0098_words.txt",
"p0099_base_exp.txt",
"p0102_triangles.txt",
"p0105_sets.txt",
"p0107_network.txt",
"p0424_kakuro200.txt",
"p0673_beds.txt",
"p0673_desks.txt",
"p0674_i_expressions.txt",
"p0828_number_challenges.txt"
);

#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
pub fn get_data_file(name: &str) -> String {
return match name {
"answers.tsv" => ANSWERS_TSV.to_string(),
"p0022_names.txt" => P0022_NAMES_TXT.to_string(),
"p0042_words.txt" => P0042_WORDS_TXT.to_string(),
"p0059_cipher.txt" => P0059_CIPHER_TXT.to_string(),
"p0067_triangle.txt" => P0067_TRIANGLE_TXT.to_string(),
"p0079_keylog.txt" => P0079_KEYLOG_TXT.to_string(),
"p0081_matrix.txt" => P0081_MATRIX_TXT.to_string(),
"p0082_matrix.txt" => P0082_MATRIX_TXT.to_string(),
"p0083_matrix.txt" => P0083_MATRIX_TXT.to_string(),
"p0089_roman.txt" => P0089_ROMAN_TXT.to_string(),
"p0096_sudoku.txt" => P0096_SUDOKU_TXT.to_string(),
"p0098_words.txt" => P0098_WORDS_TXT.to_string(),
"p0099_base_exp.txt" => P0099_BASE_EXP_TXT.to_string(),
"p0102_triangles.txt" => P0102_TRIANGLES_TXT.to_string(),
"p0105_sets.txt" => P0105_SETS_TXT.to_string(),
"p0107_network.txt" => P0107_NETWORK_TXT.to_string(),
"p0424_kakuro200.txt" => P0424_KAKURO200_TXT.to_string(),
"p0673_beds.txt" => P0673_BEDS_TXT.to_string(),
"p0673_desks.txt" => P0673_DESKS_TXT.to_string(),
"p0674_i_expressions.txt" => P0674_I_EXPRESSIONS_TXT.to_string(),
"p0828_number_challenges.txt" => P0828_NUMBER_CHALLENGES_TXT.to_string(),
"answers.tsv" => file_name_to_constant!("answers.tsv").to_string(),
"p0022_names.txt" => file_name_to_constant!("p0022_names.txt").to_string(),
"p0042_words.txt" => file_name_to_constant!("p0042_words.txt").to_string(),
"p0059_cipher.txt" => file_name_to_constant!("p0059_cipher.txt").to_string(),
"p0067_triangle.txt" => file_name_to_constant!("p0067_triangle.txt").to_string(),
"p0079_keylog.txt" => file_name_to_constant!("p0079_keylog.txt").to_string(),
"p0081_matrix.txt" => file_name_to_constant!("p0081_matrix.txt").to_string(),
"p0082_matrix.txt" => file_name_to_constant!("p0082_matrix.txt").to_string(),
"p0083_matrix.txt" => file_name_to_constant!("p0083_matrix.txt").to_string(),
"p0089_roman.txt" => file_name_to_constant!("p0089_roman.txt").to_string(),
"p0096_sudoku.txt" => file_name_to_constant!("p0096_sudoku.txt").to_string(),
"p0098_words.txt" => file_name_to_constant!("p0098_words.txt").to_string(),
"p0099_base_exp.txt" => file_name_to_constant!("p0099_base_exp.txt").to_string(),
"p0102_triangles.txt" => file_name_to_constant!("p0102_triangles.txt").to_string(),
"p0105_sets.txt" => file_name_to_constant!("p0105_sets.txt").to_string(),
"p0107_network.txt" => file_name_to_constant!("p0107_network.txt").to_string(),
"p0424_kakuro200.txt" => file_name_to_constant!("p0424_kakuro200.txt").to_string(),
"p0673_beds.txt" => file_name_to_constant!("p0673_beds.txt").to_string(),
"p0673_desks.txt" => file_name_to_constant!("p0673_desks.txt").to_string(),
"p0674_i_expressions.txt" => file_name_to_constant!("p0674_i_expressions.txt").to_string(),
"p0828_number_challenges.txt" => file_name_to_constant!("p0828_number_challenges.txt").to_string(),
_ => panic!("Unknown file name: {}", name),
}
}
Expand Down
2 changes: 1 addition & 1 deletion rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use js_sys::Array;

use seq_macro::seq;
seq!(N in 0001..=0024 {
pub mod p~N;
pub mod p~N;
});
pub mod p0027;
pub mod p0034;
Expand Down
4 changes: 2 additions & 2 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use include::problems::get_problem;
use include::utils::Answer;
use include::utils::get_answer;
seq!(N in 0001..=0024 {
pub mod p~N;
pub mod p~N;
});
pub mod p0027;
pub mod p0034;
Expand Down Expand Up @@ -75,7 +75,7 @@ fn main() {
seq!(N in 01..=20 {
#[rstest]
#(
#[case::problem_~N(N)]
#[case::problem_~N(N)]
)*
//#[case::problem_21(21)]
#[case::problem_22(22)]
Expand Down

0 comments on commit 122741a

Please sign in to comment.