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}
 
  26 total_points=$(($max_x*$max_y))
 
  34     echo -n "Progress:  0%"
 
  36     # bad implementation of djikstra's algorithm
 
  37     while [ ! "${lowest_distance[$max_x,$max_y]+abc}" ]; do
 
  38         echo -n -e "\rProgress: "
 
  39         printf "%2d" $((((${#lowest_distance[@]}*100) / $total_points)))
 
  41         lowest_distance[$cur_x,$cur_y]="${tentative["$cur_x,$cur_y"]}"
 
  42         unset tentative[$cur_x,$cur_y]
 
  43         # now check to the left / right / up / down from our current node
 
  44         # and give them tentative values
 
  45         for test_x in $((cur_x-1)) $((cur_x+1)); do
 
  46             if [ "${map[$test_x,$cur_y]+abc}" ]; then
 
  47                 if [ ! "${lowest_distance[$test_x,$cur_y]+abc}" ]; then
 
  48                     new_cost=${lowest_distance[$cur_x,$cur_y]}
 
  49                     ((new_cost+=${map[$test_x,$cur_y]}))
 
  50                     if [ "${tentative[$test_x,$cur_y]+abc}" ]; then
 
  51                         if [ $new_cost -lt ${tentative[$test_x,$cur_y]} ]; then
 
  52                             tentative[$test_x,$cur_y]=$new_cost
 
  55                         tentative[$test_x,$cur_y]=$new_cost
 
  60         for test_y in $((cur_y-1)) $((cur_y+1)); do
 
  61             if [ "${map[$cur_x,$test_y]+abc}" ]; then
 
  62                 if [ ! "${lowest_distance[$cur_x,$test_y]+abc}" ]; then
 
  63                     new_cost=${lowest_distance[$cur_x,$cur_y]}
 
  64                     ((new_cost+=${map[$cur_x,$test_y]}))
 
  65                     if [ "${tentative[$cur_x,$test_y]+abc}" ]; then
 
  66                         if [ $new_cost -lt ${tentative[$cur_x,$test_y]} ]; then
 
  67                             tentative[$cur_x,$test_y]=$new_cost
 
  70                         tentative[$cur_x,$test_y]=$new_cost
 
  76         # find the new lowest tentative
 
  79         for location in "${!tentative[@]}"; do
 
  80             if [ $new_location == "-1,-1" ]; then
 
  81                 new_location=$location
 
  82                 new_value=${tentative[$location]}
 
  83             elif [ "${tentative[$location]}" -lt $new_value ]; then
 
  84                 new_location=$location
 
  85                 new_value=${tentative[$location]}
 
  88         if [ "$new_location" == "-1,-1" ]; then
 
  91         cur_x=${new_location%,*}
 
  92         cur_y=${new_location#*,}
 
  94     echo -e "\rProgress: 100%"
 
  96 # if we've reached here, we've got the lowest risk route to the bottom right
 
  97     echo "Lowest route cost: ${lowest_distance[$max_x,$max_y]}"
 
 101     for (( y=0; y<=$max_y; y++ )); do
 
 102         for (( x=0; x<=$max_x; x++ )); do
 
 103             echo -n ${map[$x,$y]}
 
 112 # first, work left to right on the map on the first iteration, that gets this up to 5 wide
 
 114 y_height=$((max_y+1))
 
 115 for cell in $(seq 2 5); do
 
 116     for (( y=0; y<$y_height; y++ )); do
 
 117         for (( x=0; x<$x_width; x++ )); do
 
 118             old_x=$((($x_width*($cell-2))+$x))
 
 119             new_x=$((($x_width*($cell-1))+$x))
 
 120             new_value=$((${map[$old_x,$y]} + 1))
 
 121             if [ $new_value -gt 9 ]; then
 
 124             map[$new_x,$y]=$new_value
 
 128 # now, we work on the second to 5th y iteration
 
 129 # in each case, we can take the 2-5 cells of above
 
 130 # and copy them, then just do the actual work on the 5th
 
 132 for y_rep in $(seq 2 5); do
 
 133     y_copy_offset=$((($y_rep - 2)*$y_height))
 
 134     y_offset=$((($y_rep - 1)*$y_height))
 
 135     for (( y=0; y<$y_height; y++ )); do
 
 136         for (( x=0; x<$x_width*4; x++ )); do
 
 137             old_x=$((x+$x_width))
 
 138             value=${map[$old_x,$(($y_copy_offset+$y))]}
 
 139             map[$x,$(($y_offset+$y))]=$value
 
 141         # now we want to just copy the left one + 1, ish
 
 142         for (( x=0; x<$x_width; x++ )); do
 
 143             new_x=$((($x_width*4)+$x))
 
 144             old_x=$((($x_width*3)+$x))
 
 145             new_value=$((${map[$old_x,$(($y_offset+$y))]}))
 
 147             if [ $new_value -gt 9 ]; then
 
 150             map[$new_x,$(($y_offset+$y))]=$new_value
 
 155 # we now have a 5*5 grid of the evil! lets change max_x, max_y, and total_points
 
 156 max_x=$((($x_width * 5) - 1))
 
 157 max_y=$((($y_height * 5) - 1))
 
 158 total_points=$(($max_x*$max_y))
 
 161 unset lowest_distance
 
 162 declare -A tentative=()
 
 163 declare -A lowest_distance=()