Skip to content

Latest commit

 

History

History
347 lines (283 loc) · 14.5 KB

2023-day24-alt.md

File metadata and controls

347 lines (283 loc) · 14.5 KB

AoC 2023 day 24, alternative solution

Here's an entirely different take on day 24.

See 2023-day24.md (or 2023-day24-github.md if viewing on GitHub) for my initial day 24 solution, and 2023-notes.md for all the other days.

Definitions

We'll use the following notation:

  • $\mathbf{pR}$: the thrown rock's position, unknown.
  • $\mathbf{vR}$: the thrown rock's velocity, unknown.
  • $\mathbf{pA}$, $\mathbf{vA}$: position and velocity for an arbitrary hailstone $A$ (likewise $B$, $C$, ...), known.
  • $tA$ time when hailstone $A$ (likewise $B$, $C$, ...) is hit, unknown.

For each of the vectors $\mathbf{v}$, we also write $\mathbf{v}_x$ (likewise $y$, $z$) to refer to a specific component.

Finding the velocity

Here's the list of hailstones in the example:

$$\begin{aligned} &A& \mathbf{pA} &= \begin{pmatrix}19&13&30\end{pmatrix}^T & \mathbf{vA} &= \begin{pmatrix}-2&1&-2\end{pmatrix}^T \\ &B& \mathbf{pB} &= \begin{pmatrix}18&19&22\end{pmatrix}^T & \mathbf{vB} &= \begin{pmatrix}-1&-1&2\end{pmatrix}^T \\ &C& \mathbf{pC} &= \begin{pmatrix}20&25&34\end{pmatrix}^T & \mathbf{vC} &= \begin{pmatrix}-2&-2&4\end{pmatrix}^T \\ &D& \mathbf{pD} &= \begin{pmatrix}12&31&28\end{pmatrix}^T & \mathbf{vD} &= \begin{pmatrix}-1&-2&-1\end{pmatrix}^T \\ &E& \mathbf{pE} &= \begin{pmatrix}20&19&15\end{pmatrix}^T & \mathbf{vE} &= \begin{pmatrix}1&-5&-3\end{pmatrix}^T \end{aligned}$$

There's a bunch of duplicate individual velocity components between the hailstones:

  • $\mathbf{v}_x = -1$: $\mathbf{vB}, \mathbf{vD}$
  • $\mathbf{v}_x = -2$: $\mathbf{vA}, \mathbf{vC}$
  • $\mathbf{v}_y = -2$: $\mathbf{vC}, \mathbf{vD}$
  • $\mathbf{v}_z = -2$: $\mathbf{vA}, \mathbf{vB}$

Let's think about the case where two hailstones have a velocity component in common. For example, in our case both hailstones $B$ and $D$ have $\mathbf{v}_x = -1$.

At time $t=0$, these two hailstones are some distance $d_x(B,D)$ apart:

$$ d_x(B,D) = \left|\mathbf{pD}_x - \mathbf{pB}_x\right| = \left|12 - 18\right| = 6 $$

Since they're moving at the same speed in the X direction, the distance between them along the X axis stays constant. We need to hit both hailstones with a rock we've thrown at some integer velocity. To successfully hit both, its X velocity relative to those hailstones needs to be such that it covers the distance $d_x(B,D)$ in an integer amount of time steps. In other words, $\mathbf{vR}_x - \mathbf{v}_x = \mathbf{vR}_x + 1$ must be a divisor of $d_x(B,D)$, or the negated value of one. Since $d_x(B,D) = 6$, we therefore have:

$$\begin{aligned} \mathbf{vR}_x + 1 &\in {-6, -3, -2, -1, 1, 2, 3, 6} \\ \mathbf{vR}_x &\in {-7, -4, -3, -2, 0, 1, 2, 5} \end{aligned}$$

Independent to that, we also have the same situation for $A, C$, where $\mathbf{vA}_x = \mathbf{vC}_x = -2$. For these two, $d_x(A,C) = \left|\mathbf{pC}_x - \mathbf{pA}_x\right| = 1$. So we know that:

$$\begin{aligned} \mathbf{vR}_x + 2 &\in {-1, 1} \\ \mathbf{vR}_x &\in {-3, -1} \end{aligned}$$

Since these were two independent restrictions for $\mathbf{vR}_x$, we can just intersect them:

$$\begin{aligned} \mathbf{vR}_x &\in {-7, -4, -3, -2, 0, 1, 2, 5} \cap {-3, -1} \\ \mathbf{vR}_x &\in {-3} \\ \mathbf{vR}_x &= -3 \end{aligned}$$

We can also follow the same reasoning for the final two pairs, and arrive at some constraints for $\mathbf{vR}_y$ and $\mathbf{vR}_z$:

$$\begin{aligned} \mathbf{vR}_y &\in {-8, -5, -4, -3, -1, 0, 1, 4} \\ \mathbf{vR}_z &\in {-10, -6, -4, -3, -1, 0, 2, 6} \end{aligned}$$

In the example scenario, we know $\mathbf{vR} = (-3\ 1\ 2)^T$, which is consistent with this.

For the example, this wasn't sufficient to narrow down the velocity to a single answer. However, the puzzle input has a lot more data. Running the same steps on it (with an implicit assumption that the velocity components are "reasonable", say between -2000 and +2000, to avoid having to find divisors for large numbers) actually narrows down the sets to single values pretty quickly.

Here's a log for my puzzle input:

for X:
  vel 5 pos (252027155370005, 301801136650301)
  => [-1087 -723 ...58 numbers omitted for brevity... 733 1097]
  vel 5 pos (252027155370005, 322304061345021)
  => [-99 -47 -21 -8 -3 1 3 4 6 7 9 13 18 31 57 109]
  vel 5 pos (301801136650301, 322304061345021)
  => [-99 -47 -21 -8 -3 1 3 4 6 7 9 13 18 31 57 109]
  vel -6 pos (313331026360933, 281502442529605)
  => [-99 -8 -3 3 6 18]
  vel -49 pos (317912587490765, 325843512544115)
  => [-99]
for Y:
  vel -234 pos (417641320056889, 407035805550239)
  => [-584 -409 ...20 numbers omitted for brevity... -59 116]
  vel -23 pos (225633601154640, 283130841945486)
  => [-269]
for Z:
  vel -140 pos (313407769490726, 454532431171443)
  => [-429 -361 -327 -283 -157 -153 -151 -141 -139 -129 -127 -123 3 47 81 149]
  vel -10 pos (266642825749899, 283898755233783)
  => [-283 3 81]
  vel -10 pos (266642825749899, 298590923399276)
  => [3 81]
  vel -10 pos (283898755233783, 298590923399276)
  => [3 81]
  vel -113 pos (431101666985804, 430550575300350)
  => [81]

$\mathbf{vR} = ({-99}\ {-269}\ {81})^T$ is in fact correct for my puzzle input.

Finding the position

Now we know $\mathbf{vR}$, the direction of our line. Could we use that to also figure out $\mathbf{pR}$, the origin of that line?

Aside: For the argument below, the magnitude of $\mathbf{vR}$ doesn't actually matter. It's sufficient that it points in the right direction (or the opposite direction).

Let's consider hailstone $A$, which traces the line $\mathbf{pA} + t\ \mathbf{vA}$. We know our line intersects it somewhere, but not exactly where and when. Still, that does narrow the possibilities down to a single plane, defined by:

  • The point $\mathbf{pA}$, in that plane.
  • The direction $\mathbf{vA}$.
  • The direction $\mathbf{vR}$.

You can visualize this as the plane formed by "sliding" a line pointing at the direction $\mathbf{vR}$ over all the possible intersection points on the path of hailstone $A$. (Technically, it would only be a half-plane, as the intersection must not be in the past. But we can ignore that and assume there's just one unique solution, and that it's a valid one.)

Let's find a normal vector $\mathbf{nA}$ for that plane. The sign ("up" vs. "down") and magnitude don't really matter, as long as it's perpendicular to both $\mathbf{vA}$ and $\mathbf{vR}$. The cross product gives us a vector perpendicular to two others:

$$ \mathbf{nA} = \mathbf{vA} \times \mathbf{vR} $$

Given a normal $\mathbf{nA}$ and a point $\mathbf{pA}$, the equation for a plane is:

$$\begin{aligned} (\mathbf{p} - \mathbf{pA}) \cdot \mathbf{nA} &= 0 \\ \mathbf{p} \cdot \mathbf{nA} - \mathbf{pA} \cdot \mathbf{nA} &= 0 \end{aligned}$$

Here $\mathbf{p} = (x\ y\ z)^T$ is an arbitrary point on the plane, but $\mathbf{pA}$ and $\mathbf{nA}$ are known vectors, and $\mathbf{pA} \cdot \mathbf{nA}$ therefore also a known scalar.

We still don't know exactly where on that plane the line is, or even where it intersects the path of hailstone $A$, since we used that to define the plane so that line is necessarily entirely contained in the plane.

However, there's more hailstones which our target line must intersect. Assuming their paths are not parallel to the plane, they'll intersect it at exactly one point.

Let's look at hailstone $B$, and figure out when its line $\mathbf{pB} + t\ \mathbf{vB}$ intersects our plane. This happens for some value $t = tB$ when the coordinates satisfy the earlier equation for the plane.

Let's solve for $tB$ by substituting in the line of hailstone $B$ for $\mathbf{p}$ in our earlier plane equation:

$$\begin{aligned} \mathbf{p} \cdot \mathbf{nA} - \mathbf{pA} \cdot \mathbf{nA} &= 0 \\ \left(\mathbf{pB} + tB\ \mathbf{vB}\right) \cdot \mathbf{nA} - \mathbf{pA} \cdot \mathbf{nA} &= 0 \\ \mathbf{pB} \cdot \mathbf{nA} + tB\ \mathbf{vB} \cdot \mathbf{nA} - \mathbf{pA} \cdot \mathbf{nA} &= 0 \\ tB\ \mathbf{vB} \cdot \mathbf{nA} &= \mathbf{pA} \cdot \mathbf{nA} - \mathbf{pB} \cdot \mathbf{nA} \\ tB &= \frac{\mathbf{pA} \cdot \mathbf{nA} - \mathbf{pB} \cdot \mathbf{nA}}{\mathbf{vB} \cdot \mathbf{nA}} \end{aligned}$$

Once we have $tB$ (and the other known vectors), we can find $\mathbf{pR}$ by just following the rock back:

$$ \mathbf{pR} = \mathbf{pB} + tB\ \mathbf{vB} - tB\ \mathbf{vR} $$

Aside: This step does depend on $\mathbf{vR}$ having the right magnitude. However, we could always repeat the above for another hailstone $C$ to find another intersection point with known time $tC$, and use $\mathbf{vR} = \frac{(\mathbf{pC} + tC\ \mathbf{vC}) - (\mathbf{pB} + tB\ \mathbf{vB})}{tC-tB}$ to figure out the exact $\mathbf{vR}$.

To verify the solution, let's try substituting in the values from the day 24 example. The relevant numbers are:

$$\begin{aligned} \mathbf{pA} &= \begin{pmatrix} 19 & 13 & 30 \end{pmatrix} & \mathbf{vA} &= \begin{pmatrix} -2 & 1 & -2 \end{pmatrix} \\ \mathbf{pB} &= \begin{pmatrix} 18 & 19 & 22 \end{pmatrix} & \mathbf{vB} &= \begin{pmatrix} -1 & -1 & -2 \end{pmatrix} \\ && \mathbf{vR} &= \begin{pmatrix} -3 & 1 & 2 \end{pmatrix} \end{aligned}$$

Let's plug in the numbers to the equations for $tB$ and $\mathbf{pR}$:

$$\begin{aligned} \mathbf{nA} &= \mathbf{vA} \times \mathbf{vR} \\ &= \begin{pmatrix} -2 & 1 & -2 \end{pmatrix}^T \times \begin{pmatrix} -3 & 1 & 2 \end{pmatrix}^T \\ &= \begin{pmatrix} 4 & 10 & 1 \end{pmatrix}^T \\ \mathbf{pA} \cdot \mathbf{nA} &= 19 \cdot 4 + 13 \cdot 10 + 30 \cdot 1 = 236 \\ \mathbf{pB} \cdot \mathbf{nA} &= 18 \cdot 4 + 19 \cdot 10 + 22 \cdot 1 = 284 \\ \mathbf{vB} \cdot \mathbf{nA} &= (-1) \cdot 4 + (-1) \cdot 10 + (-2) \cdot 1 = -16 \\ tB &= \frac{\mathbf{pA} \cdot \mathbf{nA} - \mathbf{pB} \cdot \mathbf{nA}}{\mathbf{vB} \cdot \mathbf{nA}} \\ &= \frac{236 - 284}{-16} = \frac{236 - 284}{-16} = \frac{-48}{-16} \\ &= 3 \\ \mathbf{pB} + 3\ \mathbf{vB} &= \begin{pmatrix} 18 & 19 & 22 \end{pmatrix}^T + 3\ \begin{pmatrix} -1 & -1 & 2 \end{pmatrix}^T \\ &= \begin{pmatrix} 15 & 16 & 16 \end{pmatrix}^T \\ \mathbf{pR} &= \mathbf{pB} + 3\ \mathbf{vB} - 3\ \mathbf{vR} \\ &= \begin{pmatrix} 18 & 19 & 22 \end{pmatrix}^T + 3\ \begin{pmatrix} -1 & -1 & 2 \end{pmatrix}^T - 3\ \begin{pmatrix} -3 & 1 & 2 \end{pmatrix}^T \\ &= \begin{pmatrix} 24 & 13 & 10 \end{pmatrix}^T \end{aligned}$$

In other words, we'll hit the second hailstone at time $tB = 3$ and position $(15\ 16\ 16)^T$, after initially launching the rock from position $\mathbf{pR} = (24\ 13\ 10)^T$.

Finding the position, but differently

As an alternative solution inside an alternative solution, consider this.

If we did know the complete solution, both the position $\mathbf{pR}$ and velocity $\mathbf{vR}$, we could translate the entire problem to the reference frame of the thrown rock, by subtracting $\mathbf{pR}$ from all the initial hailstone positions, and $\mathbf{vR}$ from all the velocities.

In this formulation, the rock would stay still at position $(0\ 0\ 0)^T$, and the paths of all the hailstones would intersect at that point, representing their collision with the rock.

What would happen if we had the rock's velocity right, but the initial position wrong? For example, let's say the correct initial position was still $\mathbf{pR}$, but we instead subtracted some other value $\mathbf{p} = \mathbf{pR} - \mathbf{pO}$ from the hailstones' initial positions? Well, all the coordinates would be off by $\mathbf{pO}$, but all the lines would still intersect at a single point, only this time it would be at $\mathbf{pO}$ rather than $(0\ 0\ 0)^T$.

But hey, we of course do know the value $\mathbf{p}$ that we subtracted. So let's say we didn't change the hailstone initial positions at all: $\mathbf{p} = (0\ 0\ 0)^T$. In this case, $\mathbf{0} = \mathbf{pR} - \mathbf{pO}$, or $\mathbf{pR} = \mathbf{pO}$. In other words, we can find $\mathbf{pR}$ (the correct initial position for the rock) by just locating the point where all the hailstone paths intersect, once we change all their velocities to be relative to that of the rock.

Another way of thinking about it: if we subtract $\mathbf{vR}$ from all velocities, the rock will stand still at its initial position. Since it still has to hit all the hailstones, all the hailstone lines must intersect at that initial position.

Unfortunately, the math for intersecting two lines in three dimensions isn't really any simpler than the plane-line method above. Here's the answer for the intersection point, using $\mathbf{vA}' = \mathbf{vA} - \mathbf{vR}$ to denote the transformed velocity of hailstone $A$ (likewise for $B$):

$$ \mathbf{p} = \mathbf{pA} + \mathbf{vA}'\ \frac{ ((\mathbf{pB} - \mathbf{pA}) \times \mathbf{vB'}) \cdot (\mathbf{vA}' \times \mathbf{vB}') }{\left|\mathbf{vA}' \times \mathbf{vB}'\right|^2} $$

Note: For this method, $\mathbf{vR}$ does have to be the actual velocity, not merely an arbitrary vector pointing in the right direction.

To verify, let's again substitute in the values of the example:

$$\begin{aligned} \mathbf{pA} &= \begin{pmatrix}19&13&30\end{pmatrix}^T \\ \mathbf{pB} &= \begin{pmatrix}18&19&22\end{pmatrix}^T \\ \mathbf{pB} - \mathbf{pA} &= \begin{pmatrix}19&13&30\end{pmatrix}^T - \begin{pmatrix}18&19&22\end{pmatrix}^T = \begin{pmatrix}-1&6&-8\end{pmatrix}^T \\ \mathbf{vA}' = \mathbf{vA} - \mathbf{vR} &= \begin{pmatrix}-2&1&-2\end{pmatrix}^T - \begin{pmatrix}-3&1&2\end{pmatrix}^T = \begin{pmatrix}1&0&-4\end{pmatrix}^T \\ \mathbf{vB}' = \mathbf{vB} - \mathbf{vR} &= \begin{pmatrix}-1&-1&-2\end{pmatrix}^T - \begin{pmatrix}-3&1&2\end{pmatrix}^T = \begin{pmatrix}2&-2&-4\end{pmatrix}^T \\ (\mathbf{pB} - \mathbf{pA}) \times \mathbf{vB}' &= \begin{pmatrix}-1&6&-8\end{pmatrix}^T \times \begin{pmatrix}2&-2&-4\end{pmatrix}^T = \begin{pmatrix}-40&-20&-10\end{pmatrix}^T \\ \mathbf{vA}' \times \mathbf{vB}' &= \begin{pmatrix}1&0&-4\end{pmatrix}^T \times \begin{pmatrix}2&-2&-4\end{pmatrix}^T = \begin{pmatrix}-8&-4&-2\end{pmatrix}^T \\ \left|\mathbf{vA}' \times \mathbf{vB}'\right|^2 &= \left|\begin{pmatrix}-8&-4&-2\end{pmatrix}^T\right|^2 = 84 \\ ((\mathbf{pB} - \mathbf{pA}) \times \mathbf{vB'}) \cdot (\mathbf{vA}' \times \mathbf{vB}') &= \begin{pmatrix}-40&-20&-10\end{pmatrix}^T \cdot \begin{pmatrix}-8&-4&-2\end{pmatrix}^T = 420 \\ \mathbf{p} &= \mathbf{pA} + \mathbf{vA}'\ \frac{ ((\mathbf{pB} - \mathbf{pA}) \times \mathbf{vB'}) \cdot (\mathbf{vA}' \times \mathbf{vB}') }{\left|\mathbf{vA}' \times \mathbf{vB}'\right|^2} \\ &= \mathbf{pA} + \mathbf{vA}'\ \frac{420}{84} \\ &= \mathbf{pA} + 5\ \mathbf{vA}' \\ &= \begin{pmatrix}19&13&30\end{pmatrix}^T + 5 \begin{pmatrix}1&0&-4\end{pmatrix}^T \\ &= \begin{pmatrix}24&13&10\end{pmatrix}^T \end{aligned}$$

We've arrived at the same (correct) solution.