From: Brett Parker Date: Wed, 9 Dec 2020 02:15:03 +0000 (+0000) Subject: Optimise Day 10 X-Git-Url: https://git.sommitrealweird.co.uk/advent-of-code-2019.git/commitdiff_plain/3e8dd8ef406fe4f224dfedabca1fe6f306702bd5?hp=e89a78b0539f89e5276ce18ba0c88ee87863a778 Optimise Day 10 - Move things in to shared functions out of the way - Add zap.sh, with some hardcoding - change check_asteroids.sh to be *much* quicker by using the angle method employed by zap --- diff --git a/day10/check_asteroids.sh b/day10/check_asteroids.sh index efda359..d0600bb 100755 --- a/day10/check_asteroids.sh +++ b/day10/check_asteroids.sh @@ -9,25 +9,11 @@ basedir=$(dirname $(readlink -f ${BASH_SOURCE})) filename=${1:-$basedir/3_4_8.txt} -exec 3<$filename +read_file $filename -declare -A asteroids declare -A cansee declare -A cantsee -ln=0 -width=0 -while read -u 3 line; do - width=${#line} - for (( a=0; a<${#line}; a++ )); do - char=${line:$a:1} - if [ "${char}" == "#" ]; then - asteroids[$a,$ln]=0 - fi - done - ln=$((ln+1)) -done - debug_line=true # default to not actually outputting any debug info if [ ${DEBUG:-0} -gt 0 ]; then @@ -39,271 +25,35 @@ debug_line() { echo "$line" >&2 } -check_collision() { - # we want to know if vector2 is going to get in the way of vector1 - local vector1=$1 - local vector2=$2 - - $debug_line Comparing: $vector1 $vector2 - - vector1_x=${vector1%,*} - vector1_y=${vector1#*,} - vector2_x=${vector2%,*} - vector2_y=${vector2#*,} - - # potentially blocking asteroid - # first calculate the minimum x and y steps that are whole numbers - # min number first, we'll then halve it and work through the steps - - # work out which asteroid is furthest from use - vector1_x_val=${vector1_x#-} - vector1_y_val=${vector1_y#-} - vector2_x_val=${vector2_x#-} - vector2_y_val=${vector2_y#-} - - minimum_number=$vector2_x_val - if [ $vector2_x_val -gt $vector2_y_val -a $vector2_y_val -gt 0 ] || [ $minimum_number -eq 0 ]; then - minimum_number=$vector2_y_val - fi - - $debug_line "Min number: $minimum_number" - - minstep_x=$vector2_x - minstep_y=$vector2_y - for (( a=$minimum_number; a>0; a-- )); do - if [ $((vector2_x % $a)) -eq 0 -a $((vector2_y % $a)) -eq 0 ]; then - minstep_x=$((vector2_x / $a)) - minstep_y=$((vector2_y / $a)) - break - fi - done - $debug_line "Have minsteps: $minstep_x,$minstep_y" - - # in all other cases, we're at least heading in the right direction - cur_x=$vector2_x - cur_y=$vector2_y - - $debug_line "Looking for: $vector1_x,$vector1_y" - $debug_line "Start: $cur_x,$cur_y" - - # from the starting point, add the min steps until we're past the other asteroid - while keep_going $cur_x $cur_y $minstep_x $minstep_y $vector1_x $vector1_y; do - cur_x=$((cur_x+$minstep_x)) - cur_y=$((cur_y+$minstep_y)) - $debug_line "Step: $cur_x,$cur_y" - if [ $cur_x -eq $vector1_x -a $cur_y -eq $vector1_y ]; then - $debug_line "Collision!" - return 0 - fi - done - - return 1 -} - -fast_checks() { - asteroid=$1 - start_x=${asteroid%,*} - start_y=${asteroid#*,} - - max_x=$width - max_y=$ln - - # directions: - # -1, 0 - left - # 1, 0 - right - # 0, 1 - up - # 0,-1 - down - # -1, 1 - diagonal left down - # -1,-1 - diagonal left up - # 1, 1 - diagonal right down - # 1,-1 - diagonal right up - for direction in -1,0 1,0 0,1 0,-1 -1,1 -1,-1 1,1 1,-1; do - #for direction in "-1,0" "1,0" "0,1" "0,-1"; do - stepx=${direction%,*} - stepy=${direction#*,} - do_blocker_check $start_x $start_y $stepx $stepy - done -} - -do_blocker_check() { - startx=$1 - starty=$2 - stepx=$3 - stepy=$4 - - hitblocker=0 - - if [ $stepx -eq 0 -a $stepy -eq 0 ]; then - echo "Somehow got 0 and 0 as steps" - return - fi - - local x=$startx - local y=$starty - - # add the steps before we start the loop to not be - # on ourselves - x=$((x+$stepx)) - y=$((y+$stepy)) - - while [ $x -le $width -a $x -ge 0 -a $y -le $ln -a $y -ge 0 ]; do - if [ ${asteroids[$x,$y]+a} ]; then - if [ $hitblocker -eq 0 ]; then - cansee[$asteroid,$x,$y]=1 - cansee[$x,$y,$startx,$starty]=1 - hitblocker=1 - else - cantsee[$startx,$starty,$x,$y]=1 - cantsee[$x,$y,$startx,$starty]=1 - fi - fi - x=$((x+$stepx)) - y=$((y+$stepy)) - done -} - -possibly_in_way() { - source_x=$1 - source_y=$2 - sink_x=$3 - sink_y=$4 - blocker_x=$5 - blocker_y=$6 - - # first check the horizontal and vertical planes - 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 ) ]] || - [[ ( $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 - return 0 - fi - - # if our source asteroid is further to the right thank our sink, then to stand half a chance - # of being in the way, the blocker has to be x > $sink_x and x < source_x - # apply the same rule for y - # reverse the direction and do the same checks. - # So if source_x < sink_x then blocker_x has to be < sink_x > source_x - if [ $source_x -gt $sink_x -a $blocker_x -gt $sink_x -a $blocker_x -lt $source_x ] || - [ $source_y -gt $sink_y -a $blocker_y -gt $sink_y -a $blocker_y -lt $source_y ] || - [ $source_x -lt $sink_x -a $blocker_x -lt $sink_x -a $blocker_x -gt $source_x ] || - [ $source_y -lt $sink_y -a $blocker_y -lt $sink_y -a $blocker_y -gt $source_y ]; then - return 0 - fi - - return 1 -} - -keep_going() { - local cur_x=$1 - local cur_y=$2 - local minstep_x=$3 - local minstep_y=$4 - local ast_x=$5 - local ast_y=$6 - - if [ $minstep_x -lt 0 -a $cur_x -lt $ast_x ] || - [ $minstep_y -lt 0 -a $cur_y -lt $ast_y ] || - [ $minstep_x -gt 0 -a $cur_x -gt $ast_x ] || - [ $minstep_y -gt 0 -a $cur_y -gt $ast_y ]; then - $debug_line "keepgoing finished" - return 1 - fi - - return 0 -} - -# 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 -asteroid_loop=("${!asteroids[@]}") -index=0 -start_count=${#asteroids[@]} -for asteroid in ${asteroid_loop[@]}; do - ast1_x=${asteroid%,*} - ast1_y=${asteroid#*,} - cur_count=${#asteroid_loop[@]} - percent=$((100-(100*$cur_count / $start_count))) - - $debug_line "Doing fast checks" - fast_checks $asteroid - $debug_line "Running main loops" - +# lets be silly and use the stuff from zap to build the lists instead +best_count=0 +best_ast= +cur_ast_num=0 +total_asts=${#asteroids[@]} +for asteroid in ${!asteroids[@]}; do + percent=$((100*$cur_ast_num / $total_asts)) printf '\r%02d%% complete' $percent - for asteroid2 in ${asteroid_loop[@]}; do - if [ $asteroid2 == $asteroid ]; then - continue - fi - ast2_x=${asteroid2%,*} - ast2_y=${asteroid2#*,} - vector1=$(get_vector $ast1_x $ast1_y $ast2_x $ast2_y) - - # if we've got a cached can't see, use it to continue the loop - if [ ${cantsee[$asteroid,$asteroid2]+a} ]; then + # get a full list of angles for each other asteroid + declare -A asteroid_angles + for asteroid2 in ${!asteroids[@]}; do + if [ $asteroid == $asteroid2 ]; then continue fi - if ! [ ${cansee[$asteroid,$asteroid2]+a} ]; then # we already calculated this - for asteroid3 in "${!asteroids[@]}"; do - if [ $asteroid == $asteroid3 ] || [ $asteroid2 == $asteroid3 ]; then - continue - fi - ast3_x=${asteroid3%,*} - ast3_y=${asteroid3#*,} - - # if this asteroid can't be in the way, continue the loop - if ! possibly_in_way $ast1_x $ast1_y $ast2_x $ast2_y $ast3_x $ast3_y; then - $debug_line "Apparently $ast1_x $ast1_y to $ast2_x $ast2_y cannot be blocked by $ast3_x $ast3_y" - continue - fi - - $debug_line "Checking for collision between $asteroid and $asteroid2 by $asteroid3" - vector2=$(get_vector $ast1_x $ast1_y $ast3_x $ast3_y) - if ( check_collision $vector1 $vector2 ); then - # path is blocked go on to next in outer loop - $debug_line "We hit $asteroid3 when looking for $asteroid2 from $asteroid" - cantsee[$asteroid2,$asteroid]=1 - cantsee[$asteroid,$asteroid2]=1 - continue 2 - fi - done - fi - # nothing blocked the view of asteroid2 from asteroid1 - $debug_line "Apparently $asteroid can see $asteroid2" - 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 + angle=$(get_angle $asteroid $asteroid2) + asteroid_angles[$angle]=1 done - unset asteroid_loop[$index] - index=$((index+1)) -done -echo -n $'\r'" "$'\r' - -best_ast= -best_count=0 -for asteroid in ${!asteroids[@]}; do - count=${asteroids[$asteroid]} + count=${#asteroid_angles[@]} if [ $count -gt $best_count ]; then best_count=$count best_ast=$asteroid fi - $debug_line "$asteroid: ${asteroids[$asteroid]}" + asteroid[$asteroid]=${#asteroid_angles[@]} + unset asteroid_angles + cur_ast_num=$((cur_ast_num+1)) done +printf '\r \r' -echo "Best asteroid: $best_ast which can see $best_count asteroids" - -#echo -n "+" -#printf "%.0s-" $(seq 1 $width) -#echo "+" -## redraw the board with numbers -#for (( a=0; a<$ln; a++ )); do -# echo -n "|" -# for (( b=0; b<$width; b++ )); do -# if [ ${asteroids[$b,$a]+a} ]; then -# echo -n ${asteroids[$b,$a]} -# else -# echo -n "." -# fi -# done -# echo "|" -#done -#echo -n "+" -#printf "%.0s-" $(seq 1 $width) -#echo "+" +echo "Found $best_ast with $best_count asteroids" +exit 0 diff --git a/day10/common.sh b/day10/common.sh index db5c6b9..4f961b6 100644 --- a/day10/common.sh +++ b/day10/common.sh @@ -1,3 +1,42 @@ +declare -A asteroids +declare __asteroids_grid_height +declare __asteroids_grid_width + +get_grid_height() { + echo $__asteroids_grid_height +} + +get_grid_width() { + echo $__asteroids_grid_width +} + +read_file() { + filename="$1" + + if [ ! -e "$filename" ]; then + echo "Couldn't find file: $filename" + exit 2 + fi + + exec 3<"$filename" + unset asteroids + declare -g -A asteroids + + __asteroids_grid_height=0 + __asteroids_grid_width=0 + + while read -u 3 line; do + __asteroids_grid_width=${#line} + for (( a=0; a<${#line}; a++ )); do + char=${line:$a:1} + if [ "${char}" == "#" ]; then + asteroids[$a,$__asteroids_grid_height]=0 + fi + done + __asteroids_grid_height=$((__asteroids_grid_height+1)) + done +} + get_vector() { local x1=$1 local y1=$2 @@ -22,4 +61,68 @@ get_asteroids_vector() { get_vector $x1 $y1 $x2 $y2 } +get_angle() { + local a1=$1 + local a2=$2 + + local vector=$(get_asteroids_vector $a1 $a2) + local move_x=${vector%,*} + local move_y=${vector#*,} + + val="$(calc -p 'round(((90 + ((180/pi()) * atan2('$move_y','$move_x'))) % 360) * 100000000) / 100000000')" + echo "$val" +} + +get_closest_asteroid() { + local asteroids=$1 + + local IFS="|" + local min_dist=-1 + local nearest_asteroid= + local x=0 + local y=0 + + for asteroid in $asteroids; do + vector=$(get_asteroids_vector $our_asteroid $asteroid) + x=${vector%,*} + y=${vector#*,} + # use absolute numbers for length calculation! + x=${x#-} + y=${y#-} + + distance=$((x+y)) + if [ $distance -lt $min_dist -o $min_dist -eq -1 ]; then + min_dist=$distance + nearest_asteroid=$asteroid + fi + done + + echo $nearest_asteroid +} + +remove_asteroid_from_string() { + local asteroids=$1 + local asteroid=$2 + local new_asteroids= + local a + + local IFS="|" + local seperator="" + + for a in $asteroids; do + if [ "$a" != "$asteroid" ]; then + new_asteroids+="${seperator}$a" + fi + if [ "x$new_asteroids" != "x" ]; then + seperator="|" + fi + done + + echo "$new_asteroids" +} + +get_angles_list() { + local -n __asteroid_angles=$1 + printf "%s\n" "${!__asteroid_angles[@]}" | sort -g +} diff --git a/day10/summary.txt b/day10/summary.txt new file mode 100644 index 0000000..0bba1dc --- /dev/null +++ b/day10/summary.txt @@ -0,0 +1,167 @@ +--- Day 10: Monitoring Station --- +You fly into the asteroid belt and reach the Ceres monitoring station. The Elves here have an emergency: they're having trouble tracking all of the asteroids and can't be sure they're safe. + +The Elves would like to build a new monitoring station in a nearby area of space; they hand you a map of all of the asteroids in that region (your puzzle input). + +The map indicates whether each position is empty (.) or contains an asteroid (#). The asteroids are much smaller than they appear on the map, and every asteroid is exactly in the center of its marked position. The asteroids can be described with X,Y coordinates where X is the distance from the left edge and Y is the distance from the top edge (so the top-left corner is 0,0 and the position immediately to its right is 1,0). + +Your job is to figure out which asteroid would be the best place to build a new monitoring station. A monitoring station can detect any asteroid to which it has direct line of sight - that is, there cannot be another asteroid exactly between them. This line of sight can be at any angle, not just lines aligned to the grid or diagonally. The best location is the asteroid that can detect the largest number of other asteroids. + +For example, consider the following map: + +.#..# +..... +##### +....# +...## +The best location for a new monitoring station on this map is the highlighted asteroid at 3,4 because it can detect 8 asteroids, more than any other location. (The only asteroid it cannot detect is the one at 1,0; its view of this asteroid is blocked by the asteroid at 2,2.) All other asteroids are worse locations; they can detect 7 or fewer other asteroids. Here is the number of other asteroids a monitoring station on each asteroid could detect: + +.7..7 +..... +67775 +....7 +...87 +Here is an asteroid (#) and some examples of the ways its line of sight might be blocked. If there were another asteroid at the location of a capital letter, the locations marked with the corresponding lowercase letter would be blocked and could not be detected: + +#......... +...A...... +...B..a... +.EDCG....a +..F.c.b... +.....c.... +..efd.c.gb +.......c.. +....f...c. +...e..d..c +Here are some larger examples: + +Best is 5,8 with 33 other asteroids detected: + +......#.#. +#..#.#.... +..#######. +.#.#.###.. +.#..#..... +..#....#.# +#..#....#. +.##.#..### +##...#..#. +.#....#### +Best is 1,2 with 35 other asteroids detected: + +#.#...#.#. +.###....#. +.#....#... +##.#.#.#.# +....#.#.#. +.##..###.# +..#...##.. +..##....## +......#... +.####.###. +Best is 6,3 with 41 other asteroids detected: + +.#..#..### +####.###.# +....###.#. +..###.##.# +##.##.#.#. +....###..# +..#.#..#.# +#..#.#.### +.##...##.# +.....#.#.. +Best is 11,13 with 210 other asteroids detected: + +.#..##.###...####### +##.############..##. +.#.######.########.# +.###.#######.####.#. +#####.##.#.##.###.## +..#####..#.######### +#################### +#.####....###.#.#.## +##.################# +#####.##.###..####.. +..######..##.####### +####.##.####...##..# +.#####..#.######.### +##...#.##########... +#.##########.####### +.####.#.###.###.#.## +....##.##.###..##### +.#.#.###########.### +#.#.#.#####.####.### +###.##.####.##.#..## +Find the best location for a new monitoring station. How many other asteroids can be detected from that location? + +Your puzzle answer was 303. + +--- Part Two --- +Once you give them the coordinates, the Elves quickly deploy an Instant Monitoring Station to the location and discover the worst: there are simply too many asteroids. + +The only solution is complete vaporization by giant laser. + +Fortunately, in addition to an asteroid scanner, the new monitoring station also comes equipped with a giant rotating laser perfect for vaporizing asteroids. The laser starts by pointing up and always rotates clockwise, vaporizing any asteroid it hits. + +If multiple asteroids are exactly in line with the station, the laser only has enough power to vaporize one of them before continuing its rotation. In other words, the same asteroids that can be detected can be vaporized, but if vaporizing one asteroid makes another one detectable, the newly-detected asteroid won't be vaporized until the laser has returned to the same position by rotating a full 360 degrees. + +For example, consider the following map, where the asteroid with the new monitoring station (and laser) is marked X: + +.#....#####...#.. +##...##.#####..## +##...#...#.#####. +..#.....X...###.. +..#.#.....#....## +The first nine asteroids to get vaporized, in order, would be: + +.#....###24...#.. +##...##.13#67..9# +##...#...5.8####. +..#.....X...###.. +..#.#.....#....## +Note that some asteroids (the ones behind the asteroids marked 1, 5, and 7) won't have a chance to be vaporized until the next full rotation. The laser continues rotating; the next nine to be vaporized are: + +.#....###.....#.. +##...##...#.....# +##...#......1234. +..#.....X...5##.. +..#.9.....8....76 +The next nine to be vaporized are then: + +.8....###.....#.. +56...9#...#.....# +34...7........... +..2.....X....##.. +..1.............. +Finally, the laser completes its first full rotation (1 through 3), a second rotation (4 through 8), and vaporizes the last asteroid (9) partway through its third rotation: + +......234.....6.. +......1...5.....7 +................. +........X....89.. +................. +In the large example above (the one with the best monitoring station location at 11,13): + +The 1st asteroid to be vaporized is at 11,12. +The 2nd asteroid to be vaporized is at 12,1. +The 3rd asteroid to be vaporized is at 12,2. +The 10th asteroid to be vaporized is at 12,8. +The 20th asteroid to be vaporized is at 16,0. +The 50th asteroid to be vaporized is at 16,9. +The 100th asteroid to be vaporized is at 10,16. +The 199th asteroid to be vaporized is at 9,6. +The 200th asteroid to be vaporized is at 8,2. +The 201st asteroid to be vaporized is at 10,9. +The 299th and final asteroid to be vaporized is at 11,1. +The Elves are placing bets on which will be the 200th asteroid to be vaporized. Win the bet by determining which asteroid that will be; what do you get if you multiply its X coordinate by 100 and then add its Y coordinate? (For example, 8,2 becomes 802.) + +Your puzzle answer was 408. + +Both parts of this puzzle are complete! They provide two gold stars: ** + +At this point, you should return to your Advent calendar and try another puzzle. + +If you still want to see it, you can get your puzzle input. + +You can also [Share] this puzzle. diff --git a/day10/zap.sh b/day10/zap.sh old mode 100644 new mode 100755 index eae98d2..74838a9 --- a/day10/zap.sh +++ b/day10/zap.sh @@ -8,79 +8,7 @@ our_asteroid=26,29 declare -A asteroids declare -A asteroid_angles -exec 3