Advertisement
Guest User

AoC2023 Day24 Part2

a guest
Dec 24th, 2023
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.31 KB | None | 0 0
  1. public dynamic SolvePart2()
  2. {
  3.     var range = 300;
  4.  
  5.     // Brute force rock velocities in the XY plane
  6.     foreach (var x in Range(range))
  7.     {
  8.         foreach (var y in Range(range))
  9.         {
  10.             // Grab the intersection of the first four hailstone trajectories with their velocities modified by (x,y).
  11.             // A rock with velocity (3,1) hitting a hailstone with velocity (1,1) is the same as
  12.             // a rock with velocity (0,0) and a hailstone with velocity (-2,0).
  13.             var intersect1 = TryIntersectPos(Hails[1], Hails[0], (x, y));
  14.             var intersect2 = TryIntersectPos(Hails[2], Hails[0], (x, y));
  15.             var intersect3 = TryIntersectPos(Hails[3], Hails[0], (x, y));
  16.  
  17.             // If they don't align, keep searching
  18.             if (!intersect1.intersects || intersect1.pos != intersect2.pos || intersect1.pos != intersect3.pos) continue;
  19.  
  20.             // Brute force the Z velocity as well.
  21.             foreach (var z in Range(range))
  22.             {
  23.                 // We know at what timestamp we would intersect the rock its initial position, so we can just check where the Z would end up at
  24.                 // Check them for the first four hailstones as well
  25.                 var intersectZ = Hails[1].Pos.Z + intersect1.time * (Hails[1].Vel.Z + z);
  26.                 var intersectZ2 = Hails[2].Pos.Z + intersect2.time * (Hails[2].Vel.Z + z);
  27.                 var intersectZ3 = Hails[3].Pos.Z + intersect3.time * (Hails[3].Vel.Z + z);
  28.  
  29.                 // If they don't align, keep searching
  30.                 if (intersectZ != intersectZ2 || intersectZ != intersectZ3) continue;
  31.  
  32.                 // If four hailstones happen to align, just assume we found the answer and exit.
  33.                 return intersect1.pos.X + intersect1.pos.Y + intersectZ;
  34.             }
  35.  
  36.  
  37.         }
  38.     }
  39.  
  40.     return -1;
  41. }
  42.  
  43. // 0, -1, 1, -2, 2, -3, 3...
  44. public IEnumerable<int> Range(int max)
  45. {
  46.     var i = 0;
  47.     yield return i;
  48.  
  49.     while (i < max)
  50.     {
  51.         if (i >= 0)
  52.             i++;
  53.         i *= -1;
  54.         yield return i;
  55.     }
  56. }
  57.  
  58. // Maths:
  59. // https://math.stackexchange.com/a/3176648
  60. public (bool intersects, (BigInteger X, BigInteger Y) pos, BigInteger time) TryIntersectPos(Hail one, Hail two, (int x, int y) offset)
  61. {
  62.     var a = (Pos: (one.Pos.X, one.Pos.Y), Vel: (X: one.Vel.X + offset.x, Y: one.Vel.Y + offset.y));
  63.     var c = (Pos: (two.Pos.X, two.Pos.Y), Vel: (X: two.Vel.X + offset.x, Y: two.Vel.Y + offset.y));
  64.  
  65.     //Determinant
  66.     BigInteger D = (a.Vel.X * -1 * c.Vel.Y) - (a.Vel.Y * -1 * c.Vel.X);
  67.  
  68.     if (D == 0) return (false, (-1, -1), -1);
  69.  
  70.     var Qx = (-1 * c.Vel.Y * (c.Pos.X - a.Pos.X)) - (-1 * c.Vel.X * (c.Pos.Y - a.Pos.Y));
  71.     var Qy = (a.Vel.X * (c.Pos.Y - a.Pos.Y)) - (a.Vel.Y * (c.Pos.X - a.Pos.X));
  72.  
  73.     var t = Qx / D;
  74.     var s = Qy / D;
  75.  
  76.     var Px = (a.Pos.X + t * a.Vel.X);
  77.     var Py = (a.Pos.Y + t * a.Vel.Y);
  78.  
  79.     // Returns the intersection point, as well as the timestamp at which "one" will reach it with the given velocity.
  80.     return (true, (Px, Py), t);
  81. }
  82.  
  83. public class Hail
  84. {
  85.     public (BigInteger X, BigInteger Y, BigInteger Z) Pos;
  86.     public (int X, int Y, int Z) Vel;
  87.  
  88.     public Hail((BigInteger X, BigInteger Y, BigInteger Z) pos, (int X, int Y, int Z) vel)
  89.     {
  90.         Pos = pos;
  91.         Vel = vel;
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement