-
Notifications
You must be signed in to change notification settings - Fork 0
/
new
executable file
·25 lines (22 loc) · 914 Bytes
/
new
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
#!/bin/bash
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 )
mkdir -p "./${DAY}" || { echo "Could not make directory: ./${DAY}" >&2; exit 3; }
cp -i --no-preserve=mode,timestamps "${SCRIPTDIR}/template.py" "./${DAY}/${DAY}.py" && \
chmod 755 "./${DAY}/${DAY}.py"
COOKIE=$(<"${SCRIPTDIR}/.session_cookie") && \
tempfile=$(mktemp) && \
curl --silent --fail \
--cookie "$COOKIE" \
--user-agent 'https://github.com/jacktose/advent-of-code by jacktose' \
"https://adventofcode.com/${YEAR}/day/${DAY}/input" \
> "$tempfile" && \
mv "$tempfile" "./${DAY}/input" && \
chmod 444 "./${DAY}/input"
touch "./${DAY}/example"
rm -f "$tempfile"
exit 0