-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew
More file actions
executable file
·45 lines (39 loc) · 1.56 KB
/
new
File metadata and controls
executable file
·45 lines (39 loc) · 1.56 KB
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
#!/bin/bash
# Compute some constants
YEAR=${PWD##*/}
[[ $YEAR =~ 20[[:digit:]][[:digit:]] ]] || { echo "Run from year directory." >&2; exit 2; }
DAY=${1}
[[ $DAY =~ [[:digit:]]?[[:digit:]] && 0 -lt "$DAY" && "$DAY" -lt 32 ]] || {
echo "You have a bad day: $DAY" >&2; exit 2; }
SCRIPTDIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" && pwd )
PYFILE="./${DAY}/${DAY}.py"
URL="https://adventofcode.com/${YEAR}/day/${DAY}"
# Create day directory and python file
mkdir -p "./${DAY}" || { echo "Could not make directory: ./${DAY}" >&2; exit 3; }
cp -i --no-preserve=mode,timestamps "${SCRIPTDIR}/template.py" "$PYFILE" && \
chmod 755 "$PYFILE"
# Fill values into python file docstring
tempfile=$(mktemp) && \
curl --silent --fail \
--user-agent 'https://github.com/jacktose/advent-of-code by jacktose' \
"${URL}" \
> "$tempfile" && \
title="$(sed -n '/^<article class="day-desc"><h2>--- \(Day [12]\?[0-9]: .\+\) ---<\/h2>.*/{s//\1/p;q}' "$tempfile")" && \
sed -i '/^"""/{n;s~^https:\/\/adventofcode.com\/[0-9]\{4\}\/day\/_~'"$URL"'~;n;s/^$/'"$title"'/; :loop n; b loop}' "$PYFILE" || \
echo "Could not edit $pyfile" >&2
rm -f "$tempfile"
# Get input file
COOKIE=$(<"${SCRIPTDIR}/.session_cookie") && \
tempfile=$(mktemp) && \
curl --silent --fail \
--cookie "$COOKIE" \
--user-agent 'https://github.com/jacktose/advent-of-code by jacktose' \
"${URL}/input" \
> "$tempfile" && \
mv "$tempfile" "./${DAY}/input.txt" && \
chmod 444 "./${DAY}/input.txt" || \
echo "Could not download input" >&2
rm -f "$tempfile"
# Create example file
touch "./${DAY}/example.txt"
exit 0