-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay1.php
70 lines (55 loc) · 1.61 KB
/
Day1.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
<?php
declare(strict_types=1);
namespace App\Days;
use App\Contracts\Day;
use Illuminate\Support\Collection;
class Day1 extends Day
{
public const EXAMPLE1 = <<<eof
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
eof;
/**
* Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?
*/
public function solvePart1(mixed $input): int|string|null
{
$input = $this->parseInput($input);
return $input->map(fn ($chunk) => $chunk->sum())
->max();
}
/**
* Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total?
*/
public function solvePart2(mixed $input): int|string|null
{
$input = $this->parseInput($input);
return $input->map(fn ($chunk) => $chunk->sum())
->sortDesc()
->take(3)
->sum();
}
protected function parseInput(mixed $input): Collection
{
$input = is_array($input) ? $input : explode("\n", $input);
return collect($input)
->map(fn ($line) => trim($line))
->chunkWhile(fn ($value) => '' !== $value)
->map(fn ($chunk) => $chunk->values())
->map(fn ($chunk) => $chunk->filter(fn ($value) => '' !== $value)->values())
->map(fn ($chunk) => $chunk->values()->map(fn ($value) => (int) $value))
->values();
}
public function getExample2(): mixed
{
return static::EXAMPLE1;
}
}