6 basedir=$(dirname $(readlink -f ${BASH_SOURCE}))
7 filename=${1:-$basedir/3_4_8.txt}
17 while read -u 3 line; do
19 for (( a=0; a<${#line}; a++ )); do
21 if [ "${char}" == "#" ]; then
42 echo "$x_diff,$y_diff"
46 # we want to know if vector2 is going to get in the way of vector1
50 debug_line Comparing: $vector1 $vector2
52 vector1_x=${vector1%,*}
53 vector1_y=${vector1#*,}
54 vector2_x=${vector2%,*}
55 vector2_y=${vector2#*,}
57 # potentially blocking asteroid
58 # first calculate the minimum x and y steps that are whole numbers
59 # min number first, we'll then halve it and work through the steps
61 # work out which asteroid is furthest from use
62 vector1_x_val=${vector1_x#-}
63 vector1_y_val=${vector1_y#-}
64 vector2_x_val=${vector2_x#-}
65 vector2_y_val=${vector2_y#-}
67 minimum_number=$vector2_x_val
68 if ( [ $vector2_x_val -gt $vector2_y_val ] && [ $vector2_y_val -gt 0 ] ) ||
69 [ $minimum_number -eq 0 ]; then
70 minimum_number=$vector2_y_val
73 debug_line "Min number: $minimum_number"
77 for (( a=$minimum_number; a>0; a-- )); do
78 if [ $((vector2_x % $a)) -eq 0 ] && [ $((vector2_y % $a)) -eq 0 ]; then
79 minstep_x=$((vector2_x / $a))
80 minstep_y=$((vector2_y / $a))
84 debug_line "Have minsteps: $minstep_x,$minstep_y"
86 # in all other cases, we're at least heading in the right direction
90 debug_line "Looking for: $vector1_x,$vector1_y"
91 debug_line "Start: $cur_x,$cur_y"
93 # from the starting point, add the min steps until we're past the other asteroid
94 while keep_going $cur_x $cur_y $minstep_x $minstep_y $vector1_x $vector1_y; do
95 cur_x=$((cur_x+$minstep_x))
96 cur_y=$((cur_y+$minstep_y))
97 debug_line "Step: $cur_x,$cur_y"
98 if [ $cur_x -eq $vector1_x ] && [ $cur_y -eq $vector1_y ]; then
99 debug_line "Collision!"
115 if [ $minstep_x -lt 0 ] && [ $cur_x -lt $ast_x ]; then
116 # past the asteroid, we missed
117 debug_line "stop cond 1"
119 elif [ $minstep_y -lt 0 ] && [ $cur_y -lt $ast_y ]; then
120 # past the asteroid, we missed
121 debug_line "stop cond 2"
123 elif [ $minstep_x -gt 0 ] && [ $cur_x -gt $ast_x ]; then
124 debug_line "stop cond 3"
126 elif [ $minstep_y -gt 0 ] && [ $cur_y -gt $ast_y ]; then
127 debug_line "stop cond 4"
134 # we check for if the first asteroid is blocked viewing the second asteroid
135 # by the third asteroid, if not we add 1 to it's total
136 asteroid_loop=("${!asteroids[@]}")
138 start_count=${#asteroids[@]}
139 for asteroid in ${asteroid_loop[@]}; do
140 ast1_x=${asteroid%,*}
141 ast1_y=${asteroid#*,}
143 cur_count=${#asteroid_loop[@]}
144 percent=$((100-(100*$cur_count / $start_count)))
145 printf '\r%02d%% complete' $percent
146 for asteroid2 in ${asteroid_loop[@]}; do
147 if [ $asteroid2 == $asteroid ]; then
150 ast2_x=${asteroid2%,*}
151 ast2_y=${asteroid2#*,}
153 vector1=$(get_vector $ast1_x $ast1_y $ast2_x $ast2_y)
155 # if we've got a cached can't see, use it to continue the loop
156 if [ ${cantsee[$asteroid,$asteroid2]+a} ]; then
160 if ! [ ${cansee[$asteroid,$asteroid2]+a} ]; then # we already calculated this backwards
161 for asteroid3 in "${!asteroids[@]}"; do
162 if [ $asteroid == $asteroid3 ] || [ $asteroid2 == $asteroid3 ]; then
165 ast3_x=${asteroid3%,*}
166 ast3_y=${asteroid3#*,}
167 debug_line "Checking for collision between $asteroid and $asteroid2 by $asteroid3"
168 vector2=$(get_vector $ast1_x $ast1_y $ast3_x $ast3_y)
169 if ( check_collision $vector1 $vector2 ); then
170 # path is blocked go on to next in outer loop
171 debug_line "We hit $asteroid3 when looking for $asteroid2 from $asteroid"
172 cantsee[$asteroid2,$asteroid]=1
173 cantsee[$asteroid,$asteroid2]=1
178 # nothing blocked the view of asteroid2 from asteroid1
179 debug_line "Apparently $asteroid can set $asteroid2"
180 asteroids[$asteroid]=$((${asteroids[$asteroid]}+1))
181 asteroids[$asteroid2]=$((${asteroids[$asteroid2]}+1))
182 cansee[$asteroid2,$asteroid]=1 # don't bother doing the expensive calculations on the return path
184 unset asteroid_loop[$index]
187 echo -n $'\r'" "$'\r'
191 for asteroid in ${!asteroids[@]}; do
192 count=${asteroids[$asteroid]}
193 if [ $count -gt $best_count ]; then
197 debug_line "$asteroid: ${asteroids[$asteroid]}"
200 echo "Best asteroid: $best_ast which can see $best_count asteroids"
203 #printf "%.0s-" $(seq 1 $width)
205 ## redraw the board with numbers
206 #for (( a=0; a<$ln; a++ )); do
208 # for (( b=0; b<$width; b++ )); do
209 # if [ ${asteroids[$b,$a]+a} ]; then
210 # echo -n ${asteroids[$b,$a]}
218 #printf "%.0s-" $(seq 1 $width)