#!/bin/sh set -e usage() { printf '%b\n' "usage: ${0##*/} [options]" \ "options:" \ "\t - which year to execute" \ "\t - which day to execute" \ "\t-p - which part to execute (dont specify anything for both)" \ "\t-r - use the real input instead of the test input" } [ $# -lt 2 ] && { usage exit 1 } year=$1 day=$2 shift 2 part="" real=false for flag in "$@" do # Make sure arguments start with '-' and are atleast 2 characters long case $flag in - ) continue ;; -- ) break ;; -* ) ;; * ) continue;; esac # Split arguments to be 1 character long and determine options to use args=${flag#-} while [ "$args" ] do a=${args%"${args#?}"} case $a in p ) aargs=$* part=${aargs##*"${flag}"}; part=${part#\ }; part=${part%%\ *} [ "${part}" ] || { printf '%s\n' "${0##*/}: -p: missing part" 1>&2 exit 1 } [ "${part}" = "1" ] || [ "${part}" = "2" ] || { printf '%s\n' "${0##*/}: -p: should be either 1 or 2" 1>&2 exit 1 } ;; r ) real=true ;; * ) printf '%s\n' "${0##*/}: -$a: invalid argument" 1>&2 usage 1>&2; exit 1 ;; esac args=${args#?} done done unset args arg aargs cd "${0%/*}" cd "$year" || exit 1 ext="$(cat ./ext)" if [ $real = true ] then "./$day/solution${ext}" "$part" < "$day/input-real" else if [ -d "$day/part1-tests" ] && { [ "$part" = 1 ] || [ ! "$part" ]; } then ( cd "$day/part1-tests" || exit 1 printf '%s' "Part 1 Tests: " for test_case in input* do num=${test_case#input} expected=$(sed -n "${num}{p;q}" < ./expected) result=$("../solution$ext" 1 < "$test_case") result=${result#Part\ 1:\ } # shellcheck disable=SC2015 [ "$expected" = "$result" ] && printf '%s' "PASS " || { printf '\n%s\n' "FAIL: Expected $expected, Got $result ($test_case)" exit 1 } done printf '\n' ) fi if [ -d "$day/part2-tests" ] && { [ "$part" = 2 ] || [ ! "$part" ]; } then ( cd "$day/part2-tests" || exit 1 printf '%s' "Part 2 Tests: " for test_case in input* do num=${test_case#input} expected=$(sed -n "${num}{p;q}" < ./expected) result=$("../solution$ext" 2 < "$test_case") result=${result#Part\ 2:\ } # shellcheck disable=SC2015 [ "$expected" = "$result" ] && printf '%s' "PASS " || { printf '\n%s\n' "FAIL: Expected $expected, Got $result ($test_case)" exit 1 } done printf '\n' ) fi fi