El algoritmo está funcionando
This commit is contained in:
		
							parent
							
								
									020c85fe7d
								
							
						
					
					
						commit
						eeefbac976
					
				
					 4 changed files with 97 additions and 4 deletions
				
			
		|  | @ -9,6 +9,13 @@ class Dia extends Model | |||
| { | ||||
|     use HasFactory; | ||||
| 
 | ||||
|     /** | ||||
|      * The attributes that are mass assignable. | ||||
|      * | ||||
|      * @var array | ||||
|      */ | ||||
|     protected $fillable = ['nombre']; | ||||
| 
 | ||||
|     public function evento() { | ||||
|         return $this->belongsTo(Evento::class); | ||||
|     } | ||||
|  | @ -16,4 +23,36 @@ class Dia extends Model | |||
|     public function horaFlags() { | ||||
|         return $this->hasMany(HoraFlag::class); | ||||
|     } | ||||
| 
 | ||||
|     // ordena las horaFlags asociadas a día por minutoDelDía, de menor a mayor;
 | ||||
|     // las recorre sumando las flags asociadas a cada una y guardándo el número en cant_actual;
 | ||||
|     // cuando cant_actual supera a la cantidad guardada en best, se hace un nuevo best:
 | ||||
|         // se pone best["cantidad"] = cant_actual, en best["inicio"] el minutoDelDia de la hora actual
 | ||||
|         // y en best["fin"] el minutoDelDía de la hora siguiente (esta hora existe porque si cant_actual
 | ||||
|         // crece es porque llega a alguien, y por lo tanto hay una hora posterior en la que se va).
 | ||||
|     // finalmente se devuelve best, con la duración del horario devuelto.
 | ||||
|     public function marzullo() { | ||||
|         // INICIO
 | ||||
|         $cant_actual = 0; | ||||
|         $best = ["cantidad" => 0, "inicio" => 0, "fin" => 0]; | ||||
|             // Ordenar horaFlags por minutoDelDia
 | ||||
|         $horas = $this->horaFlags()->orderBy('minutoDelDia')->get(); | ||||
|         // var_dump($horas);
 | ||||
| 
 | ||||
|         // ALGORITMO
 | ||||
|         foreach ($horas as $i => $hora) { | ||||
|             $cant_actual += $hora->flag; | ||||
|             if ($cant_actual > $best["cantidad"] && | ||||
|                     ($horas[$i+1]->flag > 0 || $horas[$i+1]->minutoDelDia - $hora->minutoDelDia > 15))  { | ||||
|                 $best["cantidad"] = $cant_actual; | ||||
|                 $best["inicio"] = $hora->minutoDelDia; | ||||
|                 $best["fin"] = $horas[$i+1]->minutoDelDia; | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|         // RETURN
 | ||||
|         $duracion = $best["fin"] - $best["inicio"]; | ||||
|         return [$best["cantidad"], $best["inicio"], $best["fin"], $duracion]; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -13,10 +13,41 @@ class Evento extends Model | |||
|         return $this->hasMany(Dia::class); | ||||
|     } | ||||
| 
 | ||||
|     public function coordinar() { | ||||
|         foreach ($this->dias() as $dia) { | ||||
|             //guardar (nombre, horario, cantidad)
 | ||||
| 
 | ||||
|     // devuelve los tres mejores marzullos de los días que tiene asociados
 | ||||
|     public function coordinar(): array { | ||||
|         $marzullos = []; | ||||
|         //devuelve array de
 | ||||
|             // [nombre::string, cantidad::integer, inicio::integer, fin::integer, duracion::integer]
 | ||||
|         foreach ($this->dias as $dia) { | ||||
|             [$a, $b, $c, $d] = $dia->marzullo(); | ||||
|             $marzullos[] = ["dia" => $dia->nombre, "cantidad" => $a, "inicio" => $b, "fin" => $c, "duracion" => $d]; | ||||
|         } | ||||
|         usort($marzullos, array("Evento", "cmp_cant_marzullo")); | ||||
|         return array_slice($marzullos, 0, 3); | ||||
|     } | ||||
| 
 | ||||
|     /*                         ---                            */ | ||||
| 
 | ||||
|     //compara marzullos
 | ||||
|     // $a,$b::[nombre::string, cantidad::integer, horario::string, duracion::integer]
 | ||||
| 
 | ||||
|     // por cantidad
 | ||||
|     function cmp_cant_marzullo($a, $b) { | ||||
|         $cant_a = $a["cantidad"]; | ||||
|         $cant_b = $b["cantidad"]; | ||||
|         if ($cant_a == $cant_b) { | ||||
|             return $this->cmp_dur_marzullo($a, $b); | ||||
|         } | ||||
|         return ($cant_a > $cant_b) ? -1 : 1; | ||||
|     } | ||||
| 
 | ||||
|     // por duracion
 | ||||
|     private function cmp_dur_marzullo($a, $b): int { | ||||
|         $dur_a = $a["duracion"]; | ||||
|         $dur_b = $b["duracion"]; | ||||
|         if ($dur_a == $dur_b) { | ||||
|             return 0; | ||||
|         } | ||||
|         return ($dur_a > $dur_b) ? -1 : 1; | ||||
|     } | ||||
| } | ||||
|  |  | |||
|  | @ -9,6 +9,13 @@ class HoraFlag extends Model | |||
| { | ||||
|     use HasFactory; | ||||
| 
 | ||||
|     /** | ||||
|      * The attributes that are mass assignable. | ||||
|      * | ||||
|      * @var array | ||||
|      */ | ||||
|     protected $fillable = ['dia_id', 'minutoDelDia', 'flag']; | ||||
| 
 | ||||
|     public function dia() { | ||||
|         return $this->belongsTo(Dia::class); | ||||
|     } | ||||
|  |  | |||
							
								
								
									
										16
									
								
								app/Models/Utils.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								app/Models/Utils.php
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,16 @@ | |||
| <?php | ||||
| 
 | ||||
| namespace App\Models; | ||||
| 
 | ||||
| class Utils { | ||||
| 
 | ||||
| 
 | ||||
|     private function formatMinutos() { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     private function parseHora() { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| } | ||||
		Loading…
	
	Add table
		
		Reference in a new issue