aoc/day2.pl
2025-12-17 00:15:55 -05:00

49 lines
1.1 KiB
Perl

use Data::Dumper;
my $file = "input2.txt";
open(my $f, '<', $file) or die "cannot open file";
my $fh = <$f>;
my @ranges = split(/,/, $fh);
my $totalPhony = 0;
foreach my $range (@ranges) {
$totalPhony += getPhonyPins(split(/-/, $range));
}
print "Total phony: $totalPhony\n";
sub getPhonyPins {
($startInt, $endInt) = @_;
my $totalphony = 0;
my $found = 0;
NEXT: foreach my $i ($startInt .. $endInt) {
my @digits = split (//, $i);
# check for odd digits (cant have pairs)
my $digits = @digits;
if ($digits % 2 == 1) {
next;
}
for (my $len = 1; $len <= $digits/2; $len++) {
next unless $digits % $len == 0; # only use divisors
my $pattern = substr($i, 0, $len);
my $expected = $pattern x ($digits / $len);
if ($i eq $expected) {
$totalphony += $i;
$found += 1;
next NEXT;
}
}
}
print "Found: $found ($totalphony indv) for ($startInt, $endInt)\n";
return $totalphony;
}