6 basedir=$(dirname $(readlink -f ${BASH_SOURCE}))
8 filename=${1:-$basedir/3_4_8.txt}
18 while read -u 3 line; do
20 for (( a=0; a<${#line}; a++ )); do
22 if [ "${char}" == "#" ]; then
29 debug_line=true # default to not actually outputting any debug info
31 if [ ${DEBUG:-0} -gt 0 ]; then
49 echo "$x_diff,$y_diff"
53 # we want to know if vector2 is going to get in the way of vector1
57 $debug_line Comparing: $vector1 $vector2
59 vector1_x=${vector1%,*}
60 vector1_y=${vector1#*,}
61 vector2_x=${vector2%,*}
62 vector2_y=${vector2#*,}
64 # potentially blocking asteroid
65 # first calculate the minimum x and y steps that are whole numbers
66 # min number first, we'll then halve it and work through the steps
68 # work out which asteroid is furthest from use
69 vector1_x_val=${vector1_x#-}
70 vector1_y_val=${vector1_y#-}
71 vector2_x_val=${vector2_x#-}
72 vector2_y_val=${vector2_y#-}
74 minimum_number=$vector2_x_val
75 if [ $vector2_x_val -gt $vector2_y_val -a $vector2_y_val -gt 0 ] || [ $minimum_number -eq 0 ]; then
76 minimum_number=$vector2_y_val
79 $debug_line "Min number: $minimum_number"
83 for (( a=$minimum_number; a>0; a-- )); do
84 if [ $((vector2_x % $a)) -eq 0 -a $((vector2_y % $a)) -eq 0 ]; then
85 minstep_x=$((vector2_x / $a))
86 minstep_y=$((vector2_y / $a))
90 $debug_line "Have minsteps: $minstep_x,$minstep_y"
92 # in all other cases, we're at least heading in the right direction
96 $debug_line "Looking for: $vector1_x,$vector1_y"
97 $debug_line "Start: $cur_x,$cur_y"
99 # from the starting point, add the min steps until we're past the other asteroid
100 while keep_going $cur_x $cur_y $minstep_x $minstep_y $vector1_x $vector1_y; do
101 cur_x=$((cur_x+$minstep_x))
102 cur_y=$((cur_y+$minstep_y))
103 $debug_line "Step: $cur_x,$cur_y"
104 if [ $cur_x -eq $vector1_x -a $cur_y -eq $vector1_y ]; then
105 $debug_line "Collision!"
115 start_x=${asteroid%,*}
116 start_y=${asteroid#*,}
123 # first, head left of the asteroid
124 for (( x=$((start_x-1)); x>=0; x-- )); do
125 do_blocker_check $asteroid $x $start_y
127 hitblocker=0 # this gets updated in do_blocker_check, reset it
129 # now head right of the asteroid
130 for (( x=$(($start_x+1)); x<= $width; x++ )); do
131 do_blocker_check $asteroid $x $start_y
133 hitblocker=0 # this gets updated in do_blocker_check, reset it
136 for (( y=$((start_y-1)); y>=0; y-- )); do
137 do_blocker_check $asteroid $start_x $y
139 hitblocker=0 # this gets updated in do_blocker_check, reset it
142 for (( y=$((start_y+1)); y<=$ln; y++ )); do
143 do_blocker_check $asteroid $start_x $y
152 if [ ${asteroids[$x,$y]+a} ]; then
153 if [ $hitblocker -eq 0 ]; then
154 cansee[$asteroid,$x,$y]=1
155 cansee[$x,$y,$asteroid]=1
158 cantsee[$asteroid,$x,$y]=1
159 cantsee[$x,$y,$asteroid]=1
172 # first check the horizontal and vertical planes
173 if [[ ( $source_x -eq $sink_x && $sink_x -eq $blocker_x ) && ( $sink_y -gt $blocker_y && $blocker_y -gt $source_y ) || ( $sink_y -lt $blocker_y && $blocker_y -lt $source_y ) ]] ||
174 [[ ( $source_y -eq $sink_y && $sink_y -eq $blocker_y ) && ( $sink_x -gt $blocker_x && $blocker_x -gt $source_x ) || ( $sink_x -lt $blocker_x && $blocker_x -lt $source_x ) ]]; then
178 # if our source asteroid is further to the right thank our sink, then to stand half a chance
179 # of being in the way, the blocker has to be x > $sink_x and x < source_x
180 # apply the same rule for y
181 # reverse the direction and do the same checks.
182 # So if source_x < sink_x then blocker_x has to be < sink_x > source_x
183 if [ $source_x -gt $sink_x -a $blocker_x -gt $sink_x -a $blocker_x -lt $source_x ] ||
184 [ $source_y -gt $sink_y -a $blocker_y -gt $sink_y -a $blocker_y -lt $source_y ] ||
185 [ $source_x -lt $sink_x -a $blocker_x -lt $sink_x -a $blocker_x -gt $source_x ] ||
186 [ $source_y -lt $sink_y -a $blocker_y -lt $sink_y -a $blocker_y -gt $source_y ]; then
201 if [ $minstep_x -lt 0 -a $cur_x -lt $ast_x ] ||
202 [ $minstep_y -lt 0 -a $cur_y -lt $ast_y ] ||
203 [ $minstep_x -gt 0 -a $cur_x -gt $ast_x ] ||
204 [ $minstep_y -gt 0 -a $cur_y -gt $ast_y ]; then
205 $debug_line "keepgoing finished"
212 # we check for if the first asteroid is blocked viewing the second asteroid
213 # by the third asteroid, if not we add 1 to it's total
214 asteroid_loop=("${!asteroids[@]}")
216 start_count=${#asteroids[@]}
217 for asteroid in ${asteroid_loop[@]}; do
218 ast1_x=${asteroid%,*}
219 ast1_y=${asteroid#*,}
220 cur_count=${#asteroid_loop[@]}
221 percent=$((100-(100*$cur_count / $start_count)))
223 $debug_line "Doing fast checks"
224 fast_checks $asteroid
225 $debug_line "Running main loops"
227 printf '\r%02d%% complete' $percent
228 for asteroid2 in ${asteroid_loop[@]}; do
229 if [ $asteroid2 == $asteroid ]; then
232 ast2_x=${asteroid2%,*}
233 ast2_y=${asteroid2#*,}
235 vector1=$(get_vector $ast1_x $ast1_y $ast2_x $ast2_y)
237 # if we've got a cached can't see, use it to continue the loop
238 if [ ${cantsee[$asteroid,$asteroid2]+a} ]; then
242 if ! [ ${cansee[$asteroid,$asteroid2]+a} ]; then # we already calculated this
243 for asteroid3 in "${!asteroids[@]}"; do
244 if [ $asteroid == $asteroid3 ] || [ $asteroid2 == $asteroid3 ]; then
247 ast3_x=${asteroid3%,*}
248 ast3_y=${asteroid3#*,}
250 # if this asteroid can't be in the way, continue the loop
251 if ! possibly_in_way $ast1_x $ast1_y $ast2_x $ast2_y $ast3_x $ast3_y; then
252 $debug_line "Apparently $ast1_x $ast1_y to $ast2_x $ast2_y cannot be blocked by $ast3_x $ast3_y"
256 $debug_line "Checking for collision between $asteroid and $asteroid2 by $asteroid3"
257 vector2=$(get_vector $ast1_x $ast1_y $ast3_x $ast3_y)
258 if ( check_collision $vector1 $vector2 ); then
259 # path is blocked go on to next in outer loop
260 $debug_line "We hit $asteroid3 when looking for $asteroid2 from $asteroid"
261 cantsee[$asteroid2,$asteroid]=1
262 cantsee[$asteroid,$asteroid2]=1
267 # nothing blocked the view of asteroid2 from asteroid1
268 $debug_line "Apparently $asteroid can see $asteroid2"
269 asteroids[$asteroid]=$((${asteroids[$asteroid]}+1))
270 asteroids[$asteroid2]=$((${asteroids[$asteroid2]}+1))
271 cansee[$asteroid2,$asteroid]=1 # don't bother doing the expensive calculations on the return path
273 unset asteroid_loop[$index]
276 echo -n $'\r'" "$'\r'
280 for asteroid in ${!asteroids[@]}; do
281 count=${asteroids[$asteroid]}
282 if [ $count -gt $best_count ]; then
286 $debug_line "$asteroid: ${asteroids[$asteroid]}"
289 echo "Best asteroid: $best_ast which can see $best_count asteroids"
292 #printf "%.0s-" $(seq 1 $width)
294 ## redraw the board with numbers
295 #for (( a=0; a<$ln; a++ )); do
297 # for (( b=0; b<$width; b++ )); do
298 # if [ ${asteroids[$b,$a]+a} ]; then
299 # echo -n ${asteroids[$b,$a]}
307 #printf "%.0s-" $(seq 1 $width)