Advertisement
Guest User

Untitled

a guest
Dec 24th, 2023
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. private static void part2(Hailstone[] hailstones) {
  2.  
  3.         Hailstone h1 = hailstones[0];
  4.         Hailstone h2 = hailstones[1];
  5.  
  6.         int range = 500;
  7.         for (int vx = -range; vx <= range; vx++) {
  8.             for (int vy = -range; vy <= range; vy++) {
  9.                 for (int vz = -range; vz <= range; vz++) {
  10.  
  11.                     if (vx == 0 || vy == 0 || vz == 0) {
  12.                         continue;
  13.                     }
  14.  
  15.                     // Find starting point for rock that will intercept first two hailstones (x,y) on this trajectory
  16.  
  17.                     // simultaneous linear equation (from part 1):
  18.                     // H1:  x = A + a*t   y = B + b*t
  19.                     // H2:  x = C + c*u   y = D + d*u
  20.                     //
  21.                     //  t = [ d ( C - A ) - c ( D - B ) ] / ( a * d - b * c )
  22.                     //
  23.                     // Solve for origin of rock intercepting both hailstones in x,y:
  24.                     //     x = A + a*t - vx*t   y = B + b*t - vy*t
  25.                     //     x = C + c*u - vx*u   y = D + d*u - vy*u
  26.  
  27.                     long A = h1.x, a = h1.vx - vx;
  28.                     long B = h1.y, b = h1.vy - vy;
  29.                     long C = h2.x, c = h2.vx - vx;
  30.                     long D = h2.y, d = h2.vy - vy;
  31.  
  32.                     // skip if division by 0
  33.                     if (c == 0 || (a * d) - (b * c) == 0) {
  34.                         continue;
  35.                     }
  36.  
  37.                     // Rock intercepts H1 at time t
  38.                     long t = (d * (C - A) - c * (D - B)) / ((a * d) - (b * c));
  39.  
  40.                     // Calculate starting position of rock from intercept point
  41.                     long x = h1.x + h1.vx * t - vx * t;
  42.                     long y = h1.y + h1.vy * t - vy * t;
  43.                     long z = h1.z + h1.vz * t - vz * t;
  44.  
  45.                    
  46.                     // check if this rock throw will hit all hailstones
  47.  
  48.                     boolean hitall = true;
  49.                     for (int i = 0; i < hailstones.length; i++) {
  50.  
  51.                         Hailstone h = hailstones[i];
  52.                         long u;
  53.                         if (h.vx != vx) {
  54.                             u = (x - h.x) / (h.vx - vx);
  55.                         } else if (h.vy != vy) {
  56.                             u = (y - h.y) / (h.vy - vy);
  57.                         } else if (h.vz != vz) {
  58.                             u = (z - h.z) / (h.vz - vz);
  59.                         } else {
  60.                             throw new RuntimeException();
  61.                         }
  62.  
  63.                         if ((x + u * vx != h.x + u * h.vx) || (y + u * vy != h.y + u * h.vy) || ( z + u * vz != h.z + u * h.vz)) {
  64.                             hitall = false;
  65.                             break;
  66.                         }
  67.                     }
  68.  
  69.                     if (hitall) {
  70.                         System.out.printf("%d %d %d   %d %d %d   %d %n", x, y, z, vx, vy, vz, x + y + z);
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement