rm(list=ls())

library("data.table")

thedata <- read.csv(file="00_data/point_cloud.csv")

# file for result
# Oakman
# solution <- read.csv("2023_submissions/Oakman/oakman_path.txt")
# opt.path <- solution$Path

# Zellmer
# solution <- read.csv("2023_submissions/Zellmer/optimizedPath.csv")
# opt.path <- solution$Path

# Bleher
# load(file="00_data/solution.Rdata")
# opt.path <- result

# Müller
# solution <- read.csv("2023_submissions/Mueller/solution_path_Peter_Mueller.csv")
# opt.path <- solution$Path

# Puke
#puke <- readRDS(file="2023_submissions/Puke/ordered.rds")
#opt.path <- puke$ID

# Müller
solution <- read.csv("2023_submissions/Quinger/submission_file.csv")
opt.path <- solution$path

# Check validity of result
# which(!(thedata$ID %in% opt.path))
# opt.path[1] == 1
# opt.path[length(opt.path)] == 1


#Calculate distance
opt.path.dt <- data.table(opt.path=opt.path,lead.opt.path = shift(opt.path,-1))
setDT(thedata)
opt.path.dt <- merge(opt.path.dt,thedata,by.y="ID",by.x="opt.path",suffixes=c("","_0"))
opt.path.dt <- merge(opt.path.dt,thedata,by.y="ID",by.x="lead.opt.path",suffixes=c("","_1"))
opt.path.dt[,distance:=sqrt((x_1-x)^2+(y_1-y)^2)]
sum(opt.path.dt$distance)

# Puke:     386217.7
# Quinger:  416693.5
# Oakman:   424498.7
# Zellmer:  424498.7
# Bleher:   424498.7
# Müller:   529287.5

plot(thedata[,c("x","y")],pch=16,cex=0.1)
lines(thedata[opt.path[which(opt.path!=0)],c("x","y")],col="red")
