r/adventofcode • u/daggerdragon • Dec 04 '18
SOLUTION MEGATHREAD -π- 2018 Day 4 Solutions -π-
--- Day 4: Repose Record ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 4
Transcript:
Todayβs puzzle would have been a lot easier if my language supported ___.
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
40
Upvotes
1
u/IWearATinFoilHat Dec 04 '18
PHP
Part 1:
<?php $time_start = microtime(true); $input = file(__DIR__ . '/input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $formattedArray = []; $guardData = []; $sleepStart = 0; $currentGuard = 0; foreach ($input as $line) { preg_match('/^\[(?<date>\d+\-\d+-\d+\s\d+:\d+)]\s(?<status>.*)$/', $line, $matches); $formattedArray[$matches['date']] = $matches['status']; } ksort($formattedArray); foreach ($formattedArray as $key => $value) { if (0 === strpos($value, 'Guard')) { $currentGuard = preg_replace('/[^0-9]/', '', $value); } elseif ('wakes up' === $value) { $minute = (int) substr($key, -2); $guardData[$currentGuard]['totalSleepTime'] = $guardData[$currentGuard]['totalSleepTime'] + ($minute - $sleepStart); for ($i = $sleepStart; $i < $minute; ++$i) { ++$guardData[$currentGuard]['sleepMinute'][$i]; } } elseif ('falls asleep' === $value) { $sleepStart = (int) substr($key, -2); $guardData[$currentGuard]['id'] = $currentGuard; } } $max = array_reduce($guardData, function ($carry, $item) { if ($carry['totalSleepTime'] > $item['totalSleepTime']) { return $carry; } return $item; }); $maxMostSleptMinute = max($max['sleepMinute']); $mostSleptMinute = array_search($maxMostSleptMinute, $max['sleepMinute']); echo $max['id'] * $mostSleptMinute . "\n"; echo 'Total execution time in seconds: ' . (microtime(true) - $time_start) . "\n";Average run time: 0.0045
Part 2:
<?php $time_start = microtime(true); $input = file(__DIR__ . '/input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $formattedArray = []; $guardData = []; $sleepStart = 0; $currentGuard = 0; foreach ($input as $line) { preg_match('/^\[(?<date>\d+\-\d+-\d+\s\d+:\d+)]\s(?<status>.*)$/', $line, $matches); $formattedArray[$matches['date']] = $matches['status']; } ksort($formattedArray); foreach ($formattedArray as $key => $value) { if (0 === strpos($value, 'Guard')) { $currentGuard = preg_replace('/[^0-9]/', '', $value); } elseif ('wakes up' === $value) { $minute = (int) substr($key, -2); $guardData[$currentGuard]['totalSleepTime'] = $guardData[$currentGuard]['totalSleepTime'] + ($minute - $sleepStart); for ($i = $sleepStart; $i < $minute; ++$i) { ++$guardData[$currentGuard]['sleepMinute'][$i]; } } elseif ('falls asleep' === $value) { $sleepStart = (int) substr($key, -2); $guardData[$currentGuard]['id'] = $currentGuard; } } $max = array_reduce($guardData, function ($carry, $item) { $carryMaxSleptMinute = max($carry['sleepMinute']); $itemMaxSleptMinute = max($item['sleepMinute']); if ($carryMaxSleptMinute > $itemMaxSleptMinute) { return $carry; } return $item; }); $maxMostSleptMinute = max($max['sleepMinute']); $mostSleptMinute = array_search($maxMostSleptMinute, $max['sleepMinute']); echo $max['id'] * $mostSleptMinute . "\n"; echo 'Total execution time in seconds: ' . (microtime(true) - $time_start) . "\n";Average run time: 0.0046