Add percentage done ticker
authorBrett Parker <iDunno@sommitrealweird.co.uk>
Tue, 8 Dec 2020 01:28:49 +0000 (01:28 +0000)
committerBrett Parker <iDunno@sommitrealweird.co.uk>
Tue, 8 Dec 2020 01:28:49 +0000 (01:28 +0000)
day10/check_asteroids.sh

index 84958ede6959217b33a2b052704d262f9c26224e..e0bf9597eddbc6fd345d4f88e55e9d6d95ad132b 100755 (executable)
@@ -96,7 +96,7 @@ check_collision() {
         cur_y=$((cur_y+$minstep_y))
         debug_line "Step: $cur_x,$cur_y"
         if [ $cur_x -eq $vector1_x ] && [ $cur_y -eq $vector1_y ]; then
-            debug_line echo "Collision!"
+            debug_line "Collision!"
             return 0
         fi
     done
@@ -133,13 +133,17 @@ keep_going() {
 
 # we check for if the first asteroid is blocked viewing the second asteroid
 # by the third asteroid, if not we add 1 to it's total
-for asteroid in ${!asteroids[@]}; do
-#for asteroid in 9,0; do
+asteroid_loop=("${!asteroids[@]}")
+index=0
+start_count=${#asteroids[@]}
+for asteroid in ${asteroid_loop[@]}; do
     ast1_x=${asteroid%,*}
     ast1_y=${asteroid#*,}
 
-    for asteroid2 in ${!asteroids[@]}; do
-    #for asteroid2 in 4,5; do
+    cur_count=${#asteroid_loop[@]}
+    percent=$((100-(100*$cur_count / $start_count)))
+    printf '\r%02d%% complete' $percent
+    for asteroid2 in ${asteroid_loop[@]}; do
         if [ $asteroid2 == $asteroid ]; then
             continue
         fi
@@ -154,7 +158,7 @@ for asteroid in ${!asteroids[@]}; do
         fi
 
         if ! [ ${cansee[$asteroid,$asteroid2]+a} ]; then # we already calculated this backwards
-            for asteroid3 in ${!asteroids[@]}; do
+            for asteroid3 in "${!asteroids[@]}"; do
                 if [ $asteroid == $asteroid3 ] || [ $asteroid2 == $asteroid3 ]; then
                     continue
                 fi
@@ -173,10 +177,14 @@ for asteroid in ${!asteroids[@]}; do
         fi
         # nothing blocked the view of asteroid2 from asteroid1
         debug_line "Apparently $asteroid can set $asteroid2"
-        asteroids[$asteroid]=$((asteroids[$asteroid]+1))
+        asteroids[$asteroid]=$((${asteroids[$asteroid]}+1))
+        asteroids[$asteroid2]=$((${asteroids[$asteroid2]}+1))
         cansee[$asteroid2,$asteroid]=1 # don't bother doing the expensive calculations on the return path
     done
+    unset asteroid_loop[$index]
+    index=$((index+1))
 done
+echo -n $'\r'"                 "$'\r'
 
 best_ast=
 best_count=0