DEV Community

Discussion on: AoC Day 4: Repose Record

Collapse
 
carlymho profile image
Carly Ho 🌈

PHP

Had to brush up on the difference between usort, uasort, and uksort for this one, ahaha.

Part 1:

<?php
$list = file_get_contents($argv[1]);
$records = explode("\n", trim($list));
$currentguard = null;
$lastminute = 0;
$awake = true;
$guardtotals = array();

usort($records, function($a, $b) {
  preg_match('/\[([0-9]{4})\-([0-9]{2})\-([0-9]{2}) ([0-9]{2}):([0-9]{2})\]/', $a, $atimes);
  preg_match('/\[([0-9]{4})\-([0-9]{2})\-([0-9]{2}) ([0-9]{2}):([0-9]{2})\]/', $b, $btimes);
  if (intval($atimes[1].$atimes[2].$atimes[3].$atimes[4].$atimes[5]) == intval($btimes[1].$btimes[2].$btimes[3].$btimes[4].$btimes[5])) {
    return 0;
  }
  return (intval($atimes[1].$atimes[2].$atimes[3].$atimes[4].$atimes[5]) < intval($btimes[1].$btimes[2].$btimes[3].$btimes[4].$btimes[5])) ? -1 : 1;
});

foreach ($records as $record) {
  preg_match('/\[[0-9]{4}\-([0-9]{2})\-([0-9]{2}) ([0-9]{2}):([0-9]{2})\]/', $record, $timestamps);
  preg_match('/Guard #([0-9]+)/', $record, $guard);
  if (count($guard) > 0) {
    if (!$awake && $currentguard) {
      $timeasleep = 60 - $lastminute;
      $guardtotals[$currentguard]['asleep'] += $timeasleep;
      $sleep = array_slice($guardtotals[$currentguard]['minutes'], $lastminute, $timeasleep);
      $sleep = array_map(function($x) {
        return $x+1;
      }, $sleep);
      array_splice($guardtotals[$currentguard]['minutes'], $lastminute, $timeasleep, $sleep);
      $lastminute = 0;
    }
    $currentguard = intval($guard[1]);
    $awake = true;
    if (!array_key_exists($currentguard, $guardtotals)) {
      $guardtotals[$currentguard] = array(
        'asleep' => 0,
        'minutes' => array_fill(0, 60, 0)
      );
    }
  } elseif (strpos($record, 'falls asleep')) {
    $awake = false;
    $lastminute = intval($timestamps[4]);
  } elseif (strpos($record, 'wakes up')) {
    if ($currentguard) {
      $timeasleep = intval($timestamps[4]) - $lastminute;
      $guardtotals[$currentguard]['asleep'] += $timeasleep;
      $sleep = array_slice($guardtotals[$currentguard]['minutes'], $lastminute, $timeasleep);
      $sleep = array_map(function($x) {
        return $x+1;
      }, $sleep);
      array_splice($guardtotals[$currentguard]['minutes'], $lastminute, $timeasleep, $sleep);
      $lastminute = intval($timestamps[4]);
      $awake = true;
    }
  }
}
uasort($guardtotals, function($a, $b) {
  if ($a['asleep'] == $b['asleep']) {
    return 0;
  }
  return ($a['asleep'] < $b['asleep']) ? 1 : -1;
});
$chosenguard = array_keys($guardtotals)[0];
$chosenminute = array_search(max($guardtotals[$chosenguard]['minutes']), $guardtotals[$chosenguard]['minutes']);
echo $chosenguard*$chosenminute;
die(1);

Part 2:

<?php
$list = file_get_contents($argv[1]);
$records = explode("\n", trim($list));
$currentguard = null;
$lastminute = 0;
$awake = true;
$guardtotals = array();

usort($records, function($a, $b) {
  preg_match('/\[([0-9]{4})\-([0-9]{2})\-([0-9]{2}) ([0-9]{2}):([0-9]{2})\]/', $a, $atimes);
  preg_match('/\[([0-9]{4})\-([0-9]{2})\-([0-9]{2}) ([0-9]{2}):([0-9]{2})\]/', $b, $btimes);
  if (intval($atimes[1].$atimes[2].$atimes[3].$atimes[4].$atimes[5]) == intval($btimes[1].$btimes[2].$btimes[3].$btimes[4].$btimes[5])) {
    return 0;
  }
  return (intval($atimes[1].$atimes[2].$atimes[3].$atimes[4].$atimes[5]) < intval($btimes[1].$btimes[2].$btimes[3].$btimes[4].$btimes[5])) ? -1 : 1;
});

foreach ($records as $record) {
  preg_match('/\[[0-9]{4}\-([0-9]{2})\-([0-9]{2}) ([0-9]{2}):([0-9]{2})\]/', $record, $timestamps);
  preg_match('/Guard #([0-9]+)/', $record, $guard);
  if (count($guard) > 0) {
    if (!$awake && $currentguard) {
      $timeasleep = 60 - $lastminute;
      $guardtotals[$currentguard]['asleep'] += $timeasleep;
      $sleep = array_slice($guardtotals[$currentguard]['minutes'], $lastminute, $timeasleep);
      $sleep = array_map(function($x) {
        return $x+1;
      }, $sleep);
      array_splice($guardtotals[$currentguard]['minutes'], $lastminute, $timeasleep, $sleep);
      $lastminute = 0;
    }
    $currentguard = intval($guard[1]);
    $awake = true;
    if (!array_key_exists($currentguard, $guardtotals)) {
      $guardtotals[$currentguard] = array(
        'asleep' => 0,
        'minutes' => array_fill(0, 60, 0)
      );
    }
  } elseif (strpos($record, 'falls asleep')) {
    $awake = false;
    $lastminute = intval($timestamps[4]);
  } elseif (strpos($record, 'wakes up')) {
    if ($currentguard) {
      $timeasleep = intval($timestamps[4]) - $lastminute;
      $guardtotals[$currentguard]['asleep'] += $timeasleep;
      $sleep = array_slice($guardtotals[$currentguard]['minutes'], $lastminute, $timeasleep);
      $sleep = array_map(function($x) {
        return $x+1;
      }, $sleep);
      array_splice($guardtotals[$currentguard]['minutes'], $lastminute, $timeasleep, $sleep);
      $lastminute = intval($timestamps[4]);
      $awake = true;
    }
  }
}
$maximums = array_map(function($z) {
  return max($z['minutes']);
}, $guardtotals);
uasort($maximums, function($a, $b) {
  if ($a == $b) {
    return 0;
  }
  return ($a < $b) ? 1 : -1;
});
$chosenguard = array_keys($maximums)[0];
$chosenminute = array_search($maximums[$chosenguard], $guardtotals[$chosenguard]['minutes']);
echo $chosenguard*$chosenminute;
die(1);
Collapse
 
rpalo profile image
Ryan Palo

UKSort works the same, but is just more polite and has a slight accent, right? :]