-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc.sh
executable file
·300 lines (258 loc) · 7.11 KB
/
aoc.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
TEXT_AOC="$(tput rev)$(tput setaf 206)"
TEXT_INFO="$(tput bold)$(tput setaf 15)"
TEXT_INFO_SECONDARY="$(tput setaf 255)"
TEXT_ERROR="$(tput rev)$(tput setaf 196)"
TEXT_DEFAULT="$(tput sgr0)"
# --- begin config ---
YEAR=2022
EDITOR="code"
INPUT_FILE="in.txt"
TEST_INPUT_FILE="test.txt"
DAYS_PATH="./days"
API_URL="https://adventofcode.com"
# --- end config ---
# --- begin templates ---
RUST_TEMPLATE="""type Input = i32;
fn main() {
let input = parse_input(&std::include_str!("$INPUT_FILE"));
tests();
println!("{}", part_1(&input));
println!("{}", part_2(&input));
}
fn tests() {
let input = parse_input(&std::include_str!("$TEST_INPUT_FILE"));
{
let expected = 0;
let actual = part_1(&parse_input(&input));
assert_eq!(expected, actual);
}
{
let expected = 0;
let actual = part_2(&parse_input(&input));
assert_eq!(expected, actual);
}
}
fn parse_input(input: &str) -> Input {
input
.split("\n\n")
.collect::<Vec<i32>>()
}"""
JS_TEMPLATE="""const fs = require('fs');
const parse = (source) => source.split('\\\n').filter(Boolean);
const data = parse(fs.readFileSync('$INPUT_FILE', 'utf-8'));
const test = parse(fs.readFileSync('$TEST_INPUT_FILE', 'utf-8'));
console.log(test);"""
# --- end templates ---
# $1 Formatter
# $2 Message
print() {
echo "${TEXT_AOC}[aoc]${TEXT_DEFAULT} $1$2${TEXT_DEFAULT}"
}
# Returns pseudo-randomly generated christmass emoji. Just because.
get_xmas_emoji() {
EMOJIS=("👼" "🎅" "🤶" "🧑🎄" "🧝" "🧝♂️" "🧝♀️" "🦌" "🍪" "🥛" "🌟" "❄️" "⛄" "🎄" "🎁" "🔔" "🎶" "🕯️")
EMOJIS_LEN=${#EMOJIS[@]}
MAX_INDEX=$((EMOJIS_LEN - 1))
RAND_INDEX="$(shuf -i 0-$MAX_INDEX -n 1)"
echo "${EMOJIS[$RAND_INDEX]}"
}
# $1 Yes message
# $2 No message
# Returns "yes" if ($1) selected, "no" otherwise.
ask_yes_no() {
select answer in "$1", "$2"; do
case $answer in
"$1,")
echo "yes"
break
;;
"$2")
echo "no"
break
;;
esac
done
}
# $1 - aoc day number
get_day_name() {
FILE_NAME="$(printf "day-%02d" $1)"
echo $FILE_NAME
}
# $1 - aoc day number
get_day_path() {
DAY_PATH="$DAYS_PATH/$(get_day_name $1)"
echo $DAY_PATH
}
get_file_name() {
FILE_NAME="main"
echo $FILE_NAME
}
# $1 - aoc day number
get_file() {
FILE="$(get_file_name).rs"
echo $FILE
}
# $1 - aoc day number
get_js_file() {
FILE="$(get_file_name).js"
echo $FILE
}
# $1 - aoc day number
get_file_path() {
FILE_PATH="$(get_day_path $1)/$(get_file $1)"
echo $FILE_PATH
}
# $1 - aoc day number
get_js_file_path() {
FILE_PATH="$(get_day_path $1)/$(get_js_file $1)"
echo $FILE_PATH
}
# $1 - aoc day number
get_input_path() {
INPUT_PATH="$(get_day_path $1)/$INPUT_FILE"
echo $INPUT_PATH
}
# $1 - aoc day number
get_test_path() {
TEST_INPUT_PATH="$(get_day_path $1)/$TEST_INPUT_FILE"
echo $TEST_INPUT_PATH
}
# Reads ENV variable AOC_SESSION_COOKIE
get_session_cookie() {
echo "$AOC_SESSION_COOKIE"
}
help() {
echo
print $TEXT_INFO "Usage: [-r|--run <arg>] [-a|--add <arg>] [-c|--code <arg>] [-s|--stage <arg>] [-l|--list] [-h|--help]"
OPTIONS=(
"-r | --run <DAY_NUMBER> Executes given AoC day."
"-a | --add <DAY_NUMBER> Adds a new AoC day."
"-c | --code <DAY_NUMBER> Opens given AoC day in selected code editor (current: $EDITOR)."
"-s | --stage <DAY_NUMBER> Git commits given AoC day adding some christmass touch $(get_xmas_emoji)."
"-l | --list Lists all created AoC days."
"-h | --help Prints help page."
)
print $TEXT_INFO_SECONDARY "Options:"
for ((i = 0; i < ${#OPTIONS[@]}; i++)); do
print $TEXT_INFO_SECONDARY "${OPTIONS[$i]}"
done
echo
}
# $1 - aoc day number
ask_to_override() {
DAY_NAME="$(get_day_name $1)"
YES_MSG="Yes, override already existing day $DAY_NAME"
NO_MSG="No, abort"
case "$(ask_yes_no "$YES_MSG" "$NO_MSG")" in
"yes")
print $TEXT_INFO "Overriding day $DAY_NAME..."
;;
"no")
print $TEXT_INFO "Exiting..."
exit 0
;;
esac
}
# $1 - aoc day number
create_input_data() {
ENDPOINT="$API_URL/$YEAR/day/$1/input"
SESSION_COOKIE="$(get_session_cookie)"
INPUT_PATH=$(get_input_path $1)
TEST_INPUT_PATH=$(get_test_path $1)
echo "$(curl -s -H "Accept: application/json" --cookie "session=$(get_session_cookie)" $ENDPOINT)" >$INPUT_PATH
touch $TEST_INPUT_PATH
}
# $1 - aoc day number
create_day() {
DAY_NAME="$(get_day_name $1)"
FILE_PATH="$(get_file_path $1)"
JS_FILE_PATH="$(get_js_file_path $1)"
DAY_PATH="$(get_day_path $1)"
if [ -f $FILE_PATH ]; then
print $TEXT_ERROR "Day $DAY_NAME exists at $FILE_PATH. Override?"
ask_to_override $1
else
print $TEXT_INFO "Creating a new day $DAY_NAME at $FILE_PATH"
mkdir $DAY_PATH
fi
echo "$RUST_TEMPLATE" >$FILE_PATH
echo "$JS_TEMPLATE" >$JS_FILE_PATH
create_input_data $1
}
# $1 - aoc day number
run_day_in_dedicated_env() {
FILE_NAME="$(get_file_name $1)"
FILE_PATH="$(get_file_path $1)"
DAY_PATH="$(get_day_path $1)"
CARGO_MANIFEST_PATH="$DAY_PATH/Cargo.toml"
if [ -f $CARGO_MANIFEST_PATH ]; then
print $TEXT_INFO_SECONDARY 'A Cargo manifest file found at "$CARGO_MANIFEST_PATH" - running with cargo...' && cargo run --manifest-path $CARGO_MANIFEST_PATH
else
print $TEXT_INFO_SECONDARY 'No cargo manifest file found at "$CARGO_MANIFEST_PATH" - running with rustc' && rustc $FILE_PATH && echo $(./$FILE_NAME) && rm $FILE_NAME
fi
}
# $1 - aoc day number
run_day() {
print $TEXT_INFO "Running day $(get_day_name $1) at $FILE_PATH$"
run_day_in_dedicated_env $1
}
list_days() {
print $TEXT_INFO "Listing all created days:"
for f in "$DAYS_PATH/*"; do
print $TEXT_INFO_SECONDARY $f
done
}
# $1 - aoc day number
code_day() {
DAY_NAME="$(get_day_name $1)"
DAY_PATH="$(get_day_path $1)"
print $TEXT_INFO "Opening day $DAY_NAME in $EDITOR"
echo "$($EDITOR $DAY_PATH)"
}
# $1 - aoc day number
stage_day() {
DAY_NAME="$(get_day_name $1)"
DAY_PATH="$(get_day_path $1)"
print $TEXT_INFO "Commiting AoC day $DAY_NAME at $DAY_PATH"
git add "$DAY_PATH" && git commit -m "Add day $1 of year $YEAR $(get_xmas_emoji)"
}
# --- begin options parsing ---
VALID_ARGS=$(getopt -a -n $0 -o hla:c:s:r: --long help,list,add:,code:,stage:,run: -- "$@")
if [[ $? -ne 0 ]]; then
help
exit 1
fi
eval set -- "$VALID_ARGS"
while [ : ]; do
case "$1" in
-h | --help)
help
exit
;;
-l | --list)
list_days
exit
;;
-a | --add)
create_day $2
shift 2
;;
-c | --code)
code_day $2
shift 2
;;
-s | --stage)
stage_day $2
shift 2
;;
-r | --run)
run_day $2
shift 2
;;
--)
shift
break
;;
esac
done
# --- end options parsing ---