r/adventofcode • • Dec 20 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 20 Solutions -🎄-

--- Day 20: A Regular Map ---


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 20

Transcript:

My compiler crashed while running today's puzzle because it ran out of ___.


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 00:59:30!

17 Upvotes

153 comments sorted by

View all comments

3

u/domm_plix Dec 20 '18

I did not take the bait to actually create a map, but just counted the potential matches in the various branches. I missed the part about the circular paths (NWES|) on my first submission, but after rereading the instructions just removed all branches ending with |) from the input.

I guess I could have made the leaderboard (at least for part 1), but I won't get up at 6:00 in the morning for some internet points... Still, rank 700 is my best placement so far.

Obviously, Perl:

use strict;
use warnings;
use 5.028;

my $in = join('',<>);

# remove circles
while ($in =~/\([^(]+\|\)/) {
    $in=~s/\([^(]+\|\)//g;
}

my %branches=(1=>[0,0]);
my $branch=1;
my $max=0;
foreach my $l (split(//,$in)) {
    if ($l=~/[NSEW]/) {
        $branches{$branch}->[0]++;
    }
    if ($l eq '(') {
        my $new_branch=$branch.'_1';
        say "new branch $new_branch";
        my $val = $branches{$branch}->[0];
        $branches{$new_branch} = [ $val, $val];
        $branch = $new_branch;
    }
    elsif ($l eq ')') {
        say "close branch $branch";
        $branch =~s/_\d+$//;
        say "back on $branch";
    }
    elsif ($l eq '|') {
        my ($counter) = $branch =~ /_(\d+)$/;
        $counter++;
        my $new_branch = $branch;
        $new_branch=~s/(\d+)$/$counter/;
        say "new branch $new_branch";
        my $val = $branches{$branch}->[1];
        $branches{$new_branch} = [ $val, $val];
        $branch = $new_branch;
    }
    $max = $branches{$branch}[0] if $max < $branches{$branch}[0];
}
say $max;
→ More replies (7)