-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Challenge RUGBY_1 working version with dumps
- Loading branch information
1 parent
b32c5d9
commit f89b8f6
Showing
7 changed files
with
226 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Gturpin\TainixChallenges\Challenges\RUGBY_1; | ||
|
||
/** | ||
* Modelize a Rugby Player | ||
*/ | ||
final class Player { | ||
|
||
public function __construct( | ||
private string $line, | ||
private int $weight, | ||
private int $strength | ||
) {} | ||
|
||
/** | ||
* The impact is calculated by the following formula : | ||
* strength * weight * Line::IMPACT_FACTOR | ||
* | ||
* @return integer The impact power of the player | ||
*/ | ||
public function get_impact_power() : int { | ||
$line_factor = match( $this->line ) { | ||
'first_line' => 1.5, | ||
'second_line' => 1, | ||
'third_line' => .75, | ||
default => throw new \InvalidArgumentException( 'The line is not valid' ), | ||
}; | ||
|
||
return floor( $this->strength * $this->weight * $line_factor ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Gturpin\TainixChallenges\Challenges\RUGBY_1; | ||
|
||
/** | ||
* This class is used to decode the stats of a player | ||
*/ | ||
final class PlayerEncoder { | ||
|
||
private int $weight; | ||
private int $strength; | ||
|
||
/** | ||
* The delimiter used to separate the weight and the strength | ||
*/ | ||
private const DELIMITER = ':'; | ||
|
||
/** | ||
* Decode the stats and store them in the object | ||
* | ||
* @param string $encoded_stats The encoded stats of the player format : "weight-strength" | ||
*/ | ||
public function __construct( string $encoded_stats ) { | ||
$this->decode( $encoded_stats ); | ||
} | ||
|
||
/** | ||
* Decode the stats | ||
* | ||
* @param string $encoded_stats The encoded stats of the player format : "weight-strength" | ||
* | ||
* @throws \InvalidArgumentException If the encoded stats are not valid | ||
* | ||
* @return void | ||
*/ | ||
private function decode( string $encoded_stats ) : void { | ||
$stats = explode( self::DELIMITER, $encoded_stats ); | ||
|
||
if ( count( $stats ) !== 2 ) { | ||
throw new \InvalidArgumentException( 'The encoded stats are not valid' ); | ||
} | ||
|
||
$this->weight = (int) $stats[0]; | ||
$this->strength = (int) $stats[1]; | ||
} | ||
|
||
/** | ||
* Getters & Setters | ||
*/ | ||
|
||
public function get_weight() : int { | ||
return $this->weight; | ||
} | ||
|
||
public function get_strength() : int { | ||
return $this->strength; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Gturpin\TainixChallenges\Challenges\RUGBY_1; | ||
|
||
use Gturpin\TainixChallenges\Challenge; | ||
|
||
/** | ||
* @link https://tainix.fr/challenge/Coupe-du-monde-de-rugby-La-melee | ||
*/ | ||
final class Rugby_1 extends Challenge { | ||
|
||
protected const USE_DATA_TEST = true; | ||
protected const ENABLE_LOG = true; | ||
|
||
public function solve() : mixed { | ||
dump( $this->data ); | ||
|
||
$first_line = $this->data['line1'] ?? []; | ||
$second_line = $this->data['line2'] ?? []; | ||
$third_line = $this->data['line3'] ?? []; | ||
|
||
// For each players, create a Player object with their coded form | ||
$first_line_players = array_map( function( $encoded_stats ) { | ||
$encoder = new PlayerEncoder( $encoded_stats ); | ||
return new Player( 'first_line', $encoder->get_weight(), $encoder->get_strength() ); | ||
}, $first_line ); | ||
|
||
$second_line_players = array_map( function( $encoded_stats ) { | ||
$encoder = new PlayerEncoder( $encoded_stats ); | ||
return new Player( 'second_line', $encoder->get_weight(), $encoder->get_strength() ); | ||
}, $second_line ); | ||
|
||
$third_line_players = array_map( function( $encoded_stats ) { | ||
$encoder = new PlayerEncoder( $encoded_stats ); | ||
return new Player( 'third_line', $encoder->get_weight(), $encoder->get_strength() ); | ||
}, $third_line ); | ||
|
||
$players = [ ...$first_line_players, ...$second_line_players, ...$third_line_players ]; | ||
$total_impact_power = array_reduce( $players, fn( $impact_power, $player ) => $impact_power + $player->get_impact_power(), 0 ); | ||
|
||
dump( $total_impact_power); | ||
|
||
dd($players); | ||
|
||
die; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"line1":["85:11","89:52","95:24"],"line2":["89:87","100:58","109:27","95:96"],"line3":["98:48"]} |