r/adventofcode • • 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!

Click here for rules

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

387 comments sorted by

View all comments

1

u/sebastiannielsen Dec 06 '18

*** WARNING *** Takes about 1.5 minutes to execute on a i3-4010U - be PATIENT! If you run this on a rPI it gonna take a looong time.

#!/usr/bin/perl

open(INPUT, "aoc_6_A_input.txt");
@input = <INPUT>;
close(INPUT);

@blacklist = ();
@counts = ();
$ccount = 0;

#Iterate through a "map" of 500x500
for ($a = 0; $a < 500; $a++) {
for ($b = 0; $b < 500; $b++) {

$shortestd = 99999;
$shortestindex = 0;
$tied = 0;
$totalscore = 0;

for ($i = 0; $i < ($#input + 1); $i++) {
$coord = $input[$i];
$coord =~ s/[^0-9,]*//sgi; #Get rid of unneccessary whitespace
($y, $x) = split(",", $coord);

if ((int($x) > 0)&&(int($y) > 0)) { #ensue we didn't catch a blank line
$mh = Manhattan(int($x),int($y),$a,$b);
$totalscore = $totalscore + $mh; #PART_2 - add to total

if ($mh == $shortestd) { #If current shortest distance is equal to a previous, its a tie
$tied = 1;
}
if ($mh < $shortestd) { #If we found a shorter, reset tie and make this shortest. Also save "owning input coordinate".
$shortestd = $mh;
$shortestindex = $i;
$tied = 0;
}
}

} # End of loop for input coordinates.

if ($totalscore < 10000) {
$ccount++; # PART_2 if total is less than 10 000, add to count.
}

if ($tied == 0) { #If it was not a tie, increase count, keyed by the "owning" input coordinate with 1.
$counts[$shortestindex]++;
}

#If we are at the edges, coordinate is part of infinite. Add the key ("owning input coordinate") to a blacklist.
if (($a == 0)||($b == 0)||($a == 499)||($b == 499)) {
$blacklist[$shortestindex] = 1;
}

}
}

$largest = 0;

#Iterate through the result. If the count is NOT owned by a blacklisted coordinate and is part of larger coordinate, update largest to reflect it.
for ($i = 0; $i < ($#counts + 1); $i++) {
if (($counts[$i] > $largest)&&($blacklist[$i] != 1)) {
$largest = $counts[$i];
}
}

print "DANGEROUS REGIION: ".$largest."\n";
print "SAFE REGION: ".$ccount."\n";

sub Manhattan() {
$startx = $_[0];
$starty = $_[1];
$endx = $_[2];
$endy = $_[3];

$valuea = $endx - $startx;
if ($valuea < 0) {
$valuea = $valuea * -1;
}
$valueb = $endy - $starty;
if ($valueb < 0) {
$valueb = $valueb * -1;
}

$result = $valuea + $valueb;
return int($result);
}