6 declare -g earliest_leave_time
9 read -u 3 earliest_leave_time
13 filename=${1:-p1_295.txt}
17 echo "Earliest leave_time: $earliest_leave_time"
18 echo "Buses: ${buses[@]}"
23 for bus in ${buses[@]}; do
24 if [ "$bus" == "x" ]; then
27 # work out how many times this goes in
28 # to earliest time stamp
29 multiplier=$(($earliest_leave_time / $bus))
30 while [ $((bus * $multiplier)) -lt $earliest_leave_time ]; do
31 multiplier=$((multiplier+1))
33 if [ $(((bus*multiplier)-$earliest_leave_time)) -lt $time_difference ] || [ $time_difference -eq -1 ]; then
34 time_difference=$(((bus*$multiplier)-$earliest_leave_time))
40 echo " Bus to catch: $bus_to_catch"
41 echo " Gotta wait: $time_difference"
42 echo " Answer: $((bus_to_catch * $time_difference))"
46 # screw it, we've got prime bus numbers, lets
47 # just loop a bit, and make the step the multiple
52 for a in "${buses[@]:1}"; do
54 if [ $a == "x" ]; then
58 # we need to check a multiple of steps past the offset
59 while [ $((((($offset*${buses[0]}) + ($count*$step)) + $loop) % $a)) -ne 0 ]; do
62 offset=$(($offset + (($count*$step)/${buses[0]})))
63 step=$((step*$a)) # our steps are going to be a product of what we had
67 echo " Timestamp: $((offset*${buses[0]}))"