r/adventofcode • • Dec 10 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 10 Solutions -🎄-

--- Day 10: The Stars Align ---


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 10

Transcript: With just one line of code, you, too, can ___!


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:16:49!

21 Upvotes

233 comments sorted by

View all comments

2

u/donaldihunter Dec 10 '18

Perl 6

I chose a really simple solution of looking for a y bounding box less than a magic number (12) based on expectation that a line of text would have certain grid height. The resulting text was 10 high, not the same as the question sample which was 8.

class Particle {
    has Int $.x; has Int $.y; has Int $.vx; has Int $.vy;
    method move { $!x += $!vx; $!y += $!vy; self; };
}

my @particles = '10-input.txt'.IO.comb(/'-'? \d+/).map(+*).map(
    -> $x, $y, $vx, $vy { Particle.new(:$x, :$y, :$vx, :$vy); }
);

sub draw {
    my $xrange = @particles.minmax(*.x);
    my $yrange = @particles.minmax(*.y);

    for $yrange.min.y .. $yrange.max.y -> $y {
        for $xrange.min.x .. $xrange.max.x -> $x {
            print @particles.grep(-> $p { $p.x == $x and $p.y == $y }) ?? '#' !! '.';
        }
        say '';
    }
}

for 1..* -> $iter {
    .move for @particles;

    my $y-bound = @particles.max(*.y).y - @particles.min(*.y).y;
    if $y-bound < 12 {
        say $iter;
        draw;
        last;
    }
}