From 525034dbd3d7dd42c13eb34a1a89eb71e0a7a2e4 Mon Sep 17 00:00:00 2001 From: rpidanny Date: Sun, 24 Dec 2023 23:16:42 +0100 Subject: [PATCH] feat(2019): day 10 - Monitoring Station --- 2019/Day10/README.md | 201 ++++++++++++++++++++++++++++++- 2019/Day10/helpers.py | 70 ----------- 2019/Day10/main.py | 1 - 2019/Day10/monitoring_station.py | 75 ++++++++++++ 2019/Day10/solutions.py | 9 +- 2019/Day10/test.input.2.txt | 5 + 2019/Day10/test_solutions.py | 8 +- 7 files changed, 287 insertions(+), 82 deletions(-) delete mode 100644 2019/Day10/helpers.py create mode 100644 2019/Day10/monitoring_station.py create mode 100644 2019/Day10/test.input.2.txt diff --git a/2019/Day10/README.md b/2019/Day10/README.md index 98e93b9..3412af2 100644 --- a/2019/Day10/README.md +++ b/2019/Day10/README.md @@ -1,3 +1,200 @@ -# --- Day 0: Template --- +# [--- Day 10: Monitoring Station ---](https://adventofcode.com/2019/day/10) -To be used as template for the other days. +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: + +```txt +.#..# +..... + +##### + +....# +...## +``` + +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: + +```txt +.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: + +```txt +#......... +...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: + +```txt +......#.#. +#..#.#.... +..#######. +.#.#.###.. +.#..#..... +..#....#.# +#..#....#. +.##.#..### +##...#..#. +.#....#### +``` + +Best is `1`,`2` with `35` other asteroids detected: + +```txt +#.#...#.#. +.###....#. +.#....#... +##.#.#.#.# +....#.#.#. +.##..###.# +..#...##.. +..##....## +......#... +.####.###. +``` + +Best is `6`,`3` with `41` other asteroids detected: + +```txt +.#..#..### +####.###.# +....###.#. +..###.##.# +##.##.#.#. +....###..# +..#.#..#.# +#..#.#.### +.##...##.# +.....#.#.. +``` + +Best is `11`,`13` with `210` other asteroids detected: + +```txt +.#..##.###...####### +##.############..##. +.#.######.########.# +.###.#######.####.#. +#####.##.#.##.###.## +..#####..#.######### +#################### +#.####....###.#.#.## +##.################# +#####.##.###..####.. +..######..##.####### +####.##.####...##..# +.#####..#.######.### +##...#.##########... +#.##########.####### +.####.#.###.###.#.## +....##.##.###..##### +.#.#.###########.### +#.#.#.#####.####.### +###.##.####.##.#..## +``` + +Find the best location for a new monitoring station. **How many other asteroids can be detected from that location?** + +> Your puzzle answer was `329`. + +## --- 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`: + +```txt +.#....#####...#.. +##...##.#####..## +##...#...#.#####. +..#.....X...###.. +..#.#.....#....## +``` + +The first nine asteroids to get vaporized, in order, would be: + +```txt +.#....###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: + +```txt +.#....###.....#.. +##...##...#.....# +##...#......1234. +..#.....X...5##.. +..#.9.....8....76 +``` + +The next nine to be vaporized are then: + +```txt +.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: + +```txt +......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 `512`. diff --git a/2019/Day10/helpers.py b/2019/Day10/helpers.py deleted file mode 100644 index b396fc7..0000000 --- a/2019/Day10/helpers.py +++ /dev/null @@ -1,70 +0,0 @@ -import math - - -class MonitoringStation: - def __init__(self, input): - self.__grid = input - self.__astroid_locations = self.__get_astroid_locations() - - def __get_distance_between_astroids( - self, astroid1: [int, int], astroid2: [int, int] - ) -> int: - x1, y1 = astroid1 - x2, y2 = astroid2 - return math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) - - def __get_astroid_locations(self) -> list[int, int]: - astroid_locations = [] - for row in range(len(self.__grid)): - for col in range(len(self.__grid[row])): - if self.__grid[row][col] == "#": - astroid_locations.append([col, row]) - return astroid_locations - - def __get_angle_between_astroids( - self, astroid1: [int, int], astroid2: [int, int] - ) -> int: - x1, y1 = astroid1 - x2, y2 = astroid2 - - angle = math.atan2(y2 - y1, x2 - x1) - angle_degrees = math.degrees(angle) - return angle_degrees - - def get_max_viewable_astroids(self) -> [int, int, int, dict]: - max_viewable_astroids = 0 - best_astroid = None - view_points = {} - - for astroid in self.__astroid_locations: - degrees = {} - for other_astroid in self.__astroid_locations: - if astroid == other_astroid: - continue - a = self.__get_angle_between_astroids(astroid, other_astroid) + 90 - if a in degrees: - degrees[a].append(other_astroid) - else: - degrees[a] = [other_astroid] - if (len(degrees)) > max_viewable_astroids: - max_viewable_astroids = len(degrees) - best_astroid = astroid - view_points = degrees - return [best_astroid[1], best_astroid[0], max_viewable_astroids, view_points] - - def get_200th_astroid(self) -> [int, int]: - _, _, astroid, view_points = self.get_max_viewable_astroids() - - sorted_view_points = sorted( - view_points.items(), key=lambda x: x[0], reverse=True - ) - print(sorted_view_points) - - for i in range(200): - for view_point in sorted_view_points: - if len(view_point[1]) > 0: - astroid = view_point[1].pop(0) - print(astroid) - else: - sorted_view_points.remove(view_point) - break diff --git a/2019/Day10/main.py b/2019/Day10/main.py index b728c56..e8abd93 100644 --- a/2019/Day10/main.py +++ b/2019/Day10/main.py @@ -4,7 +4,6 @@ sys.path.append(".") from solutions import part1, part2 - from utils.inputs import get_inputs from utils.timings import profile_run diff --git a/2019/Day10/monitoring_station.py b/2019/Day10/monitoring_station.py new file mode 100644 index 0000000..227ef1c --- /dev/null +++ b/2019/Day10/monitoring_station.py @@ -0,0 +1,75 @@ +import math +from collections import defaultdict + + +class MonitoringStation: + def __init__(self, input): + self.__grid = input + self.__asteroids = self.__get_asteroids() + + def __get_distance_between_asteroids(self, a1: [int, int], a2: [int, int]) -> int: + x1, y1 = a1 + x2, y2 = a2 + return math.hypot(x2 - x1, y2 - y1) + + def __get_asteroids(self) -> list[int, int]: + return [ + [col, row] + for row, row_data in enumerate(self.__grid) + for col, value in enumerate(row_data) + if value == "#" + ] + + def __get_angle_between_asteroids(self, a1: [int, int], a2: [int, int]) -> int: + x1, y1 = a1 + x2, y2 = a2 + + angle = ( + math.degrees(math.atan2(y2 - y1, x2 - x1)) + 90 + ) # Adjust for the fact that 0 degrees is up + + # Ensure the angle is not negative + return angle + 360 if angle < 0 else angle + + def get_max_viewable_asteroids(self) -> [[int, int], dict]: + best_asteroid = None + asteroid_angles = defaultdict(list) + + for asteroid in self.__asteroids: + degrees = defaultdict(list) + for other_asteroid in self.__asteroids: + if asteroid == other_asteroid: + continue + a = self.__get_angle_between_asteroids(asteroid, other_asteroid) + degrees[a].append(other_asteroid) + if (len(degrees)) > len(asteroid_angles): + best_asteroid = asteroid + asteroid_angles = degrees + + return [best_asteroid, asteroid_angles] + + def get_nth_vaporized_asteroid(self, n: int) -> [int, int]: + asteroid, asteroid_angles = self.get_max_viewable_asteroids() + + # Sort the view points by angle so we can iterate through them in order of angle + sorted_asteroid_angles = sorted(asteroid_angles.items(), key=lambda x: x[0]) + + # Sort the asteroids at each angle by distance from the station + map( + lambda x: x[1].sort( + key=lambda y: self.__get_distance_between_asteroids(y, asteroid), + reverse=True, # desc so we can pop from the end + ), + sorted_asteroid_angles, + ) + + vaporized_asteroids = [] + + while len(vaporized_asteroids) < n: + for asteroid in sorted_asteroid_angles: + if len(asteroid[1]) > 0: + vaporized_asteroids.append(asteroid[1].pop()) + if len(vaporized_asteroids) == n: + break + + return vaporized_asteroids[n - 1] diff --git a/2019/Day10/solutions.py b/2019/Day10/solutions.py index ecb8053..6e7f17e 100644 --- a/2019/Day10/solutions.py +++ b/2019/Day10/solutions.py @@ -1,11 +1,12 @@ -from helpers import MonitoringStation +from monitoring_station import MonitoringStation def part1(inputs: list[str]) -> int: ms = MonitoringStation(inputs) - return ms.get_max_viewable_astroids()[2] + return len(ms.get_max_viewable_asteroids()[1]) -def part2(inputs: list[str]) -> int: +def part2(inputs: list[str], n=200) -> int: ms = MonitoringStation(inputs) - return ms.get_200th_astroid() + x, y = ms.get_nth_vaporized_asteroid(n) + return x * 100 + y diff --git a/2019/Day10/test.input.2.txt b/2019/Day10/test.input.2.txt new file mode 100644 index 0000000..18ae03f --- /dev/null +++ b/2019/Day10/test.input.2.txt @@ -0,0 +1,5 @@ +.#....#####...#.. +##...##.#####..## +##...#...#.#####. +..#.....#...###.. +..#.#.....#....## \ No newline at end of file diff --git a/2019/Day10/test_solutions.py b/2019/Day10/test_solutions.py index c8f6d80..24cb4d7 100644 --- a/2019/Day10/test_solutions.py +++ b/2019/Day10/test_solutions.py @@ -1,6 +1,5 @@ import os -import pytest from solutions import part1, part2 from utils.inputs import get_inputs @@ -8,17 +7,16 @@ input = get_inputs(f"{current_dir}/input.txt") test_input = get_inputs(f"{current_dir}/test.input.txt") +test_input_2 = get_inputs(f"{current_dir}/test.input.2.txt") class TestPart1: def test_with_test_data_1(self): assert part1([".#..#", ".....", "#####", "....#", "...##"]) == 8 - @pytest.mark.skip(reason="not implemented") def test_with_test_data_2(self): assert part1(test_input) == 210 - @pytest.mark.skip(reason="not implemented") def test_with_real_data(self): assert part1(input) == 329 @@ -26,7 +24,7 @@ def test_with_real_data(self): class TestPart2: def test_with_test_data(self): assert part2(test_input) == 802 + assert part2(test_input_2, 9) == 1501 - @pytest.mark.skip(reason="not implemented") def test_with_real_data(self): - assert part2(input) == 2 + assert part2(input) == 512