Move solutions out of the source itself
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
57
data/Y22/day1/Day1.php
Normal file
57
data/Y22/day1/Day1.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace trizzssdddAdventOfCode\Y22;
|
||||
|
||||
use trizz\AdventOfCode\Solution;
|
||||
|
||||
final class Day1 extends Solution
|
||||
{
|
||||
public static null|int|string $part1ExampleResult = 24000;
|
||||
|
||||
public static null|int|string $part1Result = 72240;
|
||||
|
||||
public static null|int|string $part2ExampleResult = 45000;
|
||||
|
||||
public static null|int|string $part2Result = 210957;
|
||||
|
||||
public bool $filterDataOnLoad = false;
|
||||
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
return $this->calculateCalories($data)[0];
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function part2(array $data): int
|
||||
{
|
||||
$results = $this->calculateCalories($data);
|
||||
|
||||
return $results[0] + $results[1] + $results[2];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $data
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function calculateCalories(array $data): array
|
||||
{
|
||||
$results = [];
|
||||
$tmpResult = 0;
|
||||
foreach ($data as $value) {
|
||||
if ($value !== '') {
|
||||
$tmpResult += (int) $value;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$results[] = $tmpResult;
|
||||
$tmpResult = 0;
|
||||
}
|
||||
|
||||
rsort($results);
|
||||
|
||||
return $results;
|
||||
}
|
||||
}
|
71
data/Y22/day2/Day2.php
Normal file
71
data/Y22/day2/Day2.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace trizz\AdventOfCode\Y22;
|
||||
|
||||
use trizz\AdventOfCode\Solution;
|
||||
use trizz\AdventOfCode\Y22\Day2\RPS;
|
||||
|
||||
final class Day2 extends Solution
|
||||
{
|
||||
public static null|int|string $part1ExampleResult = 15;
|
||||
|
||||
public static null|int|string $part1Result = 10994;
|
||||
|
||||
public static null|int|string $part2ExampleResult = 12;
|
||||
|
||||
public static null|int|string $part2Result = 12526;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
require_once __DIR__.'/RPS.php';
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
$score = 0;
|
||||
foreach ($data as $round) {
|
||||
[$opponentDraw, $ownDraw] = explode(' ', $round);
|
||||
$ownDraw = RPS::fromPuzzleInput($ownDraw);
|
||||
$opponentDraw = RPS::fromPuzzleInput($opponentDraw);
|
||||
$score += $this->calculateScore($ownDraw, $opponentDraw);
|
||||
}
|
||||
|
||||
return $score;
|
||||
}
|
||||
|
||||
#[\Override]
|
||||
public function part2(array $data): int
|
||||
{
|
||||
$score = 0;
|
||||
foreach ($data as $round) {
|
||||
[$opponentDraw, $expectedResult] = explode(' ', $round);
|
||||
$opponentDraw = RPS::fromPuzzleInput($opponentDraw);
|
||||
|
||||
$score += match ($expectedResult) {
|
||||
'X' => $this->calculateScore($opponentDraw->losingOpposite(), $opponentDraw),
|
||||
'Y' => $this->calculateScore($opponentDraw, $opponentDraw),
|
||||
'Z' => $this->calculateScore($opponentDraw->winningOpposite(), $opponentDraw),
|
||||
default => 0,
|
||||
};
|
||||
}
|
||||
|
||||
return $score;
|
||||
}
|
||||
|
||||
private function calculateScore(RPS $ownDraw, RPS $opponentDraw): int
|
||||
{
|
||||
// Win
|
||||
if ($ownDraw->losingOpposite() === $opponentDraw) {
|
||||
return $ownDraw->score() + 6;
|
||||
}
|
||||
|
||||
// Lost
|
||||
if ($ownDraw->winningOpposite() === $opponentDraw) {
|
||||
return $ownDraw->score();
|
||||
}
|
||||
|
||||
// Draw.
|
||||
return $ownDraw->score() + 3;
|
||||
}
|
||||
}
|
49
data/Y22/day2/RPS.php
Normal file
49
data/Y22/day2/RPS.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace trizz\AdventOfCode\Y22\Day2;
|
||||
|
||||
enum RPS
|
||||
{
|
||||
case ROCK;
|
||||
|
||||
case PAPER;
|
||||
|
||||
case SCISSORS;
|
||||
|
||||
public function score(): int
|
||||
{
|
||||
return match ($this) {
|
||||
self::ROCK => 1,
|
||||
self::PAPER => 2,
|
||||
self::SCISSORS => 3,
|
||||
};
|
||||
}
|
||||
|
||||
public function winningOpposite(): RPS
|
||||
{
|
||||
return match ($this) {
|
||||
self::ROCK => self::PAPER,
|
||||
self::PAPER => self::SCISSORS,
|
||||
self::SCISSORS => self::ROCK,
|
||||
};
|
||||
}
|
||||
|
||||
public function losingOpposite(): RPS
|
||||
{
|
||||
return match ($this) {
|
||||
self::ROCK => self::SCISSORS,
|
||||
self::PAPER => self::ROCK,
|
||||
self::SCISSORS => self::PAPER,
|
||||
};
|
||||
}
|
||||
|
||||
public static function fromPuzzleInput(string $value): RPS
|
||||
{
|
||||
return match ($value) {
|
||||
'A', 'X' => self::ROCK,
|
||||
'B', 'Y' => self::PAPER,
|
||||
'C', 'Z' => self::SCISSORS,
|
||||
default => throw new \LogicException('Invalid value.'),
|
||||
};
|
||||
}
|
||||
}
|
66
data/Y22/day3/Day3.php
Normal file
66
data/Y22/day3/Day3.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace trizz\AdventOfCode\Y22;
|
||||
|
||||
use trizz\AdventOfCode\Solution;
|
||||
|
||||
final class Day3 extends Solution
|
||||
{
|
||||
public static null|int|string $part1ExampleResult = 157;
|
||||
|
||||
public static null|int|string $part1Result = 8053;
|
||||
|
||||
public static null|int|string $part2ExampleResult = 70;
|
||||
|
||||
public static null|int|string $part2Result = 2425;
|
||||
|
||||
/**
|
||||
* @param array<int, string> $data
|
||||
*/
|
||||
#[\Override]
|
||||
public function part1(array $data): int
|
||||
{
|
||||
$result = 0;
|
||||
foreach ($data as $rucksack) {
|
||||
/** @var int<1, max> $length */
|
||||
$length = max(1, strlen($rucksack) / 2);
|
||||
[$left, $right] = str_split($rucksack, $length);
|
||||
$result += $this->calculateScore([str_split($left), str_split($right)]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $data
|
||||
*/
|
||||
#[\Override]
|
||||
public function part2(array $data): int
|
||||
{
|
||||
$result = 0;
|
||||
$groups = array_chunk($data, 3);
|
||||
foreach ($groups as $group) {
|
||||
$data = array_map(static fn ($items): array => str_split((string) $items), $group);
|
||||
$result += $this->calculateScore($data);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<array<int, string>> $data
|
||||
*/
|
||||
private function calculateScore(array $data = []): int
|
||||
{
|
||||
$items = array_unique(array_intersect(...$data));
|
||||
|
||||
$result = 0;
|
||||
|
||||
foreach ($items as $item) {
|
||||
$position = ord(strtoupper($item)) - ord('A') + 1;
|
||||
$result += $position + (ctype_upper($item) ? 26 : 0);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user