-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen_new_probl.sh
executable file
·178 lines (157 loc) · 6.22 KB
/
gen_new_probl.sh
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
#!/usr/bin/env zsh
zparseopts -D -E \
l:=lang -language:=lang \
n:=num -number:=num \
a:=author -author=author \
d=dep -dependencies=dep \
f=force -force=force \
g=git -git=git \
h=help -help=help
print_help() {
echo "Usage: ./gen_new_probl.sh -l [LANGUAGE] -n [NUMBER] \n"
echo "Mandatory arguments:"
printf "%-20s %s \n" "-l --language" "Programming language to use: Currently supported are 'cpp' for C++, 'c' for C and 'py' for Python"
printf "%-20s %s \n" "-n --number" "Number of the problem you want to solve"
echo "\nOptional arguments:"
printf "%-20s %s \n" "-a --author" "Author name, default is the output of 'git config user.name'"
printf "%-20s %s \n" "-d --dependencies" "Try to extract and download the input file(s) from the problem statement"
printf "%-20s %s \n" "-f --force" "Always override if file already exists without prompting"
printf "%-20s %s \n" "-g --git" "'git add' the created files"
printf "%-20s %s \n" "-h --help" "Print this message and exit"
}
if [[ -z $lang || -z $num ]]; then
echo "Missing mandatory arguments!\n"
print_help
exit 0
fi
[[ -n $help ]] && {print_help; exit 0}
# Add leading 0s to the problem number
num=$(printf "%03d" $num[2])
# Create the problem source file
is_go=false
is_js=false
is_cpp=false
case $lang[2] in
"cpp" )
is_cpp=true
template="cpp_src/template.cpp"
outfile="cpp_src/problem${num}.cpp"
headerfile="cpp_src/problem${num}.hpp"
;;
"c" )
template="c_src/template.c"
outfile="c_src/problem${num}.c"
;;
"py" )
template="py_src/template.py"
outfile="py_src/problem${num}.py"
;;
"go" )
is_go=true
template1="go_src/templates/template.go"
template2="go_src/templates/template_test.go"
template3="go_src/templates/template_main.go"
outfile1="go_src/solver${num}.go"
outfile2="go_src/solver${num}_test.go"
outfile3="go_src/main/problem${num}.go"
;;
"js" )
is_js=true
template1="js_src/template_problem.js"
template2="js_src/template_solver.js"
outfile1="js_src/problem${num}.js"
outfile2="js_src/solver${num}.js"
;;
esac
# if target language is Go, copy multiple files
if $is_go; then
if [[ -z $force && (-e $outfile1 || -e $outfile2 || -e $outfile3) ]]; then
read -q "REPLY?One of the output files already exists! Overwrite? (y/n)"
echo ""
[[ $REPLY = "n" ]] && exit 1
fi
cp $template1 $outfile1
cp $template2 $outfile2
cp $template3 $outfile3
[[ -n $git ]] && git add $outfile1 $outfile2 $outfile3
elif $is_js; then
if [[ -z $force && (-e $outfile1 || -e $outfile2) ]]; then
read -q "REPLY?One of the output files already exists! Overwrite? (y/n)"
echo ""
[[ $REPLY = "n" ]] && exit 1
fi
cp $template1 $outfile1
cp $template2 $outfile2
[[ -n $git ]] && git add $outfile1 $outfile2
elif $is_cpp; then
if [[ -z $force && (-e $outfile || -e $headerfile) ]]; then
read -q "REPLY?One of the output files already exists! Overwrite? (y/n)"
echo ""
[[ $REPLY = "n" ]] && exit 1
fi
cp $template $outfile
echo "#pragma once" > $headerfile
[[ -n $git ]] && git add $outfile $headerfile
else
if [[ -z $force && -e $outfile ]]; then
read -q "REPLY?File $outfile already exists! Overwrite? (y/n)"
echo ""
[[ $REPLY = "n" ]] && exit 1
fi
cp $template $outfile
[[ -n $git ]] && git add $outfile
fi
# Add problem number, author and date (not for Go files)
if $is_go; then
echo "Successfully created source files $outfile1, $outfile2 and $outfile3!"
echo "Please add author name, date and solution number manually."
elif $is_js; then
echo "Successfully created source files $outfile1 and $outfile2!"
echo "Please add author name, date and solution number manually."
else
# add problem number
sed -i "s/Problem X/Problem $num/g" $outfile
# add author information
[[ -z $author ]] && author=$(git config user.name) || author=$author[2]
sed -i "s/<author_name>/$author/g" $outfile
# add date
sed -i "s/Date: .*$/$(date +'%Y\/%m\/%d')/g" $outfile
# remove template comment
sed -i "/demonstrates/d" $outfile
# try to get problem text from `projecteuler.net'
url="https://projecteuler.net/problem={$num}"
statement=$(curl -s $url | tr -d '\n' | grep -o -P '(?<=problem_content" role="problem"\>).*(?=\<\/p\>\<\/div\>\<br)' | sed -e 's/<p>//g' | sed -e 's/<\/p>//g')
# TODO (bug fix): Formulas have multiple inline html tags (spans, etc.)
# that need to be removed to make this work for some problems!
if [[ -n $dep ]]; then
# Extract href tags in the statement
hrefs=$(echo $statement | grep -Po '<a href="[^"]*"[^>]*>[^>]*>')
# Iterate over the matches
while read -r href; do
# Extract link to the file and its name
link=$(echo $href | grep -oP '(?<=").*(?=")')
name=$(echo $href | grep -oP '(?<=>).*(?=<)')
# Check if this is actually a link to a input file, assuming
# that those are always stored at projecteuler.net/project/resources/
if [[ $(echo $link | grep -oP '^[^/]*/[^/]*') = "project/resources" ]]; then
link="https://projecteuler.net/${link}"
curl -s $link > input_files/$name
echo "Downloaded input file input_files/${name}!"
fi
done <<< "$hrefs"
fi
# Now remove hyperlinks to input files
statement=$(echo $statement | sed 's/<a href=".*">//g' | sed 's/<\/a>//g')
# Parse subscripts and superscripts
statement=$(echo $statement | sed 's/<sub>/_{/g; s/<\/sub>/}/g')
statement=$(echo $statement | sed 's/<sup>/^{/g; s/<\/sup>/}/g')
# replace template comment with problem statement
statement=$(echo "Problem statement: $statement")
# use @ instead of / here, otherwise sed will be confused if the problem statement contains /
sed -i "s@Problem statement:@$statement@g" $outfile
if $is_cpp; then
sed -i "s/header.hpp/problem${num}.hpp/g" $outfile
fi
echo "Successfully created source file $outfile!"
fi