Skip to content

Instantly share code, notes, and snippets.

@mrbellek
Last active January 6, 2024 13:43
Show Gist options
  • Save mrbellek/2ca46ff4cb80bf234ccb7801c9254d51 to your computer and use it in GitHub Desktop.
Save mrbellek/2ca46ff4cb80bf234ccb7801c9254d51 to your computer and use it in GitHub Desktop.
Advent of Code - day 8
<?php
declare(strict_types=1);
//$input = file('sample.txt', FILE_IGNORE_NEW_LINES);
//$input = file('sample-part2.txt', FILE_IGNORE_NEW_LINES);
$input = file('input.txt', FILE_IGNORE_NEW_LINES);
$instructions = $input[0];
printf('Instructions for %d lines: %s' . PHP_EOL, count($input) - 2, $instructions);
$nodes = parseNodes(array_slice($input, 2));
$startingNodes = findStartingNodes($nodes);
printf('Found %d nodes ending in "A"! (%s)' . PHP_EOL, count($startingNodes), implode(',', $startingNodes));
$stepsToZForNode = [];
foreach ($startingNodes as $startingNode) {
$stepsToZForNode[] = findStepsToZForNode($startingNode, $nodes, $instructions);
}
printf('Found steps to Z for all nodes (%s), and the LCM is: %s' . PHP_EOL,
implode(',', $startingNodes),
getLcmForArray($stepsToZForNode)
);
function getLcmForArray(array $numbers): GMP
{
if (count($numbers) === 2) {
return gmp_lcm($numbers[0], $numbers[1]);
}
$num = array_shift($numbers);
return gmp_lcm($num, getLcmForArray($numbers));
}
function findStepsToZForNode(string $startingNode, array $allNodes, string $instructions): int
{
printf('Starting at node: %s' . PHP_EOL, $startingNode);
$nextNode = $startingNode;
$stepsDone = 0;
for ($i = 0; true; $i++) {
if ($i >= strlen($instructions)) {
$i = 0;
}
$instruction = substr($instructions, $i, 1);
$nextNode = $allNodes[$nextNode][$instruction];
$stepsDone++;
//printf('Next instruction is %s, pointing to node %s' . PHP_EOL, $instruction, $nextNode);
if (substr($nextNode, -1, 1) === 'Z') {
printf('Node %s points to node %s after %s steps' . PHP_EOL,
$startingNode,
$nextNode,
$stepsDone
);
return $stepsDone;
}
}
}
function findStartingNodes(array $nodes): array
{
$foundNodes = [];
foreach ($nodes as $node => $nextNodes) {
if (substr($node, -1, 1) === 'A') {
$foundNodes[] = $node;
}
}
return $foundNodes;
}
/*
* PART THE FIRST
*
$currentStep = 0;
$currentNode = 'AAA';
$nextInstruction = getNextInstruction($instructions, $currentStep);
printf('Starting! First node: %s' . PHP_EOL, $currentNode);
while ($currentNode !== 'ZZZ') {
$currentStep++;
$nextNode = $nodes[$currentNode][$nextInstruction];
printf('Choosing %s node: %s' . PHP_EOL, $nextInstruction, $nextNode);
$nextInstruction = getNextInstruction($instructions, $currentStep);
$currentNode = $nextNode;
if ($currentStep > 100000) die('shit broken' . PHP_EOL);
}
printf('Arrived at node ZZZ! It took %d steps.' . PHP_EOL, $currentStep);
function getNextInstruction(string $instructions, int $currentStep): string
{
//printf('getting next instruction from %s at step %d' . PHP_EOL, $instructions, $currentStep);
while ($currentStep >= strlen($instructions)) {
$currentStep -= strlen($instructions);
}
return substr($instructions, $currentStep, 1);
}*/
function parseNodes(array $lines): array
{
$nodes = [];
for ($i = 0; $i < count($lines); $i++) {
[$node, $nextNodes] = explode(' = ', $lines[$i]);
$nodes[$node] = [
'L' => substr($nextNodes, 1, 3),
'R' => substr($nextNodes, 6, 3),
];
}
return $nodes;
}
function dd($a, $b = null, $c = null, $d = null): void
{
die(var_dump($a, $b, $c, $d));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment