Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 82 lines (70 sloc) 1.53 KB
#!/usr/bin/env zsh
set -e
set -u
set -o pipefail
cd $(dirname $0)
optimize=false
if [[ $1 == '-O' ]]; then
optimize=true
shift
fi
if [[ ! -d $1 || ! ( $2 =~ '^[0-9]+$' ) ]]; then
echo >&2 "Usage: $0 [-O] <year> <day> [<exercise>]"
exit 2
fi
TIMEFMT=$'\n%U user %S system %P cpu %*E total'
year=$1
day=$(printf '%02d' $2)
shift 2
if [[ $# -gt 0 ]]; then
exercise=${1:-}
shift
fi
input="${year}/AOC_${day}.input"
output_directory="build/${year}"
if (( ${+exercise} )); then
name="AOC_${day}_${exercise}"
else
name="AOC_${day}"
fi
mkdir -p $output_directory
if [[ -e "${year}/${name}.hs" ]]; then
executable="${output_directory}/${name}"
if $optimize; then
compilation_args=('-O2')
else
compilation_args=()
fi
stack ghc -- \
$compilation_args \
-o $executable \
-odir $output_directory -osuf "${name}.o" \
-hidir $output_directory -hisuf "${name}.hi" \
"${year}/${name}.hs"
elif [[ -e "${year}/${name}.pl" ]]; then
executable=(swipl "${year}/${name}.pl")
elif [[ -e "${year}/${name}.swift" ]]; then
files=("${year}/${name}.swift" ${year}/Helpers/*.swift)
executable="${output_directory}/${name}"
if $optimize; then
compilation_args=('-O')
else
compilation_args=()
fi
swiftformat $files
swiftc $compilation_args -o $executable $files
echo 'Compiled.'
echo
else
echo "The program does not exist."
exit 1
fi
if [[ -t 0 ]]; then
if [[ ! -e $input ]]; then
echo "${input} does not exist."
exit 1
fi
time $executable "$@" < $input
else
time $executable "$@"
fi