-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_day.sh
executable file
·191 lines (161 loc) · 5.28 KB
/
new_day.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#set -x
set -e
# Retrieve date
real_day=$(date "+%-d")
real_year=$(date "+%Y")
# Get date used based on user parameters and default to current day
day="${1:-$real_day}"
year="${2:-$real_year}"
# Configuration
browser="firefox" # Can be left empty
editor="vim" # Can be left empty
browser=""
editor=""
get_input="1"
overwrite_input="0"
get_puzzle="1"
overwrite_puzzle="1"
extract_answers="1"
get_stats="1"
overwrite_stats="1"
create_rust="1"
overwrite_rust="0"
create_python="1"
overwrite_python="0"
generate_readme="1"
git_add="1"
# URLs
puzzle_url="https://adventofcode.com/${year}/day/${day}"
input_url="https://adventofcode.com/${year}/day/${day}/input"
stats_url="https://adventofcode.com/${year}/leaderboard/self"
# Move to correct directory
cd "$(dirname "$0")"
# Check workspace status
if [ "${git_add}" = "1" ] && [ -n "$(git status --porcelain)" ];
then
git status
git_add="0"
echo "Git workspace is not clean - git commands will be skipped"
fi
# Open pages
if [ -z "${browser}" ]; then
echo "No browser set - ignored."
else
"${browser}" "${puzzle_url}"
"${browser}" "${input_url}"
fi
# Optional: read cookie config file - setting AOC_SESSION_COOKIE env variable
# See template file aoc_cookie_session.sh for instructions
source ~/aoc_cookie_session.sh 2> /dev/null
# Paths
puzzle_file="resources/year${year}_day${day}_puzzle.html"
input_file="resources/year${year}_day${day}_input.txt"
answer_file="resources/year${year}_day${day}_answer.txt"
stats_file="misc/leaderboard_self_${year}.html"
python_script_file="python/${year}/day${day}.py"
rust_src_file_rel="src/${year}/day${day}.rs"
rust_src_file="rust/${rust_src_file_rel}"
cargo_file="rust/Cargo.toml"
readme_file="README.md"
# Get input file
get_url_and_save() {
url="${1}"
dest="${2}"
overwrite="${3}"
cleanup="${4}"
# echo "About to save ${url} in ${dest}"
mkdir -p $(dirname "${dest}")
if [ "${overwrite}" == "0" -a -f "${dest}" ]; then
echo "File ${dest} already exists - ignored."
elif [ -z "${AOC_SESSION_COOKIE}" ]; then
# Create empty file
touch "${dest}"
echo "Empty file ${dest} created"
else
# echo "Cookie session AOC_SESSION_COOKIE used to get file at url ${url}"
# Get file
curl -s "${url}" -H "cookie: session=${AOC_SESSION_COOKIE}" -o "${dest}"
if [ "${cleanup}" == "1" ]; then
# Lines that keep changing (and are not so relevant)
sed -i '/div id="sponsor"/d' "${dest}" # Random sponsor
sed -i '/class="title-global"/d' "${dest}" # Random header, stars count
fi
echo "Url ${url} saved in ${dest}"
fi
}
if [ "${get_puzzle}" = "1" ]; then
get_url_and_save "${puzzle_url}" "${puzzle_file}" "${overwrite_puzzle}" "1"
fi
if [ "${get_stats}" = "1" ]; then
get_url_and_save "${stats_url}" "${stats_file}" "${overwrite_stats}" "1"
fi
if [ "${get_input}" = "1" ]; then
get_url_and_save "${input_url}" "${input_file}" "${overwrite_input}" "0"
fi
if [ "${extract_answers}" = "1" ]; then
sed -n "s/<p>Your puzzle answer was <code>\([^<]*\)<\/code>\..*/\1/gp" "${puzzle_file}" | tee "${answer_file}"
fi
create_code_from_template() {
template="${1}"
dest="${2}"
overwrite="${3}"
if [ "${overwrite}" == "0" -a -f "${dest}" ]; then
echo "Script file ${dest} already exists - ignored."
return
fi
# Create folder
mkdir -p $(dirname "${dest}")
# Create script file based on template
cp "${template}" "${dest}"
# Update file examples used
input_example="resources/yearYYYY_dayDD_input.txt"
answer_example="resources/yearYYYY_dayDD_answer.txt"
sed -i "s#${input_example}#${input_file}#g" "${dest}"
sed -i "s#${answer_example}#${answer_file}#g" "${dest}"
}
open_file_in_editor() {
filename="${1}"
if [ -z "${editor}" ]; then
echo "No editor set to open ${filename} - ignored."
else
"${editor}" "${filename}"
fi
}
# Count lines in input files to use the most relevant template
nb_lines="$(cat "${input_file}" | wc -l)"
# Create Python file
if [ "${create_python}" = "1" ]; then
case "${nb_lines}" in
0|1) python_template="python/template_one_line.py";;
*) python_template="python/template_multi_line.py";;
esac
create_code_from_template "${python_template}" "${python_script_file}" "${overwrite_python}"
open_file_in_editor "${python_script_file}"
fi
# Create Rust file
if [ "${create_rust}" = "1" ]; then
rust_bin="${year}_day${day}"
case "${nb_lines}" in
0|1) rust_template="rust/src/templates/template_one_line.rs";;
*) rust_template="rust/src/templates/template_multi_line.rs";;
esac
# Add content in Cargo.toml file
cargo_content="[[bin]]\nname = \"${rust_bin}\"\npath = \"${rust_src_file_rel}\"\n\n"
# echo -e "Add the following content to ${cargo_file}:\n${cargo_content}"
grep -q "${rust_bin}" "${cargo_file}" || sed -i "s#.*See more keys#${cargo_content}&#g" "${cargo_file}"
create_code_from_template "${rust_template}" "${rust_src_file}" "${overwrite_rust}"
# Instruction to run cargo
# echo """(cd rust && cargo run --bin "${rust_bin}")"""
open_file_in_editor "${rust_src_file}"
fi
# Generate README
if [ "${generate_readme}" = "1" ]; then
python3 generate_readme.py "${readme_file}"
fi
# Add to git
if [ "${git_add}" = "1" ]; then
git status
git add "${puzzle_file}" "${input_file}" "${answer_file}" "${stats_file}" "${python_script_file}" "${rust_src_file}" "${cargo_file}" "${readme_file}"
git status
echo "Commit with: 'git commit -m \"Year ${year} - Day ${day} - Getting started\"'"
fi