adventofcode/2024/09/Part1.pm
2024-12-10 18:34:16 -05:00

40 lines
546 B
Perl

package Part1;
use 5.36.0;
no warnings qw/ uninitialized /;
sub read_input ($file) {
my $i = 0;
my $data = 1;
my $x = [
map { ( ( $data++ % 2 ) ? $i++ : -1 ) x $_ }
split '', $file->slurp
];
return $x;
}
sub solve (@disk) {
use DDP;
my $i = -1;
while( ++$i < $#disk ) {
pop @disk while $disk[-1] == -1;
next unless $disk[$i] == -1;
$disk[$i] = pop @disk;
}
my $checksum = 0;
$i = 0;
$checksum += $i++ * $_ for @disk;
return $checksum;
}
1;