adventofcode/2024/09/Part1.pm

40 lines
546 B
Perl
Raw Normal View History

2024-12-10 23:34:16 +00:00
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;