-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc2022-2-part1.php
43 lines (33 loc) · 1021 Bytes
/
aoc2022-2-part1.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
<?php
declare(strict_types=1);
namespace App;
use Generator;
use loophp\collection\Collection;
include __DIR__ . '/vendor/autoload.php';
$input = trim(file_get_contents(__DIR__ . '/input.txt'));
$normalize = static fn (string $hand): int =>
match ($hand) {
'A', 'X' => 0, // Rock
'B', 'Y' => 1, // Paper
'C', 'Z' => 2, // Scissors
};
$getMatchScore = static fn (int $left, int $right): int =>
$left === $right
? 3
: ((($left + 1) % 3) === $right ? 0 : 6);
$getGameScore = static fn (int $left, int $right): int =>
1 + $right + $getMatchScore($right, $left);
$c = Collection::fromString($input, "\n")
->flatMap(
static fn (string $line): Generator => yield from explode(' ', $line)
)
->map(static fn (string $letter): int => $normalize($letter))
->chunk(2)
->map(
static fn (array $game): int => $getGameScore(...$game)
)
->reduce(
static fn (int $c, int $v): int => $c + $v,
0
);
dump($c);