3 basedir=$(dirname $(readlink -f $BASH_SOURCE))
9 declare -A asteroid_angles
14 while read -u 3 line; do
16 for (( a=0; a<${#line}; a++ )); do
18 if [ "${char}" == "#" ]; then
29 local vector=$(get_asteroids_vector $a1 $a2)
30 local move_x=${vector%,*}
31 local move_y=${vector#*,}
33 val="$(calc -p 'round(((90 + ((180/pi()) * atan2('$move_y','$move_x'))) % 360) * 100000000) / 100000000')"
37 get_closest_asteroid() {
42 local nearest_asteroid=
46 for asteroid in $asteroids; do
47 vector=$(get_asteroids_vector $our_asteroid $asteroid)
50 # use absolute numbers for length calculation!
55 if [ $distance -lt $min_dist -o $min_dist -eq -1 ]; then
57 nearest_asteroid=$asteroid
61 echo $nearest_asteroid
64 remove_asteroid_from_string() {
73 for a in $asteroids; do
74 if [ "$a" != "$asteroid" ]; then
75 new_asteroids+="${seperator}$a"
77 if [ "x$new_asteroids" != "x" ]; then
85 # calculate angles to each asteroid from us
86 for asteroid in ${!asteroids[@]}; do
87 if [ $asteroid == $our_asteroid ]; then
91 # otherwise, get the angle from us to there
92 angle=$(get_angle $our_asteroid $asteroid)
94 # see if we've already got this angle as a key, if so append to it,
96 if [ ${asteroid_angles[$angle]+a} ]; then
97 asteroid_angles[$angle]+="|$asteroid"
99 asteroid_angles[$angle]=$asteroid
108 printf "%s\n" "${!asteroid_angles[@]}" | sort -g
111 sorted_angles=( "$(get_angles_list)" )
113 for (( round=0; round<3; round++ )); do
114 for angle in $sorted_angles; do
115 if [ ${asteroid_angles[$angle]+a} ]; then
116 echo "$angle: ${asteroid_angles[$angle]}"
117 closest=$(get_closest_asteroid "${asteroid_angles[$angle]}")
118 #echo " - closest $closest"
119 # blow up the closest!
120 newlist=$(remove_asteroid_from_string ${asteroid_angles[$angle]} $closest)
121 if [ "x$newlist" == "x" ]; then
122 unset asteroid_angles[$angle]
124 asteroid_angles[$angle]=$newlist
126 zapped_count=$((zapped_count+1))
128 echo "$zapped_count: $closest"
130 if [ $zapped_count -eq 200 ]; then
131 echo "The 200th asteroid to be zapped was $closest"
138 echo "And we're done."