adventofcode/2018/02/sol2.pl
Yanick Champoux 8ecca99278 part 2
2018-12-02 11:40:38 -05:00

40 lines
661 B
Perl

use 5.20.0;
use warnings;
my @items = <>;
use List::AllUtils qw/ pairwise /;
use experimental qw/
signatures
postderef
current_sub
/;
while( my $next = shift @items ) {
my $match = find([ split '', $next], @items) or next;
say $match;
last;
}
sub find( $next, @items ) {
my $contender = splice @_, 1, 1 or return;
$contender = [ split '', $contender ];
my $diff = 0;
my $r = '';
for my $i ( 0..$next->@* ) {
if ( $next->[$i] eq $contender->[$i] ) {
$r .= $next->[$i];
}
else {
last if $diff++;
}
}
return $r if $diff == 1;
goto __SUB__;
}