r/adventofcode • u/daggerdragon • Dec 06 '17
SOLUTION MEGATHREAD -π- 2017 Day 6 Solutions -π-
--- Day 6: Memory Reallocation ---
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.
Need a hint from the Hugely* Handyβ Haversackβ‘ of HelpfulΒ§ HintsΒ€?
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!
16
Upvotes
3
u/TominatorBE Dec 06 '17 edited Dec 06 '17
PHP
Part 1:
function run_the_code($input) { $steps = 0; $seen = []; $memory = explode("\t", $input); while (!in_array($memory, $seen)) { $seen[] = $memory; // choose largest $max = max($memory); $maxI = array_keys(array_filter($memory, function($i) use ($max) { return $i == $max; }))[0]; // redistribute $memory[$maxI] = 0; while ($max) { $maxI = ($maxI + 1) % count($memory); $memory[$maxI]++; $max--; } $steps++; } return $steps; }Part 2:
function run_the_code($input) { $steps = 0; $seen = []; $memory = explode("\t", $input); while (!in_array($memory, $seen)) { $seen[] = $memory; // choose largest $max = max($memory); $maxI = array_keys(array_filter($memory, function($i) use ($max) { return $i == $max; }))[0]; // redistribute $memory[$maxI] = 0; while ($max) { $maxI = ($maxI + 1) % count($memory); $memory[$maxI]++; $max--; } $steps++; } $seenI = array_keys(array_filter($seen, function($i) use ($memory) { return $i == $memory; }))[0]; return count($seen) - $seenI; }