Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9f05e1f

Browse files
committed
add option to solve problem
1 parent f90c44e commit 9f05e1f

File tree

3 files changed

+80
-28
lines changed

3 files changed

+80
-28
lines changed

‎Cargo.lock

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ serde = "1.0"
1010
serde_json = "1.0"
1111
serde_derive = "1.0"
1212
rand = "0.6.5"
13+
regex = "1.3.4"
1314

1415
[lib]
1516
doctest = false

‎src/main.rs

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ extern crate serde_json;
55

66
mod fetcher;
77

8+
use regex::Regex;
89
use std::env;
910
use std::fs;
11+
use std::fs::File;
1012
use std::io;
11-
use std::io::Write;
13+
use std::io::{BufRead,Write};
1214
use std::path::Path;
1315

1416
/// main() helps to generate the submission template .rs
@@ -18,30 +20,44 @@ fn main() {
1820
loop {
1921
println!("Please enter a frontend problem id, or \"random\" to generate a random one.");
2022
let mut is_random = false;
23+
let mut is_solving = false;
2124
let mut id: u32 = 0;
2225
let mut id_arg = String::new();
2326
io::stdin()
2427
.read_line(&mut id_arg)
2528
.expect("Failed to read line");
2629
let id_arg = id_arg.trim();
27-
match id_arg {
28-
"random" => {
29-
println!("You select random mode.");
30-
id = generate_random_id(&solved_ids);
31-
is_random = true;
32-
println!("Generate random problem: {}", &id);
33-
}
34-
_ => {
35-
id = id_arg
36-
.parse::<u32>()
37-
.unwrap_or_else(|_| panic!("not a number: {}", id_arg));
38-
if solved_ids.contains(&id) {
39-
println!(
40-
"The problem you chose is invalid (the problem may have been initialized \
41-
or may have no rust version)."
42-
);
43-
continue;
44-
}
30+
31+
let random_pattern = Regex::new(r"^random$").unwrap();
32+
let solving_pattern = Regex::new(r"^solve (\d+)$").unwrap();
33+
34+
if random_pattern.is_match(id_arg) {
35+
println!("You select random mode.");
36+
id = generate_random_id(&solved_ids);
37+
is_random = true;
38+
println!("Generate random problem: {}", &id);
39+
} else if solving_pattern.is_match(id_arg) {
40+
// solve a problem
41+
// move it from problem/ to solution/
42+
is_solving = true;
43+
id = solving_pattern
44+
.captures(id_arg)
45+
.unwrap()
46+
.get(1)
47+
.unwrap()
48+
.as_str()
49+
.parse()
50+
.unwrap();
51+
} else {
52+
id = id_arg
53+
.parse::<u32>()
54+
.unwrap_or_else(|_| panic!("not a number: {}", id_arg));
55+
if solved_ids.contains(&id) {
56+
println!(
57+
"The problem you chose is invalid (the problem may have been initialized \
58+
or may have no rust version)."
59+
);
60+
continue;
4561
}
4662
}
4763

@@ -66,6 +82,40 @@ fn main() {
6682
problem.title_slug.replace("-", "_")
6783
);
6884
let file_path = Path::new("./src/problem").join(format!("{}.rs", file_name));
85+
if is_solving {
86+
// check problem/ existence
87+
if !file_path.exists() {
88+
panic!("problem does not exist");
89+
}
90+
// check solution/ no existence
91+
let solution_name = format!(
92+
"s{:04}_{}",
93+
problem.question_id,
94+
problem.title_slug.replace("-", "_")
95+
);
96+
let solution_path = Path::new("./src/solution").join(format!("{}.rs", solution_name));
97+
if solution_path.exists() {
98+
panic!("solution exists");
99+
}
100+
// rename/move file
101+
fs::rename(file_path, solution_path).unwrap();
102+
// remove from problem/mod.rs
103+
let mod_file = "./src/problem/mod.rs";
104+
let target_line = format!("mod {};", file_name);
105+
let lines: Vec<String> = io::BufReader::new(File::open(mod_file).unwrap())
106+
.lines()
107+
.map(|x| x.unwrap())
108+
.filter(|x| *x != target_line)
109+
.collect();
110+
fs::write(mod_file, lines.join("\n"));
111+
// insert into solution/mod.rs
112+
let mut lib_file = fs::OpenOptions::new()
113+
.append(true)
114+
.open("./src/solution/mod.rs")
115+
.unwrap();
116+
writeln!(lib_file, "mod {};", solution_name);
117+
break;
118+
}
69119
if file_path.exists() {
70120
panic!("problem already initialized");
71121
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /