-
Notifications
You must be signed in to change notification settings - Fork 1
/
Day10.php
79 lines (61 loc) · 1.72 KB
/
Day10.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
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace Aoc2021\Day10;
use Aoc2021\Contracts\Runnable;
use Aoc2021\Utils\Collection;
use Aoc2021\Utils\Parser;
class Day10 implements Runnable
{
/** @var array<string, int> */
private array $corruptedScore = [')' => 3, ']' => 57, '}' => 1197, '>' => 25137];
/** @var array<string, int> */
private array $incompleteScore = [')' => 1, ']' => 2, '}' => 3, '>' => 4];
/**
* @return array{int, int}
*/
private function solve(string $input): array
{
$lines = Parser::getLines($input);
$corruptedScore = 0;
$incompleteScores = new Collection();
foreach ($lines as $line) {
$toMatch = [];
$isInvalid = false;
$ll = \strlen($line);
for ($i = 0; $i < $ll; $i += 1) {
$found = $line[$i];
$expect = match ($found) {
'(' => \array_push($toMatch, ')'),
'{' => \array_push($toMatch, '}'),
'[' => \array_push($toMatch, ']'),
'<' => \array_push($toMatch, '>'),
default => \array_pop($toMatch),
};
// array_push returns numbers, so need to check if it is string
if (\is_string($expect) && $expect !== $found) {
$isInvalid = true;
$corruptedScore += $this->corruptedScore[$found];
break;
}
}
if (!$isInvalid) {
$score = 0;
for ($i = \count($toMatch) - 1; $i >= 0; $i -= 1) {
$score = $score * 5 + $this->incompleteScore[$toMatch[$i]];
}
$incompleteScores->add($score);
}
}
return [$corruptedScore, $incompleteScores->median()];
}
public function part1(string $input): string
{
[$corruptedScore] = $this->solve($input);
return (string)$corruptedScore;
}
public function part2(string $input): string
{
[, $incompleteScore] = $this->solve($input);
return (string)$incompleteScore;
}
}