scripts

Various scripts for UNIX-like systems
git clone https://git.tronto.net/scripts
Download | Log | Files | Refs | README

aoc (1519B)


      1 #!/bin/sh
      2 
      3 # Set up a working environment for Advent of Code (https://adventofcode.com/).
      4 # Requires tmux, lynx, curl and a valid AOC_SESSION_COOKIE session cookie.
      5 
      6 # usage: aoc [yyyy nn]
      7 # If yyyy (year) and nn (day, with leading zero if single digit) are not
      8 # specified, the current year and day are used.
      9 
     10 editor="vi a.py"
     11 basedir="$HOME/src/aoc"
     12 lynx_cookie_file="$(mktemp)"
     13 clibrowser="lynx -cookie_file=$lynx_cookie_file"
     14 
     15 year="${1:-$(date +'%Y')}"
     16 day="${2:-$(date +'%d')}"
     17 daynozero="$(echo "$day" | sed 's/^0//')"
     18 
     19 cd "$basedir/$year/$day"
     20 
     21 # Download the puzzle input and save it to input.txt
     22 if [ ! -e input.txt ]; then
     23 	url="https://adventofcode.com/$year/day/$daynozero/input"
     24 	curl "$url" -H "cookie: session=$AOC_SESSION_COOKIE" -o input.txt
     25 fi
     26 
     27 # Download the problem statement page and split out the code blocks
     28 url="https://adventofcode.com/$year/day/$daynozero"
     29 curl "$url" | sed -n '/<pre><code>/,/<\/code><\/pre>/ p' | (\
     30 	i=1
     31 	rm -f "code-$i.txt"
     32 	IFS=''
     33 	while read -r line; do
     34 		if [ "$line" = "</code></pre>" ]; then
     35 			i=$((i + 1))
     36 			rm -f "code-$i.txt"
     37 		else
     38 			echo "$line" | sed 's/<.*>//g' >> "code-$i.txt"
     39 		fi
     40 	done
     41 )
     42 
     43 # Open a tmux session with lynx in its own windows, and an editor and a
     44 # terminal in split view.
     45 printf 'adventofcode.com\tFALSE\t/\tTRUE\t2079722976\tsession\t%s' \
     46 	"$AOC_SESSION_COOKIE" > "$lynx_cookie_file"
     47 tmux new-session \; \
     48 	send-keys "$editor" C-m \; \
     49 	split-window -h \; \
     50 	select-pane -t 0 \; \
     51 	new-window \; \
     52 	send-keys "$clibrowser $url" C-m \;