Move to src-php to make room for Go
This commit is contained in:
38
src-php/Utils/Arr.php
Normal file
38
src-php/Utils/Arr.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace trizz\AdventOfCode\Utils;
|
||||
|
||||
final class Arr
|
||||
{
|
||||
/**
|
||||
* Flatten a multi-dimensional array into a single level.
|
||||
*
|
||||
* Based on:
|
||||
*
|
||||
* @see https://github.com/laravel/framework/blob/c16367a1af68d8f3a1addc1a819f9864334e2c66/src/Illuminate/Collections/Arr.php#L221-L249
|
||||
*
|
||||
* @param array<mixed> $array
|
||||
*
|
||||
* @return array<mixed>
|
||||
*/
|
||||
public static function flatten(iterable $array, float|int $depth = INF): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($array as $item) {
|
||||
if (!is_array($item)) {
|
||||
$result[] = $item;
|
||||
} else {
|
||||
$values = $depth === 1
|
||||
? array_values($item)
|
||||
: self::flatten($item, $depth - 1);
|
||||
|
||||
foreach ($values as $value) {
|
||||
$result[] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
35
src-php/Utils/Str.php
Normal file
35
src-php/Utils/Str.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace trizz\AdventOfCode\Utils;
|
||||
|
||||
final class Str
|
||||
{
|
||||
/**
|
||||
* Check if the entirety of string two matches string one.
|
||||
*
|
||||
* @see https://github.com/MueR/adventofcode/blob/master/src/Util/StringUtil.php
|
||||
*/
|
||||
public static function matchesAll(string $one, string $two): bool
|
||||
{
|
||||
for ($index = 0, $length = strlen($two); $index < $length; ++$index) {
|
||||
if (!str_contains($one, $two[$index])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alphabetically sort characters in a string.
|
||||
*
|
||||
* @see https://github.com/MueR/adventofcode/blob/master/src/Util/StringUtil.php
|
||||
*/
|
||||
public static function sort(string $string): string
|
||||
{
|
||||
$letters = array_unique(str_split($string));
|
||||
sort($letters, SORT_STRING);
|
||||
|
||||
return implode('', $letters);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user