DEV Community

Discussion on: AoC Day 8: Memory Maneuver

Collapse
 
carlymho profile image
Carly Ho 🌈

PHP

Recursion, or: finally a chance to use my degree. Did I need to actually build the tree in Part 2? Probably not, but it made it easier to organize the data and actually do the recursion, so ¯_(ツ)_/¯

Part 1:

<?php
$input = trim(file_get_contents($argv[1]));
$numbers = array_map(function($x) {
  return intval($x);
}, explode(" ", trim($input)));
$pos = 0;
$meta_entries = array();

function get_children($c) {
  global $numbers;
  global $pos;
  global $meta_entries;

  $count = 0;
  $pos += 2;
  while ($count < $c && $pos < count($numbers)) {
    $children = $numbers[$pos];
    $metacount = $numbers[$pos+1];

    if ($children > 0) {
      get_children($children);
    } else {
      $pos += 2;
    }

    if ($metacount > 0) {
      $meta_entries = array_merge($meta_entries, array_slice($numbers, $pos, $metacount));
      $pos += $metacount;
    }
    $count++;
  }
  return;
}

while ($pos < count($numbers)) {
  $children = $numbers[$pos];
  $metacount = $numbers[$pos+1];
  if ($children > 0) {
    get_children($children);
  } else {
    $pos += 2;
  }
  if ($metacount > 0) {
    $meta_entries = array_merge($meta_entries, array_slice($numbers, $pos, $metacount));
    $pos += $metacount;
  }
}

echo array_sum($meta_entries);
die(1);

Part 2:

<?php
$input = trim(file_get_contents($argv[1]));
$numbers = array_map(function($x) {
  return intval($x);
}, explode(" ", trim($input)));
$pos = 0;
$meta_entries = array();
$tree = array();

function get_children($c) {
  global $numbers;
  global $pos;

  $childnodes = array();

  $count = 0;
  $pos += 2;

  while ($count < $c && $pos < count($numbers)) {
    $children = $numbers[$pos];
    $metacount = $numbers[$pos+1];

    array_push($childnodes, array(
      'childcount' => $children,
      'metacount' => $metacount,
      'children' => array(),
      'meta' => array(),
      'value' => 0
    ));

    if ($children > 0) {
      $childnodes[$count]['children'] = get_children($children);
    } else {
      $pos += 2;
    }

    if ($metacount > 0) {
      $childnodes[$count]['meta'] = array_slice($numbers, $pos, $metacount);
      if ($childnodes[$count]['childcount'] == 0) {
        $childnodes[$count]['value'] = array_sum($childnodes[$count]['meta']);
      } else {
        foreach ($childnodes[$count]['meta'] as $m) {
          if (array_key_exists($m-1, $childnodes[$count]['children'])) {
            $childnodes[$count]['value'] += $childnodes[$count]['children'][$m-1]['value'];
          }
        }
      }
      $pos += $metacount;
    }
    $count++;
  }
  return $childnodes;
}

$children = $numbers[$pos];
$metacount = $numbers[$pos+1];
array_push($tree, array(
  'childcount' => $children,
  'metacount' => $metacount,
  'children' => array(),
  'meta' => array(),
  'value' => 0
));
if ($children > 0) {
  $tree[0]['children'] = get_children($children);
} else {
  $pos += 2;
}
if ($metacount > 0) {
  $tree[0]['meta'] = array_slice($numbers, $pos, $metacount);
}
if ($tree[0]['childcount'] == 0) {
  $tree[0]['value'] = array_sum($t['meta']);
} else {
  foreach ($tree[0]['meta'] as $i=>$m) {
    if (array_key_exists($m-1, $tree[0]['children'])) {
      $tree[0]['value'] += $tree[0]['children'][$m-1]['value'];
    }
  }
}
echo $tree[0]['value'];
die(1);