5 filename="${1:-example.txt}"
12 declare -A lowest_distance
14 while read -u 3 line; do
15 if [ "x$line" != "x" ]; then
17 for (( a=0; a<$max_x; a++ )); do
18 map[$a,$max_y]=${line:$a:1}
19 tentative[$a,$max_y]=-1
27 total_points=$(($max_x*$max_y))
35 echo -n "Progress: 0%"
37 # bad implementation of djikstra's algorithm
38 while [ "${tentative[$max_x,$max_y]+abc}" ]; do
39 echo -n -e "\rProgress: "
40 printf "%2d" $((100 - ((${#tentative[@]}*100) / $total_points)))
42 # where we are, we have the lowest_cost for this space, so
43 lowest_distance[$cur_x,$cur_y]="${tentative["$cur_x,$cur_y"]}"
44 unset tentative[$cur_x,$cur_y]
45 # now check to the left / right / up / down from our current node
46 # and give them tentative values
47 # lets do the calcs for the left
49 if [ "${tentative[$test_x,$cur_y]+abc}" ]; then
50 new_cost=${lowest_distance[$cur_x,$cur_y]}
51 ((new_cost+=${map[$test_x,$cur_y]}))
52 if [ $new_cost -lt ${tentative[$test_x,$cur_y]} ] || [ "${tentative[$test_x,$cur_y]}" -eq -1 ]; then
53 tentative[$test_x,$cur_y]=$new_cost
58 if [ "${tentative[$test_x,$cur_y]+abc}" ]; then
59 new_cost=${lowest_distance[$cur_x,$cur_y]}
60 ((new_cost+=${map[$test_x,$cur_y]}))
61 if [ $new_cost -lt ${tentative[$test_x,$cur_y]} ] || [ "${tentative[$test_x,$cur_y]}" -eq -1 ]; then
62 tentative[$test_x,$cur_y]=$new_cost
67 if [ "${tentative[$cur_x,$test_y]+abc}" ]; then
68 new_cost=${lowest_distance[$cur_x,$cur_y]}
69 ((new_cost+=${map[$cur_x,$test_y]}))
70 if [ $new_cost -lt ${tentative[$cur_x,$test_y]} ] || [ "${tentative[$cur_x,$test_y]}" -eq -1 ]; then
71 tentative[$cur_x,$test_y]=$new_cost
76 if [ "${tentative[$cur_x,$test_y]+abc}" ]; then
77 new_cost=${lowest_distance[$cur_x,$cur_y]}
78 ((new_cost+=${map[$cur_x,$test_y]}))
79 if [ $new_cost -lt ${tentative[$cur_x,$test_y]} ] || [ "${tentative[$cur_x,$test_y]}" -eq -1 ]; then
80 tentative[$cur_x,$test_y]=$new_cost
84 # find the new lowest tentative
87 for location in "${!tentative[@]}"; do
88 if [ "${tentative[$location]}" -gt -1 ]; then
89 if [ $new_location == "-1,-1" ]; then
90 new_location=$location
91 new_value=${tentative[$location]}
92 elif [ "${tentative[$location]}" -lt $new_value ]; then
93 new_location=$location
94 new_value=${tentative[$location]}
98 if [ "$new_location" == "-1,-1" ]; then
101 cur_x=${new_location%,*}
102 cur_y=${new_location#*,}
104 echo -e "\rProgress: 100%"
106 # if we've reached here, we've got the lowest risk route to the bottom right
107 echo "Lowest route cost: ${lowest_distance[$max_x,$max_y]}"