diff options
| -rwxr-xr-x | scripts/aoc | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/scripts/aoc b/scripts/aoc index 40f94ed..e25131e 100755 --- a/scripts/aoc +++ b/scripts/aoc | |||
| @@ -4,11 +4,21 @@ | |||
| 4 | # Requires tmux, lynx, curl and a valid AOC_SESSION_COOKIE session cookie. | 4 | # Requires tmux, lynx, curl and a valid AOC_SESSION_COOKIE session cookie. |
| 5 | 5 | ||
| 6 | # usage: aoc [yyyy nn] | 6 | # usage: aoc [yyyy nn] |
| 7 | # Must be run from inside tmux. | ||
| 7 | # If yyyy (year) and nn (day, with leading zero if single digit) are not | 8 | # If yyyy (year) and nn (day, with leading zero if single digit) are not |
| 8 | # specified, the current year and day are used. | 9 | # specified, the current year and day are used. |
| 9 | 10 | ||
| 10 | editor="vi a.py" | 11 | if [ -z "$TMUX" ]; then |
| 11 | basedir="$HOME/src/aoc" | 12 | echo "Not in a tmux session!" |
| 13 | exit 1 | ||
| 14 | elif [ -z "$AOC_SESSION_COOKIE" ]; then | ||
| 15 | echo "Session cookie not found, must be in AOC_SESSION_COOKIE" | ||
| 16 | exit 1 | ||
| 17 | fi | ||
| 18 | |||
| 19 | editor="vi" | ||
| 20 | file="a.cpp" | ||
| 21 | basedir="/tmp/$HOME/src/aoc" | ||
| 12 | lynx_cookie_file="$(mktemp)" | 22 | lynx_cookie_file="$(mktemp)" |
| 13 | clibrowser="lynx -cookie_file=$lynx_cookie_file" | 23 | clibrowser="lynx -cookie_file=$lynx_cookie_file" |
| 14 | 24 | ||
| @@ -16,37 +26,45 @@ year="${1:-$(date +'%Y')}" | |||
| 16 | day="${2:-$(date +'%d')}" | 26 | day="${2:-$(date +'%d')}" |
| 17 | daynozero="$(echo "$day" | sed 's/^0//')" | 27 | daynozero="$(echo "$day" | sed 's/^0//')" |
| 18 | 28 | ||
| 19 | cd "$basedir/$year/$day" | 29 | editpath="$basedir/$year/$day/$file" |
| 30 | inputs="$basedir/$year/inputs" | ||
| 31 | |||
| 32 | mkdir -p "$basedir/$year/$day" | ||
| 33 | mkdir -p "$inputs" | ||
| 34 | |||
| 35 | cd "$basedir/$year" | ||
| 20 | 36 | ||
| 21 | # Download the puzzle input and save it to input.txt | 37 | # Download the puzzle input and save it to input.txt |
| 22 | if [ ! -e input.txt ]; then | 38 | if [ ! -e input.txt ]; then |
| 23 | url="https://adventofcode.com/$year/day/$daynozero/input" | 39 | url="https://adventofcode.com/$year/day/$daynozero/input" |
| 24 | curl "$url" -H "cookie: session=$AOC_SESSION_COOKIE" -o input.txt | 40 | curl "$url" -H "cookie: session=$AOC_SESSION_COOKIE" -o "$inputs/$day" |
| 25 | fi | 41 | fi |
| 26 | 42 | ||
| 27 | # Download the problem statement page and split out the code blocks | 43 | # Download the problem statement page and split out the code blocks |
| 28 | url="https://adventofcode.com/$year/day/$daynozero" | 44 | url="https://adventofcode.com/$year/day/$daynozero" |
| 29 | curl "$url" | sed -n '/<pre><code>/,/<\/code><\/pre>/ p' | (\ | 45 | curl "$url" | sed -n '/<pre><code>/,/<\/code><\/pre>/ p' | (\ |
| 30 | i=1 | 46 | i=1 |
| 31 | rm -f "code-$i.txt" | 47 | file="$day/code-$i.txt" |
| 48 | rm -f "$file" | ||
| 32 | IFS='' | 49 | IFS='' |
| 33 | while read -r line; do | 50 | while read -r line; do |
| 34 | if [ "$line" = "</code></pre>" ]; then | 51 | if [ "$line" = "</code></pre>" ]; then |
| 35 | i=$((i + 1)) | 52 | i=$((i + 1)) |
| 36 | rm -f "code-$i.txt" | 53 | rm -f "$file" |
| 37 | else | 54 | else |
| 38 | echo "$line" | sed 's/<.*>//g' >> "code-$i.txt" | 55 | echo "$line" | sed 's/<.*>//g' >> "$file" |
| 39 | fi | 56 | fi |
| 40 | done | 57 | done |
| 41 | ) | 58 | ) |
| 42 | 59 | ||
| 43 | # Open a tmux session with lynx in its own windows, and an editor and a | 60 | # Open lynx in its own windows, and an editor and a terminal in split view. |
| 44 | # terminal in split view. | ||
| 45 | printf 'adventofcode.com\tFALSE\t/\tTRUE\t2079722976\tsession\t%s' \ | 61 | printf 'adventofcode.com\tFALSE\t/\tTRUE\t2079722976\tsession\t%s' \ |
| 46 | "$AOC_SESSION_COOKIE" > "$lynx_cookie_file" | 62 | "$AOC_SESSION_COOKIE" > "$lynx_cookie_file" |
| 47 | tmux new-session \; \ | 63 | |
| 64 | tmux \ | ||
| 65 | send-keys cd "$basedir/$year" C-m \; \ | ||
| 48 | send-keys "$editor" C-m \; \ | 66 | send-keys "$editor" C-m \; \ |
| 49 | split-window -h \; \ | 67 | split-window -h -c "$basedir/$year" \; \ |
| 50 | select-pane -t 0 \; \ | 68 | select-pane -t 0 \; \ |
| 51 | new-window \; \ | 69 | new-window -c "$basedir/$year" \; \ |
| 52 | send-keys "$clibrowser $url" C-m \; | 70 | send-keys "$clibrowser $url" C-m \; |
