DEV Community

Discussion on: AoC Day 7: The Sum of Its Parts

Collapse
 
jenovs profile image
Viktors Jenovs • Edited

Today I could figure out only the first part :( Wasted quite a bit of time because example input and real input had one big difference between data structures (and I assumed wrongly ;).

Part 1

<?php
$input = require_once 'readFile.php';

function findInitSteps($array) {
  $initSteps = [];
  foreach ($array as $value) {
    $finalSteps[] = $value[1];
  }
  foreach ($array as $value) {
    if (!in_array($value[0], $finalSteps) && !in_array($value[0], $initSteps)) {
      $initSteps[] = $value[0];
    }
  }
  sort($initSteps);
  return $initSteps;
}

$steps = array_map(function($str) {
  [, $first, $last] = preg_split("/.{5}(.).{30}(.).+/", $str, NULL, PREG_SPLIT_DELIM_CAPTURE);
  return [$first, $last];
}, $input);

$initSteps = findInitSteps($steps);

$completedSteps[] = array_shift($initSteps);

function findNextStep($steps, $completed) {
  $res = [];
  $incomplete = [];

  foreach ($steps as $key => [$mustBe, $nextStep]) {
    if (in_array($mustBe, $completed) && !in_array($nextStep, $completed)) {
      $res[] = $nextStep;
    } else {
      $incomplete[] = $nextStep;
    }
  }

  $cleanRes = [];
  foreach ($res as $value) {
    if (!in_array($value, $incomplete)) {
      $cleanRes[] = $value;
    }
  }
  if (count($cleanRes)) {
    sort($cleanRes);
    return $cleanRes[0];
  }
}

do {
  $next = findNextStep($steps, $completedSteps);

  if (count($initSteps)) {
    $temp = array_filter(array_merge($initSteps, [$next]));
    sort($temp);
    $temp2 = array_shift($temp);
    if ($next != $temp2) {
      $key = array_keys($initSteps, $temp2);
      array_splice($initSteps, $key[0], 1);
      $next = $temp2;
    }
  }

  $next && $completedSteps[] = $next;
} while ($next);

echo join($completedSteps) . "\n";
?>

readFile.php

<?php
$file = fopen("input.txt", "r") or exit("Unable to open file");
// $file = fopen("input_test.txt", "r") or exit("Unable to open file");

while(!feof($file)) {
  $array[] = fgets($file);
}

fclose($file);

return array_filter($array);
?>
Collapse
 
kritner profile image
Russ Hammett

Yeah, I wish the sample was more representative of the actual data. I struggled with getting this part 1 done, and was not ready for part 2, to many differences and I had already spent more time on day 7 than any other day