Skip to content

Instantly share code, notes, and snippets.

@localheinz
Created February 20, 2017 11:23
Show Gist options
  • Save localheinz/5ff9a31e190c66fa15ef5707a450578c to your computer and use it in GitHub Desktop.
Save localheinz/5ff9a31e190c66fa15ef5707a450578c to your computer and use it in GitHub Desktop.
Full Decks Solution (PHP)
<?php
declare(strict_types=1);
final class FullDeck
{
public function cards(): array
{
static $cards;
if (null === $cards) {
$ranks = ['2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'];
$suits = ['S', 'C', 'H', 'D'];
$cards = [];
foreach ($ranks as $rank) {
foreach ($suits as $suit) {
$cards[] = $rank . $suit;
}
}
}
return $cards;
}
}
<?php
declare(strict_types=1);
final class FullDecks
{
/**
* @var FullDeck
*/
private $fullDeck;
public function __construct(FullDeck $fullDeck)
{
$this->fullDeck = $fullDeck;
}
/**
* @param string[] $cards
*
* @return int
*/
public function __invoke(array $cards): int
{
$count = 0;
while (\count($cards)) {
foreach ($this->fullDeck->cards() as $card) {
$key = \array_search(
$card,
$cards,
true
);
if (false === $key) {
return $count;
}
unset($cards[$key]);
}
++$count;
}
return $count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment