Skip to content

Commit

Permalink
[2023/24] p1 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 24, 2023
1 parent fc06625 commit 7979f24
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
5 changes: 5 additions & 0 deletions 2023/24/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
19, 13, 30 @ -2, 1, -2
18, 19, 22 @ -1, -1, -2
20, 25, 34 @ -2, -2, -4
12, 31, 28 @ -1, -2, -1
20, 19, 15 @ 1, -5, -3
55 changes: 55 additions & 0 deletions 2023/24/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from GhostyUtils import aoc
from GhostyUtils.vec2 import Vec2
import math
import itertools


MIN = 200000000000000
MAX = 400000000000000


def ray_intersection(ray_a, ray_b):
a_pos, a_vel = ray_a
b_pos, b_vel = ray_b
dx = b_pos.x - a_pos.x
dy = b_pos.y - a_pos.y
det = b_vel.x * a_vel.y - b_vel.y * a_vel.x
if det == 0:
return False
u = (dy * b_vel.x - dx * b_vel.y) / det
v = (dy * a_vel.x - dx * a_vel.y) / det
p = a_pos + a_vel * u
return p, (u, v)


if __name__ == "__main__":
hail = []
for hail_txt in aoc.read_lines():
pos, v = hail_txt.split(' @ ')
pos = Vec2(tuple(map(int, pos.split(', ')[:-1])))
v = Vec2(tuple(map(int, v.split(', ')[:-1])))
hail.append((pos, v))

collisions_in_area = 0
for t1, t2 in itertools.combinations(hail, 2):
print(f'Hailstone A: {t1[0]} @ {t1[1]}')
print(f'Hailstone B: {t2[0]} @ {t2[1]}')
result = ray_intersection(t1, t2)
if result:
p, (u, v) = result
if u < 0 and v < 0:
print("Hailstones' paths crossed in the past for both hailstones")
elif u < 0:
print("Hailstones' paths crossed in the past for hailstone A")
elif v < 0:
print("Hailstones' paths crossed in the past for hailstone B")
elif MIN <= p.x <= MAX and MIN <= p.y <= MAX:
collisions_in_area += 1
print(f"Hailstones' paths will cross inside the test area (at {p})")
else:
print(f"Hailstones' paths will cross outside the test area (at {p})")
else:
print("Hailstones' paths are parallel; they never intersect")
print()

print('p1:', collisions_in_area)

0 comments on commit 7979f24

Please sign in to comment.