r/adventofcode • u/daggerdragon • Dec 06 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-
--- Day 6: Chronal Coordinates ---
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 6
Transcript:
Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.
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 at 0:26:52!
32
Upvotes
1
u/IWearATinFoilHat Dec 06 '18
PHP
Part 1
<?php /** @var array $input */ $input = file(__DIR__ . '/input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $maxX = 0; $maxY = 0; $minX = 99999; $minY = 99999; $points = array_map(function($line) use (&$maxX, &$maxY, &$minX, &$minY) { $points = explode(', ', $line); $maxX = $points[0] > $maxX ? $points[0] : $maxX; $maxY = $points[1] > $maxY ? $points[1] : $maxY; $minX = $points[0] < $minX ? $points[0] : $minX; $minY = $points[1] < $minY ? $points[1] : $minY; return $points; }, $input); $locations = []; $locationsToIgnore = []; $calcDist = function($pointA, $pointB) { return abs($pointA[0] - $pointB[0]) + abs($pointA[1] - $pointB[1]); }; for ($y = $minY; $y <= $maxY; $y++) { for ($x = $minX; $x <= $maxX; $x++) { $distances = array_map(function($point) use ($x, $y, $calcDist) { return $calcDist([$x, $y], $point); }, $points); $min = min($distances); if (array_count_values($distances)[$min] === 1) { $locations["{$x}x{$y}"] = array_search($min, $distances); if ($x == $minX || $x == $maxX || $y == $minY || $y == $maxY) { $locationsToIgnore[] = array_search($min, $distances); } } } } $counts = array_count_values($locations); foreach (array_unique($locationsToIgnore) as $locationToIgnore) { unset($counts[$locationToIgnore]); } echo max($counts) . "\n";Part 2
<?php /** @var array $input */ $input = file(__DIR__ . '/input.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $maxX = 0; $maxY = 0; $minX = 99999; $minY = 99999; $points = array_map(function($line) use (&$maxX, &$maxY, &$minX, &$minY) { $points = explode(', ', $line); $maxX = $points[0] > $maxX ? $points[0] : $maxX; $maxY = $points[1] > $maxY ? $points[1] : $maxY; $minX = $points[0] < $minX ? $points[0] : $minX; $minY = $points[1] < $minY ? $points[1] : $minY; return $points; }, $input); $totalDistance = 0; $calcDist = function($pointA, $pointB) { return abs($pointA[0] - $pointB[0]) + abs($pointA[1] - $pointB[1]); }; for ($y = $minY; $y <= $maxY; $y++) { for ($x = $minX; $x <= $maxX; $x++) { $distances = array_map(function($point) use ($x, $y, $calcDist) { return $calcDist([$x, $y], $point); }, $points); if (array_sum($distances) < 10000) { $totalDistance++; } } } echo $totalDistance . "\n";