### re: AoC Day 10: The Stars Align VIEW POST

Just waiting for the smallest constellation could be wrong, I can imagine the lights forming even smaller image after (or before) showing the message. So, I printed all the possibilities fitting to an old-school terminal. In my case, there were three, and one of them contained the message. I printed the time along the way, so I solved both the parts in one go.

``````#!/usr/bin/perl
use warnings;
use strict;

use List::Util qw{ min max };

my @lights;
while (<>) {
push @lights, [ /(-?\d+)/g ];
}

my \$time = 0;
while (1) {
show() if width() < 80;
update();
++\$time;
}

sub width { max(map \$_->, @lights) - min(map \$_->, @lights) }

sub show {
my %grid;
my (\$min_x, \$max_x, \$min_y, \$max_y)
= (\$lights, \$lights, \$lights, \$lights);
for my \$light (@lights) {
\$min_x = \$light-> if \$light-> < \$min_x;
\$min_y = \$light-> if \$light-> < \$min_y;
\$max_x = \$light-> if \$light-> > \$max_x;
\$max_y = \$light-> if \$light-> > \$max_y;
++\$grid{ \$light-> }{ \$light-> };
}

for my \$y (\$min_y .. \$max_y) {
for my \$x (\$min_x .. \$max_x) {
print \$grid{\$x}{\$y} ? '#' : '.';
}
print "\n";
}
print "\$time\n";
}

sub update {
for my \$light (@lights) {
\$light-> += \$light->;
\$light-> += \$light->;
}
}
``````
code of conduct - report abuse  