advent-of-code/lib/aoc.sh
Alfred Melch f885b70c9e Fix example extraction
Assumtion was that the first and last lines of examples are always
blank. In 2021/06 the example is only one line. It has no blank line at
the end so the example was deleted by the script.

Changed:
The script will now check if the line is actually blank before deleting
it. Requires that the indentation is removed beforehand.
2022-01-05 15:28:21 +01:00

40 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
PARENT_DIR=$(dirname $PWD)
YEAR=${PARENT_DIR##*/}
DAY_RAW=${PWD##*/}
DAY=$((10#${DAY_RAW//[^0-9]/}))
SESSION_KEY=$(cat ~/.aocrc)
echo "Fetching ${YEAR} ${DAY}"
echo $SESSION_KEY
URL_DAY="https://adventofcode.com/${YEAR}/day/${DAY}"
URL_INPUT="$URL_DAY/input"
mkdir -p input
echo -e "$URL_DAY\n" > README.md
curl -b session=$SESSION_KEY $URL_DAY | sed -n '/<article class="day-desc">/,/<\/article>/p' | html2markdown --mark-code >> README.md
curl -b session=$SESSION_KEY $URL_INPUT > input/input
cat README.md
echo "Extracting example code blocks..."
# extract example code blocks
cat README.md |
sed -n '/\[code\]/,/\[\/code\]/p' | # extract code blocks
sed 's/^ //' | # remove indentation
sed '/\[code\]/{n;/^$/d}' | # remove blank lines after opening bracket
tac | # reverse line order
sed '/\[\/code\]/{n;/^$/d}' | # remove blank lines after closing brackets (preceding blank lines in normal order)
tac | # reverse line order again
sed '/^\[\/code\]/d' | # remove closing brackets
csplit - --suppress-matched --elide-empty-files --prefix='input/example' --suffix='%d' '/\[code\]/' '{*}'
echo "done."
echo
echo "Visit: $URL_DAY"