Skip to content

Commit

Permalink
✨ Challenge RUGBY_1 working version with dumps
Browse files Browse the repository at this point in the history
  • Loading branch information
gturpin-dev committed Oct 18, 2023
1 parent b32c5d9 commit f89b8f6
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 3 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"require-dev": {
"phpunit/phpunit": "^9.5",
"vimeo/psalm": "^5.4"
"vimeo/psalm": "^5.4",
"symfony/var-dumper": "^6.3"
},
"require": {
"illuminate/support": "^10.6"
Expand Down
86 changes: 85 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
require_once __DIR__ . '/vendor/autoload.php';

$challenge_factory = new ChallengeFactory();
$challenge_factory->solve( 'MONSTERS_1' );
$challenge_factory->solve( 'RUGBY_1' );
32 changes: 32 additions & 0 deletions src/Challenges/RUGBY_1/Player.php
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 );
}
}
58 changes: 58 additions & 0 deletions src/Challenges/RUGBY_1/PlayerEncoder.php
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;
}
}
47 changes: 47 additions & 0 deletions src/Challenges/RUGBY_1/Rugby_1.php
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;
}
}
1 change: 1 addition & 0 deletions src/Challenges/RUGBY_1/data.json
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"]}

0 comments on commit f89b8f6

Please sign in to comment.