-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay12.php
49 lines (39 loc) · 1.1 KB
/
Day12.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
<?php
declare(strict_types=1);
namespace App;
use App\Contracts\DayBehaviour;
use App\Day12\Cave;
use Illuminate\Support\Collection;
class Day12 extends DayBehaviour
{
public function solvePart1(): ?int
{
$caves = $this->mapToCaves($this->input);
return $caves
->get('start')
->traverse();
}
public function solvePart2(): ?int
{
$caves = $this->mapToCaves($this->input);
return $caves
->get('start')
->traverse(visitSmallCaveTwice: true);
}
/**
* @return Collection<string, Cave>
*/
protected function mapToCaves(array $input): Collection
{
$caves = collect();
collect($input)
->map(fn (string $line) => explode('-', $line))
->eachSpread(function ($cave1, $cave2) use (&$caves) {
$caves[$cave1] ??= new Cave($cave1);
$caves[$cave2] ??= new Cave($cave2);
$caves[$cave1]->adjacent[] = $caves[$cave2];
$caves[$cave2]->adjacent[] = $caves[$cave1];
});
return $caves;
}
}