Merge branch 'main' of https://github.com/Jake-Pullen/advent_of_code
This commit is contained in:
@@ -0,0 +1,51 @@
|
|||||||
|
with open(r'advent_of_code\2023\24\input.txt', 'r') as file:
|
||||||
|
input_data = file.read()
|
||||||
|
|
||||||
|
test_input = '''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'''
|
||||||
|
|
||||||
|
#input_data = test_input
|
||||||
|
input_lines = input_data.split('\n')
|
||||||
|
|
||||||
|
class Hailstone:
|
||||||
|
def __init__(self, start_x, start_y, start_z, velocity_x, velocity_y, velocity_z):
|
||||||
|
self.start_x = start_x
|
||||||
|
self.start_y = start_y
|
||||||
|
self.start_z = start_z
|
||||||
|
self.velocity_x = velocity_x
|
||||||
|
self.velocity_y = velocity_y
|
||||||
|
self.velocity_z = velocity_z
|
||||||
|
|
||||||
|
self.a = velocity_y
|
||||||
|
self.b = -velocity_x
|
||||||
|
self.c = velocity_y * start_x - velocity_x * start_y
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return "Hailstone{" + f"a={self.a}, b={self.b}, c={self.c}" + "}"
|
||||||
|
|
||||||
|
# Parse input lines into Hailstone objects
|
||||||
|
hailstones = [Hailstone(*map(int, line.replace("@", ",").split(","))) for line in input_lines]
|
||||||
|
|
||||||
|
total_intersections = 0
|
||||||
|
|
||||||
|
# Check each pair of hailstones for intersection
|
||||||
|
for i, hailstone1 in enumerate(hailstones):
|
||||||
|
for hailstone2 in hailstones[:i]:
|
||||||
|
a1, b1, c1 = hailstone1.a, hailstone1.b, hailstone1.c
|
||||||
|
a2, b2, c2 = hailstone2.a, hailstone2.b, hailstone2.c
|
||||||
|
# If lines are parallel, they do not intersect
|
||||||
|
if a1 * b2 == b1 * a2:
|
||||||
|
continue
|
||||||
|
# Calculate intersection point
|
||||||
|
x = (c1 * b2 - c2 * b1) / (a1 * b2 - a2 * b1)
|
||||||
|
y = (c2 * a1 - c1 * a2) / (a1 * b2 - a2 * b1)
|
||||||
|
# Check if intersection point is within bounds
|
||||||
|
if 200000000000000 <= x <= 400000000000000 and 200000000000000 <= y <= 400000000000000:
|
||||||
|
# Check if intersection point is in the same direction as the hailstones' velocities
|
||||||
|
if all((x - hs.start_x) * hs.velocity_x >= 0 and (y - hs.start_y) * hs.velocity_y >= 0 for hs in (hailstone1, hailstone2)):
|
||||||
|
total_intersections += 1
|
||||||
|
|
||||||
|
print(total_intersections)
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import sympy
|
||||||
|
|
||||||
|
with open(r'advent_of_code\2023\24\input.txt', 'r') as file:
|
||||||
|
input_data = file.read()
|
||||||
|
|
||||||
|
test_input_data = '''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'''
|
||||||
|
|
||||||
|
#input_data = test_input_data
|
||||||
|
input_lines = input_data.split('\n')
|
||||||
|
|
||||||
|
hailstones = [tuple(map(int, line.replace("@", ",").split(","))) for line in input_lines]
|
||||||
|
|
||||||
|
x_ref, y_ref, z_ref, vx_ref, vy_ref, vz_ref = sympy.symbols("x_ref, y_ref, z_ref, vx_ref, vy_ref, vz_ref")
|
||||||
|
|
||||||
|
equations = []
|
||||||
|
|
||||||
|
for i, (start_x, start_y, start_z, velocity_x, velocity_y, velocity_z) in enumerate(hailstones):
|
||||||
|
equations.append((x_ref - start_x) * (velocity_y - vy_ref) - (y_ref - start_y) * (velocity_x - vx_ref))
|
||||||
|
equations.append((y_ref - start_y) * (velocity_z - vz_ref) - (z_ref - start_z) * (velocity_y - vy_ref))
|
||||||
|
if i < 2:
|
||||||
|
continue
|
||||||
|
solutions = [solution for solution in sympy.solve(equations) if all(value % 1 == 0 for value in solution.values())]
|
||||||
|
if len(solutions) == 1:
|
||||||
|
break
|
||||||
|
|
||||||
|
final_solution = solutions[0]
|
||||||
|
|
||||||
|
print(final_solution[x_ref] + final_solution[y_ref] + final_solution[z_ref])
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
def delete_specific_files(directory, file_names):
|
||||||
|
for foldername, subfolders, filenames in os.walk(directory):
|
||||||
|
for filename in filenames:
|
||||||
|
if filename in file_names:
|
||||||
|
file_path = os.path.join(foldername, filename)
|
||||||
|
os.remove(file_path)
|
||||||
|
print(f'Deleted {file_path}')
|
||||||
|
|
||||||
|
# delete_specific_files('advent_of_code', ['input.txt', 'puzzle_text.md'])
|
||||||
|
|
||||||
|
|
||||||
|
import pathlib
|
||||||
|
|
||||||
|
def rename_folders_and_update_files(directory):
|
||||||
|
for foldername, subfolders, filenames in os.walk(directory):
|
||||||
|
base_foldername = os.path.basename(foldername)
|
||||||
|
if base_foldername.isdigit() and len(base_foldername) == 1:
|
||||||
|
new_foldername = '0' + base_foldername
|
||||||
|
new_full_path = os.path.join(os.path.dirname(foldername), new_foldername)
|
||||||
|
os.rename(foldername, new_full_path)
|
||||||
|
print(f'Renamed {foldername} to {new_full_path}')
|
||||||
|
|
||||||
|
for filename in filenames:
|
||||||
|
if filename.endswith('solution.py'):
|
||||||
|
file_path = pathlib.Path(new_full_path) / filename
|
||||||
|
content = file_path.read_text()
|
||||||
|
updated_content = content.replace(foldername, new_full_path)
|
||||||
|
file_path.write_text(updated_content)
|
||||||
|
print(f'Updated file reference in {file_path}')
|
||||||
|
|
||||||
|
rename_folders_and_update_files('advent_of_code')
|
||||||
Reference in New Issue
Block a user