-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc.sh
executable file
·72 lines (58 loc) · 1.24 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
#!/bin/bash
set -e
# run with
# cp -r $(date +'%Y')/template $(date +'%Y')/$(date +'%-d') \
# && ./aoc.sh input \
# && cd $(date +'%Y')/$(date +'%-d')
cli_help() {
cli_name=${0##*/}
echo "
Advent of Code CLI
Usage: $cli_name [command]
Commands:
input Get input
submit Submit puzzle
* Help
"
exit 1
}
get_input() {
echo "Get input of '$DAY.12.$YEAR'"
mkdir -p "./$YEAR/$DAY"
curl --silent --cookie "session=$AOC_SESSION" \
"$URL/$YEAR/day/$DAY/input" |
sed -z '$ s/\n$//' >"./$YEAR/$DAY/input.txt"
echo "Saved input in './$YEAR/$DAY/input.txt'"
}
do_submission() {
PART=$1
ANSWER=$2
if [ $PART = "a" ]; then
LEVEL=1
elif [ $PART = "b" ]; then
LEVEL=2
else
echo "Invalid part: $PART"
exit 1
fi
echo "Submit answer '$ANSWER' for part '$PART' of '$DAY.12.$YEAR'"
curl --cookie "session=$AOC_SESSION" \
-X POST \
-d "{\"level\": $LEVEL, \"answer\": $ANSWER}" \
"$URL/$YEAR/day/$DAY/answer"
}
URL="https://adventofcode.com"
DAY=$(date +'%-d')
YEAR=$(date +'%Y')
AOC_SESSION=$(cat ./token)
case "$1" in
input | i)
get_input
;;
submit | s)
do_submission $2 $3
;;
*)
cli_help
;;
esac