-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPuzzleInput.php
68 lines (59 loc) · 1.62 KB
/
PuzzleInput.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
namespace PeterVanDerWal\AdventOfCode\Cli\Model;
use Symfony\Component\String\UnicodeString;
class PuzzleInput extends UnicodeString
{
public function __construct(
string $string = '',
public readonly ?string $demoInputName = null,
) {
parent::__construct(rtrim($string, "\n\r\0"));
}
public function __sleep(): array
{
return [...parent::__sleep(), 'demoInputName'];
}
public function isDemoInput(): bool
{
return $this->demoInputName !== null;
}
/**
* Split and map the input
*
* @template callbackParam of ($asUnicodeString is true ? UnicodeString : string)
* @template callbackReturn
* @param string $delimiter
* @param callable(callbackParam): callbackReturn|null $callback
* @param bool $asUnicodeString
* @return ($callback is null ? callbackParam[] : callbackReturn[])
*/
public function splitMap(
string $delimiter,
callable $callback = null,
bool $asUnicodeString = false,
) {
$callback ??= fn ($line) => $line;
if (!$asUnicodeString) {
$callback = fn ($line) => $callback((string)$line);
}
return array_map(
$callback,
$this->split($delimiter)
);
}
/**
* @return string[]
*/
public function splitLines(): array
{
return $this->splitMap("\n");
}
/**
* @return int[]
*/
public function splitInt(string $delimiter = ','): array
{
return $this->splitMap($delimiter, fn (string $value) => (int)$value);
}
}