-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·229 lines (206 loc) · 6.44 KB
/
Copy pathinit.sh
File metadata and controls
executable file
·229 lines (206 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/bin/bash
url=$1
if [ -z "$url" ]; then
echo "사용법: ./init.sh <문제_URL>"
exit 1
fi
# 문자열을 snake_case로 변환하고 패키지 이름으로 적합하게 만드는 함수
to_snake_case() {
# 숫자로 시작하는 경우 'p' 접두사 추가
local input=$1
if [[ $input =~ ^[0-9] ]]; then
input="p$input"
fi
# 특수문자를 제거하고 snake_case로 변환
echo "$input" | sed 's/[^a-zA-Z0-9-]//g' |
sed 's/[A-Z]/\L&/g' |
sed 's/-/_/g'
}
# URL에서 사이트 타입 확인
if [[ $url == *"leetcode.com"* ]]; then
# URL에서 problems/ 이후의 문제 이름만 추출
raw_name=$(echo $url | sed -E 's/.*problems\/([^/?]+).*/\1/')
problem_name=$(to_snake_case "$raw_name")
# 기본 디렉토리 경로
file_name="l_${problem_name}"
# 디렉토리 생성
mkdir -p "src/bin"
# Rust 실행 파일 생성
cat >"src/bin/${file_name}.rs" <<EOF
// LeetCode - ${raw_name}
// ${url}
fn main() {
println!("문제: ${raw_name}");
// 여기에 코드 작성
let solution = Solution::new();
println!("완료!");
}
struct Solution;
impl Solution {
fn new() -> Self {
Solution
}
// 문제 풀이 메서드 추가
}
EOF
elif [[ $url == *"acmicpc.net"* ]]; then
raw_name=$(echo $url | sed -E 's/.*\/problem\/([0-9]+).*/\1/')
# 기본 디렉토리 경로
file_name="b_${raw_name}"
# 디렉토리 생성
mkdir -p "src/bin"
# Rust 실행 파일 생성 (백준용 템플릿)
cat >"src/bin/${file_name}.rs" <<EOF
// Baekjoon - ${raw_name}
// ${url}
#[allow(clippy::all)]
#[allow(unused_must_use, unused_doc_comments)]
fn solve<R: BufRead, W: Write>(io: &mut IO<R, W>) -> Option<()> {
// let n: usize = io.get(0usize)?;
// let s: String = io.get(String::new())?;
// let line: String = io.get_line()?;
// let grid = io.get(vec![B; r])?; // 바이트 배열로 격자 읽기
// 여기에 문제 풀이 코드 작성
// io.put("결과").nl();
None
}
/// IO template - from bubbler (modified)
// boj - https://www.acmicpc.net/user/bubbler
#[allow(dead_code)]
mod io {
pub(crate) use std::io::{Write, stdin, stdout, BufWriter, BufRead};
pub(crate) struct IO<R: BufRead, W: Write> {
ii: I<R>,
oo: BufWriter<W>,
}
impl<R: BufRead, W: Write> IO<R, W> {
pub(crate) fn new(r: R, w: W) -> Self {
Self {
ii: I::new(r),
oo: BufWriter::new(w),
}
}
pub(crate) fn get<T: Fill>(&mut self, exemplar: T) -> Option<T> {
self.ii.get(exemplar)
}
pub(crate) fn put<T: Print>(&mut self, t: T) -> &mut Self {
t.print(&mut self.oo);
self
}
pub(crate) fn nl(&mut self) -> &mut Self {
self.put("\n")
}
}
pub(crate) trait Print {
fn print<W: Write>(&self, w: &mut W);
}
macro_rules! print_disp {
(\$(\$t:ty),+) => {
\$(impl Print for \$t { fn print < W : Write > (& self, w : & mut W) {
write!(w, "{}", self) .unwrap(); } })+
};
}
print_disp!(usize, i64, String, & str, char);
pub(crate) struct I<R: BufRead> {
r: R,
line: String,
rem: &'static str,
}
impl<R: BufRead> I<R> {
pub(crate) fn new(r: R) -> Self {
Self {
r,
line: String::new(),
rem: "",
}
}
pub(crate) fn next_line(&mut self) -> Option<()> {
self.line.clear();
(self.r.read_line(&mut self.line).unwrap() > 0)
.then(|| {
self
.rem = unsafe {
(&self.line[..] as *const str).as_ref().unwrap()
};
})
}
pub(crate) fn get<T: Fill>(&mut self, exemplar: T) -> Option<T> {
let mut exemplar = exemplar;
exemplar.fill_from_input(self)?;
Some(exemplar)
}
}
pub(crate) trait Fill {
fn fill_from_input<R: BufRead>(&mut self, i: &mut I<R>) -> Option<()>;
}
fn ws(c: char) -> bool {
c <= ' '
}
macro_rules! fill_num {
(\$(\$t:ty),+) => {
\$(impl Fill for \$t { fn fill_from_input < R : BufRead > (& mut self, i : &
mut I < R >) -> Option < () > { i.rem = i.rem.trim_start_matches(ws); while i
.rem.is_empty() { i.next_line() ?; i.rem = i.rem.trim_start_matches(ws); }
let tok = i.rem.split(ws).next().unwrap(); i.rem = & i.rem[tok.len()..]; *
self = tok.parse().ok() ?; Some(()) } })+
};
}
fill_num!(usize, i64, f64);
impl Fill for String {
fn fill_from_input<R: BufRead>(&mut self, i: &mut I<R>) -> Option<()> {
i.rem = i.rem.trim_start_matches(ws);
while i.rem.is_empty() {
i.next_line()?;
i.rem = i.rem.trim_start_matches(ws);
}
let tok = i.rem.split(ws).next().unwrap();
i.rem = &i.rem[tok.len()..];
*self = tok.to_string();
Some(())
}
}
impl Fill for Vec<u8> {
fn fill_from_input<R: BufRead>(&mut self, i: &mut I<R>) -> Option<()> {
i.rem = i.rem.trim_start_matches(ws);
while i.rem.is_empty() {
i.next_line()?;
i.rem = i.rem.trim_start_matches(ws);
}
let tok = i.rem.split(ws).next().unwrap();
i.rem = &i.rem[tok.len()..];
self.extend_from_slice(tok.as_bytes());
Some(())
}
}
pub(crate) const B: Vec<u8> = Vec::new();
impl<T: Fill> Fill for Vec<T> {
fn fill_from_input<R: BufRead>(&mut self, i: &mut I<R>) -> Option<()> {
for ii in self.iter_mut() {
ii.fill_from_input(i)?;
}
Some(())
}
}
impl<T: Fill, const N: usize> Fill for [T; N] {
fn fill_from_input<R: BufRead>(&mut self, i: &mut I<R>) -> Option<()> {
for ii in self.iter_mut() {
ii.fill_from_input(i)?;
}
Some(())
}
}
}
use io::*;
pub fn main() {
let stdin = stdin().lock();
let stdout = stdout().lock();
let mut io = IO::new(stdin, stdout);
solve(&mut io);
}
EOF
else
echo "지원하지 않는 사이트입니다. (지원: leetcode, acmicpc.net)"
exit 1
fi
echo "문제 생성 완료: src/bin/${file_name}.rs"
echo "실행 방법: cargo run --bin ${file_name}"