This commit is contained in:
keegan 2025-12-17 00:15:55 -05:00
commit 7855e2639b
2 changed files with 49 additions and 0 deletions

48
day2.pl Normal file
View File

@ -0,0 +1,48 @@
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;
}

1
input2.txt Normal file
View File

@ -0,0 +1 @@
9191906840-9191941337,7671-13230,2669677096-2669816099,2-12,229599-392092,48403409-48523311,96763-229430,1919163519-1919240770,74928-96389,638049-668065,34781-73835,736781-819688,831765539-831907263,5615884-5749554,14101091-14196519,7134383-7169141,413340-625418,849755289-849920418,7745350-7815119,16717-26267,4396832-4549887,87161544-87241541,4747436629-4747494891,335-549,867623-929630,53-77,1414-3089,940604-1043283,3444659-3500714,3629-7368,79-129,5488908-5597446,97922755-98097602,182-281,8336644992-8336729448,24-47,613-1077