advent-of-code/test.sh

93 lines
1.5 KiB
Bash
Raw Normal View History

2020-12-06 16:34:20 +01:00
#!/usr/bin/bash
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
2022-12-01 19:19:00 +01:00
COMMON_DIR="$(realpath common)"
export COMMON_DIR
2020-12-06 16:34:20 +01:00
2022-12-01 19:19:00 +01:00
OUTDIR=$(mktemp --directory)
trap "rm -rf '$OUTDIR'" EXIT
2021-12-01 13:58:49 +01:00
2020-12-06 16:34:20 +01:00
run_solution() {
2022-12-01 19:23:48 +01:00
local solution=$1
local input=$2
2022-12-01 19:19:10 +01:00
if [[ -d "$solution" ]]; then
if [[ -f "$solution/Cargo.toml" ]]; then
2021-12-01 09:18:38 +01:00
cargo run --quiet --manifest-path "$solution/Cargo.toml" < "$input"
else
runfile=$solution/run.sh
2022-12-01 19:19:10 +01:00
[[ -f "$runfile" ]] || { echo "Error: $runfile not found" >&2; exit 1; }
2021-12-01 09:18:38 +01:00
"$runfile" "$input"
fi
2020-12-06 16:34:20 +01:00
else
case "$(basename "$solution")" in
*.hs)
runhaskell -icommon "$solution" < "$input"
;;
*.py)
python "$solution" < "$input"
;;
*)
echo "Error: Unknown solution type: $solution" >&2
2020-12-06 16:34:20 +01:00
exit 1
;;
esac
fi
}
2022-12-01 19:19:00 +01:00
do_day() {
2022-12-01 19:23:48 +01:00
local year=$1
local day=$2
2022-12-01 19:23:48 +01:00
local input=$year/data/day$day.input
local expected=$year/data/day$day.expected
2020-12-06 16:34:20 +01:00
2022-12-01 19:19:00 +01:00
for solution in "$year/day$day"/*; do
2020-12-06 16:34:20 +01:00
echo -n "$solution... "
case "${solution##*/}" in
2020-12-13 18:15:17 +01:00
example*)
echo SKIP
continue
2022-12-01 19:19:10 +01:00
;;
*)
;;
esac
2022-12-01 19:23:48 +01:00
local solution_output=$OUTDIR/${year}_$day.out
run_solution "$solution" "$input" > "$solution_output"
2020-12-06 16:34:20 +01:00
if ! diff -u "$expected" "$solution_output"; then
echo FAIL
rv=$?
case "$rv" in
1)
exit 2
;;
*)
exit 1
;;
esac
else
echo ok
fi
2022-12-01 19:19:00 +01:00
done
}
do_year() {
2022-12-01 19:23:48 +01:00
local year=$1
2022-12-01 19:19:00 +01:00
for day in $(seq 1 25); do
do_day "$year" "$day"
2020-12-06 16:34:20 +01:00
done
2022-12-01 19:19:00 +01:00
}
shopt -s dotglob nullglob
for year in 2020 2021 2022; do
do_year "$year"
2020-12-06 16:34:20 +01:00
done