aoc/2023/6/2/solution.sh

32 lines
644 B
Bash
Raw Normal View History

2023-12-06 12:37:48 +01:00
#!/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")"
INPUT="$1"
main ()
{
time_record=$(grep Time "${INPUT}" | cut -d : -f 2 | tr -d ' ')
distance_record=$(grep Distance "${INPUT}" | cut -d : -f 2 | tr -d ' ')
for ((t=1; t<time_record; t++)); do
t_remaining=$((time_record-t))
distance=$((t*t_remaining))
if [[ ${distance} -gt ${distance_record} ]]; then
first_winning="${t}"
last_winning=$((time_record-first_winning))
winning=$((last_winning-first_winning+1))
break
fi
done
echo "${winning}"
}
main