Advertisement
Guest User

Advent of Code 2023 Day 24 Part 2

a guest
Jan 17th, 2024
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.78 KB | Source Code | 0 0
  1. #!/usr/bin/python3
  2.  
  3. #If standing on a hail we will see the rock travel in a straight line,
  4. #pass through us and two other points on two other pieces of hail that zip by
  5. #there must be two vectors from our hail to the other two collisions (v1 and v2)
  6. #such that v1 = m * v2 where m is some unknown scalar multiplier.
  7. #we can make v1 = v2 by dividing one of the x,y or z components by itself to ensure
  8. #it is equal to 1. Then solve.
  9.  
  10. #Select three hail so that the relative, x, y or z is all zero
  11. #Hail 0
  12. #176253337504656, 321166281702430, 134367602892386 @ 190, 8, 338
  13. #Hail 1
  14. #308344120080284, 193172753823598, 249535698501761 @ 60, 8, 134
  15. #Hail 2
  16. #307032218923206, 220427490765998, 286976738475573 @ 29, 8, 58
  17.  
  18. vx0 = 190
  19. vy0 = 8
  20. vz0 = 338
  21.  
  22. vx1 = 60
  23. vy1 = 8
  24. vz1 = 134
  25.  
  26. vx2 = 29
  27. vy2 = 8
  28. vz2 = 58
  29.  
  30. x0 = 176253337504656
  31. y0 = 321166281702430
  32. z0 = 134367602892386
  33.  
  34. x1 = 308344120080284
  35. y1 = 193172753823598
  36. z1 = 249535698501761
  37.  
  38. x2 = 307032218923206
  39. y2 = 220427490765998
  40. z2 = 286976738475573
  41.  
  42. #calculate relative velocities of hail 1 and 2 to hail 0
  43. #the y component is zero due to selection of hail
  44. vxr1 = vx1 - vx0
  45. vzr1 = vz1 - vz0
  46. vxr2 = vx2 - vx0
  47. vzr2 = vz2 - vz0
  48.  
  49. #relative initial position of hail 1
  50. xr1 = x1 - x0
  51. yr1 = y1 - y0
  52. zr1 = z1 - z0
  53.  
  54. #relative initial position of hail 2
  55. xr2 = x2 - x0
  56. yr2 = y2 - y0
  57. zr2 = z2 - z0
  58.  
  59. # 1st Hail equations
  60. # x = xr1 + vxr1*t1
  61. # y = yr1
  62. # z = zr1 + vzr1*t1
  63.  
  64. # 2nd Hail equations
  65. # x = xr2 + vxr2*t2
  66. # y = yr2
  67. # z = zr2
  68.  
  69. #divide all equations by the y component to make y = 1 and ensure both vectors are the same
  70. # 1st Set results
  71. # x = (xr1+vxr1*t1)/yr1
  72. # y = 1
  73. # z = (zr1+vzr1*t1)/yr1
  74.  
  75. #2nd Set results
  76. # x = (xr2+vxr2*t2)/yr2
  77. # y = 1
  78. # z = (zr2+vzr2*t2)/yr2
  79.  
  80. #Solve set of two linear equations x=x and z=z
  81. num = (yr2*xr1*vzr1)-(vxr1*yr2*zr1)+(yr1*zr2*vxr1)-(yr1*xr2*vzr1)
  82. den = yr1*((vzr1*vxr2)-(vxr1*vzr2))
  83. t2 = num / den
  84.  
  85. #Substitute t2 into a t1 equation
  86. num = (yr1*xr2)+(yr1*vxr2*t2)-(yr2*xr1)
  87. den = yr2*vxr1
  88. t1 = num / den
  89. print('t1 time of first vector @ collision', t1)
  90. print('t2 time of second vector @ collision', t2)
  91.  
  92. #calculate collision position at t1 and t2 of hail 1 and 2 in normal frame of reference
  93. cx1 = x1 + (t1*vx1)
  94. cy1 = y1 + (t1*vy1)
  95. cz1 = z1 + (t1*vz1)
  96.  
  97. cx2 = x2 + (t2*vx2)
  98. cy2 = y2 + (t2*vy2)
  99. cz2 = z2 + (t2*vz2)
  100. print('collision one occurs @', cx1, cy1, cz1)
  101. print('collision two occurs @', cx2, cy2, cz2)
  102.  
  103. #calculate the vector the rock travelled between those two collisions
  104. xm = (cx2-cx1)/(t2-t1)
  105. ym = (cy2-cy1)/(t2-t1)
  106. zm = (cz2-cz1)/(t2-t1)
  107. print('rock vector', xm, ym, zm)
  108.  
  109. #calculate the initial position of the rock based on its vector
  110. xc = cx1 - (xm*t1)
  111. yc = cy1 - (ym*t1)
  112. zc = cz1 - (zm*t1)
  113. print('rock inital position', xc, yc, zc)
  114. print('answer', int(xc+yc+zc))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement