41 lines
941 B
Bash
Executable File
41 lines
941 B
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
set -o errexit
|
|
set -o nounset
|
|
set -o pipefail
|
|
if [[ "${TRACE-0}" == "1" ]]; then
|
|
set -o xtrace
|
|
fi
|
|
|
|
cd "$(dirname "$0")"
|
|
|
|
main ()
|
|
{
|
|
IFS=' ' read -r -a times <<< "$(grep Time input | awk '{$1=""; print $0}')"
|
|
IFS=' ' read -r -a distances <<< "$(grep Distance input | awk '{$1=""; print $0}')"
|
|
declare -a winning_counts=()
|
|
records="${#times[@]}"
|
|
for ((i=0; i<records; i++)); do
|
|
t_record="${times[${i}]}"
|
|
d_record="${distances[${i}]}"
|
|
for ((t=1; t<=t_record; t++)); do
|
|
t_remaining=$((t_record-t))
|
|
distance=$((t*t_remaining))
|
|
if [[ ${distance} -gt ${d_record} ]]; then
|
|
first_winning="${t}"
|
|
last_winning=$((t_record-first_winning))
|
|
winning=$((last_winning-first_winning+1))
|
|
break
|
|
fi
|
|
done
|
|
winning_counts+=("${winning}")
|
|
done
|
|
product=1
|
|
for i in "${winning_counts[@]}"; do
|
|
product=$((product*i))
|
|
done
|
|
echo "${product}"
|
|
}
|
|
|
|
main
|