Files
s01e02/internal/domain/final_answer.go
2026-03-12 02:10:57 +01:00

67 lines
1.9 KiB
Go

package domain
// FinalAnswer represents the final answer for the task
type FinalAnswer struct {
APIKey string `json:"apikey"`
Task string `json:"task"`
Answer AnswerDetail `json:"answer"`
}
// AnswerDetail contains the person and power plant details
type AnswerDetail struct {
Name string `json:"name"`
Surname string `json:"surname"`
AccessLevel int `json:"accessLevel"`
PowerPlant string `json:"powerPlant"`
}
// FindClosestPowerPlant finds the power plant closest to any person
// Returns the person, power plant, and distance
func FindClosestPowerPlant(personDataMap map[string]*PersonData, powerPlants []Location, plantsData *PowerPlantsData) (*PersonData, *Location, float64, string) {
var closestPerson *PersonData
var closestPlant *Location
minDistance := 1e10
var powerPlantCode string
for _, personData := range personDataMap {
if len(personData.Locations) == 0 {
continue
}
// Use first location (primary)
personLoc := personData.Locations[0]
for _, plant := range powerPlants {
distance := Haversine(
personLoc.Latitude,
personLoc.Longitude,
plant.Latitude,
plant.Longitude,
)
// If distance is smaller, or same distance but higher access level
if distance < minDistance {
minDistance = distance
closestPerson = personData
closestPlant = &plant
// Get power plant code from plantsData
if info, ok := plantsData.PowerPlants[plant.Name]; ok {
powerPlantCode = info.Code
}
} else if distance == minDistance && closestPerson != nil && personData.AccessLevel > closestPerson.AccessLevel {
// Same distance, but higher access level
closestPerson = personData
closestPlant = &plant
// Get power plant code from plantsData
if info, ok := plantsData.PowerPlants[plant.Name]; ok {
powerPlantCode = info.Code
}
}
}
}
return closestPerson, closestPlant, minDistance, powerPlantCode
}