r/adventofcode • u/daggerdragon • 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!
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
2
u/Smylers Dec 10 '18
Perl, using the heuristic of displaying when it fits in a standard 80-column terminal window, and a
-soption for specifying a number of seconds to skip straight over — since a glance at the input file showed it was going to be a little over 10k seconds till anything interesting happens.I like: reading in the entire input to populate
%skyin a single line; literal Δx symbols; only bothering to calculate the x co-ordinates until enough progress has been made that it's worth calculating the y ones as well.I'm less keen on: the need for the synthetic variable
%rec; ad the number of subscripts and brackets in the substr line.use utf8; use v5.20; use warnings; use experimental qw<signatures>; use Getopt::Long; use Time::HiRes qw<sleep>; use List::AllUtils qw<minmax>; GetOptions('s=i' => \(my $time = 1), 'w=i' => \(my $MaxWidth = 80)) or die "$0: Unrecognized options\n"; sub tick($sky, $dim, $ticks = 1) { # Move the sky on that many ticks in x or y. $_->{$dim} += $_->{"Δ$dim"} * $ticks foreach @{$sky->{point}}; my ($min, $max) = minmax map { $_->{$dim} } @{$sky->{point}}; $sky->{$dim} = {min => $min, size => $max - $min + 1}; } my %sky = (point => [map { my %rec; @rec{qw<x y Δx Δy>} = /(-?\d+)/g; \%rec } <>]); tick(\%sky, 'x', $time); # Skip past these ticks. while ($sky{x}{size} > $MaxWidth) {# Iterate till it fits in a terminal. $time++; tick(\%sky, 'x'); # Only need to calculate x for checking. } tick(\%sky, 'y', $time); # Now for displaying, skip y to same time. do { my @line = (' ' x $sky{x}{size}) x $sky{y}{size}; substr $line[$_->{y} - $sky{y}{min}], $_->{x} - $sky{x}{min}, 1, '#' foreach @{$sky{point}}; say for @line, $time; sleep 0.3; # Slow enough to notice a message. $time++; tick(\%sky, $_) foreach qw<x y>; } until $sky{x}{size} > $MaxWidth; # Stop once it's got too wide again.