-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc.sh
executable file
·69 lines (58 loc) · 1.67 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
#!/bin/bash
# Dec 14, 2017
#
# Get the puzzle instructions & input from the Advent of Code website
#
# Usage:
# aoc.sh <day>
#
set -euo pipefail
# --- Configuration ---
# - Web Scraping -
SESS='<your "session" cookie goes here (its a hex-string)>'
YEAR="2017"
URL="https://adventofcode.com/${YEAR}/day"
# - Output -
PUZZLE_DIR="resource"
PUZZLE_FILE="input"
README_DIR="src/aoc_2017"
README_FILE="README.md"
require() {
command -v "$1" &>/dev/null || { echo "Missing program: $1" && exit 1; }
}
require http # HTTPie - https://httpie.org/
require pup # Pup - https://github.com/ericchiang/pup
require html2text # html2text - https://github.com/aaronsw/html2text
get_day() {
local day=$1
http --body --check-status $URL/"$day" "Cookie:session=$SESS" \
| pup '.day-desc' \
| html2text
}
get_puzzle() {
local day=$1
http --body --check-status $URL/"$day"/input "Cookie:session=$SESS"
}
main() {
local day=$1
local long_day=$(printf "%02d" "$day")
local README_PATH="$README_DIR/$long_day/$README_FILE"
local PUZZLE_PATH="$PUZZLE_DIR/$long_day/$PUZZLE_FILE"
mkdir -p "$(dirname "$README_PATH")" "$(dirname "$PUZZLE_PATH")"
if [[ -f $README_PATH ]]; then
echo "Updating day $long_day \"$README_FILE\" file"
else
echo "Fetching day $long_day instructions"
fi
echo "-> $README_PATH"
get_day "$day" > "$README_PATH"
# get input if we don't already have it
if [[ ! -f $PUZZLE_PATH ]]; then
echo "Fetching puzzle input for day $long_day"
echo "-> $PUZZLE_PATH"
get_puzzle "$day" > "$PUZZLE_PATH"
else
echo "Puzzle input already acquired"
fi
}
main "$@"