re: AoC Day 6: Chronal Coordinates VIEW POST

FULL DISCUSSION
 

Oh no, I've been affected by the bug in Day 6. My solution was correct, but the adventofcode.com didn't recognise it. I tested some of the solutions posted here and they gave the same output as mine. I'm not sure how to progress to Part 2, so only posting Part 1:

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

use List::Util qw{ max };

my %locations;
my $location = 'A';
my ($X, $Y) = (0, 0);
while (<>) {
    chomp;
    my ($x, $y) = split /,\s+/;
    $locations{$location++} = [ $x, $y ];
    $X = $x if $x > $X;
    $Y = $y if $y > $Y;
}

my @nearest;
for my $x (0 .. $X + 1) {
    for my $y (0 .. $Y + 1) {
        my @n = ( abs($locations{A}[0] - $x) + abs($locations{A}[1] - $y),
                  'A' );
        for my $location (keys %locations) {
            next if 'A' eq $location;

            my $distance = abs($locations{$location}[0] - $x)
                         + abs($locations{$location}[1] - $y);
            if ($distance <= $n[0]) {
                @n = ($distance) if $distance < $n[0];
                push @n, $location;
            }
        }
        $nearest[$x][$y] = \@n;
    }
}

my %freq;
++$freq{ $_->[1] } for grep @$_ == 2, map @$_, @nearest;
delete @freq{ map $_->[1], grep @$_ == 2, $nearest[0],
                                          $nearest[-1],
                                          map($_->[0], @nearest),
                                          map($_->[-1], @nearest) };
say max(0, values %freq);
 
code of conduct - report abuse