aoc/year2019/day10.rs
1//! # Monitoring Station
2//!
3//! Integer only solution, avoiding floating point or trignometric 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::math::*;
50use crate::util::point::*;
51use std::cmp::Ordering;
52
53type Input = (i32, i32);
54
55pub fn parse(input: &str) -> Input {
56 let raw: Vec<_> = input.lines().map(str::as_bytes).collect();
57 let width = raw[0].len() as i32;
58 let height = raw.len() as i32;
59
60 // Convert asteroids to `Point` objects.
61 let mut points = Vec::new();
62
63 for (y, row) in raw.iter().enumerate() {
64 for (x, &col) in row.iter().enumerate() {
65 if col == b'#' {
66 points.push(Point::new(x as i32, y as i32));
67 }
68 }
69 }
70
71 // Find asteroid with the highest visibility.
72 let mut visible = vec![0; points.len()];
73 let mut seen = vec![0; (4 * width * height) as usize];
74 let mut max_visible = 0;
75 let mut max_index = 0;
76
77 for i in 0..(points.len() - 1) {
78 for j in (i + 1)..points.len() {
79 let mut delta = points[j] - points[i];
80
81 // Key insight is that points on the same line are integer multiples of each other.
82 let factor = delta.x.gcd(delta.y).abs();
83 delta.x /= factor;
84 delta.y /= factor;
85
86 // This works as the points are in order from left to right and top to bottom,
87 // so we process points from nearest to furthest.
88 let index = (2 * width) * (height + delta.y) + (width + delta.x);
89
90 if seen[index as usize] <= i {
91 seen[index as usize] = i + 1;
92 visible[i] += 1;
93 visible[j] += 1;
94 }
95 }
96
97 if visible[i] > max_visible {
98 max_visible = visible[i];
99 max_index = i;
100 }
101 }
102
103 // Remove our new base of operations, then sort remaining asteroids in clockwise order to
104 // group by angle.
105 let station = points.swap_remove(max_index);
106 points.iter_mut().for_each(|p| *p -= station);
107 points.sort_unstable_by(|&a, &b| clockwise(a, b));
108
109 // Sort asteroids a second time, first by order within the group, then the group's order.
110 let mut groups = Vec::with_capacity(points.len());
111 let mut first = 0;
112 let mut second = 0;
113
114 groups.push((first, second, 0));
115
116 for i in 1..points.len() {
117 if angle(points[i], points[i - 1]) == Ordering::Greater {
118 first = 0;
119 second += 1;
120 } else {
121 first += 1;
122 }
123 groups.push((first, second, i));
124 }
125
126 groups.sort_unstable();
127
128 // The 200th asteroid is at index 199.
129 let target = station + points[groups[199].2];
130 let result = 100 * target.x + target.y;
131
132 (max_visible, result)
133}
134
135pub fn part1(input: &Input) -> i32 {
136 input.0
137}
138
139pub fn part2(input: &Input) -> i32 {
140 input.1
141}
142
143/// The [`then`] method chains [`Ordering`] results.
144///
145/// [`then`]: Ordering::then
146fn clockwise(point: Point, other: Point) -> Ordering {
147 quadrant(point)
148 .cmp(&quadrant(other))
149 .then(angle(point, other))
150 .then(distance(point).cmp(&distance(other)))
151}
152
153/// Divide points into one of four quadrants. For points exactly on an axis, for example (1, 0)
154/// or (-5, 0) we can choose either adjacent quadrant as long as we're consistent.
155fn quadrant(point: Point) -> i32 {
156 match (point.x >= 0, point.y >= 0) {
157 (true, false) => 0,
158 (true, true) => 1,
159 (false, true) => 2,
160 (false, false) => 3,
161 }
162}
163
164/// Positive if clockwise, zero if on the same line, negative if anti-clockwise.
165fn angle(point: Point, other: Point) -> Ordering {
166 (other.x * point.y).cmp(&(other.y * point.x))
167}
168
169/// Euclidean distance squared. No need to take square root since we're only interested
170/// in the *relative* distance.
171fn distance(point: Point) -> i32 {
172 point.x * point.x + point.y * point.y
173}