Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl
- use strict;
- use warnings;
- use feature qw(say);
- use List::Util qw(sum);
- my @input = map { chomp; split // } <>;
- my @disk;
- my $id = 0;
- while (@input) {
- my $size = shift @input;
- my $gap = shift @input;
- push( @disk, ($id++) x $size );
- push( @disk, ('.') x $gap ) if ($gap);
- }
- my $left = 0;
- my $right = $#disk;
- while (1) {
- $left++ while ($disk[$left] ne '.');
- $right-- while ($disk[$right] eq '.');
- last if ($left >= $right);
- $disk[$left] = $disk[$right];
- $disk[$right] = '.';
- }
- say "Part 1: ", sum map { ($disk[$_] ne '.') ? $_ * $disk[$_] : 0 } (0 .. $left);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement