re: AoC Day 3: No Matter How You Slice It VIEW POST

VIEW FULL DISCUSSION
 

Perl solutions. The first part was easy, just counting how many times each square occurred. The second part was trickier and the naive solution was too slow, so I summoned some regular expressions to help me:

#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my %grid;
while (<>) {
    my ($x, $y, $w, $h) = /#\d+ @ (\d+),(\d+): (\d+)x(\d+)/;
    for my $j ($y .. $y + $h - 1) {
        for my $i ($x .. $x + $w - 1) {
            ++$grid{"$i $j"};
        }
    }
}

say scalar grep $grid{$_} > 1, keys %grid;
#!/usr/bin/perl
use warnings;
use strict;
use feature qw{ say };

my %grid;
while (<>) {
    my ($id, $x, $y, $w, $h) = /(#\d+) @ (\d+),(\d+): (\d+)x(\d+)/;
    for my $j ($y .. $y + $h - 1) {
        for my $i ($x .. $x + $w - 1) {
            $grid{"$i $j"} .= $id;
        }
    }
}

my $all = join ':', values %grid;
my %uniq;
undef @uniq{ $all =~ /(?:^|:)(#\d+)(?:$|:)/g };
for my $id (keys %uniq) {
    say($id), last if $all !~ /\d$id/ && $all !~ /$id#/;
}
code of conduct - report abuse