2024-11-28 15:10:22 +01:00
|
|
|
#!/bin/python3
|
2024-11-29 17:16:03 +01:00
|
|
|
import sys,re,math, numpy as np
|
|
|
|
from collections import Counter, defaultdict
|
2024-11-28 15:10:22 +01:00
|
|
|
from pprint import pprint
|
|
|
|
sys.path.insert(0, '../../')
|
|
|
|
from fred import get_re,list2int
|
|
|
|
|
|
|
|
input_f = 'input'
|
|
|
|
|
2024-11-28 22:48:07 +01:00
|
|
|
part = 2
|
2024-11-28 15:10:22 +01:00
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 1 #
|
|
|
|
# #
|
|
|
|
#########################################
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
|
|
|
|
if part == 1:
|
|
|
|
with open(input_f) as file:
|
|
|
|
for line in file:
|
|
|
|
lines.append(get_re(r"^p=<(.*)>.*v=<(.*)>.*a=<(.*)>$",line.rstrip()))
|
|
|
|
|
2024-11-28 22:48:07 +01:00
|
|
|
min_acc = float('inf')
|
|
|
|
acc_idx = 0
|
2024-11-28 15:10:22 +01:00
|
|
|
|
2024-11-28 22:48:07 +01:00
|
|
|
for idx,i in enumerate(lines):
|
|
|
|
tmp = math.sqrt(sum(component ** 2 for component in list2int(i.group(3).split(','))))
|
|
|
|
if tmp < min_acc:
|
|
|
|
min_acc = tmp
|
|
|
|
acc_idx = idx
|
|
|
|
print(acc_idx)
|
2024-11-28 15:10:22 +01:00
|
|
|
|
2024-11-28 22:48:07 +01:00
|
|
|
#########################################
|
|
|
|
# #
|
|
|
|
# Part 2 #
|
|
|
|
# #
|
|
|
|
#########################################
|
2024-11-29 17:16:03 +01:00
|
|
|
|
|
|
|
def calculate_position_xyz(initial_position, velocity, acceleration):
|
|
|
|
|
|
|
|
velocity += acceleration
|
|
|
|
|
|
|
|
new_position = initial_position + velocity # + (0.5 * acceleration * time**2)
|
|
|
|
return np.array(new_position)
|
|
|
|
|
|
|
|
def find_duplicate_indices(data):
|
|
|
|
value_to_indices = defaultdict(list)
|
|
|
|
for index, value in data.items():
|
|
|
|
value_tuple = tuple(value)
|
|
|
|
value_to_indices[value_tuple].append(index)
|
|
|
|
|
|
|
|
duplicate_indices = [index for indices in value_to_indices.values() if len(indices) > 1 for index in indices]
|
|
|
|
return duplicate_indices
|
|
|
|
|
2024-11-28 22:48:07 +01:00
|
|
|
if part == 2:
|
2024-11-29 17:16:03 +01:00
|
|
|
with open(input_f) as file:
|
|
|
|
for line in file:
|
|
|
|
lines.append(get_re(r"^p=<(.*)>.*v=<(.*)>.*a=<(.*)>$",line.rstrip()))
|
|
|
|
|
|
|
|
for ldx,l in enumerate(lines):
|
|
|
|
lines[ldx] = [np.array(list2int(l.group(1).split(','))), np.array(list2int(l.group(2).split(','))), np.array(list2int(l.group(3).split(',')))]
|
|
|
|
|
|
|
|
particles = []
|
|
|
|
|
|
|
|
iterations = 10000
|
|
|
|
|
|
|
|
last_val = 0
|
|
|
|
range_since_last = 0
|
2024-11-28 15:10:22 +01:00
|
|
|
|
|
|
|
|
2024-11-29 17:16:03 +01:00
|
|
|
for iter in range(iterations):
|
|
|
|
if len(lines) != last_val:
|
|
|
|
last_val = len(lines)
|
|
|
|
else:
|
|
|
|
range_since_last += 1
|
2024-11-28 15:10:22 +01:00
|
|
|
|
2024-11-29 17:16:03 +01:00
|
|
|
if range_since_last > 100: # we dont want to run forever
|
|
|
|
break
|
2024-11-28 15:10:22 +01:00
|
|
|
|
2024-11-29 17:16:03 +01:00
|
|
|
positions = {}
|
|
|
|
length = len(lines)
|
|
|
|
duplicates = []
|
2024-11-28 15:10:22 +01:00
|
|
|
|
2024-11-29 17:16:03 +01:00
|
|
|
for ldx in range(length):
|
|
|
|
pos = []
|
|
|
|
p,v,a = lines[ldx]
|
2024-11-28 15:10:22 +01:00
|
|
|
|
2024-11-29 17:16:03 +01:00
|
|
|
pos = calculate_position_xyz(p, v, a)
|
|
|
|
positions[ldx] = pos
|
|
|
|
lines[ldx][0] = pos
|
2024-11-28 22:48:07 +01:00
|
|
|
|
2024-11-29 17:16:03 +01:00
|
|
|
duplicates = find_duplicate_indices(positions)
|
2024-11-28 22:48:07 +01:00
|
|
|
|
2024-11-29 17:16:03 +01:00
|
|
|
if len(duplicates) > 0:
|
|
|
|
lines = [item for i, item in enumerate(lines) if i not in duplicates]
|
|
|
|
print(len(lines))
|