### 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 $_->[0], @lights) - min(map$_->[0], @lights) }

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

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->[0] += $light->[2];$light->[1] += \$light->[3];
}
}

code of conduct - report abuse