utils.rs
View source code here on GitHub!
- utils::Answer
This enum can represent either a large, signed integer or a string.
- pub fn utils::Answer::Int(n: i128) -> utils::Answer
- pub fn utils::Answer::String(s: String) -> utils::Answer
- pub fn utils::read_data_file(name: &str) -> String
Reads a file from
/_data
and returns its contents as a String.
- pub fn utils::read_answer(n: usize) -> Answer
Reads the csv file of answers in
/_data
and returns a specified answer.
1use std::string::ToString;
2
3#[cfg(not(any(target_arch="wasm32", target_arch="wasm64")))]
4use std::fs::read_to_string;
5#[cfg(not(any(target_arch="wasm32", target_arch="wasm64")))]
6use std::path::Path;
7
8#[derive(Debug, PartialEq, Eq)]
9pub enum Answer {
10 String(String),
11 Int(i128),
12}
13
14#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
15const ANSWERS_TSV: &str = include_str!("../../../_data/answers.tsv");
16#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
17const P0022_NAMES_TXT: &str = include_str!("../../../_data/p0022_names.txt");
18#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
19const P0042_WORDS_TXT: &str = include_str!("../../../_data/p0042_words.txt");
20#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
21const P0054_POKER_TXT: &str = include_str!("../../../_data/p0054_poker.txt");
22#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
23const P0059_CIPHER_TXT: &str = include_str!("../../../_data/p0059_cipher.txt");
24#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
25const P0067_TRIANGLE_TXT: &str = include_str!("../../../_data/p0067_triangle.txt");
26#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
27const P0079_KEYLOG_TXT: &str = include_str!("../../../_data/p0079_keylog.txt");
28#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
29const P0081_MATRIX_TXT: &str = include_str!("../../../_data/p0081_matrix.txt");
30#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
31const P0082_MATRIX_TXT: &str = include_str!("../../../_data/p0082_matrix.txt");
32#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
33const P0083_MATRIX_TXT: &str = include_str!("../../../_data/p0083_matrix.txt");
34#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
35const P0089_ROMAN_TXT: &str = include_str!("../../../_data/p0089_roman.txt");
36#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
37const P0096_SUDOKU_TXT: &str = include_str!("../../../_data/p0096_sudoku.txt");
38#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
39const P0098_WORDS_TXT: &str = include_str!("../../../_data/p0098_words.txt");
40#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
41const P0099_BASE_EXP_TXT: &str = include_str!("../../../_data/p0099_base_exp.txt");
42#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
43const P0102_TRIANGLES_TXT: &str = include_str!("../../../_data/p0102_triangles.txt");
44#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
45const P0105_SETS_TXT: &str = include_str!("../../../_data/p0105_sets.txt");
46#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
47const P0107_NETWORK_TXT: &str = include_str!("../../../_data/p0107_network.txt");
48#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
49const P0424_KAKURO200_TXT: &str = include_str!("../../../_data/p0424_kakuro200.txt");
50#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
51const P0673_BEDS_TXT: &str = include_str!("../../../_data/p0673_beds.txt");
52#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
53const P0673_DESKS_TXT: &str = include_str!("../../../_data/p0673_desks.txt");
54#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
55const P0674_I_EXPRESSIONS_TXT: &str = include_str!("../../../_data/p0674_i_expressions.txt");
56#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
57const P0828_NUMBER_CHALLENGES_TXT: &str = include_str!("../../../_data/p0828_number_challenges.txt");
58
59#[cfg(any(target_arch="wasm32", target_arch="wasm64"))]
60pub fn get_data_file(name: &str) -> String {
61 return match name {
62 "answers.tsv" => ANSWERS_TSV.to_string(),
63 "p0022_names.txt" => P0022_NAMES_TXT.to_string(),
64 "p0042_words.txt" => P0042_WORDS_TXT.to_string(),
65 "p0054_poker.txt" => P0054_POKER_TXT.to_string(),
66 "p0059_cipher.txt" => P0059_CIPHER_TXT.to_string(),
67 "p0067_triangle.txt" => P0067_TRIANGLE_TXT.to_string(),
68 "p0079_keylog.txt" => P0079_KEYLOG_TXT.to_string(),
69 "p0081_matrix.txt" => P0081_MATRIX_TXT.to_string(),
70 "p0082_matrix.txt" => P0082_MATRIX_TXT.to_string(),
71 "p0083_matrix.txt" => P0083_MATRIX_TXT.to_string(),
72 "p0089_roman.txt" => P0089_ROMAN_TXT.to_string(),
73 "p0096_sudoku.txt" => P0096_SUDOKU_TXT.to_string(),
74 "p0098_words.txt" => P0098_WORDS_TXT.to_string(),
75 "p0099_base_exp.txt" => P0099_BASE_EXP_TXT.to_string(),
76 "p0102_triangles.txt" => P0102_TRIANGLES_TXT.to_string(),
77 "p0105_sets.txt" => P0105_SETS_TXT.to_string(),
78 "p0107_network.txt" => P0107_NETWORK_TXT.to_string(),
79 "p0424_kakuro200.txt" => P0424_KAKURO200_TXT.to_string(),
80 "p0673_beds.txt" => P0673_BEDS_TXT.to_string(),
81 "p0673_desks.txt" => P0673_DESKS_TXT.to_string(),
82 "p0674_i_expressions.txt" => P0674_I_EXPRESSIONS_TXT.to_string(),
83 "p0828_number_challenges.txt" => P0828_NUMBER_CHALLENGES_TXT.to_string(),
84 _ => panic!("Unknown file name: {}", name),
85 }
86}
87
88#[cfg(not(any(target_arch="wasm32", target_arch="wasm64")))]
89pub fn get_data_file(name: &str) -> String {
90 let data_file = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().join("_data").join(name);
91 return read_to_string(&data_file).unwrap();
92}
93
94pub fn get_answer(n: usize) -> Answer {
95 let csv = get_data_file("answers.tsv");
96 let mut lines = csv.lines();
97 lines.next();
98 for line in lines {
99 let parts: Vec<_> = line.split('\t').collect();
100 if parts.len() != 4 {
101 panic!("Wrong number of parts: {}", parts.len());
102 }
103 let (id, type_, answer) = (parts[0], parts[1], parts[3]);
104 if n == id.parse::<usize>().unwrap() {
105 return match type_ {
106 "str" => Answer::String(answer.to_string()),
107 _ => Answer::Int(answer.parse::<i128>().unwrap()),
108 }
109 }
110 }
111 panic!("Answer not found");
112}
113
114pub fn is_palindrome<I>(x: I) -> bool where I: ToString {
115 let s = x.to_string();
116 return s == s.chars().rev().collect::<String>();
117}