aoc/year2023/day24.rs
1//! # Never Tell Me The Odds
2//!
3//! ## Part One
4//!
5//! We find the intersection for each pair of hailstones by solving a pair of linear simultaneous
6//! equations in 2 unknowns:
7//!
8//! * `a` and `g` are the x positions of the pair of hailstones.
9//! * `b` and `h` are the y positions.
10//! * `d` and `j` are the x velocities.
11//! * `e` and `k` are the x velocities.
12//! * Let `t` and `u` be the times that the first and second hailstone respectively are at the
13//! intersection point.
14//!
15//! Then we can write:
16//!
17//! * `a + dt = g + ju` => `dt - ju = g - a`
18//! * `b + et = h + ku` => `et - ku = h - b`
19//!
20//! In matrix form:
21//!
22//! ```none
23//! | d -j ||u| = | g - a |
24//! | e -k ||t| | h - b |
25//! ```
26//!
27//! Solve by finding the inverse of the 2x2 matrix and premultiplying both sides. The inverse is:
28//!
29//! ```none
30//! ______1______ | -k j |
31//! d(-k) - (-j)e | -e d |
32//! ```
33//!
34//! Then we check that both times are non-negative and that the intersection point is inside the
35//! target area.
36//!
37//! ## Part Two
38//!
39//! First we choose 3 arbitrary hailstones. Then we subtract the position and velocity of the
40//! the first to make the other two relative.
41//!
42//! The two hailstones will intercept a line leaving the origin. We can determine this line
43//! by intersection the two planes that the hailstone's velocity lie in. These planes are
44//! defined by a normal vector orthogonal to the plane.
45//!
46//! This normal vector is the [cross product](https://en.wikipedia.org/wiki/Cross_product) of
47//! any two vector that lie in the plane, in this case the velocity and also the vector from the
48//! origin to the starting location of the hailstone.
49//!
50//! The direction but not necessarily the magnitude of the velocity is then given by the cross
51//! product of the two normals.
52//!
53//! Given the rock direction we can calculate the times that the two hailstones are intercepted
54//! then use this to determine the original position of the rock, as long as the two times
55//! are different.
56use crate::util::iter::*;
57use crate::util::math::*;
58use crate::util::parse::*;
59use std::ops::RangeInclusive;
60
61const RANGE: RangeInclusive<i64> = 200_000_000_000_000..=400_000_000_000_000;
62
63#[derive(Clone, Copy)]
64struct Vector {
65 x: i128,
66 y: i128,
67 z: i128,
68}
69
70/// 3D vector implementation.
71impl Vector {
72 fn add(self, other: Self) -> Self {
73 let x = self.x + other.x;
74 let y = self.y + other.y;
75 let z = self.z + other.z;
76 Vector { x, y, z }
77 }
78
79 fn sub(self, other: Self) -> Self {
80 let x = self.x - other.x;
81 let y = self.y - other.y;
82 let z = self.z - other.z;
83 Vector { x, y, z }
84 }
85
86 fn cross(self, other: Self) -> Self {
87 let x = self.y * other.z - self.z * other.y;
88 let y = self.z * other.x - self.x * other.z;
89 let z = self.x * other.y - self.y * other.x;
90 Vector { x, y, z }
91 }
92
93 // Changes the magnitude (but not direction) of the vector.
94 // Prevents numeric overflow.
95 fn gcd(self) -> Self {
96 let gcd = self.x.gcd(self.y).gcd(self.z);
97 let x = self.x / gcd;
98 let y = self.y / gcd;
99 let z = self.z / gcd;
100 Vector { x, y, z }
101 }
102
103 fn sum(self) -> i128 {
104 self.x + self.y + self.z
105 }
106}
107
108pub fn parse(input: &str) -> Vec<[i64; 6]> {
109 input.iter_signed().chunk::<6>().collect()
110}
111
112pub fn part1(input: &[[i64; 6]]) -> u32 {
113 let mut result = 0;
114
115 for (index, &[a, b, _, c, d, _]) in input[1..].iter().enumerate() {
116 for &[e, f, _, g, h, _] in &input[..index + 1] {
117 // If the determinant is zero there is no solution possible
118 // which implies the trajectories are parallel.
119 let determinant = d * g - c * h;
120 if determinant == 0 {
121 continue;
122 }
123
124 // Invert the 2x2 matrix then multiply by the respective columns to find the times.
125 let t = (g * (f - b) - h * (e - a)) / determinant;
126 let u = (c * (f - b) - d * (e - a)) / determinant;
127
128 // We can pick either the first or second hailstone to find the intersection position.
129 let x = a + t * c;
130 let y = b + t * d;
131
132 // Both times must be in the future and the position within the specified area.
133 if t >= 0 && u >= 0 && RANGE.contains(&x) && RANGE.contains(&y) {
134 result += 1;
135 }
136 }
137 }
138
139 result
140}
141
142pub fn part2(input: &[[i64; 6]]) -> i128 {
143 // Calculations need the range of `i128`.
144 let widen = |i: usize| {
145 let [px, py, pz, vx, vy, vz] = input[i].map(|n| n as i128);
146 let p = Vector { x: px, y: py, z: pz };
147 let v = Vector { x: vx, y: vy, z: vz };
148 (p, v)
149 };
150
151 // Take 3 arbitrary hailstones.
152 let (p0, v0) = widen(0);
153 let (p1, v1) = widen(1);
154 let (p2, v2) = widen(2);
155
156 // Subtract the positions and velocities to make them relative.
157 // The first hailstone is stationary at the origin.
158 let p3 = p1.sub(p0);
159 let p4 = p2.sub(p0);
160 let v3 = v1.sub(v0);
161 let v4 = v2.sub(v0);
162
163 // Find the normal to the plane that the second and third hailstones velocity lies in.
164 // This is the cross product of their respective position and velocity.
165 // The cross product `s` of these two vectors is the same direction but not necessarily the
166 // same magnitude of the desired velocity of the rock.
167 // Only the direction is relevant (not the magnitude) so we can normalize the vector by the
168 // GCD of its components in order to prevent numeric overflow.
169 let q = v3.cross(p3).gcd();
170 let r = v4.cross(p4).gcd();
171 let s = q.cross(r).gcd();
172
173 // Find the times when the second and third hailstone intercept this vector.
174 // If the times are different then we can extrapolate the original position of the rock.
175 let t = (p3.y * s.x - p3.x * s.y) / (v3.x * s.y - v3.y * s.x);
176 let u = (p4.y * s.x - p4.x * s.y) / (v4.x * s.y - v4.y * s.x);
177 assert!(t != u);
178
179 // Calculate the original position of the rock, remembering to add the first hailstone's
180 // position to convert back to absolute coordinates.
181 let a = p0.add(p3).sum();
182 let b = p0.add(p4).sum();
183 let c = v3.sub(v4).sum();
184 (u * a - t * b + u * t * c) / (u - t)
185}