# -- Code Cell --
import pandas as pd
train = pd.read_csv("dataset.csv")

# -- Code Cell --
train

# -- Code Cell --
task1 = len(train.groupby('Timestamp').agg(list))

# -- Code Cell --
groups = train.groupby('Timestamp').agg(list)

# -- Code Cell --
groups['Y'].iloc[0]

# -- Code Cell --
import matplotlib.pyplot as plt
X = groups['X'].iloc[0]
Y = groups['Y'].iloc[0]
plt.scatter(X, Y)

# -- Code Cell --
task2 = 5

# -- Code Cell --
import numpy as np

# -- Code Cell --
X = groups['X'].iloc[0]
Y = groups['Y'].iloc[0]
coords = []
for x,y in zip(X,Y):
    coords.append((x,y))
coords

# -- Code Cell --
from sklearn.cluster import KMeans
pos = []
kmeans=KMeans(n_clusters=5)
for i in range(len(groups)):
    X = groups['X'].iloc[i]
    Y = groups['Y'].iloc[i]
    coords = []
    for x,y in zip(X,Y):
        coords.append((x,y))
    kmeans.fit_predict(coords)
    pos.append(kmeans.cluster_centers_)

# -- Code Cell --
pos[0]

# -- Code Cell --
transformat = []
for i in range(len(pos)):
    for drone_coords in pos[i]:
        i=0
        combo = []
        for x in drone_coords:
            if i%2==0:
                combo.append(x)
                i+=1
                continue
            if i%2!=0:
                combo.append(x)
                transformat.append(f"{combo[0]}|{combo[1]}")   
                combo = []
                i+=1
transformat

# -- Code Cell --
task3 = transformat

# -- Code Cell --
coords=[]
for i in range(len(groups)):
    for x,y in zip(groups['X'].iloc[i],groups['Y'].iloc[i]):
        coords.append([x,y])
coords

# -- Code Cell --
labels = train['Timestamp']
labels

# -- Code Cell --
labels.unique()

# -- Code Cell --
labels_but_time = []
for t in range(len(labels)):
    time_str = labels[t].split(",")[2].replace("AM", "").strip()
    h, m, s = time_str.split(":")
    time_minutes = int(h)*60 + int(m) + int(s)/60
    labels_but_time.append(time_minutes)

# -- Code Cell --
labels_but_time

# -- Code Cell --
X = []
y =[]
timestamps = list(groups.index)
for i, ts in enumerate(timestamps):
    time_str = ts.split(",")[2].replace("AM", "").strip()
    h, m, s = time_str.split(":")
    t_val = int(h)*60 + int(m) + int(s)/60

    for drone_id in range(5):
        cx, cy = pos[i][drone_id]
        X.append([t_val, drone_id])
        y.append([cx, cy])

# -- Code Cell --
from sklearn.ensemble import RandomForestRegressor 
model = RandomForestRegressor() 
model.fit(X, y) 
new_time = 8*60 + 20 
task4=[] 
for drone_id in range(5): 
    pred = model.predict([[new_time, drone_id]]) 
    print(f"Drona {drone_id}: {pred[0]}") 
    task4.append(f"{pred[0][0]}|{pred[0][1]}")

# -- Code Cell --
task4

# -- Code Cell --
task3[0]

# -- Code Cell --
zana = []
timestamps = list(groups.index)

idx = 0
for ts in timestamps:
    for drone_id in range(5):
        zana.append([f"{ts}|{drone_id}", task3[idx]])
        idx += 1

# -- Code Cell --
zana

# -- Code Cell --
task4

# -- Code Cell --
zana2 = []
for t in range(5):
    zana2.append([f"Jan 26, 2026, 12:08:20 AM|{t}", task4[t]])

# -- Code Cell --
zana2_id = [row[0] for row in zana2]
zana_id = [row[0] for row in zana]
zana2_values = [row[1] for row in zana2]
zana_values = [row[1] for row in zana]

# -- Code Cell --
sub1 = pd.DataFrame({
    "id":["GLOBAL"],
    "subtaskID":["task1"],
    "answer":[task1]
})
sub2 = pd.DataFrame({
    "id":["GLOBAL"],
    "subtaskID":["task2"],
    "answer":[task2]
})
sub3 = pd.DataFrame({
    "id":zana_id,
    "subtaskID":"task3",
    "answer":zana_values
})
sub4 = pd.DataFrame({
    "id":zana2_id,
    "subtaskID":"task4",
    "answer":zana2_values
})
final = pd.concat([sub1,sub2,sub3,sub4]).to_csv("subs.csv",index=False)

# -- Code Cell --


# -- Code Cell --
