aoc/year2019/day10.rs
1//! # Monitoring Station
2//!
3//! Integer only solution, avoiding floating point or trigonometric lookups such as [`atan2`].
4//!
5//! ## Part One
6//!
7//! We compare each pair of points, first subtracting the current asteroid to get a relative vector.
8//! Since all coordinates are integers we can check for multiple points on the same line by
9//! reducing the vector by its [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor).
10//! For example, looking from the origin `(0, 0)`, the points `(3, 5)`, `(6, 10)` and `(21, 35)`
11//! are all on the same line, with gcds of 1, 2 and 7 respectively.
12//!
13//! For each point we build a set of previously seen values. Since we can see at most one asteroid
14//! in a given direction, if a vector is already in the set then we ignore it. The final size of
15//! the set is the number of visible asteroids.
16//!
17//! To speeds things up a little, we rely on the fact that if asteroid `a` can see `b`, then `b`
18//! can see `a`. The solution is still `O(n²)` complexity but we reduce the work by half. This
19//! works by implicitly processing asteroids in the same order as the input, from left to right and
20//! top to bottom, so that nearest asteroids are always encountered first.
21//!
22//! ## Part Two
23//!
24//! The key insight is that we only need the *relative* angle between two vectors to sort them
25//! in clockwise order. The [vector cross product](https://en.wikipedia.org/wiki/Cross_product)
26//! of `a` and `b` can be defined as `|a||b|sinθ` where `θ` is the angle between the vectors.
27//! `θ` will be negative if anti-clockwise, zero if on the same line or positive if clockwise.
28//!
29//! This works for angles up to 90°. To handle the complete 360° rotation, we first order points
30//! by [quadrant](https://en.wikipedia.org/wiki/Quadrant_(plane_geometry)) then by relative angle.
31//!
32//! Finally we also order points from nearest to furthest, so that the total ordering is:
33//! 1. Quadrant
34//! 2. Clockwise angle
35//! 3. Distance
36//!
37//! This results in a something like (where letters indicate angle and numbers indicate distance):
38//!
39//! `a1 a2 a3 b1 c1 c2 c3 c4 d1 d2`
40//!
41//! We want to process asteroids in the order:
42//!
43//! `a1 b1 c1 d1 a2 c2 d2 a3 c3 c4`
44//!
45//! We do this by first numbering the position within each group, then numbering the group and
46//! sorting a second time in this order.
47//!
48//! [`atan2`]: f64::atan2
49use crate::util::grid::*;
50use crate::util::math::*;
51use crate::util::point::*;
52use std::cmp::Ordering;
53
54type Input = (i32, i32);
55
56pub fn parse(input: &str) -> Input {
57 let grid = Grid::parse(input);
58 let width = grid.width;
59 let height = grid.height;
60
61 // Convert asteroids to `Point` objects.
62 let mut points = Vec::new();
63
64 for y in 0..height {
65 for x in 0..width {
66 let point = Point::new(x, y);
67 if grid[point] == b'#' {
68 points.push(point);
69 }
70 }
71 }
72
73 // Find asteroid with the highest visibility.
74 let mut seen = Grid::new(2 * width, 2 * height, 0);
75 let mut cache = grid.same_size_with(0);
76 let mut visible = vec![0; points.len()];
77 let mut max_visible = 0;
78 let mut max_index = 0;
79
80 for i in 0..(points.len() - 1) {
81 for j in (i + 1)..points.len() {
82 let delta = points[j] - points[i];
83 let key = Point::new(delta.x.abs(), delta.y.abs());
84
85 if cache[key] == 0 {
86 cache[key] = key.x.gcd(key.y);
87 }
88 let gcd = cache[key];
89
90 // Key insight is that points on the same line are integer multiples of each other.
91 let adjusted = Point::new(delta.x / gcd, delta.y / gcd);
92
93 // This works as the points are in order from left to right and top to bottom,
94 // so we process points from nearest to furthest.
95 let index = Point::new(width + adjusted.x, height + adjusted.y);
96
97 if seen[index] <= i {
98 seen[index] = i + 1;
99 visible[i] += 1;
100 visible[j] += 1;
101 }
102 }
103
104 if visible[i] > max_visible {
105 max_visible = visible[i];
106 max_index = i;
107 }
108 }
109
110 // Remove our new base of operations, then sort remaining asteroids in clockwise order to
111 // group by angle.
112 let station = points.swap_remove(max_index);
113 points.iter_mut().for_each(|p| *p -= station);
114 points.sort_unstable_by(|&a, &b| clockwise(a, b));
115
116 // Sort asteroids a second time, first by order within the group, then the group's order.
117 let mut groups = Vec::with_capacity(points.len());
118 let mut first = 0;
119 let mut second = 0;
120
121 groups.push((first, second, 0));
122
123 for i in 1..points.len() {
124 if angle(points[i], points[i - 1]) == Ordering::Greater {
125 first = 0;
126 second += 1;
127 } else {
128 first += 1;
129 }
130 groups.push((first, second, i));
131 }
132
133 groups.sort_unstable();
134
135 // The 200th asteroid is at index 199.
136 let target = station + points[groups[199].2];
137 let result = 100 * target.x + target.y;
138
139 (max_visible, result)
140}
141
142pub fn part1(input: &Input) -> i32 {
143 input.0
144}
145
146pub fn part2(input: &Input) -> i32 {
147 input.1
148}
149
150/// The [`then`] method chains [`Ordering`] results.
151///
152/// [`then`]: Ordering::then
153fn clockwise(point: Point, other: Point) -> Ordering {
154 quadrant(point)
155 .cmp(&quadrant(other))
156 .then(angle(point, other))
157 .then(distance(point).cmp(&distance(other)))
158}
159
160/// Divide points into one of four quadrants. For points exactly on an axis, for example (1, 0)
161/// or (-5, 0) we can choose either adjacent quadrant as long as we're consistent.
162fn quadrant(point: Point) -> i32 {
163 match (point.x >= 0, point.y >= 0) {
164 (true, false) => 0,
165 (true, true) => 1,
166 (false, true) => 2,
167 (false, false) => 3,
168 }
169}
170
171/// Positive if clockwise, zero if on the same line, negative if anti-clockwise.
172fn angle(point: Point, other: Point) -> Ordering {
173 (other.x * point.y).cmp(&(other.y * point.x))
174}
175
176/// Euclidean distance squared. No need to take square root since we're only interested
177/// in the *relative* distance.
178fn distance(point: Point) -> i32 {
179 point.x * point.x + point.y * point.y
180}