~turminal/aoc2020

2072614ce4f6c35ef4a1433ea749db3514e2c383 — Bor Grošelj Simić 3 years ago 17e5dc4
automate building and running
2 files changed, 67 insertions(+), 1 deletions(-)

M .gitignore
A run
M .gitignore => .gitignore +5 -1
@@ 1,4 1,8 @@
*.cmo
*.cmi

a.out
*.out
*.in

# contains the session cookie needed for input file download
.cookie

A run => run +62 -0
@@ 0,0 1,62 @@
#!/bin/sh -eu

source .cookie

usage () {
	printf "USAGE: ./run [day_number]\n"
	printf "If day number is provided, build and run the task for that day.\n"
	printf "Run all implemented tasks otherwise\n"
}


die () {
	printf "error: %s\n" "$1"
	usage
	exit 1
}

day () {
	test -f "day_$1.in" || curl "https://adventofcode.com/2020/day/$1/input" \
		-H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0' \
		-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' \
		-H 'Accept-Language: en-US,en;q=0.5' \
		--compressed \
		-H 'DNT: 1' \
		-H 'Connection: keep-alive' \
		-H "Cookie: session=${SESSION_COOKIE}" \
		-H 'Upgrade-Insecure-Requests: 1' \
		-H 'Cache-Control: max-age=0' \
		-H 'TE: Trailers' > "day_$1.in"

	ocamlc "day_$1.ml" -o "day_$1"
	./"day_$1"

	printf "Day $1 task 1:\n"
	cat "day_$1_1.out"

	printf "Day $1 task 2:\n"
	cat "day_$1_2.out"
}

if [ $# = 0 ]
then
	for f in day_*.ml
	do
		day $(basename "$f" .ml | cut -d _ -f 2)
		printf "\n"
	done
	exit 0
fi

case "$1" in
	*[[:digit:]])
		test -f "day_$1.ml" || die "No program source for day $1"
		day $1
		;;
	-u|-h|--usage|--help)
		usage
		;;
	*)
		die "$1 is not a day number"
		;;
esac